程序編譯沒(méi)錯(cuò)誤,下載到開(kāi)發(fā)板就顯示不出來(lái),到網(wǎng)上找別人的程序下進(jìn)去也不行,求大神分析
#include<reg52.h> #include<intrins.h> #defineuchar unsigned char #defineuint unsigned int /*DS1302 端口設(shè)置 */ sbitSCK=P3^6; //DS1302時(shí)鐘 sbitSDA=P3^4; //DS1302 IO sbit RST= P3^5; // DS1302復(fù)位 bitReadRTC_Flag; //讀DS1302全局變量 /* 12864端口定義*/ #defineLCD_data P0 //帶字庫(kù)液晶12864數(shù)據(jù)口 sbitLCD_RS = P2^4; //寄存器選擇輸入 sbitLCD_RW = P2^5; //液晶讀/寫(xiě)控制 sbitLCD_EN = P2^6; //液晶使能控制 sbit PSB=P2^1; //并口控制 sbit RES=P2^3; ucharcode dis1[] = {" 電子設(shè)計(jì)天地"}; //液晶顯示的漢字 ucharcode dis2[] = {"有志者,事竟成!"}; ucharcode dis4[] = {'0','1','2','3','4','5','6','7','8','9'}; unsignedchar temp; #definedelayNOP(); {_nop_();_nop_();_nop_();_nop_();}; voidlcd_pos(uchar X,uchar Y); //確定顯示位置 unsignedchar l_tmpdate[7]={0,7,16,19,10,1,9};//秒分時(shí)日月周年09-10-1916:07:00 codeunsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分時(shí)日月周年 最低位讀寫(xiě)位 codeunsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; voidWrite_Ds1302_byte(unsigned char temp); voidWrite_Ds1302( unsigned char address,unsigned char dat ); unsignedchar Read_Ds1302 ( unsigned char address ); voidRead_RTC(void);//read RTC voidSet_RTC(void); //set RTC voidInitTIMER0(void);//inital timer0 /*******************************************************************/ /* */ /* 延時(shí)函數(shù) */ /* */ /*******************************************************************/ void delay(unsigned int m) //延時(shí)程序 { unsigned int i,j; for(i=0;i<m;i++) for(j=0;j<10;j++); } /*******************************************************************/ /* */ /*檢查LCD忙狀態(tài) */ /*lcd_busy為1時(shí),忙,等待。lcd-busy為0時(shí),閑,可寫(xiě)指令與數(shù)據(jù)。 */ /* */ /*******************************************************************/ bitlcd_busy() { bit result; LCD_RS = 0; LCD_RW = 1; LCD_EN = 1; delayNOP(); result = (bit)(P0&0x80); LCD_EN = 0; return(result); } /*******************************************************************/ /* */ /*寫(xiě)指令數(shù)據(jù)到LCD */ /*RS=L,RW=L,E=高脈沖,D0-D7=指令碼。 */ /* */ /*******************************************************************/ voidlcd_wcmd(uchar cmd) { while(lcd_busy()); LCD_RS = 0; LCD_RW = 0; LCD_EN = 0; _nop_(); _nop_(); P0 = cmd; delay(1); LCD_EN = 1; delay(1); LCD_EN = 0; } /*******************************************************************/ /* */ /*寫(xiě)顯示數(shù)據(jù)到LCD */ /*RS=H,RW=L,E=高脈沖,D0-D7=數(shù)據(jù)。 */ /* */ /*******************************************************************/ voidlcd_wdat(uchar dat) { while(lcd_busy()); LCD_RS = 1; LCD_RW = 0; LCD_EN = 0; P0 = dat; delayNOP(); LCD_EN = 1; delay(1); LCD_EN = 0; } /*******************************************************************/ /* */ /* LCD初始化設(shè)定 */ /* */ /*******************************************************************/ voidlcd_init() { P0=0xFF; P2=0xFF; delay(40); PSB=1; //并口方式。 delay(1); RES=0; delay(1); RES=1; delay(10); lcd_wcmd(0x30); delay(100); lcd_wcmd(0x30); delay(37); lcd_wcmd(0x08); delay(100); lcd_wcmd(0x10); delay(100); lcd_wcmd(0x0C); //顯示開(kāi),關(guān)光標(biāo) delay(100); lcd_wcmd(0x01); //清除LCD的顯示內(nèi)容 delay(100); lcd_wcmd(0x06); delay(100); } voidmain() { uchar i; InitTIMER0(); Set_RTC(); lcd_init(); //初始化LCD while(1) { if(ReadRTC_Flag) { ReadRTC_Flag=0; Read_RTC(); lcd_pos(0,0); //設(shè)置顯示位置為第一行的第1個(gè)字符 i = 0; while(dis1 != '\0') { //顯示字符 lcd_wdat(dis1); i++; } lcd_pos(1,0); //設(shè)置顯示位置為第二行的第1個(gè)字符 i = 0; while(dis2 != '\0') { lcd_wdat(dis2); //顯示字符 i++; } lcd_pos(2,0); lcd_wdat(dis4[l_tmpdate[6]/16]); //DS1302是BCD碼,進(jìn)行處理得到十位與個(gè)位數(shù)。 lcd_pos(2,1); lcd_wdat(dis4[l_tmpdate[6]%16]); lcd_pos(2,2); lcd_wdat('-'); lcd_pos(2,3); lcd_wdat(dis4[l_tmpdate[4]/16]); lcd_pos(2,4); lcd_wdat(dis4[l_tmpdate[4]%16]); lcd_pos(2,5); lcd_wdat('-'); lcd_pos(2,6); lcd_wdat(dis4[l_tmpdate[3]/16]); lcd_pos(2,7); lcd_wdat(dis4[l_tmpdate[3]%16]); //設(shè)置顯示位置為第三行的第1個(gè)字符 lcd_pos(3,0); //設(shè)置顯示位置為第四行的第1個(gè)字符 lcd_wdat(dis4[l_tmpdate[2]/16]); lcd_pos(3,1); lcd_wdat(dis4[l_tmpdate[2]%16]); lcd_pos(3,2); lcd_wdat(':'); lcd_pos(3,3); lcd_wdat(dis4[l_tmpdate[1]/16]); lcd_pos(3,4); lcd_wdat(dis4[l_tmpdate[1]%16]); lcd_pos(3,5); lcd_wdat(':'); lcd_pos(3,6); lcd_wdat(dis4[l_tmpdate[0]/16]); lcd_pos(3,7); lcd_wdat(dis4[l_tmpdate[0]%16]); } } } /*********************************************************/ /* */ /* 設(shè)定顯示位置 */ /* */ /*********************************************************/ voidlcd_pos(uchar X,uchar Y) { uchar pos; if (X==0) {X=0x80;} else if (X==1) {X=0x90;} else if (X==2) {X=0x88;} else if (X==3) {X=0x98;} pos = X+Y ; lcd_wcmd(pos); //顯示地址 } voidInitTIMER0(void) { TMOD|=0x01;//定時(shí)器設(shè)置 16位 TH0=0xef;//初始化值 TL0=0xf0; ET0=1; TR0=1; EA=1; } voidWrite_Ds1302_Byte(unsigned char temp) { unsignedchar i; for(i=0;i<8;i++) //循環(huán)8次 寫(xiě)入數(shù)據(jù) { SCK=0; SDA=temp&0x01; //每次傳輸?shù)妥止?jié) temp>>=1; //右移一位 SCK=1; } } /****************************************************************************/ voidWrite_Ds1302( unsigned char address,unsigned char dat ) { RST=0; _nop_(); SCK=0; _nop_(); RST=1; _nop_(); //啟動(dòng) Write_Ds1302_Byte(address); //發(fā)送地址 Write_Ds1302_Byte(dat); //發(fā)送數(shù)據(jù) RST=0; //恢復(fù) } /****************************************************************************/ unsignedchar Read_Ds1302 ( unsigned char address ) { unsigned char i,temp=0x00; RST=0; _nop_(); SCK=0; _nop_(); RST=1; _nop_(); Write_Ds1302_Byte(address); for (i=0;i<8;i++) //循環(huán)8次 讀取數(shù)據(jù) { if(SDA) temp|=0x80; //每次傳輸?shù)妥止?jié) SCK=0; temp>>=1; //右移一位 SCK=1; } RST=0; _nop_(); //以下為DS1302復(fù)位的穩(wěn)定時(shí)間 RST=0; SCK=0; _nop_(); SCK=1; _nop_(); SDA=0; _nop_(); SDA=1; _nop_(); return(temp); //返回 } /****************************************************************************/ voidRead_RTC(void) //讀取 日歷 { unsignedchar i,*p; p=read_rtc_address;//地址傳遞 for(i=0;i<7;i++) //分7次讀取 秒分時(shí)日月周年 { l_tmpdate=Read_Ds1302(*p); p++; } } /***********************************************************************/ voidSet_RTC(void) //設(shè)定 日歷 { unsignedchar i,*p,tmp; for(i=0;i<7;i++){ //BCD處理 tmp=l_tmpdate/10; l_tmpdate=l_tmpdate%10; l_tmpdate=l_tmpdate+tmp*16; } Write_Ds1302(0x8E,0X00); p=write_rtc_address; //傳地址 for(i=0;i<7;i++) //7次寫(xiě)入 秒分時(shí)日月周年 { Write_Ds1302(*p,l_tmpdate); p++; } Write_Ds1302(0x8E,0x80); } voidtim(void) interrupt 1 using 1//中斷,用于數(shù)碼管掃描 { static unsigned char i,num; TH0=0xf5; TL0=0xe0; //P0=table[l_tmpdisplay]; //查表法得到要顯示數(shù)字的數(shù)碼段 //P2=table1; i++; if(i==8) { i=0; num++; if(20==num) //隔段時(shí)間讀取1302的數(shù)據(jù)。時(shí)間間隔可以調(diào)整 { ReadRTC_Flag=1; //使用標(biāo)志位判斷 num=0; } } }
|