Sei sulla pagina 1di 15

// Vi Du 1: bat sang va tat mot LED cong C dung lenh Delay_ms();

// Date: May 08, 2007


// Designed by Ha Ngoc Nguyen
//*********************
// Pin RC0 ~ RC7 noi voi LED
//*********************
//#include <18F452.h>
#include <16F877.h>
//#device ADC=16
//#device ADC=8
//#include <math.h>
//#include <string.h>
//#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
//--------------------------------------------------// chuong trinh nay khong xoa duoc
// Important, if not the program cannot run
//--------------------------------------------------//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, PARITY=N, BITS =8)
//--------------------------------------------------main()
{
output_C(255); // bat sang toan bo Led port C
delay_ms(1000); //
output_C(0); // tat toan bo cac chan Port C
while(1)
{
output_high(PIN_C0);
output_high(PIN_C5);
delay_ms(500);
// Lenh tat moi hoat dong cua chuong trinh
output_low(PIN_C0);
output_low(PIN_C5);
delay_ms(500);
}
}

// Vi du 2 Tao delay time bang bien thoi gian


// Ung dung ==> bat tat sang Den Led cong C theo dinh thi`
// Date: may 08, 2007
// Designed by Ha Ngoc Nguyen
//*********************
// PIN RC0 ~RC5 noi voi den LED
//*********************
//#include <18F452.h>
#include <16F877A.h>
//#device ADC=16
//#device ADC=8
//#include <math.h>
//#include <string.h>
//#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
int16 Count1=0;
int16 Count2=0;

// Tao delay_timer_01
// Tao delay_timer_02

//******* chuong trinh Ngat Timer 1 ************


#INT_TIMER1
void Sampling_Time()
{
Count1++;
Count2++;

set_timer1(54286);
// dem 65535(so) mat thoi gian 52.4 ms
// vay 10ms dem duoc 11250(so)

//******* Bat dau chuong trinh *************


main()
{
output_C(255); // bat sang toan bo Led port C
delay_ms(1000);
output_C(0); // tat toan bo Led Cong C
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
//**Timmer max = 65536*4*4/OSC=65536*4/20000000=52.4ms**
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);

while(1)
{
if(Count1<50)
{
output_high(PIN_C0);
}
else
{
output_low(PIN_C0);
if (Count1>100)
{
Count1=0;
}
}
}
}
// Cach Tinh Timer MAX
// Xung thach anh 20.000.000 Hz ==> xung noi 5.000.000 hz
// Thoi gian 1 chu Ky` = 1/xung noi = 1/5000.000 = 0.2 us
// NEU TA DAT (setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);) thi ta co
// With an internal clock at 20mhz
// and with the T1_DIV_BY_1 mode, the timer
// will increment every 0.2us. It will overflow every 40 ms
// NEU TA DAT (setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);) thi ta co
// With an internal clock at 20mhz
// and with the T1_DIV_BY_1 mode, the timer
// will increment every 0.2*8=1.6us. It will overflow every 104.8576ms
// Voi lua chon nhu sau thi ta co cac Timemax nhu sau:
//T1_DIV_BY_1
: timemax=13.1ms
//T1_DIV_BY_2
: timemax=26.2ms
//T1_DIV_BY_4
: timemax=52.4ms
//T1_DIV_BY_8
: timemax=104.8ms
// So de set Timer1 duoc tinh nhu sau: set_timer1(Bien);
//T1_DIV_BY_1
: Bien = 65536-(samplingtime (s)/(4/20000000))
//T1_DIV_BY_2
: Bien = 65536-(samplingtime (s)/(8/20000000))
//T1_DIV_BY_4
: Bien = 65536-(samplingtime (s)/(16/20000000))
//T1_DIV_BY_8
: Bien = 65536-(samplingtime(s)/(32/20000000))

// Vi du 3 Cach su dung cong tac Switch


// Ung dung ==> neu cong tac ON ==> sang va toi LED port C
// neu cong tac OFF ==> Sang LED
// Date: May 08, 2007
// Designed by Ha Ngoc Nguyen
//*********************
// PIN RC0 ~RC5 noi voi den LED
// PIN RA4 lam Switch
//*********************
//#include <18F452.h>
#include <16F877A.h>
//#device ADC=16
//#device ADC=8
//#include <math.h>
//#include <string.h>
//#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
int16 Count1=0;
int16 Count2=0;

// Tao delay_timer_01
// Tao delay_timer_02

// ****** Chuong trinh con sang va toi mot LED ****


void CHOP_LED()
{
if(Count1<50)
{
output_high(PIN_C0);
}
else
{
output_low(PIN_C0);
if (Count1>100)
{
Count1=0;
}
}
}
//******* chuong trinh Ngat Timer 1 ************
#INT_TIMER1
void Sampling_Time()

{
Count1++;
Count2++;
set_timer1(54286);
// dem 65535(so) mat thoi gian 52.4 ms
}
// vay 10ms dem duoc 11250(so)
//******* Bat dau chuong trinh *************
main()
{
output_C(255); // bat sang toan bo Led port C
delay_ms(1000);
output_C(0); // tat toan bo Led Cong C
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
//**Timmer max = 65536*4*4/OSC=65536*4/20000000=52.4ms**
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(1)
{
if(!input(PIN_A4))
// SW is ON
{
output_low (PIN_C2); // Tat LED C2
CHOP_LED();
// goi Ham CHOP_LED (Signal to known system is ON)
}
else
// SW is OFF
{
output_low(PIN_C0); // Tat LED C0
output_high(PIN_C2); // Sang LED C2 (Signal to known system is OFF)
}
}
}

// Vi du 4 Cach su dung AD Converter va xuat ra Den LED 7 doan


// Den LED 7 doan truyen noi tiep
// Ung dung ==> neu cong tac ON ==> sang va toi LED port C
//
neu cong tac OFF ==> Sang LED
// khi thay doi volume thi thoi gian sang cua den Led se thay doi
// Date: May 08, 2007
// Designed by Ha Ngoc Nguyen
//*********************
// PIN RA4 lam Switch
// PIN RA0 noi voi Volume
// Pin RC0 ~ RC7 noi voi LED
// Pin RB1 noi voi Chan Set data cua IC giai ma
// Pin RB2 noi voi Chan Set Clock
// Pin RB3 noi voi Chan Enabale
//*********************
#include <18F452.h>
//#include <16F877A.h>
#device ADC=8
#include <math.h>
#include <string.h>
#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
int16 Doc_so=0;
//****************************************
void BCD_Send(int16 BCD_Num) // MC14449
{
char k;
char temp;
temp = BCD_Num;
output_low(PIN_B3);
//Enable0 LOW -> data enter
for(k=0;k<4;k++)
{
if((temp & 0x08) != 0)
{
output_high(PIN_B2); // set clock
output_high(PIN_B1); // set data
}
else
{
output_low(PIN_B1);
// reset data

output_high(PIN_B2); // set clock


}
output_low(PIN_B2);
// reset clock
temp = temp << 1;
}
output_high(PIN_B3);
// Enable HIGH to display
return;
}
//****************************************
void BIN2BCD(int16 Num)
{
int16 Thousands,Hundreds,Tens,Units;
int16 temp;
temp = Num;
Thousands = temp/1000;
temp = temp%1000;
Hundreds = temp/100;
temp = temp%100;
Tens = temp/10;
Units = temp%10;
BCD_Send(Thousands);
BCD_Send(Hundreds);
BCD_Send(Tens);
BCD_Send(Units);
return;
}
//****** Ham de Doc A/D tu Chan A0
void READ_SO()
{
set_adc_channel(1); //khai bao chan doc A/D
delay_us(10);
Doc_so=read_adc(); // so doc duoc tu A/D
}
//****************************************
main()
{
output_C(255); // bat sang toan bo Led port C
delay_ms(1000); //
output_C(0); // tat toan bo cac chan Port C
// Khai bao chay AD

setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc(ADC_CLOCK_INTERNAL);
while(1)
{
if(!input(PIN_A4)) // Read errors
{
delay_ms(200);
READ_SO();
BIN2BCD(Doc_so);
delay_ms(200);
output_c(Doc_so);
}
else
{
BIN2BCD(1200);
delay_ms(15);
output_c(0);
}
}
}

// using internal clock

// Vi du 5 tao thoi gian tre dau chuong trinh 5 giay


// Ung dung ==> neu cong tac ON ==> sang va toi LED port C
//
neu cong tac OFF ==> Sang LED
// Date: May 08, 2007
// Designed by Ha Ngoc Nguyen
//*********************
// PIN RC0 ~RC5 noi voi den LED
// PIN RA4 lam Switch
//*********************
// Led C5 sang chi dau hieu ready
// nhan nut SW RA4 bat dau tinh thoi gian ==> Den C5 tat
// Khi du 5 giay ==> Den C0 chop sang
#include <18F452.h>
//#include <16F877A.h>
#device ADC=8
#include <math.h>
#include <string.h>
#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
int16 Count1=0;
int16 Count2=0;
int16 Count3=0;

// Tao delay_timer_01
// Tao delay_timer_02
// Ttao delay tre giua chuong trinh

// ****** Chuong trinh con sang va toi mot LED ****


void CHOP_LED()
{
if(Count1<50)
{
output_high(PIN_C0);
}
else
{
output_low(PIN_C0);
if (Count1>100)
{
Count1=0;
}
}
}

//******* chuong trinh Ngat Timer 1 ************


#INT_TIMER1
void Sampling_Time()
{
Count1++;
Count2++;
Count3++;
set_timer1(54286);
// dem 65535(so) mat thoi gian 52.4 ms
}
// vay 10ms dem duoc 11250(so)
//******* Bat dau chuong trinh *************
main()
{
// Khai bao ban dau ( chi can 1 lan la du)
output_C(255); // bat sang toan bo Led port C
delay_ms(1000);
output_C(0); // tat toan bo Led Cong C
// cac khai bao de chay Interrup timer
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
//**Timmer max = 65536*4*4/OSC=65536*4/20000000=52.4ms**
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(1)
{
if(!input(PIN_A4))
// SW is ON
{
output_low(PIN_C5); // Tat tien Hieu ready <==> bat dau tinh thoi gian
while(Count3<500)
// tao tre 5 giay dau chuong trinh ***
{
output_high(PIN_C7);
// Dau hieu de biet dang delay 5 giay
}
if (Count3>40000) Count3=600; //reject loop while(Count5<30) ***
output_low(PIN_C0);
// Dau hieu de biet HET delay 5 giay
CHOP_LED();
}
else
// SW is OFF
{
output_low(PIN_C0); // Tat Pin C4
output_low(PIN_C7); // Tat Pin c7
output_high(PIN_C5); // dau hieu biet mach ready
Count3=0;
// reset bien tao tre 5 giay ***
}
}
}

// Vi du 6
// Ung dung ==> neu cong tac ON ==> sang va toi LED port C
//
neu cong tac OFF ==> Sang LED
// Date: May 08, 2007
// Designed by Ha Ngoc Nguyen
//*********************
// PIN RC0 ~RC5 noi voi den LED
// PIN RA4 lam Switch
//*********************
#include <18F452.h>
//#include <16F877A.h>
//#device ADC=16
//#device ADC=8
//#include <math.h>
//#include <string.h>
//#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
int16 Count1=0;
int16 Count2=0;
int16 Doc_so=0;

// Tao delay_timer_01
// Tao delay_timer_02

// ****** Chuong trinh con sang va toi mot LED ****


void CHOP_LED()
{
if(Count1<Doc_so)
{
output_high(PIN_C0);
}
else
{
output_low(PIN_C0);
if (Count1>130)
{
Count1=0;
}
}
}
//****** Ham de Doc A/D tu Chan A0
void READ_SO()
{
set_adc_channel(0);

//khai bao chan doc A/D

delay_us(10);
Doc_so=read_adc()/2; // so doc duoc tu A/D
}
//******* chuong trinh Ngat Timer 1 ************
#INT_TIMER1
void Sampling_Time()
{
Count1++;
Count2++;
set_timer1(54286);
// dem 65535(so) mat thoi gian 52.4 ms
}
// vay 10ms dem duoc 11250(so)
//******* Bat dau chuong trinh *************
main()
{
output_C(255); // bat sang toan bo Led port C
delay_ms(1000);
output_C(0); // tat toan bo Led Cong C
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
//**Timmer max = 65536*4*4/OSC=65536*4/20000000=52.4ms**
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc(ADC_CLOCK_INTERNAL);

// using internal clock

while(1)
{
if(!input(PIN_A4))
// SW is ON
{
READ_SO();
output_low(PIN_C2); // Tat LED C2
output_high(PIN_C7);
CHOP_LED();
// goi Ham CHOP_LED (Signal to known system is ON)
}
else
// SW is OFF
{
output_low(PIN_C0); // Tat LED C0
output_high(PIN_C2); // Sang LED C2 (Signal to known system is OFF)
output_low(PIN_C7);
}
}
}

// Vi Du 7 Doc AD va Xuat mo so ra LCD


// Date: May 08, 2007
// Designed by Ha Ngoc Nguyen
//*********************
// Pin RC0 ~ RC7 noi voi LED
// Pin RA4 noi voi SW
// Pin RD0 noi voi LCD
// Chan RA1 noi voi volume
//*********************
#include <18F452.h>
//#include <DEFFS_16F877A>
//#include <16F877A.h>
#device ADC=10
#include <math.h>
#include <string.h>
#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
//--------------------------------------------------int16 Count1 =0; //
int16 Count2 =0; // Bien thoi gian cho Test PIC
int16 Count3 =0; // Bien thoi gian cho LCD
int16 Doc_AD=0; // Bien doc AD
int16 Doc_Chia=0;
//****************************************
void READ_A0()
{
set_adc_channel(1);
delay_us(10);
Doc_AD=read_adc();
Doc_Chia=Doc_AD/3;
}
//*****************************************
void LCD() // the hien hang thu nha gia tri doc tu AD va AD/2
// Hang 2 the hien chu thong bao
{
#use rs232(baud=9600, parity=N, xmit=PIN_D0)
printf("%c%c",0xa3,0x01); //clear the scren
printf("%c%c%c",0xa1,0,0); // locate 0,0
printf("%cAD:%ld %c E3:%ld %c",0xa2,Doc_AD,0,Doc_Chia,0);

printf("%c%c%c",0xa1,0,1); // locate 0,1


printf("%c SYSTEM ON %c");
//printf("%cSPEED(mm/s): %ld %c",0xa2,15,0);
}
//*****************************************
void OFF_LCD() // the hien hang thu nha gia tri doc tu AD va AD/2 =0
// Hang 2 the hien chu thong bao
{
#use rs232(baud=9600, parity=N, xmit=PIN_D0)
printf("%c%c",0xa3,0x01); //clear the scren
printf("%c%c%c",0xa1,0,0); // locate 0,0
printf("%cAD:%ld %c E3:%ld %c",0xa2,0,0,0,0);
printf("%c%c%c",0xa1,0,1); // locate 0,1
printf("%c SYSTEM OFF %c");
}
//*******Chuong trinh lam cho Den LED C0 nhap nhay ********
void TEST_PIC()
{
if(Count2<5)
{
output_high(PIN_C0);
}
else
{
output_low(PIN_C0);
if (Count2>10)
{
Count2=0;
}
}
}
//*******************************************
#INT_TIMER1
void Sampling_Time()
{
Count1++; //
Count2++; // Cho Test PIC
Count3++; // Cho LCD
set_timer1(15700); // 100ms
(timer_max=54ms)
}
//*******************************************

void main()
{
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc(ADC_CLOCK_INTERNAL);

// using internal clock

setup_timer_1(T1_INTERNAL|T1_DIV_BY_4); //Timmer max = 26ms


//Timmer max = 65536*4*4/OSC=65536*4/4000000=65.5ms
enable_interrupts(INT_TIMER1);
enable_interrupts(global);
output_C(255); // bat sang toan bo Led port C
delay_ms(1000); //
output_C(0); // tat toan bo cac chan Port C
while(1)
{
if(!input(PIN_A4)) // Cong tac bat
{
TEST_PIC();
if(Count3>20) // Bo dinh thoi gian doc va xuat
{
READ_A0();
LCD();
Count3=0;
}
}
else
{
if(Count3>20) // Bo dinh thoi gian doc va xuat
{
OFF_LCD();
Count3=0;
output_low(PIN_C0);
}
}
}
}

Potrebbero piacerti anche