|
我這個是采用pcf8591實現AD采樣功能的電壓表,但是不知道是電路圖哪里出了問題,在開發板上可以實現,卻仿真不出來,拜托大家幫看看
- #include"reg52.h"
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit SCL=P1^1; //IIC接口
- sbit SDA=P1^2;
- ucharcodeseg[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xff,0x40};
- uchar dis[8];
- void delayms(uchar ms)
- {
- uchar i;
- while(ms--)
- for(i=0;i<123;i++);
- }
- void Start(void)
- {
- SDA=1;
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SDA=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=0;
- }
- void Stop(void)
- {
- SDA=0;
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SDA=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=0;
- }
- void Ack(void)
- {
- SDA=0;
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=0;
- SDA=1;
- }
- void NoAck(void)
- {
- SDA=1;
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=0;
- SDA=0;
- }
- void SendByte(uchar dat)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- SDA=(bit)(dat&0x80);
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=0;
- dat<<=1;
- }
- }
- uchar RcvByte(void)
- {
- uchar i,dat=0;
- SDA=1;
- for(i=0;i<8;i++)
- {
- SCL=1;
- dat<<=1;
- if(SDA==1)dat|=0x01;
- SCL=0;
- }
- return dat;
- }
- uchar PCF8591_ADC(uchar ch)
- {
- uchar dat;
- Start(); //寫入芯片地址
- SendByte(0x90);
- Ack();
- SendByte(0x40|ch);//寫入選擇的通道,
- Ack();
- Start();
- SendByte(0x91); //讀入地址
- Ack();
- dat=RcvByte(); //讀數據
- NoAck();
- Stop();
- return dat; //返回值
- }
- void main(void)
- {
- uchar temp0;
- uint temp1;
- uchar i = 2;
- dis[0]=11;
- dis[1]=11;
- dis[2]=11;
- dis[3]=11;
- dis[4]=11;
- while(1)
- {
- temp0 = PCF8591_ADC(2);
- temp1 = (uint)(temp0 * 196);
- for(i=0;i<8;i++)
- {
- switch(i)
- {
- case 5:dis[5]=(temp1/10000); case 6:dis[6]=((temp1%10000)/1000);
- case 7:dis[7]=((temp1%1000)/100);
- }
- if(i==5)
- P0 = ((seg[dis[ i]])|0x80);
- else
- P0 = seg[dis[ i]];
- P2 = i;
- delayms(1);
- }
- }
- }
復制代碼
|
|