包含程序及仿真,pcf8951驱动程序,不仅可以检测电压,还能检测光照强度以及温度,很适合小白
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
-
- #include <reg52.h>
- #include <I2C.H>
- #define uchar unsigned char
- #define uint unsigned int
- #define PCF8591 0x90 //PCF8591 地址
- unsigned char code tab[]={0xfe,0xfd,0xfb,0xf7,
- 0xef,0xdf,0xbf,0x7f};
- unsigned char code graph[11][8]={
- {0x0,0x18,0x24,0x24,0x24,0x24,0x24,0x18},//0
- {0x0,0x8,0x18,0x8,0x8,0x8,0x8,0x8},//1
- {0x0,0x18,0x24,0x24,0x8,0x10,0x20,0x3C},//2
- {0x0,0x18,0x24,0x4,0x8,0x24,0x24,0x18},//3
- {0x0,0x8,0x8,0x18,0x28,0x3C,0x8,0x8},//4
- {0x0,0x3C,0x20,0x38,0x4,0x4,0x24,0x18},//5
- {0x0,0x18,0x24,0x20,0x38,0x24,0x24,0x18},//6
- {0x0,0x3C,0x24,0x4,0x8,0x10,0x10,0x10},//7
- {0x0,0x18,0x24,0x24,0x18,0x24,0x24,0x18},//8
- {0x0,0x18,0x24,0x24,0x1C,0x4,0x24,0x18},//9
- {0x4C,0xD2,0x52,0x52,0x52,0x52,0x52,0xEC},//10
- };
- unsigned char count;
- unsigned char cnta;
- unsigned char AD_CHANNEL;
- unsigned int D[4];
- void delay11(unsigned int cnt)
- {
- while(--cnt);
- }
-
- /*******************************************************************
- ADC发送字节[命令]数据函数
- *******************************************************************/
- bit ISendByte(unsigned char sla,unsigned char c)
- {
- Start_I2c(); //启动总线
- SendByte(sla); //发送器件地址
- if(ack==0)return(0);
- SendByte(c); //发送数据
- if(ack==0)return(0);
- Stop_I2c(); //结束总线
- return(1);
- }
- /*******************************************************************
- ADC读字节数据函数
- *******************************************************************/
- unsigned char IRcvByte(unsigned char sla)
- { unsigned char c;
- Start_I2c(); //启动总线
- SendByte(sla+1); //发送器件地址
- if(ack==0)return(0);
- c=RcvByte(); //读取数据0
- Ack_I2c(1); //发送非就答位
- Stop_I2c(); //结束总线
- return(c);
- }
- //******************************************************************/
- main()
- { unsigned char jj,kk,ll,bb,cc,dd;
- double Data; //
- double V; //转化成的电压值
- while(1)
- {
- ISendByte(PCF8591,0x40);
- Data=IRcvByte(PCF8591); //ADC0 模数转换1
- V=Data*5/256;
- D[0]=V*100;
- for(bb=0;bb<10;bb++){
- for(jj=0;jj<8;jj++) //共要扫描八行
- {
- P1=graph[D[0]/100][jj]; //送字模数据
- P2=tab[jj];
- delay11(200); //选中第一行
- P2=0xff;
- delay11(500);
- }
- delay11(500);}
- for(bb=0;bb<10;bb++){
- for(jj=0;jj<8;jj++) //共要扫描八行
- {
- P1=0x06;
- P2=0x7f;
- delay11(500);
- P2=0xbf;
- delay11(500);
- }
- delay11(500);}
- for(cc=0;cc<10;cc++){
- for(kk=0;kk<8;kk++) //共要扫描八行
- {
- P1=graph[D[0]%100/10][kk]; //送字模数据
- P2=tab[kk];
- delay11(200); //选中第一行
- P2=0xff;
- delay11(500);
- }
- delay11(500);}
- for(dd=0;dd<10;dd++)
- {
- for(ll=0;ll<8;ll++) //共要扫描八行
- {
- P1=graph[D[0]%10][ll]; //送字模数据
- P2=tab[ll];
- delay11(200); //选中第一行
- P2=0xff;
- delay11(500);
- }
- delay11(500);}
-
- }
- }
复制代码 注意现在用的8*8一次只能显示一个字符 所以需要翻页显示 不太直观 大家可以改为数码管或者 多点阵显示
Keil代码与Proteus8.13仿真下载:
仿真程序.7z
(79.42 KB, 下载次数: 23)
|