|
智能電加熱定時(shí)控制電路原程序 程序代碼 #include <reg51.h> #include <intrins.h> #include<stdlib.h> #define uchar unsigned char #define uint unsigned int #define AddWr 0xa0 //器件地址選擇及寫標(biāo)志 #define AddRd 0xa1 //器件地址選擇及讀標(biāo)志 sbit Sda= P1^3 //串行數(shù)據(jù) sbit Scl= P1^4 //串行時(shí)鐘 sbit WP= P1^5 //硬件寫保護(hù) idata byte rom_sed[9]; idata byte rom_rec[7]; rom_sed[0]=0x00; rom_sed[1]=0x00; rom_sed[2]=0x00; rom_sed[3]=0x55; rom_sed[4]=0x23; rom_sed[5]=0x31; rom_sed[6]=0x06; rom_sed[7]=0x92; rom_sed[8]=0x99; sbit U3 CLK = P1^2;ˆ sbit U3 SD = P1^1 sbit U4CLK = P1^3; sbit U4SD =P1^4; sbit TSOR = P1^0; sbit FUZAI = P1^6; sbit BELL = P1^7; sbit RS = P2^7; sbit RW = P2^1; sbit EN = P2^5; sbit S0 = P3^0 sbit S1 = P3^1 sbit S2 = P3^2 sbit S3 = P3^3 sbit S4 = P3^4 sbit S5 = P3^5 /*------------------------------------------全局變量-------------------------------------------------------*/ static unsigned char max temp1,maxtemp2; //溫度值的最大值整數(shù)部分、小數(shù)部分 static unsigned char min temp1, mintemp2; //溫度值的最小值整數(shù)部分、小數(shù)部分 static unsigned char hour, min,; // 小時(shí) ,分鐘、 static unsigned char hourset str1[]; //設(shè)定的小時(shí)數(shù) static unsigned char minset str2[] ; //設(shè)定的分鐘數(shù) static unsigned char countset; //設(shè)定的定時(shí)次數(shù) static char line0[] =" 00:00 "; static char line1[] =" . C ”; /*-------------------------------------------------------------------------------------------------------------*/ void KeyboardDelay(); /*-------------------------------------------LCD驅(qū)動(dòng)函數(shù)------------------------------------------------*/ void DelayL(); void DelayS(); void WriteCommand(unsigned char c); void WriteData(unsigned char c); void ShowChar(unsigned char pos,unsigned char c); void ShowString(unsigned char line,char *ptr); void InitLcd(); /*---------------------------------------------時(shí)鐘函數(shù)-----------------------------------------------*/ void PCF8563WriteRead() /*------------------------------------------溫度傳感器驅(qū)動(dòng)----------------------------------------------*/ void Delay15(); void Delay60(); void Delay100ms(); void Write0TS(); void Write1TS(); bit ReadTS(); void ResetTS(); void WriteByteTS(unsigned char byte); unsigned char ReadByteTS(); void InitTS(); void GetTempTS(); /*-------------------------------------------------主程序---------------------------------------------------*/ void main (void) { char code str1[] =" Hello World! "; char code str2[] =" 2005-4-12 "; unsigned char i; countset=0; SP=0x50; TSOR=1; //1-wire總線釋放 DelayL(); InitLcd(); //初始化LCD DelayL(); ShowString(0,str1); //啟動(dòng)畫面 ShowString(1,str2); for(i=0;i<15;i++) Delay100ms(); InitInterupt(); //初始化中斷設(shè)置 Hoursetstr1[0]=00; //缺省定時(shí)00小時(shí) Minsetstr2[0]=00; //缺省定時(shí)00分鐘 min=rom-rec[0] //初始化數(shù)據(jù) Hour = rom-rec[1] BELL = 0; FUZAI = 0; count=0; P1=0xFF; InitTS(); //初始化溫度計(jì) while(1) //循環(huán)顯示溫度值 { GetTempTS(); , line1[0]=0x20; i=temp1; if(I< max temp1&& i> min temp1) //告警燈亮 ALERT=1; FUZAI =1 line1[1]=i/10+0x30; //顯示溫度的十位 line1[2]=i%10+0x30; //顯示個(gè)位 line1[4]=temp2+0x30; //顯示小數(shù)位 ShowString(1,line1); line0[5]=hour/10+0x30; //顯示時(shí)間 line0[6]=hour%10+0x30; line0[8]=min/10+0x30; line0[9]=min%10+0x30; ShowString(0,line0); Delay100ms(); } } //------------------------------------------------初始化中斷設(shè)置--------------------------------------------------// void InitInterupt() { TMOD=0x21; TL1=0x00; TH1=0x00; EA=1; TR1=1; TL0=0x00; TH0=0x00; ET0=1; IT0=1; } void KeyboardDelay() //按鍵中斷延時(shí) { unsigned char i,j; i=0x40; j=0xFF; while(i--) while(j--); } /*-----------------------------------------T0中斷處理-----------------------------------------------------*/ Timer0_process() interrupt 1 using 0 { EA=0; TR0=0; TL0=0x00; TH0=0x00 if(hour==hoursetstr1[]) { if(min==minsetstr2[]) { if(temp<maxtempset&&temp>mintempset) { FUZAI =1 else { FUZAI =0 } } } TR0=1; EA=1; } /*--------------------------------------LCD驅(qū)動(dòng)子程序--------------------------------------------------*/ void DelayL() {unsigned char i,j; i=0xF0; j=0xFF; while(i--) while(j--); } void DelayS() { unsigned char i; i=0x1F; while(i--); } void WriteCommand(unsigned char c) { DelayS(); EN=0; RS=0; RW=0; _nop_(); EN=1; P0=c; EN=0; } void WriteData(unsigned char c) { DelayS(); EN=0; RS=1; RW=0; _nop_(); EN=1; P0=c; EN=0; RS=0; } void ShowChar(unsigned char pos,unsigned char c) { unsigned char p; if(pos>=0x10) p=pos+0xB0; else p=pos+0x80; WriteCommand(p); WriteData(c); } void ShowString(unsigned char line,char *ptr) { unsigned char l,i; l=line<<4; for(i=0;i<16;i++) ShowChar(l++,*(ptr+i)); } void InitLcd() { DelayL(); WriteCommand(0x38); WriteCommand(0x38); WriteCommand(0x06); WriteCommand(0x0C); WriteCommand(0x01); WriteCommand(0x80); } /*--------------------------------------------T1中斷處理-鍵盤子程序-------------------------------------------------*/ Timer1_process() interrupt3 using 0 { EA=0; TR1=0; TL1=0x00; TH1=0x00 unsigned char k bit ho/mi; bit tempfiag ho/mi = 0; tempfiag =0 P3 =0x3F k=P3; k=k&0x3F; void 24C04 WriteRead() while(k&0x02&& ho/mi= 0 ) { ho/mi =~ho/mi KeyboardDelay() While(K&0x08) { KeyboardDelay() hour++; } While(K&0x10) { KeyboardDelay() Hour——; } } while( k&0x02&& ho/mi= 1) { ho/mi =~ho/mi KeyboardDelay() While(K&0x08) { KeyboardDelay() min++; } While(K&0x10) { KeyboardDelay() Min——; } } while(k&0x01) { while(k&0x02&& ho/mi= 0 ) { ho/mi =~ho/mi KeyboardDelay() While(K&0x08) { KeyboardDelay() hourset++; } While(K&0x10) { KeyboardDelay() Hourset——; } } while( k&0x02&& ho/mi= 1) { ho/mi =~ho/mi KeyboardDelay() While(K&0x08) { KeyboardDelay() minset++; } While(K&0x10) { KeyboardDelay() Minset——-; } } Hoursetstr[]=hourset Minsetstr[]=minset countset++; *Hoursetstr++ *Minsetstr++ While(K&0X04&&tempfiag=0 ) { tempfiag=~ tempfiag While(K&0x08) { max temp1++} While(K&0x10) {max temp1++} While(K&0X04&&tempfiag=1 ) { tempfiag=~ tempfiag While(K&0x08) {min temp1——} While(K&0x10) {min temp1——} } } } } While(K&0X20) {return} void 24C04 WriteRead() TR1=1; EA=1; /*------------------------------------------溫度傳感器DS1820子程序-------------------------------------------*/ void Delay100ms() //延時(shí)100ms { unsigned char i,j,k; for(i=0;i<8;i++) for(j=0;j<25;j++) for(k=0;k<250;k++); } void Delay15() //延時(shí)15us { unsigned char i; for(i=0;i<8;i++); } void Delay60() //延時(shí)60us { unsigned char i; for(i=0;i<30;i++); } void Write0TS() //寫bit 0 { TSOR=1; TSOR=0; Delay15(); Delay15(); Delay15(); Delay15(); TSOR=1; _nop_(); _nop_(); } void Write1TS() //寫bit 1 { TSOR=1; TSOR=0; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); TSOR=1; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); Delay15(); Delay15(); Delay15(); } bit ReadTS() { bit b; TSOR=1; TSOR=0; _nop_(); _nop_(); _nop_(); _nop_(); TSOR=1; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); b=TSOR; Delay15(); Delay15(); Delay15(); _nop_(); _nop_(); return b; } void ResetTS() //復(fù)位 {unsigned char i; TSOR=1; TSOR=0; for(i=0;i<8;i++) Delay60(); TSOR=1; while(TSOR); for(i=0;i<8;i++) Delay60(); } void WriteByteTS(unsigned char byte) //寫一個(gè)字節(jié)(byte) {unsigned char i; for(i=0;i<8;i++) { if(byte&0x01) Write1TS(); else Write0TS(); byte=byte>>1; } } unsigned char ReadByteTS() //讀一個(gè)字節(jié)(byte) { unsigned char i,j; bit b; j=0; for(i=0;i<8;i++) { b=ReadTS(); if(b) j+=1; j=_cror_(j,1); } return j; } void InitTS() //初始化溫度轉(zhuǎn)換 { ResetTS(); WriteByteTS(0xCC); WriteByteTS(0x4E); WriteByteTS(0x64); WriteByteTS(0x00); WriteByteTS(0x1F); } void GetTempTS() //獲取溫度 { ResetTS(); WriteByteTS(0xCC); WriteByteTS(0x44); Delay100ms(); ResetTS(); WriteByteTS(0xCC); WriteByteTS(0xBE); temp2=ReadByteTS(); temp1=ReadByteTS(); ReadByteTS(); ReadByteTS(); ReadByteTS(); ReadByteTS(); ReadByteTS(); ReadByteTS(); ReadByteTS(); temp1=temp1<<4; temp1+=(temp2&0xF0)>>4; temp2=(temp2&0x0F)?5:0; /-----------------------------------------------------時(shí)鐘函數(shù)--------------------------------------------------------// void PCF8563 WriteRead() { data byte i; rom_sed[0]=0x00; rom_sed[1]=0x00; rom_sed[2]=0x00; rom_sed[3]=min; rom_sed[4]= hour; rom_sed[5]=0x300; rom_sed[6]=00 rom_sed[7]=0x00 rom_sed[8]=0x00 for(i=0;i<255;i++)delay(); I_start(); sbit scl=P1^2; //定義串行I/O口 sbit sda=P1^1 idata byte rom_sed[9]; idata byte rom_rec[7]; idata byte j,k; bit flag,flag1; void delay(void) //延時(shí)子程序 { data byte i; for(i=0;i<6;i++); } void I_start(void) //發(fā)送I2C總線起始條件子程序 { sda=1; ; scl=1; delay(); sda=0; delay(); scl=0; ; } void I_stop(void) //I2C總線停止條件子程序 { sda=0; ; scl=1; delay(); sda=1; delay(); } bit I_send(byte I_data) //字節(jié)數(shù)據(jù)傳送子程序 { data byte i; for(i=0;i<8;i++) { sda=(bit)(I_data&0x80); I_data=I_data<<1; ; scl=1; delay(); scl=0; } ; ; sda=1; ; ; //準(zhǔn)備接收ACK位 scl=1; ; ; //開始接收ack位 flag=0; if(sda==0)flag=0; else flag=1; //返回(~I_clock()); scl=0; return(flag); } byte I_receive(void) //字節(jié)數(shù)據(jù)接收子程序 { data byte i; byte I_data=0; sda=1; for(i=0;i<8;i++) { I_data*=2; ; scl=0; delay(); scl=1; ; ; if(sda==1)I_data++; ; ; } scl=0; ; ; ; sda=0; if(flag1==0) //不是最后一個(gè)ACK { ; ; scl=1; delay(); scl=0; } else //是最后一個(gè)ACK { sda=1; ; ; scl=1; delay(); scl=0; flag1=0; } return(I_data); } if(~I_send(0xa2)) //pcf_write地址 { if(~I_send(0x02)) //pcf_status寄存器地址 { for(i=0;i<9;i++) { if(~I_send(rom_sed[i])); else; } I_stop(); } else; } else; start: I_start(); if(~I_send(0xa2)) // pcf_write地址 { if(~I_send(0x02)) // pcf_status寄存器地址 { I_start(); if(~I_send(0xa3)) //寫狀態(tài)寄存器 { for(i=0;i<7;i++) { if(i==6) flag1=1; else flag1=0; rom_rec[i]=I_receive(); switch(i) { case 1:rom_rec[i]=rom_rec[i]&0x7f;break; case 2: case 3:rom_rec[i]=rom_rec[i]&0x3f;break; case 4:rom_rec[i]=rom_rec[i]&0x07;break; case 5:rom_rec[i]=rom_rec[i]&0x9f;break; default:break; } } I_stop(); } } } goto start; } /--------------------------------------------存儲(chǔ)器24c04存取函數(shù)------------------------------------------------/ void 24C04 WriteRead { uchar Number[5]={hourset,minset,countset,maxtemp1,mintemp1}; WP= 1; WrToROM(Number[],5,5); //將初始化后的數(shù)值寫入EEPROM mDelay(20); Number[0]=0; Number[1]=0; Number[2]=0; Number[3]=0; //將數(shù)組中的值清掉,以驗(yàn)證讀出的數(shù)是否正確 Number[4]=0; RdFromROM(Number[],5,5); void mDelay(uchar j) { uint i; for(;j>0;j--) { for(i=0;i<125;i--) {;} } } /*起始條件*/ void Start(void) { Sda=1; Scl=1; _nop_();_nop_();_nop_();_nop_(); Sda=0; _nop_();_nop_();_nop_();_nop_(); } /*停止條件*/ void Stop(void) { Sda=0; Scl=1; _nop_();_nop_();_nop_();_nop_(); Sda=1; _nop_();_nop_();_nop_();_nop_(); } /*應(yīng)答位*/ void Ack(void) { Sda=0; _nop_();_nop_();_nop_();_nop_(); Scl=1; _nop_();_nop_();_nop_();_nop_(); Scl=0; } /*反向應(yīng)答位*/ void NoAck(void) { Sda=1; _nop_();_nop_();_nop_();_nop_(); Scl=1; _nop_();_nop_();_nop_();_nop_(); Scl=0; } /*發(fā)送數(shù)據(jù)子程序,Data為要求發(fā)送的數(shù)據(jù)*/ void Send(uchar Data) { uchar BitCounter=8; //位數(shù)控制 uchar temp; //中間變量控制 do { temp=Data; Scl=0; _nop_();_nop_();_nop_();_nop_(); if((temp&0x80)==0x80) //如果最高位是1 Sda=1; else Sda=0; Scl=1; temp=Data<<1; //RLC Data=temp; BitCounter--; }while(BitCounter); Scl=0; } /*讀一個(gè)字節(jié)的數(shù)據(jù),并返回該字節(jié)值*/ uchar Read(void) { uchar temp=0; uchar temp1=0; uchar BitCounter=8; Sda=1; do{ Scl=0; _nop_();_nop_();_nop_();_nop_(); Scl=1; _nop_();_nop_();_nop_();_nop_(); if(Sda) //如果Sda=1 temp=temp|0x01; //temp的最低位置1 else temp=temp&0xfe; //否則temp的最低位清0 if(BitCounter-1) { temp1=temp<<1; temp=temp1; } BitCounter--; }while(BitCounter); return(temp); } void WrToROM(uchar Data[],uchar Address,uchar Num) { uchar i; uchar *PData; PData=Data; for(i=0;i<Num;i++) { Start(); //發(fā)送啟動(dòng)信號(hào) Send(0xa0); //發(fā)送SLA+W Ack(); Send(Address+i); //發(fā)送地址 Ack(); Send(*(PData+i)); Ack(); Stop(); mDelay(20); } } void RdFromROM(uchar Data[],uchar Address,uchar Num) { uchar i; uchar *PData; PData=Data; for(i=0;i<Num;i++) { Start(); Send(0xa0); Ack(); Send(Address+i); Ack(); Start(); Send(0xa1); Ack(); *(PData+i)=Read(); Scl=0; NoAck(); Stop(); } } } |