跟這爛芯片杠上了,重新把代碼手工輸了一遍。但還是不行。請各位高人指點迷津。
- /**********************************手工輸入********
- 名稱:aip650的驅動
- 時間:2022-11-13
- 版本:
- **************************************************/
- //頭文件
- #include <STC89X52RC.H>
- #include <absacc.h>
- #include <intrins.h>
- /*************************************************
- 常量、變量定義區
- ***************************************************/
- //常量定義
- #define true 1
- #define false 0
- #define uchar unsigned char
- #define uint unsigned int
- //數組定義
- uchar Display_Code[13]={0xf5,0x05,0x73,0x57,0x87,0xd6,0xf6,0x45,0xf7,0xd7,0x08,0x02,0x00};
- //共陰數碼管段碼 0, 1, 2, 3, 4 , 5, 6, 7, 8, 9, ., -, 不亮
- uchar Display_16Code[16]={0xf5,0x05,0x73,0x57,0x87,0xd6,0xf6,0x45,0xf7,0xd7,0xe7,0xb6,0xf0,0x37,0xf2,0xe2};
- //16進 制段碼(用于顯示鍵值)0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f
- uchar Dig_Bit_Code[4]={0x6a,0x6e,0x6c,0x68};//650位碼
- uchar Light_Level_Code[8]={0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x01};//亮度調節
- uchar keyvalue;
- uint wendu;//用于存儲溫度值
- 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^7;
- sbit SCL=P3^6;
- sbit DQ=P1^0; //ds18b20溫度輸入口
- sbit DIN=P1^1; //LED小數點控制
- /**********************************函數定義區************************************/
- //I2C相關
- /*******************************************************************************
- 功能:I2CWait
- 描述:I2C延時
- 參數:
- 返回:
- *******************************************************************************/
- void I2CWait(void)
- {_nop_();_nop_();_nop_();_nop_();
- }
- /*******************************************************************************
- 功能:I2CStart
- 描述:開啟I2C總線
- 參數:
- 返回:位變量,1=I2C總線可用,0=不可用
- *******************************************************************************/
- 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(void)
- 描述:關閉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信號
- 參數:
- 返回:位變量, 1=有ACK,0=無ACK
- *******************************************************************************/
- bit I2CWaitAck(void)
- {
- uchar errtime=255;
- SCL=0;
- SDA=1;
- I2CWait();
- SCL=1;
- I2CWait();
- while(SDA)
- {
- errtime--;
- if(!errtime)
- SCL=0;
- return false;
- }
- SCL=0;
- return true;
- }
- /*******************************************************************************
- 功能:I2CSendByte
- 描述:向總線發送一個字節
- 參數:待發送字節demand,發送順序指示order
- order=1,從HI-->LO發送8bit數
- order=0,從LO-->HI發送8bit數
- 返回:
- *******************************************************************************/
- void I2CSendByte(uchar demand,bit order)
- {
- uchar 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
- 描述:從總線讀一個字節
- 參數:
- 返回:
- *******************************************************************************/
- uchar I2CReceiveByte(void)
- {
- uchar i=8;
- uchar 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;
- }
- /*******************************************************************************
- 功能:從650讀鍵值
- 描述:
- 參數:
- 返回:uchar鍵值
- *******************************************************************************/
- uchar AipReadKey()
- {
- uchar key;
- I2CStart();
- I2CSendByte(0x4f,1);
- if(I2CWaitAck())
- {
- key=I2CReceiveByte();
-
- I2CSendAck();
- }
- I2CStop();
- return key;
- }
- /*******************************************************************************
- 功能:向650發送地址,和數據
- 描述:
- 參數:
- 返回:
- *******************************************************************************/
- void Aip650_Set(uchar add,uchar dat)
- {
- //寫顯存必須從高地址開始寫
- I2CStart();
- I2CSendByte(add,1);
- I2CSendAck();
- I2CSendByte(dat,1);
- I2CSendAck();
- I2CStop();
- }
- /*******************************************************************************
- 功能:驅動數碼管顯示數字
- 描述:位碼數組中選擇對應的位地址,在段碼數組中選擇對應數字的段碼發送給650
- 參數:
- 返回:
- *******************************************************************************/
- void Aip650_DisPlay(uchar Dig_Bit,uchar Display_num)
- {
- Aip650_Set(Dig_Bit_Code[Dig_Bit-1],Display_Code[Display_num]);
- }
- /*******************************************************************************
- 功能:驅動數碼管顯示16進制數的代碼
- 描述:
- 參數:
- 返回:
- *******************************************************************************/
- void Aip650_DisPlay16(uchar Dig_Bit16,uchar Display_num16)
- {
- Aip650_Set(Dig_Bit_Code[Dig_Bit16-1],Display_16Code[Display_num16]);
- }
- /*******************************************************************************
- 功能:650清屏
- 描述:
- 參數:
- 返回:
- *******************************************************************************/
- void Aip650_CLR()
- {
- uchar i;
- for(i=0;i<4;i++)
- {
- Aip650_Set(Dig_Bit_Code[i],0x00);
- }
- }
- /*******************************************************************************
- 功能:設置顯示亮度
- 描述:
- 參數:
- 返回:
- *******************************************************************************/
- void Light_Level_Set(uchar level)
- {
- Aip650_Set(0x48,Light_Level_Code[level-1]);
- }
- /*******************************************************************************
- 功能:delay 延時程序 11us
- 描述:
- 參數:
- 返回:
- *******************************************************************************/
- void delay(uint t)
- {
- for(;t>0;t--);
- }
- /*******************************************************************************
- 功能:Ds18b20 復位函數
- 描述:
- 參數:
- 返回:
- *******************************************************************************/
- void ow_reset(void)
- {
- uchar presence=1;
- while(presence)
- {while(presence)
- {
- DQ=1;
- _nop_();_nop_();//從高拉到低
- DQ=0;
- delay(50);//550us
- DQ=1;
- delay(6);//66us
- presence=DQ;//presence=0復位成功,繼續下一步
- }
- delay(45);//延時500us
- presence=~DQ;
- }
- DQ=1; //拉高電平
- }
- /*******************************************************************************
- 功能:Ds18b20寫命令函數
- 描述:向總線上寫1個字節
- 參數:
- 返回:
- *******************************************************************************/
- void write_byte(uchar val)
- {
- uchar i;
- for(i=8;i>0;i--)
- {DQ=1;_nop_();_nop_();//從高拉低
- DQ=0; _nop_();_nop_();_nop_();_nop_();//5us
- DQ=val&0x01;// 最低位移出
- delay(6);//66us
- val=val/2;//右移一位}
- }
- DQ=1;
- delay(1);
- }
- /*******************************************************************************
- 功能:從18b20總線上讀1字節
- 描述:
- 參數:
- 返回:value
- *******************************************************************************/
- 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_();//4us
- DQ=1;_nop_();_nop_();_nop_();_nop_();//4us
- if(DQ)value|=0x80;
- delay(6);
- }
-
- 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]=0x0c; //最高位為0時不顯示
- if(!display[2])
- {
- display[2]=0x0c; //次高位為0時不顯示
- }
- if(n)
- {display[3]=0x0b;} //負溫度時最高位顯示“-”
- }
- }
- /*******************************************************************************
- 功能:顯示溫度
- 描述:處理溫度數值,發送650
- 參數:
- 返回:
- *******************************************************************************/
- void xianshiwendu(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]);//小數
- }
- /*******************************************************************************
- 功能:顯示鍵值
- 描述:將按鍵的16進制代碼送顯示
- 參數:
- 返回:
- *******************************************************************************/
- void xianshijianzhi(uchar key1)
- {uchar a,b;
- a=key1&0x0f;
- Aip650_ClR();
- Light_Level_Set(4);
- Aip650_DisPlay16(1,a);
- b=key1>>4;
- b=b&0x0f;
- Aip650_DisPlay(2,b);
- }
- /******************************主函數*****************************************
- 功能:
- 描述:
- 參數:
- 返回:
- *******************************************************************************/
- void main()
- {
- P3=0xff;
- P1=0xff;
-
-
- while(1)
- {Aip650_CLR();
- Light_Level_Set(4);
- Aip650_DisPlay(1,8);
- Aip650_DisPlay(2,8);
- Aip650_DisPlay(3,8);
- //keyvalue=AipReadKey();
- P16=~P16;
- P17=~P17;
- delay(150);
- //wendu=read_temp();
- //xianshiwendu(wendu);
- //xianshijianzhi(keyvalue);
- //if(wendu>29)
- //P13=~P13;
- }
- }
復制代碼 |