單片機源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include<stdio.h>
- #include<stdlib.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define CHECK_BUSY
- #define ADD_ADS112C04 0x80
- #define START 0x08 //上電
- #define PowerDown 0x02 //掉電
- #define Rdata 0x10
- #define RESET 0x06
- #define WREG_0 0x40
- #define WREG_1 0x44
- #define WREG_2 0x48
- #define WREG_3 0x4c
- #define RREG_0 0x20
- #define RREG_1 0x24
- #define RREG_2 0x28
- #define RREG_3 0x2c
- sbit POWER=P1^6;
- sbit key1_S2=P3^0;
- sbit key2_S3=P3^1;
- sbit key3_S4=P3^2;
- sbit key4_S5=P3^3;
- sbit SDA = P1^1 ;
- sbit SCL = P1^0 ;
- sbit DRDY = P1^2;
- sbit RS = P3^5;
- sbit RW = P3^6;
- sbit EN = P3^4;
- sbit wela=P2^7;
- sbit dula=P2^6;
- int returnn;
- float typic;
- uchar disp[4]={0};
- uchar dis[3]={0};
- uchar num;
- long qwe;
- long result;
- float math;
- double c;
- double d;
- double R;
- uchar disp[];
- /*I2C協議*/
- void delay_5us()
- {
- _nop_();
- }
- void I2C_Init()
- {
- SCL = 1;
- delay_5us();
- SDA = 1;
- delay_5us();
- }
- /*I2C起始信號*/
- void I2C_start()
- {
- SDA = 1;
- delay_5us();
- SCL = 1;
- delay_5us();
- SDA = 0;
- delay_5us();
- }
- /*I2C終止信號*/
- void I2C_Stop()
- {
- SDA = 0;
- delay_5us();
- SCL = 1;
- delay_5us();
- SDA = 1;
- delay_5us();
- }
- /*主機發送應答*/
- void Master_ACK(uchar ack)
- {
- SDA=ack;
- delay_5us();
- SCL=1;
- delay_5us();
- SDA=ack;
- delay_5us();
- SCL=0;
- }
- /*檢測從機應答*/
- void Test_ACK()
- {
- uchar i;
- SCL=1;
- delay_5us();
- while((SDA==1)&&(i<250))i++;
- SCL=0;
- delay_5us();
- }
- /*發送一個字節*/
- void I2C_send_byte(uchar byte)
- {
- unsigned char i,temp;
- temp=byte;
- for (i=0;i<8;i++)
- {
- temp=temp<<1;
- SCL =0;
- delay_5us();
- SDA =CY; //temp左移時,移出的值放入了CY中
- delay_5us();
- SCL = 1; //待sda線上的數據穩定后,將scl拉高
- delay_5us();
- }
- SCL =0;
- delay_5us();
- SDA =1;
- delay_5us();
- }
- /*I2C 讀一字節*/
- uchar I2C_read_byte()
- {
- unsigned char i,k=0;
- SCL =0;
- delay_5us();
- SDA =1;
- for (i=0;i<8;i++)
- {
- delay_5us();
- SCL =1;
- delay_5us();
- k=(k<<1)|SDA;
- SCL=0;
- delay_5us();
- }
- delay_5us();
- return k;
- }
- /*adc*/
- /* power-up delay*/
- void delay_800us()
- {
- uint i;
- for(i=92; i > 0 ; i--);
- }
- void Reset()
- {
- I2C_start();
- I2C_send_byte(ADD_ADS112C04);
- Test_ACK();
- I2C_send_byte(RESET);
- delay_5us( );
- Test_ACK();
- I2C_Stop();
- }
- int read_volt() //讀取數據:兩個字節
- {
- short int k;
- uchar i,j;
- I2C_start();
- I2C_send_byte(0x80);
- Test_ACK();
- I2C_send_byte(0x10);
- Test_ACK();
- I2C_start();
- I2C_send_byte(0x81);
- Test_ACK();
- i=I2C_read_byte();
- Master_ACK(0);
- j=I2C_read_byte();
- Master_ACK(1);
- I2C_Stop();
- k=(i<<8)|j;
- return k;
- }
- void DelayMs(int z)
- {
- int x,y;
- for (x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /***********LED判忙函數***********/
- bit LCD_Check_Busy()
- {
- #ifdef CHECK_BUSY
- P0= 0xFF;
- RS=0;
- RW=1;
- EN=0;
- _nop_();
- EN=1;
- return (bit)(P0 & 0x80);
- #else
- return 0;
- #endif
- }
- /***********LED寫入命令函數***********/
- void write_com(uchar com)
- {
- while(LCD_Check_Busy()); //忙則等待
- RS=0;
- RW=0;
- EN=1;
- P0= com;
- _nop_();
- EN=0;
- }
- /**********LED寫入數據函數**********/
- void write_dat(uchar dat)
- {
- while(LCD_Check_Busy()); //忙則等待
- RS=1;
- RW=0;
- EN=1;
- P0= dat;
- _nop_();
- EN=0;
- }
- /*******LED寫入字符函數***********/
- void LCD_Write_Char(uchar x,uchar y,uchar dat)
- {
- if (y == 0)
- {
- write_com(0x80 + x);
- }
- else
- {
- write_com(0xC0 + x);
- }
- write_dat( dat);
- }
- /******寫入字符串函數***********/
- void Write_String(uchar x,uchar y,uchar *s)
- {
-
- while (*s)
- {
- LCD_Write_Char(x,y,*s);
- s++;
- x++;
- }
- }
- /*****LED初始化函數******/
- void LCD_Init()
- {
- RW=0;
- dula=0; //關閉數碼管顯示
- wela=0; //關閉數碼管顯示
- write_com(0x38); /*顯示模式設置*/
- DelayMs(5);
- write_com(0x06);/*顯示光標移動設置*/
- DelayMs(5);
- write_com(0x0C); /*顯示開及光標設置*/
- write_com(0x01); /*顯示清屏*/
- }
- void write_add(uchar add,uchar date) //向指定寄存器發送指定數據
- {
- I2C_start();
- I2C_send_byte(0x80);
- Test_ACK();
- I2C_send_byte(add);
- Test_ACK();
- I2C_send_byte(date);
- Test_ACK();
- I2C_Stop();
- }
- void startAD() //發送ADC開始轉換信號
- {
- DRDY = 1;
- delay_5us();
- DRDY = 0;
- I2C_start();
- I2C_send_byte(0x80);
- Test_ACK();
- I2C_send_byte(0x08);
- Test_ACK();
- I2C_Stop();
- DRDY = 1;
- }
-
- /***200MV電壓測量***/
- void voltagemin()
- {
- while(1)
- {c =read_volt();
- POWER=0;
- result =(c*20480)/32767; //transfer
- //if(result == 0)
- //{
- // qwe = 0;
- //} else
- //{
- qwe= result*10 +140;
- //}
- disp[0]=qwe/10000;
- disp[1]=qwe%10000/1000;
- disp[2]=qwe%10000%1000/100;
- disp[3]=qwe%10000%1000%100/10;
- disp[4]=qwe%10000%1000%100%10;
- write_com(0x01);
- if(disp[0]>=2)
- {Write_String(0,0,"Voltage.MEASURE");
- Write_String(0,1,"Beyond the range!"); }
- if(disp[0]<2)
- {Write_String(0,0,"Voltage.MEASURE");
- Write_String(0,1,"value=");
- LCD_Write_Char(9,1,'.');
- if(disp[4]==0)
- disp[4]=0;
- if(disp[4]>=1&&disp[4]<=5)
- disp[4]=3;
- if(disp[4]>=6&&disp[4]<=9)
- disp[4]=7;
- LCD_Write_Char(14,1,'M');
- LCD_Write_Char(15,1,'V');
- LCD_Write_Char(6,1,disp[0]+48);
- LCD_Write_Char(7,1,disp[1]+48);
- LCD_Write_Char(8,1,disp[2]+48);
- LCD_Write_Char(10,1,disp[3]+48);
- LCD_Write_Char(11,1,disp[4]+48);
- DelayMs(5);
- }
- if((key2_S3==0)||(key3_S4==0)||(key4_S5==0))
- break;
- }
- }
- /***2歐姆電阻測量***/
- void resmin ()
- {
- while(1)
- {
- int m;
- POWER=1;
- c=read_volt();
- math =(c*2.048)/32767;//transfer
- R=(math*11.6)/(3.26-math);
- if(R<=0)
- {
- qwe = 0;
- } else
- {
- m=(R-8.7)*1000;
- qwe=m;
- }
- dis[0]=qwe/1000;
- dis[1]=qwe%1000/100;
- dis[2]=qwe%1000%100/10;
- dis[3]=qwe%1000%100%10;
- write_com(0x01);
- if(dis[0]>=2)
- {Write_String(0,0,"RES.MEASURE");
- Write_String(0,1,"Beyond the range!"); }
- if(dis[0]<2)
- {Write_String(0,0,"RES.MEASURE");
- Write_String(0,1,"value=");
- LCD_Write_Char(7,1,'.');
- LCD_Write_Char(15,1,0xf4);
- LCD_Write_Char(6,1,dis[0]+48);
- LCD_Write_Char(8,1,dis[1]+48);
- LCD_Write_Char(9,1,dis[2]+48);
- LCD_Write_Char(10,1,dis[3]+48);
- DelayMs(5);
- }
- if((key2_S3==0)||(key1_S2==0)||(key4_S5==0))
- break;
- }
- }
- /***200k歐姆電阻測量***/
- void resmax ()
- {
- while(1)
- {
- int m;
- POWER=0;
- c=read_volt();
- math =(c*2.048)/32767;//transfer
- R=(math*2964.4)/(3.25-math);
- if(R == 0)
- {
- qwe = 0 ;
- } else
- {
- m=R-0.10078;
- qwe=R;
- }
- dis[0]=qwe/1000;
- dis[1]=qwe%1000/100;
- dis[2]=qwe%1000%100/10;
- dis[3]=qwe%1000%100%10;
- write_com(0x01);
- if(dis[0]>=2)
- {Write_String(0,0,"RES.MEASURE");
- Write_String(0,1,"Beyond the range!"); }
- if(dis[0]<2)
- {Write_String(0,0,"RES.MEASURE");
- Write_String(0,1,"value=");
- LCD_Write_Char(9,1,'.');
- if(disp[3]==0)
- {disp[3]=0;}
- if((disp[3]>=1)&&(disp[3]<=5))
- {disp[3]=3; }
- if((disp[3]>=6)&&(disp[3]<=9))
- {disp[3]=7;}
- LCD_Write_Char(14,1,'k');
- LCD_Write_Char(15,1,0xf4);
- LCD_Write_Char(6,1,dis[0]+48);
- LCD_Write_Char(7,1,dis[1]+48);
- LCD_Write_Char(8,1,dis[2]+48);
- LCD_Write_Char(10,1,dis[3]+48);
- DelayMs(5);
- }
- if((key2_S3==0)||(key1_S2==0)||(key3_S4==0))
- break;
- }
- }
-
- /***2V電壓測量***/
- void voltagemax ()
- {while(1)
- {
- c =read_volt();
- POWER = 0;
- result =(c*20480)/32767; //transfer
- if(result == 0)
- {
- qwe = 0;
- }
- if(result < 10000 && result > 0)
- {
- qwe=result+18;
- }
- if(result >= 10000)
- {
- qwe=result+20;
- }
- disp[0]=qwe/10000;
- disp[1]=qwe%10000/1000;
- disp[2]=qwe%10000%1000/100;
- disp[3]=qwe%10000%1000%100/10;
- disp[4]=qwe%10000%1000%100%10;
- write_com(0x01);
- if(disp[0]>=2)
- {Write_String(0,0,"Voltage.MEASURE");
- Write_String(0,1,"Beyond the range!"); }
- if(disp[0]<2)
- {Write_String(0,0,"Voltage.MEASURE");
- Write_String(0,1,"value=");
- LCD_Write_Char(7,1,'.');
- if(disp[4]==0)
- disp[4]=0;
- if(disp[4]>=1&&disp[4]<=5)
- disp[4]=3;
- if(disp[4]>=6&&disp[4]<=9)
- disp[4]=7;
-
- LCD_Write_Char(15,1,'V');
- LCD_Write_Char(6,1,disp[0]+48);
- LCD_Write_Char(8,1,disp[1]+48);
- LCD_Write_Char(9,1,disp[2]+48);
- LCD_Write_Char(10,1,disp[3]+48);
- LCD_Write_Char(11,1,disp[4]+48);
- DelayMs(5);
- }
- if((key1_S2==0)||(key3_S4==0)||(key4_S5==0))
- break;
- }
- }
- void keyscan()
- {
-
- if(key1_S2==0)
- {
- DelayMs(10);
- if(key1_S2==0)
- {
- voltagemin();
- while(!key1_S2);
- }
- }
- if(key2_S3==0)
- {
- DelayMs(10);
- if(key2_S3==0)
- {
- voltagemax();
- while(!key2_S3);
- }
- }
- if(key3_S4==0)
- {
- DelayMs(10);
- if(key3_S4==0)
- { resmin();
- while(!key3_S4);
- }
- }
- if(key4_S5==0)
- {
- DelayMs(10);
- if(key4_S5==0)
- { resmax();
- while(!key4_S5);
- }
- }
- }
- /*主函數*/
- void main()
- {
- delay_800us();
- I2C_Init(); //總線初始化
- Reset(); //重置ADC
- write_add(0x44,0xc8); //向01h寄存器發送數據0xc8
- I2C_start();
- I2C_send_byte(0x80);
- Test_ACK();
- I2C_send_byte(0x24);
- Test_ACK();
- I2C_start();
- I2C_send_byte(0x81);
- Test_ACK();
- Master_ACK(1);
- I2C_Stop();
- startAD(); //發送ADC開始轉換信號
- LCD_Init();
- DelayMs(10);
- while(1)
- {
- keyscan();
- }
- }
復制代碼
所有資料51hei提供下載:
ADCtext.rar
(44.7 KB, 下載次數: 36)
2019-4-14 09:55 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|