|
- #include <reg51.h>
- #include <intrins.h>
- sbit SDA=P2^0; // 將p1.2口模擬數(shù)據(jù)口
- sbit SCL=P2^1; // 將p1.7口模擬時鐘口
- sbit DS1302 =P2^4;
- sbit DU = P2^0;
- sbit WE = P2^1;
- #define uchar unsigned char
- #define uint unsigned int
- #define disdata P0 //顯示數(shù)據(jù)碼輸出口
- sbit LCD_RS = P1^0;
- sbit LCD_RW = P1^1;
- sbit LCD_EN = P2^5;
- uint data dis[4]={0x00,0x00,0x00,0x00};
- //定義3個顯示數(shù)據(jù)單元和1個數(shù)據(jù)存儲單元
- uchar code dis4[] = {"1- . V 2- . V"};
- uchar code dis5[] = {"3- . V 4- . V"};
- #define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
-
- bit bdata SystemError; //從機(jī)錯誤標(biāo)志位
- /*********************** PCF8591專用變量定義 ***********************/
- #define PCF8591_WRITE 0x90
- #define PCF8591_READ 0x91
- #define NUM 4 //接收和發(fā)送緩存區(qū)的深度
- uchar idata receivebuf[NUM]; //數(shù)據(jù)接收緩沖區(qū)
- void cmg88()//關(guān)數(shù)碼管,點陣函數(shù)
- {
- DU=1;
- P0=0X00;
- DU=0;
- }
- /*******************************************************************/
- /* */
- /* 延時函數(shù) */
- /* */
- /*******************************************************************/
- void delay(int ms)
- {
- while(ms--)
- {
- uchar i;
- for(i=0;i<250;i++)
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- }
- }
- /*******************************************************************/
- /* */
- /*檢查LCD忙狀態(tài) */
- /*lcd_busy為1時,忙,等待。lcd-busy為0時,閑,可寫指令與數(shù)據(jù)。 */
- /* */
- /*******************************************************************/
- bit lcd_busy()
- {
- bit result;
- LCD_RS = 0;
- LCD_RW = 1;
- LCD_EN = 1;
- delayNOP();
- result = (bit)(P0&0x80);
- LCD_EN = 0;
- return(result);
- }
- /*******************************************************************/
- /* */
- /*寫指令數(shù)據(jù)到LCD */
- /*RS=L,RW=L,E=高脈沖,D0-D7=指令碼。 */
- /* */
- /*******************************************************************/
- void lcd_wcmd(uchar cmd)
- {
- while(lcd_busy());
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_EN = 0;
- _nop_();
- _nop_();
- P0 = cmd;
- delayNOP();
- LCD_EN = 1;
- delayNOP();
- LCD_EN = 0;
- }
- /*******************************************************************/
- /* */
- /*寫顯示數(shù)據(jù)到LCD */
- /*RS=H,RW=L,E=高脈沖,D0-D7=數(shù)據(jù)。 */
- /* */
- /*******************************************************************/
- void lcd_wdat(uchar dat)
- {
- while(lcd_busy());
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_EN = 0;
- P0 = dat;
- delayNOP();
- LCD_EN = 1;
- delayNOP();
- LCD_EN = 0;
- }
- /*******************************************************************/
- /* */
- /* LCD初始化設(shè)定 */
- /* */
- /*******************************************************************/
- void lcd_init()
- {
- delay(15);
- lcd_wcmd(0x38); //16*2顯示,5*7點陣,8位數(shù)據(jù)
- delay(5);
- lcd_wcmd(0x38);
- delay(5);
- lcd_wcmd(0x38);
- delay(5);
- lcd_wcmd(0x0c); //顯示開,關(guān)光標(biāo)
- delay(5);
- lcd_wcmd(0x06); //移動光標(biāo)
- delay(5);
- lcd_wcmd(0x01); //清除LCD的顯示內(nèi)容
- delay(5);
- }
- /*******************************************************************/
- /* */
- /* 設(shè)定顯示位置 */
- /* */
- /*******************************************************************/
- void lcd_pos(uchar pos)
- {
- lcd_wcmd(pos | 0x80); //數(shù)據(jù)指針=80+地址變量
- }
- /******************************************************************/
- /* */
- /* 數(shù)據(jù)處理與顯示 */
- /* 將采集到的數(shù)據(jù)進(jìn)行16進(jìn)制轉(zhuǎn)換為ASCLL碼。 */
- /* */
- /******************************************************************/
- show_value(uchar ad_data)
- {
- dis[2]=ad_data/51; //AD值轉(zhuǎn)換為3為BCD碼,最大為5.00V。
- dis[2]=dis[2]+0x30; //轉(zhuǎn)換為ACSII碼
- dis[3]=ad_data%51; //余數(shù)暫存
- dis[3]=dis[3]*10; //計算小數(shù)第一位
- dis[1]=dis[3]/51;
- dis[1]=dis[1]+0x30; //轉(zhuǎn)換為ACSII碼
- dis[3]=dis[3]%51;
- dis[3]=dis[3]*10; //計算小數(shù)第二位
- dis[0]=dis[3]/51; //
- dis[0]=dis[0]+0x30; //轉(zhuǎn)換為ACSII碼
- }
- //-------------------------------------------------------------------
- // 函數(shù)名稱: iic_start()
- // 函數(shù)功能: 啟動I2C總線子程序
- //-------------------------------------------------------------------
- void iic_start(void)
- { //時鐘保持高,數(shù)據(jù)線從高到低一次跳變,I2C通信開始
- SDA = 1;
- SCL = 1;
- delayNOP(); // 延時5us
- SDA = 0;
- delayNOP();
- SCL = 0;
- }
- //-------------------------------------------------------------------
- // 函數(shù)名稱: iic_stop()
- // 函數(shù)功能: 停止I2C總線數(shù)據(jù)傳送子程序
- //-------------------------------------------------------------------
- void iic_stop(void)
- {
- SDA = 0; //時鐘保持高,數(shù)據(jù)線從低到高一次跳變,I2C通信停止
- SCL = 1;
- delayNOP();
- SDA = 1;
- delayNOP();
- SCL = 0;
- }
- //------------------------------------------------------------------
- // 函數(shù)名稱: iicInit_()
- // 函數(shù)功能: 初始化I2C總線子程序
- //------------------------------------------------------------------
- void iicInit(void)
- {
- SCL = 0;
- iic_stop();
- }
- //-------------------------------------------------------------------
- // 函數(shù)名稱: slave_ACK
- // 函數(shù)功能: 從機(jī)發(fā)送應(yīng)答位子程序
- //-------------------------------------------------------------------
- void slave_ACK(void)
- {
- SDA = 0;
- SCL = 1;
- delayNOP();
- SCL = 0;
- }
- //-------------------------------------------------------------------
- // 函數(shù)名稱: slave_NOACK
- // 函數(shù)功能: 從機(jī)發(fā)送非應(yīng)答位子程序,迫使數(shù)據(jù)傳輸過程結(jié)束
- //-------------------------------------------------------------------
- void slave_NOACK(void)
- {
- SDA = 1;
- SCL = 1;
- delayNOP();
- SDA = 0;
- SCL = 0;
- }
- //-------------------------------------------------------------------
- // 函數(shù)名稱: check_ACK
- // 函數(shù)功能: 主機(jī)應(yīng)答位檢查子程序,迫使數(shù)據(jù)傳輸過程結(jié)束
- //-------------------------------------------------------------------
- void check_ACK(void)
- {
- SDA = 1; // 將SDA設(shè)置成輸入,必須先向端口寫1
- SCL = 1;
- F0 = 0;
- delayNOP();
- if(SDA == 1) // 若SDA=1表明非應(yīng)答,置位非應(yīng)答標(biāo)志F0
- F0 = 1;
- SCL = 0;
- }
- //-------------------------------------------------------------------
- // 函數(shù)名稱: IICSendByte
- // 入口參數(shù): ch
- // 函數(shù)功能: 發(fā)送一個字節(jié)
- //-------------------------------------------------------------------
- void IICSendByte(uchar ch)
-
- {
- unsigned char idata n=8; // 向SDA上發(fā)送一位數(shù)據(jù)字節(jié),共八位
- while(n--)
- {
- if((ch&0x80) == 0x80) // 若要發(fā)送的數(shù)據(jù)最高位為1則發(fā)送位1
- {
- SDA = 1; // 傳送位1
- SCL = 1;
- delayNOP();
- // SDA = 0;
- SCL = 0;
- }
- else
- {
- SDA = 0; // 否則傳送位0
- SCL = 1;
- delayNOP();
- SCL = 0;
- }
- ch = ch<<1; // 數(shù)據(jù)左移一位
- }
- }
- //-------------------------------------------------------------------
- // 函數(shù)名稱: IICreceiveByte
- // 返回接收的數(shù)據(jù)
- // 函數(shù)功能: 接收一字節(jié)子程序
- //-------------------------------------------------------------------
- uchar IICreceiveByte(void)
- {
- uchar idata n=8; // 從SDA線上讀取一上數(shù)據(jù)字節(jié),共八位
- uchar tdata=0;
- while(n--)
- {
- SDA = 1;
- SCL = 1;
- tdata =tdata<<1; //左移一位
- if(SDA == 1)
- tdata = tdata|0x01; // 若接收到的位為1,則數(shù)據(jù)的最后一位置1
- else
- tdata = tdata&0xfe; // 否則數(shù)據(jù)的最后一位置0
- SCL = 0;
- }
- return(tdata);
- }
- //-------------------------------------------------------------------
- // 函數(shù)名稱: DAC_PCF8591
- // 入口參數(shù): slave_add從機(jī)地址,n要發(fā)送的數(shù)據(jù)個數(shù)
- // 函數(shù)功能: 發(fā)送n位數(shù)據(jù)子程序
- //-------------------------------------------------------------------
- void DAC_PCF8591(uchar controlbyte,uchar w_data)
- {
-
- iic_start(); // 啟動I2C
- delayNOP();
- IICSendByte(PCF8591_WRITE); // 發(fā)送地址位
- check_ACK(); // 檢查應(yīng)答位
- if(F0 == 1)
- {
- SystemError = 1;
- return; // 若非應(yīng)答,置錯誤標(biāo)志位
- }
- IICSendByte(controlbyte&0x77); //Control byte
- check_ACK(); //檢查應(yīng)答位
- if(F0 == 1)
- {
- SystemError = 1;
- return; // 若非應(yīng)答,置錯誤標(biāo)志位
- }
- IICSendByte(w_data); //data byte
- check_ACK(); // 檢查應(yīng)答位
- if(F0 == 1)
- {
- SystemError = 1;
- return; // 若非應(yīng)答表明器件錯誤或已壞,置錯誤標(biāo)志位SystemError
- }
- iic_stop(); // 全部發(fā)完則停止
- delayNOP();
- delayNOP();
- delayNOP();
- delayNOP();
- }
- //-------------------------------------------------------------------
- // 函數(shù)名稱: ADC_PCF8591
- // 入口參數(shù): controlbyte控制字
- // 函數(shù)功能: 連續(xù)讀入4路通道的A/D轉(zhuǎn)換結(jié)果到receivebuf
- //-------------------------------------------------------------------
- void ADC_PCF8591(uchar controlbyte)
- {
- uchar idata receive_da,i=0;
- iic_start();
- IICSendByte(PCF8591_WRITE); //控制字
- check_ACK();
- if(F0 == 1)
- {
- SystemError = 1;
- return;
- }
- IICSendByte(controlbyte); //控制字
- check_ACK();
- if(F0 == 1)
- {
- SystemError = 1;
- return;
- }
- iic_start(); //重新發(fā)送開始命令
- IICSendByte(PCF8591_READ); //控制字
- check_ACK();
- if(F0 == 1)
- {
- SystemError = 1;
- return;
- }
-
- IICreceiveByte(); //空讀一次,調(diào)整讀順序
- slave_ACK(); //收到一個字節(jié)后發(fā)送一個應(yīng)答位
- while(i<4)
- {
- receive_da=IICreceiveByte();
- receivebuf[i++]=receive_da;
- slave_ACK(); //收到一個字節(jié)后發(fā)送一個應(yīng)答位
- }
- slave_NOACK(); //收到最后一個字節(jié)后發(fā)送一個非應(yīng)答位
- iic_stop();
- }
- //-------------------------------------------------------------------
- // 函數(shù)名稱: main
- // 函數(shù)功能: 主程序
- //-------------------------------------------------------------------
- main()
- {
- uchar i,l;
- delay(10); //延時
- cmg88();//關(guān)數(shù)碼管,點陣函數(shù)
- DS1302 =0;
- lcd_init(); //初始化LCD
-
- lcd_pos(0); //設(shè)置顯示位置為第一行的第1個字符
- i = 0;
- while(dis4[i] != '\0')
- { //顯示字符
- lcd_wdat(dis4[i]);
- i++;
- }
- lcd_pos(0x40); //設(shè)置顯示位置為第二行第1個字符
- i = 0;
- while(dis5[i] != '\0')
- {
- lcd_wdat(dis5[i]); //顯示字符
- i++;
- }
-
- while(1)
- {
- iicInit(); //I2C總線初始化
- ADC_PCF8591(0x04);
- if(SystemError == 1) //有錯誤,重新來
- {
- iicInit(); //I2C總線初始化
- ADC_PCF8591(0x04);
- }
-
- for(l=0;l<4;l++)
- {
- show_value(receivebuf[0]); //顯示通道0
- lcd_pos(0x02);
- lcd_wdat(dis[2]); //整數(shù)位顯示
- lcd_pos(0x04);
- lcd_wdat(dis[1]); //第一位小數(shù)顯示
- lcd_pos(0x05);
- lcd_wdat(dis[0]); //第二位小數(shù)顯示
- show_value(receivebuf[1]); //顯示通道1
- lcd_pos(0x0b);
- lcd_wdat(dis[2]); //整數(shù)位顯示
- lcd_pos(0x0d);
- lcd_wdat(dis[1]); //第一位小數(shù)顯示
- lcd_pos(0x0e);
- lcd_wdat(dis[0]); //第二位小數(shù)顯示
- show_value(receivebuf[2]); //顯示通道2
- lcd_pos(0x42);
- lcd_wdat(dis[2]); //整數(shù)位顯示
- lcd_pos(0x44);
- lcd_wdat(dis[1]); //第一位小數(shù)顯示
- lcd_pos(0x45);
- lcd_wdat(dis[0]); //第二位小數(shù)顯示
- show_value(receivebuf[3]); //顯示通道3
- lcd_pos(0x4b);
- lcd_wdat(dis[2]); //整數(shù)位顯示
- lcd_pos(0x4d);
- lcd_wdat(dis[1]); //第一位小數(shù)顯示
- lcd_pos(0x4e);
- lcd_wdat(dis[0]); //第二位小數(shù)顯示
- iicInit(); //I2C總線初始化
- DAC_PCF8591(0x40,receivebuf[0]); //D/A輸出
- if(SystemError == 1) //有錯誤,重新來
- {
- iicInit(); //I2C總線初始化
- DAC_PCF8591(0x40,receivebuf[0]); //D/A輸出
- }
- //delay(50); //延時
- }
- }
- }
復(fù)制代碼 |
|