aip650做一個孵化器的溫控,溫度顯示搞定了,按鍵響應不知怎么搞。有會的嗎?請高手指點下。
單片機源程序如下:
- #include <absacc.h>
- #include <intrins.h>
- #include <STC89C5xRC.H>
- //定義常量
- #define true 1
- #define false 0
- #define uchar unsigned char
- #define uint unsigned int
- //定義變量
- uchar Display_Code[12] ={0xf5,0x05,0x73,0x57,0x87,0xd6,0xf6,0x45,0xf7,0xd7,0x08,0x02}; //段碼
- //0, 1, 2 , 3, 4, 5, 6, 7, 8, 9, . , -
- uchar Dig_Bit_Code[4]={0x6a,0x6e,0x6c,0x68};//位碼
- uchar Light_Level_Code[8]={0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x01 };//亮度調節
- uint h; //溫度使用
- uint temp;//溫度
- //**************溫度小數部分用查表法***********//
- uchar ditab[16]=
- {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
- uchar temp_data[2]={0x00,0x00}; //讀出溫度暫放
- uchar display[5]={0x00,0x00,0x00,0x00,0x00}; //顯示單元數據,共4個數據和一個運算暫用
-
- //定義IO口
- sbit SDA=P3^6;
- sbit SCL=P3^7;
- sbit DQ=P1^0; //溫度輸入口
- sbit DIN=P1^1; //LED小數點控制
- //函數聲名
- /*溫度使用*/
- void delay(uint t);
- void work_temp(uint tem);
- read_temp();
- void xiashiwendu(uint tem1);
- uchar read_byte(void);
- void write_byte(uchar val);
- void ow_reset(void);
- /*I2C相關*/
- void I2CWait(void);
- bit I2CStart(void);
- void I2CStop(void);
- void I2CSendAck(void);
- void I2CSendNoAck(void);
- bit I2CWaitAck(void);
- unsigned char AipReadKey();
- void I2CSendByte(unsigned char demand,bit order);
- unsigned char I2CReceiveByte(void);
- //void Delay(unsigned int nn);
- void AiP650_CLR();
- void AiP650_DisPlay(unsigned char Dig_Bit,unsigned char Display_num);
- void AiP650_Set(unsigned char add,unsigned char dat);//數碼管顯示
- void Delay(unsigned int nn);
- void Light_Level_Set(unsigned char level);
- void xianshijianzhi(uint key);
- /*******************************************************
- 功能:主函數
- 描述:初始化、讀溫度顯示,讀鍵值處理
- 參數:無
- 返回:無
- ********************************************************/
- main()
- {unsigned char keyvalue;
-
- P3=0xFF;
-
- while(1)
- {
- uint wendu;//溫度變量
- uint maxwd=40.0;//停止溫度
- uint minwd=30.0;//開始溫度
-
- //AiP650_CLR();
- wendu=read_temp();
- xiashiwendu(wendu);
-
- keyvalue=AipReadKey();
- //if(keyvalue==0x44)
- // xianshijianzhi(keyvalue);
- //if(keyvalue!=0x2e)
- //{ delay(2000);
- //if(keyvalue!=0x2e)
- switch(keyvalue)
- {case 0x47:
- Light_Level_Set(4);
- AiP650_DisPlay(1,1);
-
- case 0x57:
- Light_Level_Set(4);
- AiP650_DisPlay(1,2);
-
- case 0x5f:
- Light_Level_Set(4);
- AiP650_DisPlay(1,3);
-
- case 0x67:
- Light_Level_Set(4);
- AiP650_DisPlay(1,4);
-
- case 0x6f:
- Light_Level_Set(4);
- AiP650_DisPlay(1,5);
- case 0x6c:
- Light_Level_Set(4);
- AiP650_DisPlay(1,6);
-
- case 0x77:
- Light_Level_Set(4);
- AiP650_DisPlay(1,7);
- case 0x6e:
- Light_Level_Set(4);
- AiP650_DisPlay(1,8);
-
-
- break;
-
-
-
-
- ; }
- }
-
- /* unsigned char i;for(i=0;i<12;i++)
- {
- AiP650_DisPlay(1,i);
- AiP650_DisPlay(2,i);
- AiP650_DisPlay(3,i);
- Light_Level_Set(4);*/
- //此處添加顯示程序,可參考5、6、7章
- // delay(500);}
-
- }
- /*******************************************************
- 功能:I2CWait
- 描述:I2C延時
- 參數:無
- 返回:無
- ********************************************************/
- void I2CWait(void)
- {
- _nop_();_nop_();_nop_();_nop_();
- }
- /*******************************************************
- 功能:I2CStart
- 描述:開啟SD2000的I2C總線
- 參數:無
- 返回:位變量I2CStart,1=I2C總線可用,0=I2C總線不可用
- ********************************************************/
- bit I2CStart(void)
- {
- SDA=1;
- SCL=1;
- I2CWait();
- if(!SDA)return false;//SDA線為低電平則總線忙,退出
- SDA=0;
- I2CWait();
- while(SDA)return false;//SDA線為高電平則總線出錯,退出
- SCL=0;
- I2CWait();
- return true;
- }
- /*******************************************************
- 功能:I2CStop
- 描述:關閉SD2000的I2C總線
- 參數:無
- 返回:無
- ********************************************************/
- void I2CStop(void)
- {
- SDA=0;
- SCL=0;
- I2CWait();
- SCL=1;
- I2CWait();
- SDA=1;
- }
- /*******************************************************
- 功能:I2CSendAck
- 描述:發送 ACK
- 參數:無
- 返回:無
- ********************************************************/
- void I2CSendAck(void)
- {
- SDA=0;
- SCL=0;
- I2CWait();
- SCL=1;
- I2CWait();
- SCL=0;
- }
- /*******************************************************
- 功能:I2CSendNoAck
- 描述:發送NO ACK
- 參數:無
- 返回:無
- ********************************************************/
- void I2CSendNoAck(void)
- {
- SDA=1;
- SCL=0;
- I2CWait();
- SCL=1;
- I2CWait();
- SCL=0;
- }
- /*******************************************************
- 功能:I2CWaitAck
- 描述:讀取ACK信號
- 參數:無
- 返回:位變量I2CWaitAck,返回為:1=有ACK,0=無ACK
- ********************************************************/
- bit I2CWaitAck(void)
- {
- unsigned char errtime=255;
- SCL=0;
- SDA=1;
- I2CWait();
- SCL=1;
- I2CWait();
- while(SDA)
- {
- errtime--;
- if(!errtime)
- SCL=0;
- return false;
- }
- SCL=0;
- return true;
- }
- /*******************************************************
- 功能:I2CSendByte
- 描述:向SD2000發送一個字節
- 參數:待發送字節demand,發送順序指示order
- order=1,從HI-->LO發送8bit數據
- order=0,從LO-->HI發送8bit數據
- 返回:無
- ********************************************************/
- void I2CSendByte(unsigned char demand,bit order)
- {
- unsigned char i=8;
-
- if(order)
- {
- while(i--)
- {
- SCL=0;
- _nop_();
- SDA=(bit)(demand&0x80);
- demand<<=1;
- I2CWait();
- SCL=1;
- I2CWait();
- }
- SCL=0;
- }
- else
- {
- while(i--)
- {
- SCL=0;
- _nop_();
- SDA=(bit)(demand&0x01);
- demand>>=1;
- I2CWait();
- SCL=1;
- I2CWait();
- }
- SCL=0;
- }
- }
- /*******************************************************
- 功能:I2CReceiveByte
- 描述:從SD2000讀入一字節
- 參數:無
- 返回:讀取的字節I2CReceiveByte
- ********************************************************/
- unsigned char I2CReceiveByte(void)
- {
- unsigned char i=8;
- unsigned char ddata=0;
- SDA=1;
- while(i--)
- {
- ddata>>=1; //數據從低位開始讀取
- SCL=0;
- I2CWait();
- SCL=1;
- I2CWait(); //從低位開始 ddata|=SDA;ddata>>=1
- if(SDA)
- {
- ddata|=0x80;
- }
- }
- SCL=0;
- return ddata;
- }
- /*******************************************************
- 功能:讀鍵值
- 描述:
- 參數:
- 返回:鍵值
- ********************************************************/
- ///*
- unsigned char AipReadKey()
- {
- unsigned char key;
- AiP650_Set(0x4f,0x00);
- if(I2CWaitAck())
- {I2CStart();
- key=I2CReceiveByte();
-
- I2CStop();}
- return key;}//*/
- /*******************************************************
- 功能:發送地址,數據, 數碼管顯示
- 描述:
- 參數:
- 返回:
- ********************************************************/
- void AiP650_Set(unsigned char add,unsigned char dat) //數碼管顯示
- {
- //寫顯存必須從高地址開始寫
- I2CStart();
- I2CSendByte(add,1); //第一個顯存地址
- I2CSendAck();
- I2CSendByte(dat,1);
- I2CSendAck();
- I2CStop();
- }
- /*******************************************************
- 功能:驅動數碼管顯示數字
- 描述:顯示數字
- 參數:
- 返回:
- ********************************************************/
- void AiP650_DisPlay(unsigned char Dig_Bit,unsigned char Display_num) //顯示一位數字
- {
- AiP650_Set(Dig_Bit_Code[Dig_Bit-1],Display_Code[Display_num]);
- }
- /*******************************************************
- 功能:清屏
- 描述:
- 參數:
- 返回:
- ********************************************************/
- void AiP650_CLR() //清屏
- {
- unsigned char i;
- for(i=0;i<4;i++)
- {
- AiP650_Set(Dig_Bit_Code[i],0x00); [/i][i]
- }
- }
- /*******************************************************
- 功能:設置顯示亮度
- 描述:
- 參數:
- 返回:
- ********************************************************/
- void Light_Level_Set(unsigned char level)
- {AiP650_Set(0x48,Light_Level_Code[level-1]);}
- /*******************************************************
- 功能:Delay
- 描述:延時子程序
- 參數:延時長度nn
- 返回:無
- ********************************************************/
- /*void Delay(unsigned int nn)
- {
- while(nn--);
- }
- */
- //
- /*****************11us延時函數*************************/
- //
- void delay(uint t)
- {
- for (;t>0;t--);
- }
- //
- //
- /****************DS18B20復位函數************************/
- void ow_reset(void)
- {
- char presence=1;
- while(presence)
- {
- while(presence)
- {
- DQ=1;_nop_();_nop_();//從高拉倒低
- DQ=0;
- delay(50); //550 us
- DQ=1;
- delay(6); //66 us
- presence=DQ; //presence=0 復位成功,繼續下一步
- }
- delay(45); //延時500 us
- presence=~DQ;
- }
- DQ=1; //拉高電平
- }
- //
- //
- /****************DS18B20寫命令函數************************/
- //向1-WIRE 總線上寫1個字節
- void write_byte(uchar val)
- {
- uchar i;
- for(i=8;i>0;i--)
- {
- DQ=1;_nop_();_nop_(); //從高拉倒低
- DQ=0;_nop_();_nop_();_nop_();_nop_(); //5 us
- DQ=val&0x01; //最低位移出
- delay(6); //66 us
- val=val/2; //右移1位
- }
- DQ=1;
- delay(1);
- }
- //
- /****************DS18B20讀1字節函數************************/
- //從總線上取1個字節
- uchar read_byte(void)
- {
- uchar i;
- uchar value=0;
- for(i=8;i>0;i--)
- {
- DQ=1;_nop_();_nop_();
- value>>=1;
- DQ=0;_nop_();_nop_();_nop_();_nop_(); //4 us
- DQ=1;_nop_();_nop_();_nop_();_nop_(); //4 us
- if(DQ)value|=0x80;
- delay(6); //66 us
- }
- DQ=1;
- return(value);
- }
- //
- /****************讀出溫度函數************************/
- //
- read_temp()
- {
- ow_reset(); //總線復位
- delay(200);
- write_byte(0xcc); //發命令
- write_byte(0x44); //發轉換命令
- ow_reset();
- delay(1);
- write_byte(0xcc); //發命令
- write_byte(0xbe);
- temp_data[0]=read_byte(); //讀溫度值的第字節
- temp_data[1]=read_byte(); //讀溫度值的高字節
- temp=temp_data[1];
- temp<<=8;
- temp=temp|temp_data[0]; // 兩字節合成一個整型變量。
- return temp; //返回溫度值
- }
- //
- /****************溫度數據處理函數************************/
- //二進制高字節的低半字節和低字節的高半字節組成一字節,這個
- //字節的二進制轉換為十進制后,就是溫度值的百、十、個位值,而剩
- //下的低字節的低半字節轉化成十進制后,就是溫度值的小數部分
- /********************************************************/
- void work_temp(uint tem)
- {
- uchar n=0;
- if(tem>6348) // 溫度值正負判斷
- {tem=65536-tem;n=1;} // 負溫度求補碼,標志位置1
- display[4]=tem&0x0f; // 取小數部分的值
- display[0]=ditab[display[4]]; // 存入小數部分顯示值
- display[4]=tem>>4; // 取中間八位,即整數部分的值
- display[3]=display[4]/100; // 取百位數據暫存
- display[1]=display[4]%100; // 取后兩位數據暫存
- display[2]=display[1]/10; // 取十位數據暫存
- display[1]=display[1]%10;
- /******************符號位顯示判斷**************************/
- if(!display[3])
- {
- display[3]=0x0a; //最高位為0時不顯示
- if(!display[2])
- {
- display[2]=0x0a; //次高位為0時不顯示
- }
- }
- if(n){display[3]=0x0b;} //負溫度時最高位顯示"-"
- }
- //
- //
- /*************************************************************
- 顯示溫度
- 功能:溫度數值處理,發送650顯示
- ***********************************************************/
- void xiashiwendu(uint tem1)
- {
- work_temp(tem1);
- AiP650_CLR();
- Light_Level_Set(4);
- AiP650_DisPlay(2,10);//顯示小數點
- AiP650_DisPlay(3,display[2]);//百位
- AiP650_DisPlay(2,display[1]);//顯示個位
- AiP650_DisPlay(1,display[0]);//小數
- }
- /******************顯示鍵值*************************************
- 功能:將鍵值發送650顯示
- ****************************************************************/
- void xianshijianzhi(uint key)
- {key=key&0x0a;
-
- Light_Level_Set(4);
-
- AiP650_DisPlay(2,key);
- }
復制代碼 |