你的地址写错了
线路接错了
我给你来个程序试试
- #include "reg52.h"
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit K1=P1^0;//设置三个独立键盘
- sbit K2=P1^3;
- sbit K3=P1^5;
- sbit lcdwrite=P2^3;
- sbit lcddatecommand=P2^2;
- sbit lcde=P2^4;
- uchar code secondline[]="00-00-00";
- uchar i;
- uchar hour,minutes,seconds;
- uchar count,num;
- uchar cnt;
- sbit SCL = P2^0; //模拟I2C时钟控制位
- sbit SDA = P2^1; //模拟I2C数据传送位
- #define ACK 0 //应答标志位
- #define NACK 1
- void IIC_Start(void); //启动总线
- void IIC_Stop(void); //结束总线
- void IIC_SendACK(bit ACK_Type); //应答子函数
- bit IIC_RecvACK_Error(void);
- void IIC_SendOneByte(unsigned char TxValue); //字节数据传送函数
- unsigned char IIC_RecvOneByte(void); //字节数据传送函数
- void Delay_us(unsigned int DelayCycle); //uS延时函数
- #define INA219_RD_Operation 0x81
- #define INA219_WR_Operation 0x80
- unsigned short INA219_ReadReg(unsigned char RegBase);
- bit INA219_WriteReg(unsigned char RegBase, unsigned int RegValue);
- void INA219_Init(void);
- unsigned int INA219_GetBusVolt(void);
- unsigned int INA219_GetPower(void);
- unsigned int INA219_GetCurrent(void);
- void write_command(uchar command);
- void write_date(uchar date);
- void Delay_us(unsigned int DelayCycle)
- {
- unsigned char i;
- while(DelayCycle--)
- {
- i = 2;
- while (--i);
- }
- }
- void nop4()
- {
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- }
- //Bus free time between a STOP and START condition: >1.3us
- //LOW period of the SCL clock: >1.3us
- //HIGH period of the SCL clock: >0.6us
- //Set-up time for a repeated START condition: >0.6us
- //Hold time (repeated) START condition: >0.6us
- //Data set-up time: >0.1us
- //Set-up time for STOP condition >0.6us
- //Machine Cycle Time = 1.08us @ 11.0592MHz
- void IIC_Start(void)
- {
- SCL = 1;
- _nop_();
- SDA = 1;
- nop4();
- SDA = 0;
- nop4();
- SCL = 0;
- _nop_();
- _nop_();
- }
- void IIC_Stop(void)
- {
- SDA = 0;
- _nop_();
- SCL=0;
- nop4();//>4us后SCL跳变
- SCL = 1;
- nop4();
- SDA = 1;
- _nop_();
- _nop_();
- }
- void IIC_SendACK(bit ACK_Type)
- {
- SDA = ACK_Type;
- SCL = 1;
- SCL = 0;
- SDA = 1; //Release SDA.
- }
- bit IIC_RecvACK_Error(void)
- {
- bit RecvAckError;
- SDA = 1; //Write "1" before read.
- SCL = 1;
- RecvAckError = SDA; //Read SDA.
- SCL = 0;
- return RecvAckError;
- }
- void IIC_SendOneByte(unsigned char TxValue)
- {
- unsigned char i;
- for(i=0; i<8; i++)
- {
- TxValue <<= 1; //Data is transferred with MSB first.
- SDA = CY; //If MSB is 1, the Carry Flag (CY) will be set to 1 after left logical shift, and vice versa.
- _nop_();
- SCL = 1;
- nop4();
- _nop_();
- SCL = 0;
- }
- }
- unsigned char IIC_RecvOneByte(void)
- {
- unsigned char RxValue = 0;
- unsigned char i;
- SDA = 1; //Write "1" before read.
- for(i=0; i<8; i++)
- {
- _nop_();
- SCL = 0;
- nop4();
- SCL = 1;
- _nop_();
- _nop_();
- RxValue <<= 1;
- RxValue |= SDA;
- _nop_();
- }
- SCL = 0;
- _nop_();
- return RxValue;
- }
- unsigned int INA219_ReadReg(unsigned char RegBase)
- {
- unsigned short RegValue = 0;
- IIC_Start();
- IIC_SendOneByte(INA219_WR_Operation);
- if(!IIC_RecvACK_Error())
- {
- IIC_SendOneByte(RegBase);
- if(!IIC_RecvACK_Error())
- {
- //IIC_Stop();
- IIC_Start();
- IIC_SendOneByte(INA219_RD_Operation);
- if(!IIC_RecvACK_Error())
- {
- RegValue = IIC_RecvOneByte();
- IIC_SendACK(ACK);
- RegValue = RegValue << 8;
- RegValue |= IIC_RecvOneByte();
- IIC_SendACK(NACK);
- }
- }
- }
- IIC_Stop();
- return RegValue;
- }
- bit INA219_WriteReg(unsigned char RegBase, unsigned int RegValue)
- {
- bit WrRegError = 1;
- IIC_Start();
- IIC_SendOneByte(INA219_WR_Operation);
- if(!IIC_RecvACK_Error())
- {
- IIC_SendOneByte(RegBase);
- if(!IIC_RecvACK_Error())
- {
- IIC_SendOneByte((RegValue & 0xFF00) >> 8);
- IIC_RecvACK_Error();
- IIC_SendOneByte(RegValue & 0x00FF);
- WrRegError = IIC_RecvACK_Error();
- }
- }
- IIC_Stop();
- return !WrRegError;
- }
- void INA219_Init(void)
- { //0+
- INA219_WriteReg(0x00, 0x3C1F);//32V FSR, GAIN = 8, 16 samples, Shunt and Bus, Continuous.
- INA219_WriteReg(0x05, 0x1000);//Use INA219 EVM software to calculate a full-scale calibration value (initial calbration), and then compute the corrected full-scale calibration value based on measured current (second calbration).
- }
- unsigned int INA219_GetBusVolt(void)
- {
- unsigned int BusVoltRegVal;
- // Sometimes a sharp load will reset the INA219, which will
- // reset the cal register, meaning CURRENT and POWER will
- // not be available ... avoid this by always setting a cal
- // value even if it's an unfortunate extra step
- //INA219_WriteReg(0x05, 0x1000);
- BusVoltRegVal = INA219_ReadReg(0x02);
- if(BusVoltRegVal & 0x0001) //Overflow
- return 0x0FA0;
- else
- return (BusVoltRegVal & 0xFFF8) >> 1; //((BusVoltRegVal & 0xFFF8) >> 3)*4mV
- }
- unsigned int INA219_GetPower(void)
- {
- unsigned int PowerRegVal;
- //INA219_WriteReg(0x05, 0x1000);
- PowerRegVal = INA219_ReadReg(0x03);
- return (PowerRegVal >> 1)*5; //2.5mW/bit
- }
- unsigned int INA219_GetCurrent(void)
- {
- unsigned int CurrentRegVal;
- //INA219_WriteReg(0x05, 0x1050);
- CurrentRegVal = INA219_ReadReg(0x04);
- return CurrentRegVal >> 3; //0.125mA/bit
- }
- void RefreshData(void)
- {
- unsigned int BusVolt, Current, Power;
- unsigned int OffsetCurrent, OffsetPower;
-
- BusVolt = INA219_GetBusVolt();
-
- OffsetCurrent = (BusVolt >> 9) + 2; //Gather statistics of the measured current under various voltage conditions when no load is connected to the output port. Use Excel to do curve fitting.
- Current = INA219_GetCurrent();
- if(Current > OffsetCurrent)
- Current -= OffsetCurrent;
- else
- Current = 0;
- write_command(0x80);
- write_date( BusVolt/10000+48);
- write_date( (BusVolt%10000)/1000+48);
- write_date( '.');
- write_date( (BusVolt%1000)/100+48);
- write_date( (BusVolt%100)/10+48);
- write_date( BusVolt%10+48);
- write_date( 'V');
- write_date( ' ');
- write_date( ' ');
-
- write_date(Current/1000+48);
- write_date( '.');
- write_date( (Current%1000)/100+48);
- write_date( (Current%100)/10+48);
- write_date( Current%10+48);
- write_date( 'A');
-
- OffsetPower = ((BusVolt >> 3) * OffsetCurrent)/125 + 6;
- Power = INA219_GetPower();
- if(Power > OffsetPower)
- Power -= OffsetPower;
- else
- Power = 0;
- write_command(0x80+0x49);
- write_date(Power/10000+48);
- write_date((Power%10000)/1000+48);
- write_date( '.');
- write_date((Power%1000)/100+48);
- write_date( (Power%100)/10+48);
- write_date(Power%10+48);
- write_date( 'W');
- }
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- {
- for(y=0;y<=112;y++)
- {
- }
- }
- }
- void write_command(uchar command)
- {
- lcddatecommand=0;
- lcdwrite=0;
- P0=command;
- delay(1);
- lcde=1;
- delay(1);
- lcde=0;
- }
- void write_date(uchar date)
- {
- lcddatecommand=1;
- lcdwrite=0;
- P0=date;
- delay(1);
- lcde=1;
- delay(1);
- lcde=0;
- }
- void LCDinit()
- {
- lcde=0;
- write_command(0x38);//设置16*2显示,5*7点阵,8位数据接口
- write_command(0x0f);//设置开显示,不显示光标
- write_command(0x06);// 写一个字符后地址指针加1
- write_command(0x0C);
- write_command(0x80);
- write_command(0x80+0x40);
- for(i=0;i<8;i++)
- {
- write_date(secondline[i]);
- delay(1);
- }
- }
- void Time0init()
- {
- TMOD=0x01;//开启定时器1
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void Clockinit()
- {
- hour=0;
- minutes=0;
- seconds=0;
- count=0;
- num=0;
- }
- void display(uchar hour,uchar minutes,uchar seconds)//接收并发送数据到液晶屏幕
- {
- uchar hourge,hourshi,minutesshi,minutesge,secondsshi,secondsge;
- hourshi=hour/10;
- hourge=hour%10;
- write_command(0x80+0x40);
- write_date(hourshi+48);
- delay(1);
- write_date(hourge+48);
- delay(1);
- minutesshi=minutes/10;
- minutesge=minutes%10;
- write_command(0x0C);
- write_command(0x80+0x43);
- write_date(minutesshi+48);
- delay(1);
- write_date(minutesge+48);
- delay(1);
- secondsshi=seconds/10;
- secondsge=seconds%10;
- write_command(0x80+0x46);
- write_date(secondsshi+48);
- delay(1);
- write_date(secondsge+48);
- delay(1);
- }
- void revise()
- {
- if(K1==0)
- {
- delay(5);
- if(K1==0)
- {
- cnt++;//计数第几次按下
- TR0=0;
- }
- while(!K1);
- delay(5);
- while(!K1);
- }
- if(cnt==1)//第一次按下,调整秒钟,调整时秒钟有光标闪烁
- {
- write_command(0x80+0x46);
- write_command(0x0f);
- if(K2==0)
- {
- delay(5);
- if(K2==0)
- {
- seconds++;
- if(seconds==60)
- {
- seconds=0;
- }
- }
- while(!K2);
- delay(5);
- while(!K2);
- }
- if(K3==0)
- {
- delay(5);
- if(K3==0)
- {
- seconds--;
- if(seconds==-1)//here is a bug
- {
- seconds=59;
- }
- }
- while(!K3);
- delay(5);
- while(!K3);
- }
- }
- if(cnt==2)//第二次按下,调整分钟,调整时秒钟有光标闪烁
- {
- write_command(0x80+0x43);
- write_command(0x0f);
- if(K2==0)
- {
- delay(5);
- if(K2==0)
- {
- minutes++;
- if(minutes==60)
- {
- minutes=0;
- }
- }
- while(!K2);
- delay(5);
- while(!K2);
- }
- if(K3==0)
- {
- delay(5);
- if(K3==0)
- {
- minutes--;
- if(minutes==-1)//here is a bug
- {
- minutes=59;
- }
- }
- while(!K3);
- delay(5);
- while(!K3);
- }
- }
- if(cnt==3)//第三次按下,调整时钟,调整时秒钟有光标闪烁
- {
- write_command(0x80+0x40);
- write_command(0x0f);
- if(K2==0)
- {
- delay(5);
- if(K2==0)
- {
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- while(!K2);
- delay(5);
- while(!K2);
- }
- if(K3==0)
- {
- delay(5);
- if(K3==0)
- {
- hour--;
- if(hour==-1)//here is a bug
- {
- hour=23;
- }
- }
- while(!K3);
- delay(5);
- while(!K3);
- }
- }
- if(cnt==4)
- {
- TR0=1;
- cnt=0;
- }
- }
- void main()
- {
- INA219_Init();
- LCDinit();
- Time0init();
- Clockinit();
- Delay_us(2000);
- RefreshData();
- while(1)
- {
- revise();
- if(count>=19)
- {
- display(hour,minutes,seconds);
- RefreshData();
- }
- }
- }
- void Time0() interrupt 1 using 2
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- count++;
- if(count==20)
- {
- count=0;
- seconds++;
- if(seconds==60)
- {
- seconds=0;
- minutes++;
- if(minutes==60)
- {
- minutes=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- }
- }
- }
复制代码
|