完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
驱动:
//IIC.h #include ***it SDA = P2^1; //数据线 ***it SCL = P2^0; //时钟线 void Delay_IIC(unsigned char t); //延时函数 void IIC_Start(void); //起始信号 void IIC_Stop(void); //停止信号 void IIC_Ack(unsigned char ackbit); //产生应答 bit IIC_WaitAck(void); //等待应答 void IIC_SendByte(unsigned char byte); //发送数据 unsigned char IIC_RecByte(void); //接收数据 //IIC.c #include void Delay_IIC(unsigned char time) { while(time--); } void IIC_Start(void) { SDA = 1; SCL = 1; Delay_IIC(5); SDA = 0; //在SCL高电平期间,SDA由高变低 Delay_IIC(5); SCL = 0; } void IIC_Stop(void) { SDA = 0; SCL = 1; Delay_IIC(5); SDA = 1; //在SCL高电平期间,SDA由高变低 Delay_IIC(5); } void IIC_Ack(unsigned char ackbit) { if(ackbit) SDA = 0; //产生应答信号 else SDA = 1; //产生非应答信号 Delay_IIC(5); SCL = 1; Delay_IIC(5); //第9个时钟周期 SCL = 0; SDA = 1; //释放SDA线 Delay_IIC(5); } bit IIC_WaitAck(void) { SDA = 1; Delay_IIC(5); SCL = 1; Delay_IIC(5); if(SDA) //在SCL高电平期间,SDA为高电平,从机非应答。 { SCL = 0; IIC_Stop(); return 0; } else //在SCL高电平期间,SDA为低电平,从机有应答。 { SCL = 0; return 1; } } void IIC_SendByte(unsigned char byt) { unsigned char i; for(i=0;i<8;i++) //循环发送8位数据 { if(byt & 0x80) //数据位是高电平 { SDA = 1; } else //数据位是低电平 { SDA = 0; } Delay_IIC(5); SCL = 1; //SCL高电平期间,SDA的数据要保持稳定 byt <<= 1; //发送的数据左移,准备发送下一位 Delay_IIC(5); //等待SDA的数据被读取 SCL = 0; } } unsigned char IIC_RecByte(void) { unsigned char da; unsigned char i; for(i=0;i<8;i++) { SCL = 1; Delay_IIC(5); //在SCL高电平期间,读取SDA的数据 da <<= 1; if(SDA) da |= 0x01; SCL = 0; Delay_IIC(5); } return da; } 主程序: #include #include"IIC.h" unsigned char dat1 = 0; unsigned char dat2 = 0; unsigned char dat3 = 0; //存放数据 unsigned char code SEG_code[18] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e, 0xbf,0x7f}; //定义共阳数码管段码内容:0~F,—,. void Select_HC138(unsigned char n) { switch(n) { case 5: P2 = (P2 & 0x1f) | 0xa0; break; case 6: P2 = (P2 & 0x1f) | 0xc0; break; case 7: P2 = (P2 & 0x1f) | 0xe0; break; } } void Delay(unsigned int t) { while(t--); } void Show_tube(unsigned char position,unsigned char value) { Select_HC138(6); P0 = 0x01 << position; Select_HC138(7); P0 = value; } void Display_tube() { Show_tube(0,SEG_code[dat1/10]); Delay(100); Show_tube(1,SEG_code[dat1%10]); Delay(100); Show_tube(2,SEG_code[16]); Delay(100); Show_tube(3,SEG_code[dat2/10]); Delay(100); Show_tube(4,SEG_code[dat2%10]); Delay(100); Show_tube(5,SEG_code[16]); Delay(100); Show_tube(6,SEG_code[dat3/10]); Delay(100); Show_tube(7,SEG_code[dat3%10]); Delay(100); } void Write_24C02(unsigned char addr,unsigned dat) { IIC_Start(); //IIC总线起始信号 IIC_SendByte(0xa0); //写设备地址 IIC_WaitAck(); //等待从机应答 IIC_SendByte(addr); //写内存字节地址 IIC_WaitAck(); //等待从机应答 IIC_SendByte(dat); //写入目标数据 IIC_WaitAck(); //等待从机应答 IIC_Stop(); //IIC总线停止信号 } unsigned char Read_24C02(unsigned char addr) { unsigned char temp; //进行一个伪写操作 IIC_Start(); //IIC总线起始信号 IIC_SendByte(0xa0); //写设备地址 IIC_WaitAck(); //等待从机应答 IIC_SendByte(addr); //写内存字节地址 IIC_WaitAck(); //等待从机应答 IIC_Start(); //IIC总线起始信号 IIC_SendByte(0xa1); //读设备地址 IIC_WaitAck(); //等待从机应答 temp = IIC_RecByte(); //读取目标数据 IIC_Ack(0); //产生一个非应答信号 IIC_Stop(); //IIC总线停止信号 return temp; } void Read_unit() { dat1 = Read_24C02(0x01); //读取0x01内存单元的数据 dat2 = Read_24C02(0x03); //读取0x03内存单元的数据 dat3 = Read_24C02(0x05); //读取0x05内存单元的数据 } void Write_unit() { dat1 = 10; dat2 = 21; dat3 = 35; Write_24C02(0x01,dat1); Delay(1000); Write_24C02(0x03,dat2); Delay(1000); Write_24C02(0x05,dat3); Delay(1000); } void main() { Select_HC138(5); P0 = 0x00; Write_unit(); Read_unit(); while(1) { Display_tube(); } } |
|
|
|
驱动:
//IIC.h #include ***it SDA = P2^1; //数据线 ***it SCL = P2^0; //时钟线 void Delay_IIC(unsigned char t); //延时函数 void IIC_Start(void); //起始信号 void IIC_Stop(void); //停止信号 void IIC_Ack(unsigned char ackbit); //产生应答 bit IIC_WaitAck(void); //等待应答 void IIC_SendByte(unsigned char byte); //发送数据 unsigned char IIC_RecByte(void); //接收数据 //IIC.c #include void Delay_IIC(unsigned char time) { while(time--); } void IIC_Start(void) { SDA = 1; SCL = 1; Delay_IIC(5); SDA = 0; //在SCL高电平期间,SDA由高变低 Delay_IIC(5); SCL = 0; } void IIC_Stop(void) { SDA = 0; SCL = 1; Delay_IIC(5); SDA = 1; //在SCL高电平期间,SDA由高变低 Delay_IIC(5); } void IIC_Ack(unsigned char ackbit) { if(ackbit) SDA = 0; //产生应答信号 else SDA = 1; //产生非应答信号 Delay_IIC(5); SCL = 1; Delay_IIC(5); //第9个时钟周期 SCL = 0; SDA = 1; //释放SDA线 Delay_IIC(5); } bit IIC_WaitAck(void) { SDA = 1; Delay_IIC(5); SCL = 1; Delay_IIC(5); if(SDA) //在SCL高电平期间,SDA为高电平,从机非应答。 { SCL = 0; IIC_Stop(); return 0; } else //在SCL高电平期间,SDA为低电平,从机有应答。 { SCL = 0; return 1; } } void IIC_SendByte(unsigned char byt) { unsigned char i; for(i=0;i<8;i++) //循环发送8位数据 { if(byt & 0x80) //数据位是高电平 { SDA = 1; } else //数据位是低电平 { SDA = 0; } Delay_IIC(5); SCL = 1; //SCL高电平期间,SDA的数据要保持稳定 byt <<= 1; //发送的数据左移,准备发送下一位 Delay_IIC(5); //等待SDA的数据被读取 SCL = 0; } } unsigned char IIC_RecByte(void) { unsigned char da; unsigned char i; for(i=0;i<8;i++) { SCL = 1; Delay_IIC(5); //在SCL高电平期间,读取SDA的数据 da <<= 1; if(SDA) da |= 0x01; SCL = 0; Delay_IIC(5); } return da; } 主程序: #include #include"IIC.h" unsigned char dat1 = 0; unsigned char dat2 = 0; unsigned char dat3 = 0; //存放数据 unsigned char code SEG_code[18] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e, 0xbf,0x7f}; //定义共阳数码管段码内容:0~F,—,. void Select_HC138(unsigned char n) { switch(n) { case 5: P2 = (P2 & 0x1f) | 0xa0; break; case 6: P2 = (P2 & 0x1f) | 0xc0; break; case 7: P2 = (P2 & 0x1f) | 0xe0; break; } } void Delay(unsigned int t) { while(t--); } void Show_tube(unsigned char position,unsigned char value) { Select_HC138(6); P0 = 0x01 << position; Select_HC138(7); P0 = value; } void Display_tube() { Show_tube(0,SEG_code[dat1/10]); Delay(100); Show_tube(1,SEG_code[dat1%10]); Delay(100); Show_tube(2,SEG_code[16]); Delay(100); Show_tube(3,SEG_code[dat2/10]); Delay(100); Show_tube(4,SEG_code[dat2%10]); Delay(100); Show_tube(5,SEG_code[16]); Delay(100); Show_tube(6,SEG_code[dat3/10]); Delay(100); Show_tube(7,SEG_code[dat3%10]); Delay(100); } void Write_24C02(unsigned char addr,unsigned dat) { IIC_Start(); //IIC总线起始信号 IIC_SendByte(0xa0); //写设备地址 IIC_WaitAck(); //等待从机应答 IIC_SendByte(addr); //写内存字节地址 IIC_WaitAck(); //等待从机应答 IIC_SendByte(dat); //写入目标数据 IIC_WaitAck(); //等待从机应答 IIC_Stop(); //IIC总线停止信号 } unsigned char Read_24C02(unsigned char addr) { unsigned char temp; //进行一个伪写操作 IIC_Start(); //IIC总线起始信号 IIC_SendByte(0xa0); //写设备地址 IIC_WaitAck(); //等待从机应答 IIC_SendByte(addr); //写内存字节地址 IIC_WaitAck(); //等待从机应答 IIC_Start(); //IIC总线起始信号 IIC_SendByte(0xa1); //读设备地址 IIC_WaitAck(); //等待从机应答 temp = IIC_RecByte(); //读取目标数据 IIC_Ack(0); //产生一个非应答信号 IIC_Stop(); //IIC总线停止信号 return temp; } void Read_unit() { dat1 = Read_24C02(0x01); //读取0x01内存单元的数据 dat2 = Read_24C02(0x03); //读取0x03内存单元的数据 dat3 = Read_24C02(0x05); //读取0x05内存单元的数据 } void Write_unit() { dat1 = 10; dat2 = 21; dat3 = 35; Write_24C02(0x01,dat1); Delay(1000); Write_24C02(0x03,dat2); Delay(1000); Write_24C02(0x05,dat3); Delay(1000); } void main() { Select_HC138(5); P0 = 0x00; Write_unit(); Read_unit(); while(1) { Display_tube(); } } |
|
|
|
只有小组成员才能发言,加入小组>>
3309 浏览 9 评论
2988 浏览 16 评论
3490 浏览 1 评论
9049 浏览 16 评论
4083 浏览 18 评论
1167浏览 3评论
601浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
592浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2329浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1892浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-18 23:58 , Processed in 1.242463 second(s), Total 81, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号