//停止总线 //延时
Stop(); Delay5ms(); Delay5ms(); return 0; }
//向I2C总线连续写入7个数据
unsigned char i2c_WriteData(unsigned char RomAddress) {
unsigned char i;
Start(); Wait(); if(TestAck()!=START) return 1;
Write8Bit(wr_device_add); Wait(); if(TestAck()!=MT_SLA_ACK) return 2;
//发总线起始命令 //等待应答 //测试应答
//写器件地址 //等待应答 //测试应答
//写器件内的寄存器首地址 //等待应答 //测试应答
Write8Bit(RomAddress); Wait(); if(TestAck()!=MT_DATA_ACK)return 3; for(i=0;i<7;i++) { Write8Bit(write_shijian[i]);
//循环7次 //写数据
Wait(); //等待应答 if(TestAck()!=MT_DATA_ACK)return 4; //测试应答 }
//停止总线 //延时 //延时
Stop();
Delay5ms(); Delay5ms(); return 0; }
//连续读取I2C总线
unsigned char i2c_Read(unsigned char RomAddress) {
unsigned char i; 冲 }
Start(); Wait(); if(TestAck()!=START) return 1;
Write8Bit(rd_device_add); Wait(); if(TestAck()!=MR_SLA_ACK) return 5;
for(i=0;i<7;i++) {
Ack();
//发总线起始命令
//等待应答 //测试应答 //装入读器件地址 //等待应答 //测试应答
//使能EA,如果接受到数据发送ACK脉
Wait(); if(TestAck()!=MR_DATA_ACK)return 6; read_shijian[i]=TWDR; }
Stop(); return 0;
//等待应答
//测试应答
//将读取的数据放入数组中
//停止总线
//INT0中断函数,刷新显示时间
#pragma interrupt_handler Int0_isr:3 //中断向量2是INT0,3是INT1 void Int0_isr(void) {
uchar result,result1,result2,result3,result4,result5;
i2c_Write(0x19,0x01); //向8563的01H控制状态寄存器2写入0x19
i2c_Read(0x02); read_shijian[] result1=read_shijian[0]&0x7f; sprintf(s,\转换成字符串 秒 if(result1<0x10) {DisplayOneChar(6,1,'0'); DisplayListChar(7,1,s);
//读取8563的02H-08H装入
}
else DisplayListChar(6,1,s); //刷新时间
result2=read_shijian[1]&0x7f;
sprintf(s,\转换成字符串 分 if(result2<0x10) { DisplayOneChar(3,1,'0'); DisplayListChar(4,1,s); } else DisplayListChar(3,1,s); //刷新时间 result3=read_shijian[2]&0x3f; sprintf(s,\转换成字符串 小时 if(result3<0x10) { DisplayOneChar(0,1,'0'); DisplayListChar(1,1,s); } else DisplayListChar(0,1,s); //刷新时间
result4=read_shijian[3]&0x3f; sprintf(s,\转换成字符串 日 if(result4<0x10) {
DisplayOneChar(8,0,'0'); DisplayListChar(9,0,s); }
else DisplayListChar(8,0,s); //刷新时间 Delay1ms();
sprintf(s,\转换成字符串 DisplayListChar(11,0,s); //刷新时间
result5=read_shijian[5]&0x1f;
sprintf(s,\转换成字符串 月 if(result5<0x10) {
DisplayOneChar(5,0,'0');
星期
DisplayListChar(6,0,s); } else DisplayListChar(5,0,s); //刷新时间 result=read_shijian[6]; sprintf(s,\ //转换成字符串 年 if(result<0x10) { DisplayOneChar(2,0,'0'); DisplayListChar(3,0,s); }
else DisplayListChar(2,0,s); //刷新时间 if((result5==0x01)&&(result4==0x01)&&(result3==0x00)&&(result2==0x00)&&(result1==0x00)) {sprintf(s,\ DisplayListChar(0,0,s); } }
//初始化TWI总线 void Twi_Init(void) {
TWBR=32; //设置波特率 TWSR=0; //复位TWI总线 i2c_Write(0,0); //设置时钟状态 i2c_Write(0x81,0x0d); i2c_Write(0x82,0x0e); i2c_Write(1,0x0f); i2c_Write(0x81,0x01); i2c_WriteData(0x02); i2c_Write(0x19,0x01); //向8563的01H控制状态寄存器2写入0x19 i2c_Read(0x02); //读取8563的02H-08H装入read_shijian[] }
void main(void) {
uint k=19;//年 DDRD=0xfb; DDRC=0xff;
CLI(); Delay400ms();//延时,启动 //MCUCR=(1< DDRB=0xff; //启动后灯亮 PORTB&=~(1< Twi_Init(); LCMInit(); //初始化 PORTB|=(1< Delay5ms(); //年 sprintf(s,\ DisplayListChar(0,0,s); Delay5ms(); DisplayOneChar(4,0,'.'); Delay5ms(); DisplayOneChar(7,0,'.'); Delay5ms(); DisplayOneChar(10,0,'.'); Delay5ms(); DisplayOneChar(2,1,':'); Delay5ms(); DisplayOneChar(5,1,':'); //DisplayOneChar(10,0,'.'); //sprintf(s,\ //DisplayListChar(0,0,s); // DisplayListChar(5,0,net); SEI(); while(1); } //关中断 //设置INT0下降沿触发中断//使能INT1中断 //初始化TWI总线 //开中断