我用STC15F2K6S2芯片做简易电压表测量,但下载进去总是显示一条白行怎么回事啊?电路纯自己焊的P0口输出数据到LCD1602,p1.0,p1.1借FPC8591
单片机源程序如下:
- #include<STC15F2K60S2.h>
- #include <intrins.h>
- #define AddWr 0x90
- #define Data P0
- unsigned char AD_CHANNEL;
- sbit scl=P1^0;
- sbit sda=P1^1;
- bit ack;
- sbit RS = P2^0;
- sbit RW = P2^1;
- sbit E = P2^2;
- unsigned char TempData[8];
- unsigned char FirstLine[16]="Voltage: ";
- unsigned char SecondLine[16] ="Current: ";
- void Start_I2c()
- {
- sda=1;
- _nop_();
- scl=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- sda=0;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- scl=0;
- _nop_();
- _nop_();
- }
- void Stop_I2c()
- {
- sda=0;
- _nop_();
- scl=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- sda=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- void I2C_SendByte(unsigned char c)
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- if((c<<i)&0x80)sda=1;
- else sda=0;
- _nop_();
- scl=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- scl=0;
- }
- _nop_();
- _nop_();
- sda=1;
- _nop_();
- _nop_();
- scl=1;
- _nop_();
- _nop_();
- _nop_();
- if(sda==1)ack=0;
- else ack=1;
- scl=0;
- _nop_();
- _nop_();
- }
-
- unsigned char I2C_RcvByte()
- {
- unsigned char retc=0,i;
- sda=1;
- for(i=0;i<8;i++)
- {
- _nop_();
- scl=0;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- scl=1;
- _nop_();
- _nop_();
- retc=retc<<1;
- if(sda==1)retc=retc+1;
- _nop_();
- _nop_();
- }
- scl=0;
- _nop_();
- _nop_();
- return(retc);
- }
- void Ack_I2c(bit a)
- {
- if(a==0)
- {
- sda=0;
- _nop_();
- _nop_();
- _nop_();
- scl=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- scl=0;
- _nop_();
- _nop_();
- sda=1;
- _nop_();
- _nop_();
- }
- else
- { sda=1;
- _nop_();
- _nop_();
- _nop_();
- scl=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- scl=0;
- _nop_();
- _nop_();
- sda=0;
- _nop_();
- _nop_();
- }
- }
- bit PCF8591_SendByte(unsigned char addr,unsigned char channel)
- {
- Start_I2c();
- I2C_SendByte(addr);
- if(ack==0)return(0);
- I2C_SendByte(0x40|channel);
- if(ack==0)return(0);
- Stop_I2c();
- return(1);
- }
- unsigned char PCF8591_RcvByte(unsigned char addr)
- {
- unsigned char dat;
- Start_I2c();
- I2C_SendByte(addr+1);
- if(ack==0)return(0);
- dat=I2C_RcvByte();
- Ack_I2c(1);
- Stop_I2c();
- return(dat);
- }
- void DelayUs(unsigned char us)
- {
- unsigned char uscnt;
- uscnt=us>>1;
- while(--uscnt);
- }
- void DelayMs(unsigned char ms)
- {
- while(--ms)
- {
- DelayUs(250);
- DelayUs(250);
- DelayUs(250);
- DelayUs(250);
- }
- }
- void WriteCommand(unsigned char c)
- {
- DelayMs(5);
- E=0;
- RS=0;
- RW=0;
- _nop_();
- E=1;
- Data=c;
- E=0;
- }
- void WriteData(unsigned char c)
- {
- DelayMs(5);
- E=0;
- RS=1;
- RW=0;
- _nop_();
- E=1;
- Data=c;
- E=0;
- RS=0;
- }
- void ShowChar(unsigned char pos,unsigned char c)
- {
- unsigned char p;
- if (pos>=0x10)
- p=pos+0xb0;
- else
- p=pos+0x80;
- WriteCommand (p);
- WriteData (c);
- }
- void ShowString (unsigned char line,char *ptr)
- {
- unsigned char l,i;
- l=line<<4;
- for (i=0;i<16;i++)
- ShowChar (l++,*(ptr+i));
- }
- void InitLcd()
- {
- DelayMs(15);
- WriteCommand(0x38);
- WriteCommand(0x38);
- WriteCommand(0x38);
- WriteCommand(0x06);
- WriteCommand(0x0c);
- WriteCommand(0x01);
- }
- /
- void disp(void)
- {
- unsigned char i=0;
- while(FirstLine[i]!=' ')
- {
- ShowString(0,FirstLine);
- i++;
- }
- FirstLine[8]= '0'+TempData[0];
- FirstLine[9]= '0'+TempData[1];
- FirstLine[10]='.';
- FirstLine[11]='0'+TempData[2];
- FirstLine[12]='0'+TempData[3];
- FirstLine[14]='V';
- ShowString(0,FirstLine);
- while(SecondLine[i]!=' ')
- {
- ShowString(1,SecondLine);
- i++;
- }
- SecondLine[8]='0'+TempData[4];
- SecondLine[11]='0'+TempData[6];
- SecondLine[10]='.';
- SecondLine[14]='V';
- ShowString(1,SecondLine);
- }
- void mDelay(unsigned char j)
- {
- unsigned int i;
- for(;j>0;j--)
- {
- for(i=0;i<125;i++)
- {;}
- }
- }
- main()
- {
- unsigned char ADtemp;
- InitLcd();
- mDelay(20);
- while(1)
- {
-
-
- switch(AD_CHANNEL)
- {
- case 0: PCF8591_SendByte(AddWr,1);
- ADtemp = PCF8591_RcvByte(AddWr);
- TempData[0]=(ADtemp*10/51)/10;
- TempData[1]=(ADtemp*10/51)%10;
- TempData[2]=((ADtemp*10)%51)/5;
- TempData[3]=((ADtemp*10)%51)%5;
-
- break;
-
- case 1: PCF8591_SendByte(AddWr,0);
- ADtemp=PCF8591_RcvByte(AddWr);
- TempData[4]=ADtemp/51;
- TempData[6]=(ADtemp%51)/5;
- break;
-
-
- }
- if(++AD_CHANNEL>2) AD_CHANNEL=0;
- disp();
- }
- }
复制代码 |