完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我的程序不能按键校验,而且显示也不对 求教!!!
#include unsigned char display[]= { 0xc0,0xf9,0xa4,0xb0,0x99, 0x92,0x82,0xf8,0x80,0x90 }; //数字0-9 unsigned char dispbitcode[]= { 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f };//位码相应位的点亮 unsigned char second; unsigned char minute; unsigned char hour; ***it P1_4 = P1^4; ***it P1_5 = P1^5; ***it P1_6 = P1^6; void delayms(unsigned int k) { unsigned int i,j; for(i=0;i } void main(main) { P0=0xff; TMOD=0x01; //定时器0,模式1 TH0=0x3c; //初值 TL0=0xcd; TR0=1; ET0=1; EA=1; while(1) { /* 秒钟校准 */ if(P1_4==0) { delayms(10); //消抖 if(P1_4==0) { second++; if(second==60) { second=0; } P2=0xfe; P0=display[second%10]; P2=0xfd; P0=display[second/10]; while(P1_4==0); //等待按键松开 } } /* 分钟校准 */ if(P1_5==0) { delayms(10); //消抖 if(P1_5==0) { minute++; if(minute==60) { minute=0; } P2=0xfb; P0=display[minute%10]; P2=0xf7; P0=display[minute/10]; while(P1_5==0); //等待按键松开 } } /* 小时校准 */ if(P1_6==0) { delayms(10); //消抖 if(P1_6==0) { hour++; if(hour==24) { hour=0; } P2=0xef; P0=display[hour%10]; P2=0xdf; P0=display[hour/10]; while(P0_6==0); //等待按键松开 } } /* 显示时间 */ P2=0xfe; P0=display[second%10]; P2=0xfd; P0=display[second/10]; P2=0xfb; P0=display[minute%10]; P2=0xf7; P0=display[minute/10]; P2=0xef; P0=display[hour%10]; P2=0xdf; P0=display[hour/10]; } } //while循环 void time0() interrupt 1 using 1 { static unsigned char tcount; TH0=0x3c; TL0=0xcd; if(++tcount==20) { tcount=0; if(second++>=60) { second=0; if(minute++>=60) { minute=0; if(hour++>=24) { hour=0; } } } } } |
|
相关推荐
3 个讨论
|
|
这是1302单片机 1602液晶显示
/**************************************************************** ????: LCD1602???? ??:????????? SE3???1-2?? SE5???2-3?? *****************************************************************/ /*???*/ #include #include #define uint unsigned int #define uchar unsigned char #define nop() _nop_() ***it lcd_rs_port = P3^7; /*??LCD????*/ ***it lcd_rw_port = P3^6; /*??LCD????*/ ***it lcd_en_port = P3^5; /*??LCD????*/ #define lcd_data_port P1 /*??LCD????*/ ***it T_CLK = P0^2; /*????????? */ ***it T_IO = P0^1; /*????????? */ ***it T_RST = P0^0; /*????????? */ ***it ACC0 = ACC^0; ***it ACC7 = ACC^7; uchar code mun_to_char[] = {"0123456789ABCDEF"}; /*?????ASCII????*/ uchar data time_data_buff[7]={0x00,0x11,0x14,0x10,0x12,0x2,0x13};/*???: ? ? ? ? ? ?? ? */ uchar data lcd1602_line1[]={" 20XX-XX-XX XXX "}; uchar data lcd1602_line2[]={" XX:XX:XX "}; uchar code Weeks[][3]={{"Sun"},{"Mon"},{"Tue"},{"Wen"},{"Thu"},{"Fri"},{"Sat"},{"Sun"}}; /******************************************************************** ? ? ?:RTInputByte() ? ?:????????? ? ?:?DS1302??1Byte?? (????) ????:d ????? ? ? ?:? ? ?:zhaojunjie ? ?:2002-03-19 ? ?: ? ?: ***********************************************************************/ void RTInputByte(uchar d) { uchar i; ACC = d; for(i=8; i>0; i--) { T_IO = ACC0; /*??????? RRC */ T_CLK = 1; T_CLK = 0; ACC = ACC >> 1; } } /******************************************************************** ? ? ?:RTOutputByte() ? ?:????????? ? ?:?DS1302??1Byte?? (????) ????:? ? ? ?:ACC ? ?:zhaojunjie ? ?:2002-03-19 ? ?: ? ?: ***********************************************************************/ uchar RTOutputByte(void) { uchar i; for(i=8; i>0; i--) { ACC = ACC >>1; /*??????? RRC */ ACC7 = T_IO; T_CLK = 1; T_CLK = 0; } return(ACC); } /******************************************************************** ? ? ?:W1302() ? ?:?DS1302???? ? ?:????,????/?? (????) ? ?:RTInputByte() , RTOutputByte() ????:ucAddr: DS1302??, ucData: ????? ? ? ?:? ? ?:zhaojunjie ? ?:2002-03-19 ? ?: ? ?: ***********************************************************************/ void W1302(uchar ucAddr, uchar ucDa) { T_RST = 0; T_CLK = 0; T_RST = 1; RTInputByte(ucAddr); /* ??,?? */ RTInputByte(ucDa); /* ?1Byte??*/ T_CLK = 1; T_RST = 0; } /******************************************************************** ? ? ?:R1302() ? ?:??DS1302?????? ? ?:????,????/?? (????) ? ?:RTInputByte() , RTOutputByte() ????:ucAddr: DS1302?? ? ? ?:ucData :????? ? ?:zhaojunjie ? ?:2002-03-19 ? ?: ? ?: ***********************************************************************/ uchar R1302(uchar ucAddr) { uchar ucData; T_RST = 0; T_CLK = 0; T_RST = 1; RTInputByte(ucAddr); /* ??,?? */ ucData = RTOutputByte(); /* ?1Byte?? */ T_CLK = 1; T_RST = 0; return(ucData); } /******************************************************************** ? ? ?:Set1302() ? ?:?????? ? ?:????,????/??(????????) ? ?:W1302() ????:pClock: ???????? ???: ? ? ? ? ? ?? ? 7Byte (BCD?)1B 1B 1B 1B 1B 1B 1B ? ? ?:? ? ?:zhaojunjie ? ?:2002-03-19 ? ?: ? ?: ***********************************************************************/ void Set1302(uchar *pClock) { uchar i; uchar ucAddr = 0x80; EA = 0; W1302(0x8e,0x00); /* ????,WP=0,????*/ for(i =7; i>0; i--) { W1302(ucAddr,*pClock); /* ? ? ? ? ? ?? ? */ pClock++; ucAddr +=2; } W1302(0x8e,0x80); /* ????,WP=1,????*/ EA = 1; } /******************************************************************** ? ? ?:Get1302() ? ?:??DS1302???? ? ?: ? ?:R1302() ????:ucCurtime: ????????????????: ? ? ? ? ? ?? ? 7Byte (BCD?) 1B 1B 1B 1B 1B 1B 1B ? ? ?:? ? ?:zhaojunjie ? ?:2002-03-19 ? ?: ? ?: ***********************************************************************/ void Get1302(uchar ucCurtime[]) { uchar i; uchar ucAddr = 0x81; EA = 0; for (i=0; i<7; i++) { ucCurtime[i] = R1302(ucAddr);/*???: ? ? ? ? ? ?? ? */ ucAddr += 2; } EA = 1; } //--------------------------------------------- void lcd_delay(uchar ms) /*LCD1602 ??*/ { uchar j; while(ms--){ for(j=0;j<250;j++) {;} } } void lcd_busy_wait() /*LCD1602 ???*/ { lcd_rs_port = 0; lcd_rw_port = 1; lcd_en_port = 1; lcd_data_port = 0xff; while (lcd_data_port&0x80); lcd_en_port = 0; } void lcd_command_write(uchar command) /*LCD1602 ?????*/ { lcd_busy_wait(); lcd_rs_port = 0; lcd_rw_port = 0; lcd_en_port = 0; lcd_data_port = command; lcd_en_port = 1; lcd_en_port = 0; } void lcd_system_reset() /*LCD1602 ???*/ { lcd_delay(20); lcd_command_write(0x38); lcd_delay(100); lcd_command_write(0x38); lcd_delay(50); lcd_command_write(0x38); lcd_delay(10); lcd_command_write(0x08); lcd_command_write(0x01); lcd_command_write(0x06); lcd_command_write(0x0c); lcd_data_port = 0xff; /*??????*/ } void lcd_char_write(uchar x_pos,y_pos,lcd_dat) /*LCD1602 ????*/ { x_pos &= 0x0f; /* X???? 0~15 */ y_pos &= 0x01; /* Y???? 0~ 1 */ if(y_pos==1) x_pos += 0x40; x_pos += 0x80; lcd_command_write(x_pos); lcd_busy_wait(); lcd_rs_port = 1; lcd_rw_port = 0; lcd_en_port = 0; lcd_data_port = lcd_dat; lcd_en_port = 1; lcd_en_port = 0; lcd_data_port = 0xff; /*??????*/ } void main() { uchar i; lcd_system_reset(); /*LCD1602 ???*/ Set1302(time_data_buff); /*????*/ while(1){ Get1302(time_data_buff); /*??????*/ /*????*/ lcd1602_line1[3] = mun_to_char[time_data_buff[6]/0x10]; lcd1602_line1[4] = mun_to_char[time_data_buff[6]%0x10]; /*?*/ lcd1602_line1[6] = mun_to_char[time_data_buff[4]/0x10]; lcd1602_line1[7] = mun_to_char[time_data_buff[4]%0x10]; /*?*/ lcd1602_line1[9] = mun_to_char[time_data_buff[3]/0x10]; lcd1602_line1[10] = mun_to_char[time_data_buff[3]%0x10]; /*?*/ for(i=0;i<3;i++) lcd1602_line1[i+12]=Weeks[time_data_buff[5]&0x07][i]; /*??*/ lcd1602_line2[4] = mun_to_char[time_data_buff[2]/0x10]; lcd1602_line2[5] = mun_to_char[time_data_buff[2]%0x10]; /*?*/ lcd1602_line2[7] = mun_to_char[time_data_buff[1]/0x10]; lcd1602_line2[8] = mun_to_char[time_data_buff[1]%0x10]; /*?*/ lcd1602_line2[10] = mun_to_char[time_data_buff[0]/0x10]; lcd1602_line2[11] = mun_to_char[time_data_buff[0]%0x10]; /*?*/ for(i=0;i<16;i++) lcd_char_write(i,0,lcd1602_line1[i]); for(i=0;i<16;i++) lcd_char_write(i,1,lcd1602_line2[i]); } } |
|
|
|
|
|
只有小组成员才能发言,加入小组>>
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-4 02:11 , Processed in 1.591755 second(s), Total 48, Slave 38 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号