#include <reg52.h> // 引用标准库的头文件 #include <absacc.h> #include <stdio.h> #include <math.h>
#define uchar unsigned char #define uint unsigned int #define Timer1_H 0x06 #define Timer1_L 0x06
uchar code Table[]="I have a dream!"; uchar code Table_1[]="I'm not alone!"; sbit RS=P2^0; //data\command =H\L sbit RW=P2^1; //read/write=H/L sbit EN=P2^2; //LCD使能信号 sbit DQ = P3^5; // 数据通信线DQ bit Conver_flag; /************************Delay function************************/ void Delay(uint count) // Delay 1ms 11.0592MHz { uint i; while(count--) { for (i=0;i<125;i++) {} } } /**************************LCD function****************************/ void Write_Com(uchar COM) { Delay(5); EN=1; RS=0; RW=0; P0=COM; Delay(5) EN=0; } void Write_Dat(uchar DAT) { Delay(5); EN=1; RS=1; RW=0; P0=DAT; EN=0; EN=0; EN=1; Delay(5); } void Init_Lcd() //初始化LCD { uchar i; Write_Com(0x38); Write_Com(0x0f); //恢复显示模式:显示器开、光标开、闪烁开。 Write_Com(0x07); Write_Com(0x80); for(i=0;i<15;i++) { Write_Dat(Table[i++]); } Write_Com(0x40); for(i=0;i<15;i++) { Write_Dat(Table_1[i++]); } } void Show_One_Char(uchar show_dat) //把1个字符显示到当前光标处 { switch(show_dat) { case ' ': Write_Dat(0x20);break; case '!': Write_Dat(0x21);break; case '"': Write_Dat(0x22);break; case '#': Write_Dat(0x23);break; case '$': Write_Dat(0x24);break; case '%': Write_Dat(0x25);break; case '&': Write_Dat(0x26);break; case '>': Write_Dat(0x27);break; case '(': Write_Dat(0x28);break; case ')': Write_Dat(0x29);break; case '*': Write_Dat(0x2A);break; case '+': Write_Dat(0x2B);break; case '-': Write_Dat(0x2D);break; case '.': Write_Dat(0x2E);break; case '/': Write_Dat(0x2F);break; case '=': Write_Dat(0x3D);break; case '<': Write_Dat(0x3E);break; case '?': Write_Dat(0x3F);break; case '^': Write_Dat(0x5E);break; case ':': Write_Dat(0x3A);break;
case '0': Write_Dat(0x30);break; case '1': Write_Dat(0x31);break; case '2': Write_Dat(0x32);break; case '3': Write_Dat(0x33);break; case '4': Write_Dat(0x34);break; case '5': Write_Dat(0x35);break; case '6': Write_Dat(0x36);break; case '7': Write_Dat(0x37);break; case '8': Write_Dat(0x38);break; case '9': Write_Dat(0x39);break;
case 'A': Write_Dat(0x41);break; case 'B': Write_Dat(0x42);break; case 'C': Write_Dat(0x43);break; case 'D': Write_Dat(0x44);break; case 'E': Write_Dat(0x45);break; case 'F': Write_Dat(0x46);break; case 'G': Write_Dat(0x47);break; case 'H': Write_Dat(0x48);break; case 'I': Write_Dat(0x49);break; case 'J': Write_Dat(0x4A);break; case 'K': Write_Dat(0x4B);break; case 'L': Write_Dat(0x4C);break; case 'M': Write_Dat(0x4D);break; case 'N': Write_Dat(0x4E);break; case 'O': Write_Dat(0x4F);break; case 'P': Write_Dat(0x50);break; case 'Q': Write_Dat(0x51);break; case 'R': Write_Dat(0x52);break; case 'S': Write_Dat(0x53);break; case 'T': Write_Dat(0x54);break; case 'U': Write_Dat(0x55);break; case 'V': Write_Dat(0x56);break; case 'W': Write_Dat(0x57);break; case 'X': Write_Dat(0x58);break; case 'Y': Write_Dat(0x59);break; case 'Z': Write_Dat(0x5A);break;
case 'a': Write_Dat(0x61);break; case 'b': Write_Dat(0x62);break; case 'c': Write_Dat(0x63);break; case 'd': Write_Dat(0x64);break; case 'e': Write_Dat(0x65);break; case 'f': Write_Dat(0x66);break; case 'g': Write_Dat(0x67);break; case 'h': Write_Dat(0x68);break; case 'i': Write_Dat(0x69);break; case 'j': Write_Dat(0x6A);break; case 'k': Write_Dat(0x6B);break; case 'l': Write_Dat(0x6C);break; case 'm': Write_Dat(0x6D);break; case 'n': Write_Dat(0x6E);break; case 'o': Write_Dat(0x6F);break; case 'p': Write_Dat(0x70);break; case 'q': Write_Dat(0x71);break; case 'r': Write_Dat(0x72);break; case 's': Write_Dat(0x73);break; case 't': Write_Dat(0x74);break; case 'u': Write_Dat(0x75);break; case 'v': Write_Dat(0x76);break; case 'w': Write_Dat(0x77);break; case 'x': Write_Dat(0x78);break; case 'y': Write_Dat(0x79);break; case 'z': Write_Dat(0x7A);break;
default: break; } } void Show_Char(uchar Char[]) // 把1个字符串显示到当前光标处 { uchar Count; for(Count=0;;Count++) { Show_One_Char(Char[Count]); if(Char[Count+1]=='\0') break; } }
/*********************DS18B20***function*************************/
void Reset_Tx(void) //reset { uint i; DQ=0; i=105; while(i--); //approx 900us DQ=1; i=4; while(i--) ; } void Wait_Rx(void) //Wait for Rx { uint i; while(DQ); //DQ=1,then circulation while(~DQ); //DQ=0, detected the Rx i=4; while(i--); } bit Rd_Bit(void) //read one bit { uint i; bit dat; DQ=0; i++; DQ=1; i++;i++; // 延时15us以上,读时隙下降沿后15us,DS18B20输出数据才有效 dat=DQ; i=8; while(i--); return(dat); } uchar Rd_Byte(void) //read one byte { uchar i,j,dat; dat=0; for(i=8;i>=1;i--) { j=Rd_Bit(); dat=(j<<7)|(dat>>1); } return(dat); } void Wt_Byte(uchar dat) //write one byte { uint i; uchar j; bit temp; for(j=8;j>=1;j--) { temp=dat&0x01; dat=dat>>1; if(temp) //write 1 { DQ=0; i++;i++; // 延时,使得15us以内拉高 DQ=1; i=8;while(i--); // 整个写1时隙不低于60us } else //write 0 { DQ=0; i=8;while(i--); // 保持低在60us到120us之间 DQ=1;i++;i++; } } } bit Convert_T(void) //convert T { Reset_Tx(); Wait_Rx(); Delay(1); Wt_Byte(0xcc); //skip rom Wt_Byte(0x44); //convert return(1); } uchar Rd_T(void) //read T { uchar i,j,x,y,z; Reset_Tx(); Wait_Rx(); Delay(1); Wt_Byte(0xcc); //skip rom Wt_Byte(0xbe); //read scratchpad i=Rd_Byte(); //LSB j=Rd_Byte(); //MSB x=i>>4; y=j<<4; z=x|y; return(z); }
/******************************main**************************************/ void main(void) { uchar t_last; Delay(1); TMOD=0x21; TH1=Timer1_H; TL1=Timer1_L; ET1=1; //开TIMER1中断 TR1=1; //启动 EA=1; //开总中断
while(1) { Init_Lcd(); Convert_T(); if(Convert_T()) { Conver_flag=0; TH0=55536/256; //开TIMER0 TL0=55536%256; TR0=1; EA=1; } if(Conver_flag) { Conver_flag=0; t_last=Rd_T(); Show_Char("t_last"); Delay(1000); //Delay 1s } } }
void Timer0(void) interrupt 1 //T0用于定时1s时间 { uint Timer0_count; Timer0_count++; TR0=0; if(Timer0_count==0) { Timer0_count=0; Convert_T(); Conver_flag=1; } TH0=55536/256; //开TIMER0 TL0=55536%256; TR0=1; } void Timer1(void) interrupt 3 { uchar t_last; TR1=0; EA=0; if(Conver_flag) { t_last=Rd_T(); Show_Char("t_last"); Delay(1000); //Delay 1s } TH1=Timer1_H; TL1=Timer1_L; EA=1; //开总中断 ET1=1; //开TIMER1中断 TR1=1; //启动 }
我是个初学者,在用proteus仿真的时候,LCD不显示,而且没有进中断.看了好久没有头绪,不知道是否要用中断去显示,问题出在哪呢?哪位仁兄指点一下! 非常感激! |