You are on page 1of 97

//BAI s 1 ADC=8

#include <16f887.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#device *=16 ADC=8
#use delay(clock=40000000)
int16
adc1,ketqua,ketqua1,ketqua2,ketqua3,ketqua4,ketqua5,ketqua6,ketqua7,ketq
ua8,ketqua9;
int32 adc;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int
led7doan1[10]={0b01000000,0b01111001,0b00100100,0b00110000,0b000
11001,0b00010010,
0b00000010,0b01111000,0b00000000,0b00010000};
#byte PortD=0x08
#bit LE1=PortD.1
LE2=PortD.3
LE3=PortD.5
LE4=PortD.7

#bit OE1=PortD.0
#bit OE2=PortD.2
#bit OE3=PortD.4
#bit OE4=PortD.6

void main()
{
setup_ADC(ADC_clock_internal);
set_tris_D(0x00000000);
OE1=1;
OE2=1;
OE4=1;
while(true)
{
adc=read_adc();
if(adc!=adc1)
{
LE1=1;
LE2=1;
LE4=1;
}

#bit
#bit
#bit

setup_ADC_ports(AN0);
OE3=1;

LE3=1;

adc1=adc;
adc=adc*1000;
ketqua=adc*5/255;
ketqua1=ketqua/1000;//PHAN NGAN
ketqua2=ketqua1*1000;
ketqua3=ketqua-ketqua2;
ketqua4=ketqua3/100;//PHAN TRAM
ketqua5=ketqua4*100;
ketqua6=ketqua3ketqua5;
ketqua7=ketqua6/10;//PHAN CHUC
ketqua8=ketqua7*10;
ketqua9=ketqua6-ketqua8;//PHAN DON VI
output_B(adc1);
OE1=0;
output_C(led7doan1[ketqua1]);
LE1=0;
OE2=0;
output_C(led7doan[ketqua4]);
LE2=0;
OE3=0;
output_C(led7doan[ketqua7]);
LE3=0;
OE4=0;
output_C(led7doan[ketqua9]);
LE4=0;
}
}
//BAI ADC 10bit
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#device *=16 ADC=10
#use delay(clock=40000000)
int16
adc1,ketqua,ketqua1,ketqua2,ketqua3,ketqua4,ketqua5,ketqua6,ketqua7,ketq
ua8,ketqua9;
int32 adc;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
2

int
led7doan1[10]={0b01000000,0b01111001,0b00100100,0b00110000,0b000
11001,0b00010010,
0b00000010,0b01111000,0b00000000,0b00010000};
#byte PortD=0x08 // 0x08 l a ch ca PortD
#bit OE1=PortD.0 //v tr bit trong PortD
#bit LE1=PortD.1
#bit OE2=PortD.2
#bit LE2=PortD.3
#bit OE3=PortD.4
#bit LE3=PortD.5
#bit OE4=PortD.6
#bit LE4=PortD.7
void main()
{
setup_ADC(ADC_clock_internal);//ci t thi gian ly mu bng xung
Clock IC
setup_ADC_ports(AN0);//Ly tn hiu tng t v in th ngun +5
Vchun vo t chn AN0
set_tris_D(0x00000000);//Cng D xut ra
OE1=1;//v tr bt u tin ti PortD=1
OE2=1;
OE3=1;
OE4=1;
while(true)
{
adc=read_adc();//gn adc bng c gi tr trong thanh ghi cha kt qu bin
i
if(adc!=adc1) //adc Khng bng adc1
{
LE1=1;
LE2=1;
LE3=1;
LE4=1;
}
adc1=adc;
adc=adc*1000;
ketqua=adc*5/1023;
3

ketqua1=ketqua/1000;//PHAN NGAN
ketqua2=ketqua1*1000;
ketqua3=ketqua-ketqua2;
ketqua4=ketqua3/100;//PHAN TRAM
ketqua5=ketqua4*100;
ketqua6=ketqua3-ketqua5;
ketqua7=ketqua6/10;//PHAN CHUC
ketqua8=ketqua7*10;
ketqua9=ketqua6-ketqua8;//PHAN DON VI
OE1=0;
output_C(led7doan1[ketqua1]);
LE1=0;
OE2=0;
output_C(led7doan[ketqua4]);
LE2=0;
OE3=0;
output_C(led7doan[ketqua7]);
LE3=0;
OE4=0;
output_C(led7doan[ketqua9]);
LE4=0;
}
}
//BAI chon kenh 8 bit
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#device *=16 ADC=8
#use delay(clock=20000000)

//int16
adc1,ketqua,ketqua1,ketqua2,ketqua3,ketqua4,ketqua5,ketqua6,ketqua7,ketq
ua8,ketqua9;
int8 adc,a=0;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int
led7doan1[10]={0b01000000,0b01111001,0b00100100,0b00110000,0b000
11001,0b00010010,
0b00000010,0b01111000,0b00000000,0b00010000};
//A0,A1,A2,A5,E0,E1,E2
#int_ext
void ngat_ngoai()
{
a=a+1;
if(a==1)
{
output_D(led7doan[1]);// xut cng D gi tr led7doan[1]
set_ADC_channel(0);//chn chn A0 c vo gi tr analog
}
if(a==2)
{
output_D(led7doan[2]);
set_ADC_channel(1);//chn chn A1 c vo gi tr analog
}
if(a==3)
{
output_D(led7doan[3]);
set_ADC_channel(2);//A2
}
if(a==4)
{
output_D(led7doan[4]);
set_ADC_channel(4);//A5
}
if(a==5)
{
5

output_D(led7doan[5]);
set_ADC_channel(5);//E0
}
if(a==6)
{
output_D(led7doan[6]);
set_ADC_channel(6);//E1
}
if(a==7)
{
output_D(led7doan[7]);
set_ADC_channel(7);//E2
}
if(a>7)
{
a=0;
output_D(led7doan[0]);
}
}
void main()
{
setup_ADC(ADC_clock_internal);xc nh hot ng b bin i ADC
setup_ADC_ports(AN0_AN1_AN2_AN4_AN5_AN6_AN7_VSS_VREF);//
ext_int_edge( H_TO_L );// ci t dng xung u vo l sn dng
enable_interrupts(INT_EXT);//cho php ngt
enable_interrupts(GLOBAL);// cho php ngt ton cc
output_D(led7doan[0]);//cng D xut gi tr Led7doan[0]
while(true)
{
;
if(a>0)
{
adc=read_adc();gn gi tr
output_C(adc);//cng C xut gi tr adc
}
}
}

//BAI s 2 IC Nhiet do
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#device *=16 ADC=10
#use delay(clock=20000000)
int16 adc=0,adc1=0,adc2=0,adc3=0,a=0;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
//A0,A1,A2,A5,E0,E1,E2
#int_ext
void ngat_ngoai()
{
a=a+1;
if(a==1)
{
set_ADC_channel(0);//A0
}
if(a==2)
{
set_ADC_channel(1);//A1
}
if(a>2)
{
a=0;
}
}
void main()
{
setup_ADC(ADC_clock_internal);
setup_ADC_ports(AN0_AN1_VSS_VREF);
ext_int_edge( H_TO_L );
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
7

output_C(0xFF);
output_D(0xFF);
while(true)
{
;
if(a>0)
{
adc=read_adc();
if(adc>255)
{
adc=adc-255;
output_high(pin_B6);
output_C(led7doan[adc]);
}
/*
adc1=adc*100*2/1023;
adc2=adc1/10;
adc3=adc1-adc2*10;
output_D(led7doan[adc3]);
output_C(led7doan[adc2]);
*/
}
}
}
//BAI s 3DEN GIAO THONG
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD
#use delay(clock=20000000)
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
signed int i;//I l bit c du
int1 vang_enable=0;

#byte PortC=0x07
#bit DA=PortC.0
#bit VA=PortC.1
#bit XA=PortC.2
#bit DB=PortC.3
#bit VB=PortC.4
#bit XB=PortC.5
#bit OE5=PortC.6
#bit LE5=PortC.7
#byte PortD=0x08
#bit OE1=PortD.0
#bit LE1=PortD.1
#bit OE2=PortD.2
#bit LE2=PortD.3
#bit OE3=PortD.4
#bit LE3=PortD.5
#bit OE4=PortD.6
#bit LE4=PortD.7
void main()
{
set_tris_C(0b00000000);
set_tris_D(0b00000000);
output_D(0b1111111);
output_high(pin_C6);
output_high(pin_C7);
output_high(pin_A0);
output_high(pin_A1);
while(true)
{
DA=1;
XB=1;
for(i=7;i>=0;i--)
{
OE1=0;
LE1=1;
output_B(led7doan[i+2]);
LE1=0;
9

output_low(pin_A0);
output_high(pin_A1);
output_B(led7doan[i]);
output_low(pin_A1);
delay_ms(1000);
}
for(i=1;i>=0;i--)
{
VB=1;
XB=0;
OE1=0;
LE1=1;
output_B(led7doan[i]);
LE1=0;
OE5=0;
LE5=1;
output_B(led7doan[i]);
LE5=0;
output_high(pin_A0);
output_high(pin_A1);
delay_ms(1000);
}
DB=1;
XA=1;
VB=0;
DA=0;
for(i=7;i>=0;i--)
{
OE1=1;
LE1=1;
OE5=1;
LE5=1;
OE4=0;
LE4=1;
output_B(led7doan[i+2]);
LE4=0;
OE3=0;
LE3=1;
10

output_B(led7doan[i]);
LE3=0;
delay_ms(1000);
}
for(i=1;i>=0;i--)
{
VA=1;
XA=0;
OE4=0;
LE4=1;
output_B(led7doan[i]);
LE4=0;
OE2=0;
LE2=1;
output_B(led7doan[i]);
LE2=0;
OE3=1;
LE3=1;
delay_ms(1000);
}
DB=0;
VA=0;
}
}
///BAI Dieu khien dong co
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int16 a=0;
int16 time=0,hoang=0,kiemtra=0,xung_thuc_te;
int8 dem1=0,dem2=0,tocdo=0;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
float Kp,Ki,Kd,T,a0,a1,a2,e,e1=0,e2=0,u,u1=0;
int8 do_rong_xung;
11

// PHAN THIET LAP TOC DO QUAY CUA DONG CO


#INT_RB
void ngat_PortB()
{
if(input(pin_B4)==0)
{
dem1=dem1+1;
if(dem1>9)
{
dem1=0;
}
output_high(pin_C4);
output_D(led7doan[dem1]);
output_low(pin_C4);
}
if(input(pin_B5)==0)
{
dem2=dem2+1;
if(dem2>9)
{
dem2=0;
}
output_high(pin_C5);
output_D(led7doan[dem2]);
output_low(pin_C5);
}
if(input(pin_B6)==0)
{
tocdo=dem1*10+dem2;
xung_thuc_te=(tocdo*2)/12;//Tinh Xung Thuc Te Chay Trong 50ms
set_pwm1_duty(100);
disable_interrupts(INT_RB);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_CCP2);
}
}
#INT_RTCC
12

void ngat_timer0()
{
time=time+1;
kiemtra=kiemtra+1;
set_timer0(5);
if(kiemtra>=78)
{
output_B(hoang);
hoang=0;
kiemtra=0;
}
if(time>=4)
{
e=a-xung_thuc_te;
u=u1+a0*e+a1*e1+a2*e2;
u1=u;
e2=e1;
e1=e;
if(u<=0)
{
do_rong_xung=0;
}
else if(u>=12)
{
do_rong_xung=250;
}
else
{
do_rong_xung=(int8)(u*20.833);
}
set_pwm1_duty(do_rong_xung);
a=0;
time=0;
}
}

13

#INT_CCP2
void ngat_CCP2()
{
a=a+1;
hoang=hoang+1;
}
void main()
{
Kp=0.058;//Chi su dung khau Kp=0.05,khong dung Ki va Kd.
Ki=0.00004;
Kd=0.1;
T=0.1;
a0=Kp+(Ki*T)/2+Kd/T;
a1=-Kp+(Ki*T)/2-(2*Kd)/T;
a2=Kd/T;
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_CAPTURE_RE);// CCP_CAPTURE_DIV_1
setup_timer_2(t2_div_by_16,250,1);
set_pwm1_duty(0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
set_timer0(5);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
output_D(led7doan[0]);
output_low(pin_C4);
output_low(pin_C5);
output_high(pin_C6);
output_low(pin_C7);
while(true)
{
;
}
}
//Dieu khien ON-OFF Dong co
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
14

#use delay(clock=20000000)
int16 a=0;
int16 time=0,hoang=0,kiemtra=0,xung_thuc_te;
int8 dem1=0,dem2=0,tocdo=0;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int8 dotre=0;
int8 min_xung,max_xung;
signed int8 saiso=0;
// PHAN THIET LAP TOC DO QUAY CUA DONG CO
#INT_RB
void ngat_PortB()
{
if(input(pin_B4)==0)
{
dem1=dem1+1;
if(dem1>9)
{
dem1=0;
}
output_high(pin_C4);
output_D(led7doan[dem1]);
output_low(pin_C4);
}
if(input(pin_B5)==0)
{
dem2=dem2+1;
if(dem2>9)
{
dem2=0;
}
output_high(pin_C5);
output_D(led7doan[dem2]);
output_low(pin_C5);
}
if(input(pin_B6)==0)
15

{
tocdo=dem1*10+dem2;
xung_thuc_te=(tocdo*2)/6;//Tinh Xung Thuc Te Chay Trong 100ms
if(xung_thuc_te<5)
{
min_xung=20+xung_thuc_te;
max_xung=50+xung_thuc_te;
}
else if((xung_thuc_te>=5)&(xung_thuc_te<15))
{
min_xung=40+xung_thuc_te*2;
max_xung=80+xung_thuc_te*2;
}
else
{
min_xung=50+xung_thuc_te*3;
max_xung=90+xung_thuc_te*3;
}
set_pwm1_duty(100);
disable_interrupts(INT_RB);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_CCP2);
}
}
#INT_RTCC
void ngat_timer0()
{
time=time+1;
kiemtra=kiemtra+1;
set_timer0(5);
if(kiemtra>=78)
{
output_B(hoang);
hoang=0;
kiemtra=0;
}
if(time>=8)
16

{
saiso=a-xung_thuc_te;
if(saiso>dotre)
{
set_pwm1_duty(min_xung);//60
}
else
{
set_pwm1_duty(max_xung);//100
}
time=0;
a=0;
}
}
#INT_CCP2
void ngat_CCP2()
{
a=a+1;
hoang=hoang+1;
}
void main()
{
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_CAPTURE_RE);// CCP_CAPTURE_DIV_1
setup_timer_2(t2_div_by_16,250,1);
set_pwm1_duty(0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
set_timer0(5);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
output_D(led7doan[0]);
output_low(pin_C4);
output_low(pin_C5);
output_high(pin_C6);
17

output_low(pin_C7);
while(true)
{
;
}
}
// Bai Dieu khien dong co ON-OFF 11111
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int16 a=0;
int16 time=0;
int16 kiemtra=0,hoang=0;
//int8 dem1=0,dem2=0,tocdo=0;
//int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
//0b10000010,0b11111000,0b10000000,0b10010000};
#INT_RTCC
void ngat_timer0()
{
time=time+1;
kiemtra=kiemtra+1;
set_timer0(5);
if(kiemtra>=78)
{
output_B(hoang);
hoang=0;
kiemtra=0;
}
if(time>=8)
{
if(a>10)
18

{
set_pwm1_duty(40);//140
}
else
{
set_pwm1_duty(80);
}
time=0;
a=0;
}
}
#INT_CCP2
void ngat_CCP2()
{
a=a+1;
hoang=hoang+1;
}
void main()
{
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_CAPTURE_RE);//CCP_CAPTURE_DIV_4
setup_timer_2(t2_div_by_16,250,1);
set_pwm1_duty(100);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
set_timer0(5);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);
output_high(pin_C6);
output_low(pin_C7);
while(true)
{
;
}
}
19

//BAI Dieu khien PID Dong co


#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int16 a=0;
int16 time=0,hoang=0,kiemtra=0,xung_thuc_te;
int8 dem1=0,dem2=0,tocdo=0;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
float Kp,Ki,Kd,T,a0,a1,a2,e,e1=0,e2=0,u,u1=0;
int8 do_rong_xung;
// PHAN THIET LAP TOC DO QUAY CUA DONG CO
#INT_RB
void ngat_PortB()
{
if(input(pin_B4)==0)
{
dem1=dem1+1;
if(dem1>9)
{
dem1=0;
}
output_high(pin_C4);
output_D(led7doan[dem1]);
output_low(pin_C4);
}
if(input(pin_B5)==0)
{
dem2=dem2+1;
if(dem2>9)
{
dem2=0;
}
output_high(pin_C5);
output_D(led7doan[dem2]);
output_low(pin_C5);
20

}
if(input(pin_B6)==0)
{
tocdo=dem1*10+dem2;
xung_thuc_te=(tocdo*2)/12;//Tinh Xung Thuc Te Chay Trong 50ms
set_pwm1_duty(100);
disable_interrupts(INT_RB);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_CCP2);
}
}
#INT_RTCC
void ngat_timer0()
{
time=time+1;
kiemtra=kiemtra+1;
set_timer0(5);
if(kiemtra>=78)
{
output_B(hoang);
hoang=0;
kiemtra=0;
}
if(time>=4)
{
e=a-xung_thuc_te;
u=u1+a0*e+a1*e1+a2*e2;
u1=u;
e2=e1;
e1=e;
if(u<=0)
{
do_rong_xung=0;
}
else if(u>=12)
{
21

do_rong_xung=250;
}
else
{
do_rong_xung=(int8)(u*20.833);
}
set_pwm1_duty(do_rong_xung);
a=0;
time=0;
}
}
#INT_CCP2
void ngat_CCP2()
{
a=a+1;
hoang=hoang+1;
}
void main()
{
Kp=0.058;//Chi su dung khau Kp=0.05,khong dung Ki va Kd.
Ki=0.00004;
Kd=0.1;
T=0.1;
a0=Kp+(Ki*T)/2+Kd/T;
a1=-Kp+(Ki*T)/2-(2*Kd)/T;
a2=Kd/T;
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_CAPTURE_RE);// CCP_CAPTURE_DIV_1
setup_timer_2(t2_div_by_16,250,1);
set_pwm1_duty(0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
set_timer0(5);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
output_D(led7doan[0]);
22

output_low(pin_C4);
output_low(pin_C5);
output_high(pin_C6);
output_low(pin_C7);
while(true)
{
;
}
}
//BAI DIEU KHIEN PID DONG CO
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int16 a=0;
int16 time=0,hoang=0,kiemtra=0,xung_thuc_te;
int8 dem1=0,dem2=0,tocdo=0;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
float Kp,Ki,Kd,T,a0,a1,a2,e,e1=0,e2=0,u,u1=0;
int8 do_rong_xung;
// PHAN THIET LAP TOC DO QUAY CUA DONG CO
#INT_RB
void ngat_PortB()
{
if(input(pin_B4)==0)
{
dem1=dem1+1;
if(dem1>9)
{
dem1=0;
}
output_high(pin_C4);
output_D(led7doan[dem1]);
output_low(pin_C4);
}

23

if(input(pin_B5)==0)
{
dem2=dem2+1;
if(dem2>9)
{
dem2=0;
}
output_high(pin_C5);
output_D(led7doan[dem2]);
output_low(pin_C5);
}
if(input(pin_B6)==0)
{
tocdo=dem1*10+dem2;
xung_thuc_te=(tocdo*2)/12;//Tinh Xung Thuc Te Chay Trong 50ms
set_pwm1_duty(100);
disable_interrupts(INT_RB);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_CCP2);
}
}
#INT_RTCC
void ngat_timer0()
{
time=time+1;
kiemtra=kiemtra+1;
set_timer0(5);
if(kiemtra>=78)
{
output_B(hoang);
hoang=0;
kiemtra=0;
}
if(time>=4)
{
e=a-xung_thuc_te;
24

u=u1+a0*e+a1*e1+a2*e2;
u1=u;
e2=e1;
e1=e;
if(u<=0)
{
do_rong_xung=0;
}
else if(u>=12)
{
do_rong_xung=250;
}
else
{
do_rong_xung=(int8)(u*20.833);
}
set_pwm1_duty(do_rong_xung);
a=0;
time=0;
}
}
#INT_CCP2
void ngat_CCP2()
{
a=a+1;
hoang=hoang+1;
}
void main()
{
Kp=0.058;//Chi su dung khau Kp=0.05,khong dung Ki va Kd.
Ki=0.00004;
Kd=0.1;
T=0.1;
a0=Kp+(Ki*T)/2+Kd/T;
a1=-Kp+(Ki*T)/2-(2*Kd)/T;
25

a2=Kd/T;
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_CAPTURE_RE);// CCP_CAPTURE_DIV_1
setup_timer_2(t2_div_by_16,250,1);
set_pwm1_duty(0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
set_timer0(5);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
output_D(led7doan[0]);
output_low(pin_C4);
output_low(pin_C5);
output_high(pin_C6);
output_low(pin_C7);
while(true)
{
;
}
}
// Bai Dieu khien PID DONG CO
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int16 a=0;
int16 time=0,hoang=0,kiemtra=0,xung_thuc_te;
int8 dem1=0,dem2=0,tocdo=0;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
float Kp,Ki,Kd,T,a0,a1,a2,e,e1=0,e2=0,u,u1=0;
int8 do_rong_xung;
// PHAN THIET LAP TOC DO QUAY CUA DONG CO
#INT_RB
void ngat_PortB()
{
if(input(pin_B4)==0)
{
dem1=dem1+1;
26

if(dem1>9)
{
dem1=0;
}
output_high(pin_C4);
output_D(led7doan[dem1]);
output_low(pin_C4);
}
if(input(pin_B5)==0)
{
dem2=dem2+1;
if(dem2>9)
{
dem2=0;
}
output_high(pin_C5);
output_D(led7doan[dem2]);
output_low(pin_C5);
}
if(input(pin_B6)==0)
{
tocdo=dem1*10+dem2;
xung_thuc_te=(tocdo*2)/12;//Tinh Xung Thuc Te Chay Trong 50ms
set_pwm1_duty(100);
disable_interrupts(INT_RB);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_CCP2);
}
}
#INT_RTCC
void ngat_timer0()
{
time=time+1;
kiemtra=kiemtra+1;
set_timer0(5);
if(kiemtra>=78)
27

{
output_B(hoang);
hoang=0;
kiemtra=0;
}
if(time>=4)
{
e=a-xung_thuc_te;
u=u1+a0*e+a1*e1+a2*e2;
u1=u;
e2=e1;
e1=e;
if(u<=0)
{
do_rong_xung=0;
}
else if(u>=12)
{
do_rong_xung=250;
}
else
{
do_rong_xung=(int8)(u*20.833);
}
set_pwm1_duty(do_rong_xung);
a=0;
time=0;
}
}
#INT_CCP2
void ngat_CCP2()
{
a=a+1;
hoang=hoang+1;
}

28

void main()
{
Kp=0.058;//Chi su dung khau Kp=0.05,khong dung Ki va Kd.
Ki=0.00004;
Kd=0.1;
T=0.1;
a0=Kp+(Ki*T)/2+Kd/T;
a1=-Kp+(Ki*T)/2-(2*Kd)/T;
a2=Kd/T;
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_CAPTURE_RE);// CCP_CAPTURE_DIV_1
setup_timer_2(t2_div_by_16,250,1);
set_pwm1_duty(0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
set_timer0(5);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
output_D(led7doan[0]);
output_low(pin_C4);
output_low(pin_C5);
output_high(pin_C6);
output_low(pin_C7);
while(true)
{
;
}
}
//Bai Dong co Capture _PWM
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int8 a=0;
#INT_CCP2
void ngat_CCP2()
{
a=a+16;
output_B(a);
29

if(a>=240)
{
output_low(pin_D0);
setup_ccp1(CCP_OFF);
}
}
void main()
{
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_CAPTURE_DIV_16);
setup_timer_2(t2_div_by_16,250,1);
set_pwm1_duty(150);
output_high(pin_D0);
output_low(pin_D1);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);
while(true)
{
;
}
}
//BAI dong co dung pwm
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
void main()
{
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
setup_timer_2(t2_div_by_16,250,1);
set_pwm1_duty(200);
set_pwm2_duty(100);
output_high(pin_D0);
output_low(pin_D1);
output_high(pin_D2);
output_low(pin_D3);
while(true)
30

{
;
}
}
/// BAI IC_2 _MASTER
IC_2 _MASTER
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use I2C(master,sda=PIN_C4, scl=PIN_C3)
int8 a=0;
#INT_EXT
void ngat_ngoai()
{
a=a+1;
if(a==1)
{
i2c_start();
i2c_write(0x0a);
delay_us(50);
i2c_write(0b11000000);// Low byte of command
i2c_stop(); // Stop condition
}
if(a==2)
{
i2c_start();
i2c_write(0xa0);
delay_us(50);
i2c_write(0b00001111);// Low byte of command
i2c_stop(); // Stop condition
}
if(a==3)
{
i2c_start();
i2c_write(0x0b);
i2c_write(0b00111100);// Low byte of command
31

i2c_stop();
a=0;
}

// Stop condition

}
void main()
{
ext_int_edge(H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
while (true)
{
;
}
}
// i2c_slave_1
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0xa0)
int8 b=0;
int8 c=0;
#INT_SSP
void ngat()
{
//c=c+1;
//if(c>=2)
//{
while (!i2c_poll());
b = i2c_read();
//}
}
void main()
{
32

enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while (true)
{
output_D(b);
;
}
}
//i2c_slave_2
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0x0b)
int8 data3=0;
int8 d=0;
#INT_SSP
void ngat()
{
//d=d+1;
//if(d>=2)
//{
data3 = i2c_read(1);
output_D(data3);
//}
}
void main()
{
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while (true)
{
;
}
}
//i2c_slave
#include <16f877a.h>

33

#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,


NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0x0a)
int8 a=0;
#INT_SSP
void ngat()
{
while (!i2c_poll());
a = i2c_read();
}
void main()
{
//,sda=PIN_C4,scl=PIN_C3
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
output_D(0x00);
while (true)
{
output_D(a);
;
}
}
//BAI I2C_MASTER
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0x0a)
int8 a=0;
#INT_SSP
void ngat()
{
while (!i2c_poll());
a = i2c_read();
}

34

void main()
{
//,sda=PIN_C4,scl=PIN_C3
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
output_D(0x00);
while (true)
{
output_D(a);
;
}
}
//i2c_slave_1
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0x0a)
int8 a=0;
#INT_SSP
void ngat()
{
while (!i2c_poll());
a = i2c_read();
}
void main()
{
//,sda=PIN_C4,scl=PIN_C3
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
output_D(0x00);
while (true)
{
output_D(a);
;
}
}
//i2c_slave2
35

#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0x0b)
int8 data3=0;
int8 d=0;
#INT_SSP
void ngat()
{
//d=d+1;
//if(d>=2)
//{
data3 = i2c_read(1);
output_D(data3);
//}
}
void main()
{
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while (true)
{
;
}
}
//I2C_Slave
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0x0a)
int8 a=0;
#INT_SSP
void ngat()
{
while (!i2c_poll());
36

a = i2c_read();
}
void main()
{
//,sda=PIN_C4,scl=PIN_C3
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
output_D(0x00);
while (true)
{
output_D(a);
;
}
}
//BAI LCD16_2
#include <16F877A.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int8
chu[15]={0x41,0x43,0x44,0x45,0x47,0x48,0x4E,0x4F,0x55,0x59,0x56,0x4
9,0x4B,0x54,0x4D };
int8 h1,h2,i=1;
int16 thoi_gian_chay_chu=1000;
int8 chay1=0x93,chay2=0xA7;
int8
enable1,enable2,enable3,enable4,enable5,enable6,enable7,enable8,enable9,e
nable10;
//int8
enable11,enable12,enable13,enable14,enable15,enable16,enable17,enable18
,enable19,enable20,enable21;
//int8
enable22=1,enable23,enable24,enable25,enable26,enable27,enable28,enable
29,enable30;
//int8
enable31,enable32,enable33,enable34,enable35,enable36,enable37,enable38
,enable39,enable40;
//"A":0x41 : Vi tri 0
37

//"C":0x43 : Vi tri 1
//"D":0x44 : Vi tri 2
//"E":0x45 : Vi tri 3
//"G":0x47 : Vi tri 4
//"H":0x48 : Vi tri 5
//"N":0x4E : Vi tri 6
//"O":0x4F : Vi tri 7
//"U":0x55 : Vi tri 8
//"Y":0x59 : Vi tri 9
//"V":0x56 : Vi tri 10
//"I":0x49 : Vi tri 11
//"K":0x4B : Vi tri 12
//"T":0x54 : Vi tri 13
//"M":0x5D : Vi tri 14
void WAIT_LCD()
{
output_high(pin_B7);
output_low(pin_B5);
output_high(pin_B6);
output_D(0xff);
while(input(pin_D7))
{
;
}
output_low(pin_B7);
output_low(pin_B6);
}
void INIT_LCD()
{
output_high(pin_B7);
output_low(pin_B5);
output_D(0x3C);
output_low(pin_B7);
WAIT_LCD();
output_high(pin_B7);
output_low(pin_B5);
38

output_D(0x0E);
output_low(pin_B7);
WAIT_LCD();
output_high(pin_B7);
output_low(pin_B5);
output_D(0x06);
output_low(pin_B7);
WAIT_LCD();
}
void CLEAR_LCD()
{
output_high(pin_B7);
output_low(pin_B5);
output_D(0x01);
output_low(pin_B7);
delay_ms(i);
}
void WRITE_TEXT(unsigned int8 X)
{
output_high(pin_B7);
output_high(pin_B5);
output_D(X);
output_low(pin_B7);
delay_ms(i);
}
void init_1(int chay1)
{
output_high(pin_B7);
output_low(pin_B5);
output_D(chay1);
output_low(pin_B7);
delay_ms(i);
}
void init_2(int chay2)
39

{
output_high(pin_B7);
output_low(pin_B5);
output_D(chay2);
output_low(pin_B7);
delay_ms(i);
}
void chaychu1()
{
for (h1=0;h1<40;h1++)
{
enable1=enable1+1;
enable2=enable2+1;
enable3=enable3+1;
enable4=enable4+1;
enable5=enable5+1;
enable6=enable6+1;
if(enable1==1)
{
init_1(chay1);
WRITE_TEXT(chu[6]);//N////////////////////////////////
init_2(chay2);
WRITE_TEXT(chu[5]);//H
}
if(enable2==2)
{
init_1(chay1);
WRITE_TEXT(chu[6]);//N////////////////////////////////
WRITE_TEXT(chu[4]);//G///////////////////////////////
init_2(chay2);
WRITE_TEXT(chu[5]);//H
WRITE_TEXT(chu[0]);//A
}
if(enable3==3)
40

{
init_1(chay1);
WRITE_TEXT(chu[6]);//N////////////////////////////////
WRITE_TEXT(chu[4]);//G///////////////////////////////
WRITE_TEXT(chu[8]);//U//////////////////////////////
init_2(chay2);
WRITE_TEXT(chu[5]);//H
WRITE_TEXT(chu[0]);//A
WRITE_TEXT(chu[6]);//N
}
if(enable4==4)
{
init_1(chay1);
WRITE_TEXT(chu[6]);//N////////////////////////////////
WRITE_TEXT(chu[4]);//G///////////////////////////////
WRITE_TEXT(chu[8]);//U//////////////////////////////
WRITE_TEXT(chu[9]);//Y///////////////////////////////
init_2(chay2);
WRITE_TEXT(chu[5]);//H
WRITE_TEXT(chu[0]);//A
WRITE_TEXT(chu[6]);//N
WRITE_TEXT(chu[4]);//G
}
if(enable5==5)
{
init_1(chay1);
WRITE_TEXT(chu[6]);//N////////////////////////////////
WRITE_TEXT(chu[4]);//G///////////////////////////////
WRITE_TEXT(chu[8]);//U//////////////////////////////
WRITE_TEXT(chu[9]);//Y///////////////////////////////
WRITE_TEXT(chu[3]);//E///////////////////////////////
init_2(chay2);
WRITE_TEXT(chu[5]);//H
WRITE_TEXT(chu[0]);//A
WRITE_TEXT(chu[6]);//N
41

WRITE_TEXT(chu[4]);//G
WRITE_TEXT(chu[12]);//K
}
delay_ms(thoi_gian_chay_chu);
CLEAR_LCD();
chay1=chay1-1;
chay2=chay2-1;
}
}
void main()
{
INIT_LCD();
CLEAR_LCD();
chaychu1();
}
//bai LCD_OK
//lcd.c
#include <16F877A.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int8
chu[15]={0x41,0x43,0x44,0x45,0x47,0x48,0x4E,0x4F,0x55,0x59,0x56,0x4
9,0x4B,0x54,0x4D };
int8 k=2,dem;
int8 h,m=2,thoi_gian_chay_chu=50,i=3;
int8 chay=0xA7;
int8
enable1=1,enable2,enable3,enable4,enable5,enable6,enable7,enable8,enable
9,enable10;
int8
enable11,enable12,enable13,enable14,enable15,enable16,enable17,enable18
,enable19,enable20;
42

int8
enable21,enable22,enable23,enable24,enable25,enable26,enable27,enable28
,enable29,enable30;
int8
enable31,enable32,enable33,enable34,enable35,enable36,enable37,enable38
,enable39,enable40,enable41;
//"A":0x41 : Vi tri 0
//"C":0x43 : Vi tri 1
//"D":0x44 : Vi tri 2
//"E":0x45 : Vi tri 3
//"G":0x47 : Vi tri 4
//"H":0x48 : Vi tri 5
//"N":0x4E : Vi tri 6
//"O":0x4F : Vi tri 7
//"U":0x55 : Vi tri 8
//"Y":0x59 : Vi tri 9
//"V":0x56 : Vi tri 10
//"I":0x49 : Vi tri 11
//"K":0x4B : Vi tri 12
//"T":0x54 : Vi tri 13
//"M":0x5D : Vi tri 14
void WAIT_LCD()
{
output_high(pin_B7);
output_low(pin_B5);
output_high(pin_B6);
output_D(0xff);
while(input(pin_D7))
{
;
}
output_low(pin_B7);
output_low(pin_B6);
}
void INIT_LCD()
{
output_high(pin_B7);
43

output_low(pin_B5);
output_D(0x38);
output_low(pin_B7);
WAIT_LCD();
output_high(pin_B7);
output_low(pin_B5);
output_D(0x0C);
output_low(pin_B7);
WAIT_LCD();
output_high(pin_B7);
output_low(pin_B5);
output_D(0x06);
output_low(pin_B7);
WAIT_LCD();
}
void CLEAR_LCD()
{
output_high(pin_B7);
output_low(pin_B5);
output_D(0x01);
output_low(pin_B5);
delay_ms(i);
}
void WRITE_TEXT(unsigned int8 X)
{
output_high(pin_B7);
output_high(pin_B5);
output_D(X);
output_low(pin_B7);
delay_ms(i);
}
void chaychu1()
{
for (h=0;h<40;h++)
44

{
output_high(pin_B7);
output_low(pin_B5);
output_D(chay);
output_low(pin_B7);
delay_ms(10);
if(enable1==1)
{
WRITE_TEXT(chu[6]);//N
enable2=enable2+1;
}
if(enable2==2)
{
WRITE_TEXT(chu[4]);//G
enable2=1;
enable3=enable3+1;
}
if(enable3==2)
{
WRITE_TEXT(chu[8]);//U
enable3=1;
enable4=enable4+1;
}
if(enable4==2)
{
WRITE_TEXT(chu[9]);//Y
enable4=1;
enable5=enable5+1;
}
if(enable5==2)
{
WRITE_TEXT(chu[3]);//E
enable5=1;
enable6=enable6+1;
}
if(enable6==2)
{
WRITE_TEXT(chu[6]);//N
enable6=1;
45

enable7=enable7+1;
}
if(enable7==2)
{
WRITE_TEXT(0x11);
enable7=1;
enable8=enable8+1;
}
if(enable8==2)
{
WRITE_TEXT(chu[2]);//D
enable8=1;
enable9=enable9+1;
}
if(enable9==2)
{
WRITE_TEXT(chu[8]);//U
enable9=1;
enable10=enable10+1;
}
if(enable10==2)
{
WRITE_TEXT(chu[1]);//C
enable10=1;
enable11=enable11+1;
}
if(enable11==2)
{
WRITE_TEXT(0x11);
enable11=1;
enable12=enable12+1;
}
if(enable12==2)
{
WRITE_TEXT(chu[6]);//N
enable12=1;
enable13=enable13+1;
}
if(enable13==2)
{
46

WRITE_TEXT(chu[4]);//G
enable13=1;
enable14=enable14+1;
}
if(enable14==2)
{
WRITE_TEXT(chu[7]);//O
enable14=1;
enable15=enable15+1;
}
if(enable15==2)
{
WRITE_TEXT(chu[1]);//C
enable15=1;
enable16=enable16+1;
}
if(enable16==2)
{
WRITE_TEXT(0x11);
enable16=1;
enable17=enable17+1;
}
if(enable17==2)
{
WRITE_TEXT(chu[5]);//H
enable17=1;
enable18=enable18+1;
}
if(enable18==2)
{
WRITE_TEXT(chu[7]);//O
enable18=1;
enable19=enable19+1;
}
if(enable19==2)
{
WRITE_TEXT(chu[0]);//A
enable19=1;
enable20=enable20+1;
}
47

if(enable20==2)
{
WRITE_TEXT(chu[6]);//N
enable20=1;
enable21=enable21+1;
}
if(enable21==2)
{
WRITE_TEXT(chu[4]);//G
enable21=1;
enable22=enable22+1;
}
if(enable22==2)
{
WRITE_TEXT(0x11);
enable22=1;
enable23=enable23+1;
}
if(enable23==2)
{
WRITE_TEXT(chu[5]);//H
enable23=1;
enable24=enable24+1;
}
if(enable24==2)
{
WRITE_TEXT(chu[0]);//A
enable24=1;
enable25=enable25+1;
}
if(enable25==2)
{
WRITE_TEXT(chu[6]);//N
enable25=1;
enable26=enable26+1;
}
if(enable26==2)
{
WRITE_TEXT(chu[4]);//G
enable26=1;
48

enable27=enable27+1;
}
if(enable27==2)
{
WRITE_TEXT(0x11);
enable27=1;
enable28=enable28+1;
}
if(enable28==2)
{
WRITE_TEXT(chu[12]);//K
enable28=1;
enable29=enable29+1;
}
if(enable29==2)
{
WRITE_TEXT(chu[5]);//H
enable29=1;
enable30=enable30+1;
}
if(enable30==2)
{
WRITE_TEXT(chu[7]);//O
enable30=1;
enable31=enable31+1;
}
if(enable31==2)
{
WRITE_TEXT(chu[6]);//N
enable31=1;
enable32=enable32+1;
}
if(enable32==2)
{
WRITE_TEXT(chu[4]);//G
enable32=1;
enable33=enable33+1;
}
if(enable33==2)
{
49

WRITE_TEXT(0x11);
enable33=1;
enable34=enable34+1;
}
if(enable34==2)
{
WRITE_TEXT(chu[10]);//V
enable34=1;
enable35=enable35+1;
}
if(enable35==2)
{
WRITE_TEXT(chu[11]);//I
enable35=1;
enable36=enable36+1;
}
if(enable36==2)
{
WRITE_TEXT(chu[3]);//E
enable36=1;
enable37=enable37+1;
}
if(enable37==2)
{
WRITE_TEXT(chu[13]);//T
enable37=1;
enable38=enable38+1;
}
if(enable38==2)
{
WRITE_TEXT(0x11);
enable38=1;
enable39=enable39+1;
}
if(enable39==2)
{
WRITE_TEXT(chu[6]);//N
enable39=1;
enable40=enable40+1;
}
50

if(enable40==2)
{
WRITE_TEXT(chu[0]);//A
enable40=1;
enable41=enable41+1;
}
if(enable41==2)
{
WRITE_TEXT(chu[14]);//M
enable41=1;
}
delay_ms(thoi_gian_chay_chu);
CLEAR_LCD();
chay=chay-1;
}
}
void chaychu2()
{
for (dem=0;dem<42;dem++)
{
output_high(pin_B7);
output_low(pin_B5);
output_D(0x80);
output_low(pin_B7);
if (k==1)
{
WRITE_TEXT(chu[6]);//N
k=k+1;
}
if (k==2)
{
WRITE_TEXT(chu[4]);//G
k=k+1;
}
if (k==3)
{
WRITE_TEXT(chu[8]);//U
51

k=k+1;
}
if (k==4)
{
WRITE_TEXT(chu[9]);//Y
k=k+1;
}
if (k==5)
{
WRITE_TEXT(chu[3]);//E
k=k+1;
}
if (k==6)
{
WRITE_TEXT(chu[6]);//N
k=k+1;
}
if (k==7)
{
WRITE_TEXT(0x11);
k=k+1;
}
if (k==8)
{
WRITE_TEXT(chu[2]);//D
k=k+1;
}
if (k==9)
{
WRITE_TEXT(chu[8]);//U
k=k+1;
}
if (k==10)
{
WRITE_TEXT(chu[1]);//C
k=k+1;
}
if (k==11)
{
WRITE_TEXT(0x11);
52

k=k+1;
}
if (k==12)
{
WRITE_TEXT(chu[6]);//N
k=k+1;
}
if (k==13)
{
WRITE_TEXT(chu[4]);//G
k=k+1;
}
if (k==14)
{
WRITE_TEXT(chu[7]);//O
k=k+1;
}
if (k==15)
{
WRITE_TEXT(chu[1]);//C
k=k+1;
}
if (k==16)
{
WRITE_TEXT(0x11);
k=k+1;
}
if (k==17)
{
WRITE_TEXT(chu[5]);//H
k=k+1;
}
if (k==18)
{
WRITE_TEXT(chu[7]);//O
k=k+1;
}
if (k==19)
{
WRITE_TEXT(chu[0]);//A
53

k=k+1;
}
if (k==20)
{
WRITE_TEXT(chu[6]);//N
k=k+1;
}
if (k==21)
{
WRITE_TEXT(chu[4]);//G
k=k+1;
}
if (k==22)
{
WRITE_TEXT(0x11);
k=k+1;
}
if (k==23)
{
WRITE_TEXT(chu[5]);//H
k=k+1;
}
if (k==24)
{
WRITE_TEXT(chu[0]);//A
k=k+1;
}
if (k==25)
{
WRITE_TEXT(chu[6]);//N
k=k+1;
}
if (k==26)
{
WRITE_TEXT(chu[4]);//G
k=k+1;
}
if (k==27)
{
WRITE_TEXT(0x11);
54

k=k+1;
}
if (k==28)
{
WRITE_TEXT(chu[12]);//K
k=k+1;
}
if (k==29)
{
WRITE_TEXT(chu[5]);//H
k=k+1;
}
if (k==30)
{
WRITE_TEXT(chu[7]);//O
k=k+1;
}
if (k==31)
{
WRITE_TEXT(chu[6]);//N
k=k+1;
}
if (k==32)
{
WRITE_TEXT(chu[4]);//G
k=k+1;
}
if (k==33)
{
WRITE_TEXT(0x11);
k=k+1;
}
if (k==34)
{
WRITE_TEXT(chu[10]);//V
k=k+1;
}
if (k==35)
{
WRITE_TEXT(chu[11]);//I
55

k=k+1;
}
if (k==36)
{
WRITE_TEXT(chu[3]);//E
k=k+1;
}
if (k==37)
{
WRITE_TEXT(chu[13]);//T
k=k+1;
}
if (k==38)
{
WRITE_TEXT(0x11);
k=k+1;
}
if (k==39)
{
WRITE_TEXT(chu[6]);//N
k=k+1;
}
if (k==40)
{
WRITE_TEXT(chu[0]);//A
k=k+1;
}
if (k==41)
{
WRITE_TEXT(chu[14]);//M
k=k+1;
}
if (k==42)
{
WRITE_TEXT(0x11);
m=m+1;
k=m;
}
delay_ms(thoi_gian_chay_chu);
CLEAR_LCD();
56

delay_ms(i);
}
}
void main()
{
INIT_LCD();
CLEAR_LCD();
delay_ms(i);
while (true)
{
chaychu1();
chaychu2();
chay=0xA7;
enable1=1,enable2=0,enable3=0,enable4=0,enable5=0,enable6=0,enable7=0
,enable8=0,enable9=0,enable10=0;
enable11=0,enable12=0,enable13=0,enable14=0,enable15=0,enable16=0,en
able17=0,enable18=0,enable19=0,enable20=0;
enable21=0,enable22=0,enable23=0,enable24=0,enable25=0,enable26=0,en
able27=0,enable28=0,enable29=0,enable30=0;
enable31=0,enable32=0,enable33=0,enable34=0,enable35=0,enable36=0,en
able37=0,enable38=0,enable39=0,enable40=0,enable41=0;
m=2;
k=2;
}

}
//LCD_OK_
//lcd_lib_4bit
#include <stddef.h>
#define LCD_RS
//#define LCD_RW
#define LCD_EN

PIN_D2
PIN_A1
PIN_D3
57

#define LCD_D4
#define LCD_D5
#define LCD_D6
#define LCD_D7

PIN_D4
PIN_D5
PIN_D6
PIN_D7

// misc display defines#define Line_1


0x80
#define Line_2
0xC0
#define Clear_Scr
0x01
// prototype statements
#separate void LCD_Init ( void );// ham khoi tao LCD
#separate void LCD_SetPosition ( unsigned int cX );//Thiet lap vi tri con tro
#separate void LCD_PutChar ( unsigned int cX );// Ham viet1kitu/1chuoi
len LCD
#separate void LCD_PutCmd ( unsigned int cX) ;// Ham gui lenh len LCD
#separate void LCD_PulseEnable ( void );// Xung kich hoat
#separate void LCD_SetData ( unsigned int cX );// Dat du lieu len chan Data
// D/n Cong
#use standard_io ( B )
#use standard_io (A)
//khoi tao LCD**********************************************
#separate void LCD_Init ( void )
{
LCD_SetData ( 0x00 );
delay_ms(200);
/* wait enough time after Vdd rise >> 15ms */
output_low ( LCD_RS );// che do gui lenh
LCD_SetData ( 0x03 ); /* init with specific nibbles to start 4-bit mode */
LCD_PulseEnable();
LCD_PulseEnable();
LCD_PulseEnable();
LCD_SetData ( 0x02 ); /* set 4-bit interface */
LCD_PulseEnable();
/* send dual nibbles hereafter, MSN first */
LCD_PutCmd ( 0x2C ); /* function set (all lines, 5x7 characters) */
LCD_PutCmd ( 0b00001100); /* display ON, cursor off, no blink */
LCD_PutCmd ( 0x06 ); /* entry mode set, increment & scroll left */
LCD_PutCmd ( 0x01 ); /* clear display */
}
58

#separate void LCD_SetPosition ( unsigned int cX )


{
/* this subroutine works specifically for 4-bit Port A */
LCD_SetData ( swap ( cX ) | 0x08 );
LCD_PulseEnable();
LCD_SetData ( swap ( cX ) );
LCD_PulseEnable();
}
#separate void LCD_PutChar ( unsigned int cX )
{
/* this subroutine works specifically for 4-bit Port A */
output_high ( LCD_RS );
LCD_PutCmd( cX );
output_low ( LCD_RS );
}
#separate void LCD_PutCmd ( unsigned int cX )
{
/* this subroutine works specifically for 4-bit Port A */
LCD_SetData ( swap ( cX ) ); /* send high nibble */
LCD_PulseEnable();
LCD_SetData ( swap ( cX ) ); /* send low nibble */
LCD_PulseEnable();
}
#separate void LCD_PulseEnable ( void )
{
output_high ( LCD_EN );
delay_us ( 3 );
// was 10
output_low ( LCD_EN );
delay_ms ( 3 );
// was 5
}
#separate void LCD_SetData ( unsigned int cX )
{
output_bit ( LCD_D4, cX & 0x01 );
output_bit ( LCD_D5, cX & 0x02 );
output_bit ( LCD_D6, cX & 0x04 );
output_bit ( LCD_D7, cX & 0x08 );
59

}
//BAI lcd_ok_16f877
Lcd.c
#include <16F887.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#include <lcd_lib_4bit.c>

void main()
{
// LCD_init();
// Printf(LCD_putchar,"Lop DT8 - BKHN");
//LCD_putcmd(0xC0);
//Printf(LCD_putchar,"Khoi tao...");
}
Lcd_lit_4bit
#include <stddef.h>
#define LCD_RS
//#define LCD_RW
#define LCD_EN

PIN_D2
PIN_A1
PIN_D3

#define LCD_D4
#define LCD_D5
#define LCD_D6
#define LCD_D7

PIN_D4
PIN_D5
PIN_D6
PIN_D7

// misc display defines#define Line_1


0x80
#define Line_2
0xC0
#define Clear_Scr
0x01
// prototype statements
#separate void LCD_Init ( void );// ham khoi tao LCD
60

#separate void LCD_SetPosition ( unsigned int cX );//Thiet lap vi tri con tro
#separate void LCD_PutChar ( unsigned int cX );// Ham viet1kitu/1chuoi
len LCD
#separate void LCD_PutCmd ( unsigned int cX) ;// Ham gui lenh len LCD
#separate void LCD_PulseEnable ( void );// Xung kich hoat
#separate void LCD_SetData ( unsigned int cX );// Dat du lieu len chan Data
// D/n Cong
#use standard_io ( B )
#use standard_io (A)
//khoi tao LCD**********************************************
#separate void LCD_Init ( void )
{
LCD_SetData ( 0x00 );
delay_ms(200);
/* wait enough time after Vdd rise >> 15ms */
output_low ( LCD_RS );// che do gui lenh
LCD_SetData ( 0x03 ); /* init with specific nibbles to start 4-bit mode */
LCD_PulseEnable();
LCD_PulseEnable();
LCD_PulseEnable();
LCD_SetData ( 0x02 ); /* set 4-bit interface */
LCD_PulseEnable();
/* send dual nibbles hereafter, MSN first */
LCD_PutCmd ( 0x2C ); /* function set (all lines, 5x7 characters) */
LCD_PutCmd ( 0b00001100); /* display ON, cursor off, no blink */
LCD_PutCmd ( 0x06 ); /* entry mode set, increment & scroll left */
LCD_PutCmd ( 0x01 ); /* clear display */
}
#separate void LCD_SetPosition ( unsigned int cX )
{
/* this subroutine works specifically for 4-bit Port A */
LCD_SetData ( swap ( cX ) | 0x08 );
LCD_PulseEnable();
LCD_SetData ( swap ( cX ) );
LCD_PulseEnable();
}
#separate void LCD_PutChar ( unsigned int cX )
{
/* this subroutine works specifically for 4-bit Port A */
61

output_high ( LCD_RS );
LCD_PutCmd( cX );
output_low ( LCD_RS );
}
#separate void LCD_PutCmd ( unsigned int cX )
{
/* this subroutine works specifically for 4-bit Port A */
LCD_SetData ( swap ( cX ) ); /* send high nibble */
LCD_PulseEnable();
LCD_SetData ( swap ( cX ) ); /* send low nibble */
LCD_PulseEnable();
}
#separate void LCD_PulseEnable ( void )
{
output_high ( LCD_EN );
delay_us ( 3 );
// was 10
output_low ( LCD_EN );
delay_ms ( 3 );
// was 5
}
#separate void LCD_SetData ( unsigned int cX )
{
output_bit ( LCD_D4, cX & 0x01 );
output_bit ( LCD_D5, cX & 0x02 );
output_bit ( LCD_D6, cX & 0x04 );
output_bit ( LCD_D7, cX & 0x08 );
}
//BAI Led_7 doan_ dung 74ls74373
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD
#use delay(clock=20000000)
#byte PortD=0x08
#bit OE1=PortD.0
#bit LE1=PortD.1
#bit OE2=PortD.2
62

#bit LE2=PortD.3
#bit OE3=PortD.4
#bit LE3=PortD.5
#bit OE4=PortD.6
#bit LE4=PortD.7
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int a=2,b=0,c=0,d=0;
int16 i;
int1 a_enable,b_enable,c_enable;
void main()
{
set_tris_D (0B00000000) ;
while(true)
{
for (i=1;i<=200;i++)
{
//LE1=1;
output_B(led7doan[a]);
OE1=0;
LE1=0;
//LE2=1;
delay_us(100);
output_B(led7doan[b]);
OE2=0;
LE2=0;
//LE3=1;
delay_us(100);
output_B(led7doan[c]);
OE3=0;
LE3=0;
//LE4=1;
63

delay_us(100);
output_B(led7doan[d]);
OE4=0;
LE4=0;
delay_us(100);
}
d=d+1;
LE4=1;
if (d>9)
{
d=0;
c=c+1;
c_enable=1;
}
if (c_enable==1)
{
LE3=1;
}
if (c>9)
{
c=0;
b=b+1;
b_enable=1;
}
if (b_enable==1)
{
LE2=1;
}
if (b>9)
{
b=0;
a=a+1;
a_enable=1;
64

}
if (a_enable==1)
{
LE1=1;
}
if (a>9)
{
a=2;
}
}
}
//BAI LED 7 DOAN OK
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD
#use delay(clock=20000000)
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int i;
signed int m;

void main()
{
while(true)
{
for(i=0;i<10;i++)
{
output_c(led7doan[i]);
65

if(i==9)
{
output_high(pin_D0);
output_low(pin_D1);
}
delay_ms(500);
}
for( m=9;m>=0;m--)
{
output_C(led7doan[m]);
if(m==0)
{
output_low(pin_D0);
output_high(pin_D1);
}
delay_ms(500);
}

}
}
//BAI LED_DON_OK
Lcd.c
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG,
NOBROWNOUT,NOLVP, NOCPD//, NOWRT
#use delay(clock=20000000)
//#byte PortB=0x06
//#bit c=PortB.4
//#bit a=PortB.6
//#bit b=PortB.5
int8 i,a,b,c;
// LUU Y &:AND;|:OR
//Trang Thai 1 Led Sang Lan Luot Tu Tren Xuong Duoi
void trangthai1()
66

{
b=0b00000001;
for (i=0;i<8;i++)
{
a=0b00000000|b;
output_B(b);
b=b<<1;
delay_ms(500);
}
}
//Trang Thai 2 Led Sang Lan Luot Tu Duoi Len Tren
void trangthai2()
{
b=0b10000000;
for (i=0;i<8;i++)
{
a=0b00000000|b;
output_B(b);
b=b>>1;
delay_ms(500);
}
}
//Trang Thai 3 Led Sang Lan Luot Tu Duoi Len Tren va Tu Tren Xuong
Duoi
void trangthai3()
{
a=0b00000001;
b=0b10000000;
for (i=0;i<8;i++)
{
c=a|b;
output_B(c);
a=a<<1;
b=b>>1;
delay_ms(500);
}
}

67

//Trang Thai 4 Led Sang Lan Luot Tung Cap Mot


void trangthai4()
{
a=0b00000011;
b=0b11000000;
for (i=0;i<8;i++)
{
c=a|b;
output_B(c);
a=a<<1;
b=b>>1;
delay_ms(500);
}
}
void main()
{
//set_tris_B(0b00001111);
while(true)
{
trangthai1();
trangthai2();
trangthai3();
trangthai4();
}
}
///BAI led matran 2 con chay chu_ok
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int8 i=100;
#byte PortB=0x06
#bit COT1=PortB.0
#bit COT2=PortB.1
#bit COT3=PortB.2
#bit COT4=PortB.3
68

#bit COT5=PortB.4
#bit COT6=PortB.5
#bit COT7=PortB.6
#bit COT8=PortB.7
#byte PortC=0x07
#bit HANG1=PortC.0
#bit HANG2=PortC.1
#bit HANG3=PortC.2
#bit HANG4=PortC.3
#bit HANG5=PortC.4
#bit HANG6=PortC.5
#bit HANG7=PortC.6
#bit HANG8=PortC.7
void main()
{
set_tris_B(0b00000000);
set_tris_C(0b00000000);
while(true)
{
HANG8=1,HANG1=0,COT1=0,COT2=1,COT3=1,COT4=0,COT5=0,COT
6=1,COT7=1,COT8=0;
delay_us(i);
HANG1=1,HANG2=0,COT1=1,COT2=1,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=1,COT8=1;
delay_us(i);
HANG2=1,HANG3=0,COT1=1,COT2=1,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=1,COT8=1;
delay_us(i);
HANG3=1,HANG4=0,COT1=1,COT2=1,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=1,COT8=1;
delay_us(i);
HANG4=1,HANG5=0,COT1=1,COT2=1,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=1,COT8=1;
delay_us(i);
HANG5=1,HANG6=0,COT1=0,COT2=1,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=1,COT8=0;
delay_us(i);
69

HANG6=1,HANG7=0,COT1=0,COT2=0,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=0,COT8=0;
delay_us(i);
HANG7=1,HANG8=0,COT1=0,COT2=0,COT3=0,COT4=1,COT5=1,COT
6=0,COT7=0,COT8=0;
delay_us(i);
}
}
//Bai led matra_ok
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int8 i=100;
#byte PortB=0x06
#bit COT1=PortB.0
#bit COT2=PortB.1
#bit COT3=PortB.2
#bit COT4=PortB.3
#bit COT5=PortB.4
#bit COT6=PortB.5
#bit COT7=PortB.6
#bit COT8=PortB.7
#byte PortC=0x07
#bit HANG1=PortC.0
#bit HANG2=PortC.1
#bit HANG3=PortC.2
#bit HANG4=PortC.3
#bit HANG5=PortC.4
#bit HANG6=PortC.5
#bit HANG7=PortC.6
#bit HANG8=PortC.7
void main()
{
set_tris_B(0b00000000);
set_tris_C(0b00000000);
70

while(true)
{
HANG8=1,HANG1=0,COT1=0,COT2=1,COT3=1,COT4=0,COT5=0,COT
6=1,COT7=1,COT8=0;
delay_us(i);
HANG1=1,HANG2=0,COT1=1,COT2=1,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=1,COT8=1;
delay_us(i);
HANG2=1,HANG3=0,COT1=1,COT2=1,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=1,COT8=1;
delay_us(i);
HANG3=1,HANG4=0,COT1=1,COT2=1,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=1,COT8=1;
delay_us(i);
HANG4=1,HANG5=0,COT1=1,COT2=1,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=1,COT8=1;
delay_us(i);
HANG5=1,HANG6=0,COT1=0,COT2=1,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=1,COT8=0;
delay_us(i);
HANG6=1,HANG7=0,COT1=0,COT2=0,COT3=1,COT4=1,COT5=1,COT
6=1,COT7=0,COT8=0;
delay_us(i);
HANG7=1,HANG8=0,COT1=0,COT2=0,COT3=0,COT4=1,COT5=1,COT
6=0,COT7=0,COT8=0;
delay_us(i);
}
}
//BAI matran phim _ok
Quet ban phim
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD
#use delay(clock=20000000)
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
71

int8 enable=0,a=0,m=1;
//int1 a1=0,a2=0,a3=0,a4=0;
//int1 xoa1=0,xoa2=0,xoa3=0,xoa4=0;
#INT_RTCC
void ngat_timer0()
{
set_timer0(5);
a=a+1;
if(a>=10)
{
a=0;
enable=enable+1;
if(enable==1)
{
output_low(pin_B0);
output_high(pin_B1);
output_high(pin_B2);
output_high(pin_B3);
if((input(pin_B4)==0))
{
output_C(0xFF);
output_D(led7doan[1]);
}
if((input(pin_B5)==0))
{
output_C(0xFF);
output_D(led7doan[5]);
}
if((input(pin_B6)==0))
{
output_C(0xFF);
output_D(led7doan[9]);
}
if((input(pin_B7)==0))
{
output_C(led7doan[1]);
output_D(led7doan[3]);
72

}
}
if(enable==2)
{
output_low(pin_B1);
output_high(pin_B0);
if((input(pin_B4)==0))
{
output_C(0xFF);
output_D(led7doan[2]);
}
if((input(pin_B5)==0))
{
output_C(0xFF);
output_D(led7doan[6]);
}
if((input(pin_B6)==0))
{
output_C(led7doan[1]);
output_D(led7doan[0]);
}
if((input(pin_B7)==0))
{
output_C(led7doan[1]);
output_D(led7doan[4]);
}
}
if(enable==3)
{
output_low(pin_B2);
output_high(pin_B1);
if((input(pin_B4)==0))
{
output_C(0xFF);
output_D(led7doan[3]);
73

}
if((input(pin_B5)==0))
{
output_C(0xFF);
output_D(led7doan[7]);
}
if((input(pin_B6)==0))
{
output_C(led7doan[1]);
output_D(led7doan[1]);
}
if((input(pin_B7)==0))
{
output_C(led7doan[1]);
output_D(led7doan[5]);
}
}
if(enable==4)
{
output_low(pin_B3);
output_high(pin_B2);
if((input(pin_B4)==0))
{
output_C(0xFF);
output_D(led7doan[4]);
}
if((input(pin_B5)==0))
{
output_C(0xFF);
output_D(led7doan[8]);
}
if((input(pin_B6)==0))
{
output_C(led7doan[1]);
output_D(led7doan[2]);
74

}
if((input(pin_B7)==0))
{
output_C(led7doan[1]);
output_D(led7doan[6]);
}
enable=0;
}
}
}

void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
set_timer0(5);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
output_B(0b11111111);
output_D(0b11111111);
while(true)
{
;
}
}
// Bai ngat ngoai test dong co
Ngat ngoai
#include <16f877a.h>
#include <math.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD
#use delay(clock=20000000)

75

void main()
{
while (true)
{
output_high(pin_D0);
delay_ms(1000);
output_low(pin_D0);
delay_ms(500);
}
}
//BAI ngat ngoai _ok
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD
#use delay(clock=20000000)
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};// ma led ung voi cac
chan theo tuan tu
int a=0;
#int_ext
void ngat_ngoai()
{
a=a+1;
if (a>5)
{
a=0;
}
output_C(led7doan[a]);
}
void main()
76

{
ext_int_edge( H_TO_L );
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
while(true)
{
if (a==1)
{
output_C(led7doan[1]);
output_D(0b00001111);
}
else if (a==2)
{
output_C(led7doan[2]);
output_D(0b11110000);
}
else if (a==3)
{
output_C(led7doan[3]);
output_D(0b10101010);
}
else if (a==4)
{
output_C(led7doan[4]);
output_D(0b01010101);
}
else if (a==5)
{
output_C(led7doan[5]);
output_D(0b11100111);
}
else
{
output_C(0xFF);
output_D(0b00000000);
}
;
}
}
77

//Bai ngat ngoai port _b_ok


#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD
#use delay(clock=20000000)
int pass1,pass2,pass3,pass4,OK;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};// ma led ung voi cac
chan theo tuan tu

#int_ext
void ngat_ngoai()
{
OK=1;
}
#int_RB
void PASS_WORD()
{
IF(input(pin_B4)==0)
{
pass1=pass1+1;
if (pass1>9)
{
pass1=0;
}
output_high(pin_C5);
output_low(pin_C7);
output_low(pin_A1);
output_low(pin_A3);
output_D(led7doan[pass1]);
78

}
IF(input(pin_B5)==0)
{
pass2=pass2+1;
if (pass2>9)
{
pass2=0;
}
output_high(pin_C7);
output_low(pin_C5);
output_low(pin_C5);
output_low(pin_A1);
output_low(pin_A3);
output_D(led7doan[pass2]);
}
IF(input(pin_B6)==0)
{
pass3=pass3+1;
if (pass3>9)
{
pass3=0;
}
output_high(pin_A1);
output_low(pin_C7);
output_low(pin_C5);
output_low(pin_C7);
output_low(pin_A3);
output_D(led7doan[pass3]);
}
IF(input(pin_B7)==0)
{
pass4=pass4+1;
if (pass4>9)
{
pass4=0;
}
output_high(pin_A3);
79

output_low(pin_A1);
output_low(pin_C5);
output_low(pin_C7);
output_low(pin_A1);
output_D(led7doan[pass4]);
}
}
void main()
{
enable_interrupts(GLOBAL);
ext_int_edge(0,H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(INT_RB);
output_low(pin_C4);
output_low(pin_C6);
output_low(pin_A0);
output_low(pin_A2);
output_D(led7doan[0]);
while(true)
{
if (OK==1)
{
if(pass1==2&&pass2==6&&pass3==0&&pass4==8)
{
output_high(pin_C0);
output_low(pin_C1);
output_low(pin_C2);
OK=0;
}
else
{
output_low(pin_C0);
output_high(pin_C2);
output_low(pin_C3);
80

OK=0;
}
}
;
}
}
//BAI PIC 18F5420
#include <18f4550.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG,
NOBROWNOUT,NOLVP, NOCPD//, NOWRT
#use delay(clock=4000000)
//#byte portB=0x06
void main()
{
// Thi Du Ve Nhap Nhay Led;//TRISB = 0x00;//Port B la Xuat Du Lieu
//set_tris_B(0x00);
while(true)
{
output_high(PIN_B7);
output_high(PIN_B6);
delay_ms(500);
output_low(PIN_B7);
output_low(PIN_B6);
delay_ms(500);
}
}
//BAI RS232
Rs_232_1
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use rs232 (baud=19200 , parity=n , xmit=pin_C6 , rcv=pin_C7 )
char a[7]={'1','2','3','4','5','6','7'};
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int8 dem=0;
81

#INT_EXT
void ngat_ngoai()
{
dem=dem+1;
if(dem==1)
{
output_D(led7doan[1]);
putc(a[0]);
}
if(dem==2)
{
output_D(led7doan[2]);
putc(a[1]);
}
if(dem==3)
{
output_D(led7doan[3]);
putc(a[2]);
}
if(dem==4)
{
output_D(led7doan[4]);
putc(a[3]);
}
if(dem==5)
{
output_D(led7doan[5]);
putc(a[4]);
}
if(dem==6)
{
output_D(led7doan[6]);
putc(a[5]);
}
if(dem==7)
{
output_D(led7doan[7]);
putc(a[6]);
}
82

if(dem>7)
{
dem=0;
output_D(led7doan[0]);
}
}
main ()
{
ext_int_edge(H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
output_D(led7doan[0]);
while (true)
{
;
}
}
//rs_232_2
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use rs232 (baud=19200 , parity=n , xmit=pin_C6 , rcv=pin_C7 )
char m;
main ()
{
while (true)
{
m=getc();
if(m=='1')
{
output_D(0b10001001);//Chu H
}
}
}
//bai spi_don gian
Spi_master
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
83

#use delay(clock=20000000)
int8 data_in;
#INT_SSP
void isr_ssp()
{
data_in=spi_read();
//Output_D(data_in);
}
void main()
{
setup_spi(spi_slave | spi_l_to_h |spi_clk_div_16);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(true)
{
switch (data_in)
{
case 1:
output_high(pin_B0);
output_low(pin_B1);
output_low(pin_B2);
output_low(pin_B3);
break;
case 2:
output_low(pin_B0);
output_high(pin_B1);
output_low(pin_B2);
output_low(pin_B3);
break;
case 3:
output_high(pin_B2);
output_low(pin_B3);
output_low(pin_B0);
output_low(pin_B1);
break;
case 4:
output_low(pin_B2);
84

output_high(pin_B3);
output_low(pin_B0);
output_low(pin_B1);
break;
default:
output_low(pin_B0);
output_low(pin_B1);
output_low(pin_B2);
output_low(pin_B3);
break;
}
}//While
}
//BAI SPI Truyen Nhan Qua Lai Giua Master va Slave
Spi_master
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int8 data_in;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
#INT_SSP
void ngat_nhan()
{
data_in=spi_read();
delay_us(30);
output_high(pin_C0);
if(data_in==100)
{
output_D(led7doan[1]);
}
if(data_in==200)
{
output_D(led7doan[2]);
85

}
}
#INT_RB
void ngat_ngoai()
{
///////////////////////////////////////
if(input(pin_B0)==0)
{
output_low(pin_B6);
spi_write(1);
delay_us(200);
}
else
{
output_high(pin_B6);
}
//////////////////////////////////////
if(input(pin_B1)==0)
{
output_low(pin_B7);
spi_write(2);
delay_us(200);
}
else
{
output_high(pin_B7);
}
/////////////////////////////////////
}

void main()
{
setup_spi(spi_master| spi_l_to_h |spi_clk_div_16);
enable_interrupts(INT_RB);
enable_interrupts(INT_SSP);
86

enable_interrupts(GLOBAL);
output_D(led7doan[0]);
while(true)
{
;
}
}
//slave-1
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int8 data_in,i;
#INT_SSP
void isr_ssp()
{
data_in=spi_read();
if(data_in==1)
{
for(i=0;i<=4;i++)
{
output_D(led7doan[i]);
delay_ms(1000);
}
}
spi_write(100);
delay_us(30);
}
void main()
{
setup_spi(spi_slave);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
spi_write(100);
87

delay_us(30);
while(true)
{
spi_write(100);
delay_us(30);
;
}
}
//slave2
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int8 data_in,i;
#INT_SSP
void isr_ssp()
{
data_in=spi_read();
if(data_in==2)
{
for(i=5;i<=9;i++)
{
output_D(led7doan[i]);
delay_ms(1000);
}
}
spi_write(200);
delay_us(30);
}
void main()
{
setup_spi(spi_slave);
enable_interrupts(INT_SSP);
88

enable_interrupts(GLOBAL);
while(true)
{
;
}
}
//bai SPI____1 MASTER___NHIEU SLAVE_OK
Master
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int8 a=0;
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
#INT_RB
void ngat_ngoai()
{
if(input(pin_B4)==0)
{
a=a+1;
if(a>9)
{
a=0;
}
output_D(led7doan[a]);
}
if(input(pin_B5)==0)
{
///////////////////////////////////////
if(input(pin_B0)==0)
{
output_low(pin_B6);
}
89

else
{
output_high(pin_B6);
}
//////////////////////////////////////
if(input(pin_B1)==0)
{
output_low(pin_B7);
}
else
{
output_high(pin_B7);
}
/////////////////////////////////////
if(input(pin_B2)==0)
{
output_low(pin_C0);
}
else
{
output_high(pin_C0);
}
////////////////////////////////////
if(input(pin_B3)==0)
{
output_low(pin_C1);
}
else
{
output_high(pin_C1);
}
spi_write(a);
delay_us(200);
}
}

void main()
{
90

setup_spi(spi_master| spi_l_to_h |spi_clk_div_16);


enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
output_D(led7doan[0]);
while(true)
{
;
}
}
Spi_Slave
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int8 data_in;
#INT_SSP
void isr_ssp()
{
data_in=spi_read();
output_D(led7doan[data_in]);
}
void main()
{
setup_spi(spi_slave);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(true)
{
;
}
}
//BAI TIMER0
#include <16f877a.h>
91

#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,


NOBROWNOUT, NOLVP, NOCPD//, NOWRT
#use delay(clock=20000000)
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int
led7doan1[10]={0b01000000,0b01111001,0b00100100,0b00110000,0b000
11001,0b00010010,
0b00000010,0b01111000,0b0000000,0b0010000};
#byte PortD=0x08
#bit OE1=PortD.0
#bit LE1=PortD.1
#bit OE2=PortD.2
#bit LE2=PortD.3
#bit OE3=PortD.4
#bit LE3=PortD.5
#bit OE4=PortD.6
#bit LE4=PortD.7
int16 a=0;
int8 i=0,k=0,t=0,j=0;
//int1 enable=1;
//int1 b=1;
#INT_RTCC
void ngat_timer0()
{
a=a+1;
set_timer0(5);
if(a>=10000)
{
LE4=1;
i=i+1;
output_B(led7doan[i]);
LE4=0;
a=0;
92

if(i>=10)
{
i=0;
LE4=1;
output_B(led7doan[0]);
LE4=0;
LE3=1;
j=j+1;
output_B(led7doan[j]);
LE3=0;
if(j>=6)
{
j=0;
LE3=1;
output_B(led7doan[0]);
LE3=0;
k=k+1;
LE2=1;
output_B(led7doan1[k]);
LE2=0;
if(k>=10)
{
k=0;
t=t+1;
LE2=1;
output_B(led7doan1[0]);
LE2=0;
LE1=1;
output_B(led7doan[t]);
LE1=0;
if(t>=6)
{
t=0;
}
}
}
}
}
93

}
void main()
{
set_tris_D(0b00000000);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
//setup_timer_2(T2_DIV_BY_4,0xfe, 1); //0xff la thiet lap chu ky
set_timer0(5);
//set_timer2(5);
//set_timer1(3035);//3035 la thiet lap de no chay den 65535 thi se dung
100ms,va chay nhu the 10 lan thi se duoc 1 s
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
output_D(0b10101010);
OE1=0;
output_B(led7doan[0]);
LE1=0;
OE2=0;
output_B(led7doan1[0]);
LE2=0;
OE3=0;
output_B(led7doan[0]);
LE3=0;
OE4=0;
output_B(led7doan[0]);
LE4=0;
while(true)
{
;
}
}
//BAI TIMER 1
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD//, NOWRT
#use delay(clock=20000000)
94

int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int8 a=0,i=1;
int1 b=1;
#INT_TIMER1
void ngat_timer1()
{
a=a+1;
set_timer1(3035);
if(a==10)
{
output_D(led7doan[i]);
a=0;
i=i+1;
if(i>=10)
{
i=0;
}
}
}

void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
set_timer1(3035);//3035 la thiet lap de no chay den 65535 thi se dung
100ms,va chay nhu the 10 lan thi se duoc 1 s
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
output_D(led7doan[0]);
while(1)
95

{
;
}
}
//BAI TIMER 2
#include <16f877a.h>
#FUSES NOWDT, HS, NOPUT,NOPROTECT, NODEBUG,
NOBROWNOUT, NOLVP, NOCPD//, NOWRT
#use delay(clock=20000000)
int
led7doan[10]={0b11000000,0b11111001,0b10100100,0b10110000,0b1001
1001,0b10010010,
0b10000010,0b11111000,0b10000000,0b10010000};
int16 a=0;
int8 i=1;
#INT_TIMER2
void ngat_timer2()
{
a=a+1;
set_timer2(5);
if(a>=5000)
{
a=0;
output_B(led7doan[i]);
i=i+1;
if(i>9)
{
i=0;
}
}
}

96

void main()
{
setup_timer_2(T2_DIV_BY_4,0xfe, 8); //0xfe la thiet lap chu ky cho timer
2
set_timer2(5);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
output_B(led7doan[0]);
while(true)
{
;
}
}

97

You might also like