0% found this document useful (0 votes)
15 views19 pages

Tonghopcode

Uploaded by

22161193
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views19 pages

Tonghopcode

Uploaded by

22161193
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

NÚT NHẤN,ĐIỀU KHIỂN 24LED ĐƠN

#include<16f887.h>
#fuses intrc_io
#use delay(clock =8M)
#define UP pin_e0
#define DOWN pin_e1
signed int8 n,m;
unsigned int32 y;
unsigned int8 state;
void tang()
{ if(input(UP)==0){
delay_ms(20);
if(input(UP)==0){
state++;
if(state>9) state=0;
while(input(UP)==0);}
void giam()
{if(input(DOWN)==0){
delay_ms(20);
if(input(DOWN)==0){
state--;
if(state<0) state=9;
while(input(DOWN)==0);}}}
void choptat()
{
output_c(0xff);
output_d(0xff);
output_b(0xff);
delay_ms(100);
output_c(0x00);
output_d(0x00);
output_b(0x00);
delay_ms(100);
if ((input(UP)==0) || (input(DOWN)==0))
{ break; }}
void tatdanpst()
{
for(n=0;n<=24;n++)
{
y=0xffffff>>n;// n=0 y=1111 1111 1111 1111 1111 1111
// n=1 y=1111 1111 1111 1111 1111 1110
output_c(y);
output_d(y>>8); // 1111 1111 1111 1111 0000 0000
output_b(y>>16);
delay_ms(20);
if ((input(UP)==0) || (input(DOWN)==0))
{ break; }}}
void tatdantsp()
{
for(n=0;n<=24;n++)
{
y=0xffffff<<n;// n=0 y=1111 1111 1111 1111 1111 1111
// n=1 y=1111 1111 1111 1111 1111 1110
output_c(y);
output_d(y>>8); // 1111 1111 1111 1111 0000 0000
output_b(y>>16);
delay_ms(20);
if((input(UP)==0)||((input(DOWN)==0)))
{break;} }}
void sangdanpst()
{
for(n=24;n>=0;n--)
{
y=0xffffff<<n;//n=24 y= 0000 0000 0000 0000 0000 0000
//n=23 y= 1000 0000 0000 0000 0000 0000
output_c(y);
output_d(y>>8);
output_b(y>>16);
delay_ms(20);
if((input(UP)==0)||((input(DOWN)==0)))
{break;}}}
void sangdantsp()
{ for(n=24;n>=0;n--)
{
y=0xffffff>>n;//n=24 y= 0000 0000 0000 0000 0000 0000
//n=23 y= 0000 0000 0000 0000 0000 0001
output_c(y);
output_d(y>>8);
output_b(y>>16);
delay_ms(20);
if((input(UP)==0)||((input(DOWN)==0)))
{break;}}}
void sangdichtrai()
{
for(n=0;n<=24;n++)
{
y=0x800000>>n;//n=0 y=1000 0000 0000 0000 0000 0000
//n=1 y=0100 0000 0000 0000 0000 0000
output_c(y);
output_d(y>>8);
output_b(y>>16);
delay_ms(50);
if((input(UP)==0)||((input(DOWN)==0)))
{break;} }}
void sangdichphai()
{
for(n=24;n>=0;n--)
{
y=0x800000>>n;//n=24 y=0000 0000 0000 0000 0000 0001
//n=23 y=0000 0000 0000 0000 0000 0010
output_c(y);
output_d(y>>8);
output_b(y>>16);
delay_ms(50);
if((input(UP)==0)||((input(DOWN)==0)))
{break;}}}
void dich3bittrai()
{
for(n=0;n<=24;n++)
{
y=0xe00000>>n;//n=0 y=1110 0000 0000 0000 0000 0000
//n=1 y=0111 0000 0000 0000 0000 0000
output_c(y);
output_d(y>>8);
output_b(y>>16);
delay_ms(50);
if((input(UP)==0)||((input(DOWN)==0)))
{break;}}}
void dich3bitphai()
{
for(n=24;n>=0;n--)
{
y=0xe00000>>n;//n=24 y=0000 0000 0000 0000 0000 0111
//n=1 y=0000 0000 0000 0000 0000 1110
output_c(y);
output_d(y>>8);
output_b(y>>16);
delay_ms(50);
if((input(UP)==0)||((input(DOWN)==0)))
{break;}}}
void sangdon()
{
for(n=24;n>=0;n--)
{
for(m=24;m>=24-n;m--)
{
y=(0x800000>>m)|(0xffffff<<n); //n=24 y= 0000 0000 0000 0000 0000 0001
//n=23 y= 0000 0000 0000 0000 0000 0011
output_c(y);
output_d(y>>8);
output_b(y>>16);
delay_ms(50);
if((input(UP)==0)||((input(DOWN)==0)))
{break;}}}
void main()
{
set_tris_c(0x00);
set_tris_b(0x00);
set_tris_d(0x00);
set_tris_e(0x03); //11
state=0;

while(true)
{
tang();
giam();
switch(state){
case 0: {
choptat();
break;}
case 1: {
tatdanpst();
break;}
case 2: {
tatdantsp();
break;}
case 3: {
sangdanpst();
break;}
case 4: {
sangdantsp();
break;}
case 5: {
sangdichtrai();
break;}
case 6: {
sangdichphai();
break;}
case 7: {
dich3bittrai();
break;}
case 8: {
dich3bitphai();
break;}
case 9: {
sangdon();
break;}}}}
BÀN PHÍM MA TRẬN
#include<16f887.h>
#include<TV_KEY4x4.c>
#fuses intrc_io
#use delay(clock =8M)
signed int8 n,m;
unsigned int32 y;
unsigned int8 s;
unsigned int8 mp;
void choptat()
{
mp=key_4x4();
if(mp!=0xff)
{s=mp;
break;}
}
..............
void main()
{
set_tris_c(0x00);
set_tris_b(0x00);
set_tris_d(0x00);
set_tris_a(0x0f); //0000 1111
s=0;

while(true)
{

switch(s){
case 0: {
choptat();
break;}
case 1: {
tatdanpst();
break;}
case 2: {
tatdantsp();
break;}
case 3: {
sangdanpst();
break;}
case 4: {
sangdantsp();
break;}
case 5: {
sangdichtrai();
break;}
case 6: {
sangdichphai();
break;}
case 7: {
dich3bittrai();
break;}
case 8: {
dich3bitphai();
break;}
case 9: {
sangdon();
break;}
default:
{ choptat();
break;}}}}
LED 7 ĐOẠN
#include<16f887.h>
#fuses intrc_io
#use delay(clock =8M)
#define UP pin_e0
#define DOWN pin_e1
signed int8 n,m;
unsigned int32 y;
unsigned int8 state;
const unsigned int8
ma7doan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void tang()
{
if(input(UP)==0){
delay_ms(20);
if(input(UP)==0){
state++;
if(state>10) state=0;
while(input(UP)==0);}}}
void giam()
{
if(input(DOWN)==0){
delay_ms(20);
if(input(DOWN)==0){
state--;
if(state<0) state=10;
while(input(DOWN)==0);}}}
void choptat()
{

output_a(ma7doan[state/1%10]);
output_b(ma7doan[state/10%10]);
if((input(UP)==0)||((input(DOWN)==0)))
{break;}}
Void dich3bitrai()
{
for(n=0;n<=16;n++)
{
y=0xe000>>n;//n=0 y=1110 0000 0000 0000 0000 0000
//n=1 y=0111 0000 0000 0000 0000 0000
output_c(y);
output_d(y>>8);
delay_ms(100);
output_a(ma7doan[state/1%10]);
output_b(ma7doan[state/10%10]);
if((input(UP)==0)||((input(DOWN)==0)))
{break;}}}
void main()
{
set_tris_a(0x00);
set_tris_c(0x00);
set_tris_b(0x00);
set_tris_d(0x00);
set_tris_e(0x03); //11
state=1;

while(true)
{

tang();
giam();
switch(state){
case 1: {
choptat();
break;}
case 2: {
tatdanpst();
break;}
case 3: {
tatdantsp();
break;}
case 4: {
sangdanpst();
break;}
case 5: {
sangdantsp();
break;}
case 6: {
sangdichtrai();
break;}
case 7: {
sangdichphai();
break;}
case 8: {
dich3bittrai();
break;}
case 9: {
dich3bitphai();
break;}
case 10: {
sangdon();
break;}}}}
LED QUET
#include<16f887.h>
#fuses intrc_io
#use delay(clock =8M)
#define UP pin_e0
#define DOWN pin_e1
#define DK1 pin_a0
#define DK2 pin_a1
signed int8 n,m;
unsigned int32 y, i;
unsigned int8 state;
const unsigned int8
ma7doan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void ledquet()
{
output_b(ma7doan[state/10%10]); output_low(DK1); delay_ms(1);
output_high(DK1);
output_b(ma7doan[state/1%10]); output_low(DK2); delay_ms(1);
output_high(DK2);}

void tang()

{
if(input(UP)==0){
delay_ms(20);
if(input(UP)==0){
state++;
if(state>10) state=1;
while(input(UP)==0);}
void giam()
{
if(input(DOWN)==0){
delay_ms(20);
if(input(DOWN)==0){
state--;
if(state<0) state=10;
while(input(DOWN)==0);}
void choptat()
{
output_c(0xff);
output_d(0xff);
for(i=0;i<25;i++) ledquet();
output_c(0x00);
output_d(0x00);
for(i=0;i<25;i++) ledquet();
if ((input(UP)==0) || (input(DOWN)==0))
{ break; }

}
void dich3bitphai()
{
for(n=16;n>=0;n--)
{
y=0xe000>>n;//n=24 y=0000 0000 0000 0000 0000 0111
//n=1 y=0000 0000 0000 0000 0000 1110
output_c(y);
output_d(y>>8);
for(i=0;i<25;i++) ledquet();
if ((input(UP)==0) || (input(DOWN)==0))
{ break; }}}
void main()
{
set_tris_a(0x00);
set_tris_b(0x00);
set_tris_c(0x00);
set_tris_d(0x00);
set_tris_e(0x03); //11
state=1;

while(true)
{
tang();
giam();
switch(state){
case 1: {
choptat();
break;}
case 2: {
tatdanpst();
break;}
case 3: {
tatdantsp();
break;}
case 4: {
sangdanpst();
break;}
case 5: {
sangdantsp();
break;}
case 6: {
sangdichtrai();
break;}
case 7: {
sangdichphai();
break;}
case 8: {
dich3bittrai();
break;}
case 9: {
dich3bitphai();
break;}
case 10: {
sangdon();
break;}}}}
LCD
#include<16f887.h>
#fuses intrc_io
#use delay(clock =8M)
#define UP pin_e0
#define DOWN pin_e1
#define lcd_rs pin_a0
#define lcd_rw pin_a1
#define lcd_e pin_a2
#define output_lcd output_b
signed int8 n,m;
unsigned int32 y ;
unsigned int8 state;
#include<tv_lcd.c>
void tang()
{
if(input(UP)==0){
delay_ms(20);
if(input(UP)==0){
state++;
if(state>10) state=1;
while(input(UP)==0);}}}
void giam()
{
if(input(DOWN)==0){
delay_ms(20);
if(input(DOWN)==0){
state--;
if(state<0) state=10;
while(input(DOWN)==0);}}}
void tatdanpst()
{
for(n=0;n<=24;n++)
{
y=0xffff>>n;// n=0 y=1111 1111 1111 1111 1111 1111
// n=1 y=1111 1111 1111 1111 1111 1110
output_c(y);
output_d(y>>8); // 1111 1111 1111 1111 0000 0000
delay_ms(50);

if ((input(UP)==0) || (input(DOWN)==0))
{ break; }}}
void dich3bitphai()
{
for(n=16;n>=0;n--)
{
y=0xe000>>n;//n=24 y=0000 0000 0000 0000 0000 0111
//n=1 y=0000 0000 0000 0000 0000 1110
output_c(y);
output_d(y>>8);
delay_ms(100);
if ((input(UP)==0) || (input(DOWN)==0))
{ break; }}}
void tenhieuung()
{
lcd_command(0x94+5);
switch(state){
case 1: {
lcd_data("CHOPTAT");
break;}
case 2: {
lcd_data("TATDANPST");
break;}
case 3: {
lcd_data("TATDANTSP");
break;}
case 4: {
lcd_data("SANGDANPST");
break;}
case 5: {
lcd_data("SANGDANTSP");
break;}
case 6: {
lcd_data("SANGDICHTRAI");
break;}
case 7: {
lcd_data("SANGDICHPHAI");
break;}
case 8: {
lcd_data("DICH3BITTRAI");
break;}
case 9: {
lcd_data("DICH3BITPHAI");
break;}
case 10: {
lcd_data(" SANGDON ");
break;}}}
void hienthi()
{
lcd_command(0x80+2);
lcd_data("NGUYENTHIDIEMTHUY");
lcd_command(0xc0+2);
lcd_data("HIEUUNG:");
lcd_command(0xd4+2);
lcd_data(" VXL THAT TUYET ");
}
void main()
{
set_tris_b(0x00);
set_tris_a(0x03); // 11
set_tris_c(0x00);
set_tris_d(0x00);
set_tris_e(0x03); //11
lcd_setup();
state=1;
while(true)
{lcd_command(0xc0+11);
lcd_data(state/10%10+0x30); // hangchuc
lcd_data(state/1%10+0x30); // hangdonvi
tenhieuung();
hienthi();
tang();
giam();
switch(state){
case 1: {
choptat();
tenhieuung();
break;}
case 2: {
tatdanpst();
tenhieuung();
break;}
case 3: {
tatdantsp();
tenhieuung();
break;}
case 4: {
sangdanpst();
tenhieuung();
break;}
case 5: {
sangdantsp();
tenhieuung();
break;}
case 6: {
sangdichtrai();
tenhieuung();
break;}
case 7: {
sangdichphai();
tenhieuung();
break;}
case 8: {
dich3bittrai();
tenhieuung();
break;}
case 9: {
dich3bitphai();
tenhieuung();
break;}
case 10: {
sangdon();
tenhieuung();
break;}}}}
TIMER-COUNTER
#include <16f887.h>
#fuses hs
#use delay(clock=20M)
#define lcd_rs pin_c5
#define lcd_rw pin_c6
#define lcd_e pin_c7
#define output_lcd output_d
#include <tv_lcd.c>
#bit TM2IF = 0x0c.1
#bit TM1IF = 0x0c.0
unsigned int8 T0,T1,bdn1, bdn2,gio=0,phut=0,giay=0,ts,nap,bct;
void inv()
{ if(input(pin_e0)==0)
{
delay_ms(20);
if(input(pin_e0)==0)
{
ts++;
if(ts > 5 ) ts= 1;
while(input(pin_e0)==0);}}}
void check()
{
if(ts==1)
{ set_timer1(3036);
nap=3036;
bct=5;}
if(ts==2)
{ set_timer1(13452);
nap=13452;
bct=3;}
if(ts==3)
{ set_timer1(39494);
nap=39494;
bct=4;}
if(ts==4)
{ set_timer1(26477);
nap=26477;
bct=2;}
if(ts==5)
{ set_timer1(3036);
nap=3036;
bct=1;}}
void dongho()
{ if(TM2IF == 1)
{ TM2IF=0; bdn2++;
if(bdn2==76)
{ giay++;
bdn2=0;
if(giay==60)
{ phut++;
giay=0;
if(phut==60)
{ gio++;
phut=0;
if(gio=13) gio=1;}}}}
void main()
{
set_tris_a(0x01);
set_tris_b(0);
set_tris_e(0x03); //011
set_tris_d(0);
set_tris_c(0);
lcd_setup();
lcd_command(0x80);
lcd_data(" NGUYENTHIDIEMTHUY ");
setup_timer_0(T0_EXT_H_TO_L | T0_DIV_1);
set_timer0(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
setup_timer_2(T2_DIV_BY_16,255,16);

while(true) {
dongho();
inv();
if(TM1IF==1)
{ set_timer1(nap);
TM1IF=0; bdn1++;
check();
if(bdn1 >= bct)
{ output_toggle(pin_b0);
bdn1=0;}}
T0 = get_timer0();
if(T0 > 24) set_timer0(1);
lcd_command(0xc0+2);
printf(lcd_data, "SO SAN PHAM: %u ",T0);
lcd_command(0x94);
lcd_data(" DONG HO: ");
lcd_data(gio/10%10+0x30); //hangchuc
lcd_data(gio/1%10+0x30); //hangdonvi
lcd_data(":");
lcd_data(phut/10%10+0x30); //hangchuc
lcd_data(phut/1%10+0x30); //hangdonvi
lcd_data(":");
lcd_data(giay/10%10+0x30); //hangchuc
lcd_data(giay/1%10+0x30); //hangdonvi
lcd_command(0xd4);
printf(lcd_data," TAN SO: %u ", ts);}}

Một hệ thống điều khiển quang báo gồm 12 led đơn và 1 màn hình LCD 20x4 giao tiếp
với VĐK . 3 nút nhấn UP và DW và SPEED kết nối với chân RE2,RE1 và RE0. 1 led 7
đoạn điều khiển bằng phương pháp trực tiếp kết nối với PORTD.
#include<16f887.h>
#include<TV_KEY4x4.c>
#fuses intrc_io
#use delay(clock =8M)
#define SPEED pin_e0
#define UP pin_e2
#define DW pin_e1
#define SD pin_e0
#define lcd_rs pin_b5
#define lcd_rw pin_b6
#define lcd_e pin_b7
#define output_lcd output_c
signed int16 n;
unsigned int32 y,i=40,m;
unsigned int8 state,thutu;
#include <tv_lcd.c>
const unsigned int8
ma7doan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void tang();
void giam();
void tocdo();
void sangdan();
void tatdan();
void hienthi();
void sangdich();
void tatdich();
void muiten();
void tanso();
void main()
{
set_tris_a(0);
set_tris_b(0);
set_tris_c(0);
set_tris_d(0);
set_tris_e(0);
lcd_setup();
state=1;
thutu=1;
i=40;
m=1;
while(true)
{

tocdo();
tang();
giam();
hienthi();
tanso();
muiten();

switch(state)
{
case 1:{
sangdan();
break;}
case 2:{
tatdan();
break;}
case 3:{
sangdich();
break;}
case 4:{
tatdich();
break;}}}}
void tang()

{
if(input(UP)==0){
delay_ms(20);
if(input(UP)==0){
state++;
thutu++;
i=40;
if(state>4) state=1;
if(thutu>4) thutu=1;
}

while(input(UP)==0);}}
void giam()
{
if(input(DW)==0){
delay_ms(20);
if(input(DW)==0){
state--;
thutu--;
i=40;
if(state<1) state=4;
if(thutu<1) thutu=4;
}

while(input(DW)==0);}}
void tocdo()
{
if(input(SD)==0)
{ delay_ms(10);
if(input(SD)==0){
i++;
m++;
if( i> 50 ) i=40;
if( m> 9 ) m=1;
}
while(input(SD)==0);
}
}
void hienthi()
{
lcd_command(0x80+3);
lcd_data("SANGDAN12LED");
lcd_command(0xC0+3);
lcd_data("TATDAN12LED");
lcd_command(0x94+3);
lcd_data("SANGDICH12LED");
lcd_command(0xd4+3);
lcd_data("TATDICH12LED");
}
void sangdan()
{
for(n=12;n>=0;n--)
{
y = 0xfff<<n;
output_a(y);
output_b(y>>8);
delay_ms(i);
if ((input(UP)==0) || (input(DW)==0) || (input(SD)==0))

{ break; } }}

void tatdan()
{
for(n=0;n<=12;n++)
{
y = 0xfff>>n;
output_a(y);
output_b(y>>8);
delay_ms(i);
if ((input(UP)==0) || (input(DW)==0) || (input(SD)==0))

{ break; } }}
void sangdich()
{

for(n=12;n>=0;n--)
{
y=0x800>>n;
output_a(y);
output_b(y>>8);
delay_ms(i);
if ((input(UP)==0) || (input(DW)==0) || (input(SD)==0) )
{ break; } }}
void tatdich()
{

for(n=12;n>=0;n--)
{
y= ~ (0x800>>n);
output_a(y);
output_b(y>>8);
delay_ms(i);
if ((input(UP)==0) || (input(DW)==0) || (input(SD)==0) )

{ break; }}}

void muiten()
{
switch (thutu) {
case 1: {
lcd_command(0x80);
lcd_data(0x7e);
lcd_command(0xc0);
lcd_data(" ");
lcd_command(0x94);
lcd_data(" ");
lcd_command(0xd4);
lcd_data(" ");
break;}
case 2: {
lcd_command(0x80);
lcd_data(" ");
lcd_command(0xc0);
lcd_data(0x7e);
lcd_command(0x94);
lcd_data(" ");
lcd_command(0xd4);
lcd_data(" ");
break;}
case 3: {
lcd_command(0x80);
lcd_data(" ");
lcd_command(0xc0);
lcd_data(" ");
lcd_command(0x94);
lcd_data(0x7e);
lcd_command(0xd4);
lcd_data(" ");
break;}
case 4: {
lcd_command(0x80);
lcd_data(" ");
lcd_command(0xc0);
lcd_data(" ");
lcd_command(0x94);
lcd_data(" ");
lcd_command(0xd4);
lcd_data(0x7e);
break;}
}}
void tanso()
{
switch(m) {
case 1:{output_d(ma7doan[m%10]);
break;}
case 2:{output_d(ma7doan[m%10]);
break;}
case 3:{output_d(ma7doan[m%10]);
break;}
case 4:{output_d(ma7doan[m%10]);
break;}
case 5:{output_d(ma7doan[m%10]);
break;}
case 6:{output_d(ma7doan[m%10]);
break;}
case 7:{output_d(ma7doan[m%10]);
break;}
case 8:{output_d(ma7doan[m%10]);
break;}
case 9:{output_d(ma7doan[m%10]);
break;}
}}
Một hệ thống đếm sản phẩm gồm 1 mạch thu pháp hồng ngoại, 1 màn hình LCD 20x4.
1 bàn phím ma trận 4x4. 8 led 7 đoạn điều khiển theo phương pháp quét
a. Vẽ sơ đồ nguyên lý của hệ thống
b. Viết chương trình đồng hồ số “gio-phut-giay” hiển thị 8 led quét
c. Viết chương trình khi nhấn phím F thì bắt đầu đếm sản phẩm từ 00 đến 20 và
hiển thị hàng đầu LCD theo mẫu : SAN PHAM : XX CAI
d. Nếu nhấn phím F lần nữa thì dừng đếm sản phẩm – có sản phẩm đi qua cũng
không đếm. Và nếu nhấn F lần nữa thì cho phép đếm tiếp từ vị trí đã dừng trước
đó.
e. Nhấn 2 phím liên tiếp bé hơn 10 để đặt lại giới hạn đếm sản phẩm và hiển thị
hàng 1 LCD theo mẫu: GIOI HAN : XX Cai
f. Tính năng suất tạo ra sản phẩm và hiển thị hàng 2 LCD theo mẫu: NANG SUAT:
XXX Cai
(biết rằng năng suất là số sản phẩm làm ra trong 1 phút)
#include<16f887.h>
#include<TV_KEY4x4.c>
#fuses intrc_io
#use delay(clock =8M)
#define lcd_rs pin_e0
#define lcd_e pin_e1
#define output_lcd output_d
#include<tv_lcd.c>
#define dk0 pin_a0
#define dk1 pin_a1
#define dk2 pin_a2
#define dk3 pin_a3
#define dk4 pin_a5
#define dk5 pin_a6
#define dk6 pin_a7
#define dk7 pin_e2
#bit TM1IF = 0x0c.0
const unsigned int8
ma7doan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned int8 gio=20,phut=0,giay=0, bdn1=0,
mp,tt=0,T0,gioihan=20,i=0,a[2]={2,0},ns=0,cap=0;
void dongho();
void ledquet();
void datlai();
void demsp();
void hienthi();
void main()
{
set_tris_a(0x10); //0000 1000 A4
set_tris_b(0x0f);// 1111 0000
set_tris_c(0);
set_tris_d(0);
set_tris_e(0);
lcd_setup();
setup_timer_0(T0_EXT_H_TO_L | T0_DIV_1);
set_timer0(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
set_timer1(3036);
while(true)
{ledquet();
demsp();
hienthi();
}}
void dongho(){
if(TM1IF)
{ set_timer1(3036);
bdn1++; TM1IF=0;
if(bdn1==4){
bdn1=0;
giay++;
if(giay==60)
{ cap+=T0;
ns=cap;
cap=0;
giay=0;
phut++;
if(phut==60)
{ phut=0;
gio++;
if(gio>24){ gio=0;}
}}}}
}
void ledquet()
{ dongho();
output_c(ma7doan[gio/10%10]); output_low(dk0); delay_ms(1);
output_high(dk0); //hang chuc
output_c(ma7doan[gio/1%10]); output_low(dk1); delay_ms(1);
output_high(dk1); //hang don vi
output_c(0xbf); output_low(dk2); delay_ms(1); output_high(dk2);
output_c(ma7doan[phut/10%10]); output_low(dk3); delay_ms(1);
output_high(dk3);
output_c(ma7doan[phut/1%10]); output_low(dk4); delay_ms(1);
output_high(dk4);
output_c(0xbf); output_low(dk5); delay_ms(1); output_high(dk5);
output_c(ma7doan[giay/10%10]); output_low(dk6); delay_ms(1);
output_high(dk6);
output_c(ma7doan[giay/1%10]); output_low(dk7); delay_ms(1);
output_high(dk7);}
void datlai()
{ if(mp!=0xff) //gh=20 a[2]={2,0}; a[0]=2 a[1]=0
{ if(mp<10) {
a[i] = mp;
i++;
if(i > 1) i=0;}}
gioihan=a[0]*10+a[1]*1;
}
void demsp()
{ mp= key_4x4();
datlai();
if(mp==15) tt++; // 1 DEM 2 KH DEM 3 DEM 4 KH DEM
if(tt%2==0) {
set_timer0(T0);
T0= get_timer0();}
if(tt>=1 || tt%2!=0){
if(T0 > gioihan) {
cap+=gioihan;
set_timer0(1);}
T0= get_timer0();
}}
void hienthi()
{ lcd_command(0x80);
printf(lcd_data,"SAN PHAM: %02u CAI", T0);
lcd_command(0xc0);
printf(lcd_data,"GIOI HAN: %02u CAI",gioihan);
lcd_command(0x94);
printf(lcd_data,"NANG SUAT: %03u CAI", ns);

You might also like