51单片机之IIC&EEPROM的驱动程序
2016-09-22 20:32
302 查看
#include <reg52.h> #include "./delay/delay.h" sbit SCL = P2^0; sbit SDA = P2^1; bit ack = 0; unsigned char flag = 1; #define LCDPORT P0 #define LCD_WRITE_DATA 1 #define LCD_WRITE_COM 0 sbit RS = P2^4; sbit RW = P2^5; sbit E = P2^6; #define SUCC 0 #define ERR 1 void iic_start() { SDA = 1; //先操作SDA,在操作SCL SCL = 1; delay_us(1); SDA = 0; delay_us(1); SCL = 0; //钳住总线 } void iic_stop() { SDA = 0; SCL = 1; delay_us(1); SDA = 1; delay_us(1); SCL = 0; //钳住总线 } bit iic_send_byte(unsigned char byte) //发送数据,先发送高位,再发送低位 只发送一个字节 { unsigned char i; for(i = 0; i < 8; i++) { SDA = byte & 0x80; //与上为非0时,SDA = 1; 与上为0时,SDA = 0; SCL = 1; delay_us(1); //延时1us SCL = 0; byte <<= 1; } SCL = 1; SDA = 1; delay_us(2); if(0 == SDA) { ack = 1; } else { ack = 0; } SCL = 0; return ack; } unsigned char iic_rcv_byte() //接收数据 { unsigned char i; unsigned char temp = 0; unsigned char a; SDA = 1; for(i = 0; i < 8; i++) { SCL = 0; delay_us(1); SCL = 1; if(SDA) { a = 0x01; } else { a = 0; } temp |= (a << (7 - i)); delay_us(1); } SCL = 0; return temp; } void iic_ack() //响应信号 { SDA = 0; //拉低数据总线 SCL = 1; //时钟总线拉高 delay_us(1); SCL = 0; //钳住总线 } void iic_noack() //非响应信号 { SDA = 1; //释放数据总线 SCL = 1; //拉高时钟总线 delay_us(1); SCL = 0; //钳住总线 } unsigned char AT24c02_send_byte(unsigned char devaddr, unsigned char romaddr, unsigned char *s, unsigned char num) //发送字符串 { unsigned char i; iic_start(); //起始信号 iic_send_byte(devaddr); //写硬件地址 if(0 == ack) //等待响应 { return ERR; } iic_send_byte(romaddr); //写物理地址 if(0 == ack) //等待响应 { return ERR; } for(i = 0; i < num; i++) //发送num个字符 { iic_send_byte(*s); //逐个字节发送 if(0 == ack) { return ERR; } s++; } iic_stop(); return SUCC; } unsigned char AT24c02_rcv_byte(unsigned char devaddr, unsigned char romaddr, unsigned char *s, unsigned char num) { unsigned char i; iic_start(); delay_us(10); iic_send_byte(devaddr); //写硬件地址 if(0 == ack) //等待响应 { return ERR; } iic_send_byte(romaddr); //写物理地址 if(0 == ack) //等待响应 { return ERR; } iic_start(); iic_send_byte(devaddr + 1); if(0 == ack) //等待响应 { return ERR; } for(i = 0; i < num -1; i++) //开始读数据 { *s = iic_rcv_byte(); iic_ack(); s++; } *s = iic_rcv_byte(); iic_noack(); delay_us(10); iic_stop(); return SUCC; } void lcd1602_write(unsigned char byte, unsigned char flag) { if(flag) { RS = 1; } else { RS = 0; } RW = 0; E = 1; LCDPORT = byte; delay_ms(5); E = 0; } void lcd_init() { delay_ms(15); lcd1602_write(0x38,LCD_WRITE_COM); delay_ms(5); lcd1602_write(0x38,LCD_WRITE_COM); delay_ms(5); lcd1602_write(0x38,LCD_WRITE_COM); delay_ms(5); lcd1602_write(0x38,LCD_WRITE_COM); delay_ms(5); lcd1602_write(0x08,LCD_WRITE_COM); delay_ms(5); lcd1602_write(0x01,LCD_WRITE_COM); delay_ms(5); lcd1602_write(0x06,LCD_WRITE_COM); delay_ms(5); lcd1602_write(0x0c,LCD_WRITE_COM); delay_ms(5); } void lcd_dis_char(unsigned char x, unsigned char y, unsigned char *str) { if((x > 15) || (y > 1)) { return ; } if(0 == y) { lcd1602_write(0x80 + x, LCD_WRITE_COM); } else { lcd1602_write(x + 0xc0,LCD_WRITE_COM); } while(*str != '\0') { lcd1602_write(*str , LCD_WRITE_DATA); str++; } } void main() { //unsigned char i; unsigned char test[10] = {'0','1','2','3','4','5','6','7','8','9'}; unsigned char temp[11]; lcd_init(); AT24c02_send_byte(0xae,100,test,10); delay_s(1); AT24c02_rcv_byte(0xae,100,temp,10); temp[10] = '\0'; lcd_dis_char(0,0,temp); lcd_dis_char(0,1,temp); while(1) { } }
delay()延时函数之前的程序中有.
相关文章推荐
- IIC&EEPROM
- IIC&EEPROM总结
- 51单片机——EEPROM
- 用Proteus学习51单片机之I2C(IIC)总线
- 51单片机的DS1302的驱动程序
- 基于CS16312控制芯片的VFD显示屏STC51单片机驱动程序
- 单片机控制IIC协议EEPROM芯片AT24C02模块化编程(持续更新中)
- 十天学会单片机Day5 IIC总线AT2402芯片(EEPROM)应用
- 单片机模拟 IIC 总线与 EEPROM(AT24C02)通信
- 51单片机中iic
- 用Proteus学习51单片机之I2C(IIC)总线
- 【ARM&Linux】基于c3c2440 Linux IIC驱动程序移植与测试
- 单片机控制IIC协议EEPROM芯片24C512之模块化编程(持续更新中)
- 51单片机之IIC通信原理及软件仿真
- IIC程序51 调试通过
- 面试你该如何回答---->IIC总线协议?
- 51单片机最小系统制作笔记
- 单片机MCS-51系列指令快速记忆法
- STM32单片机的IIC硬件编程---查询等待方式
- 单片机MCS-51系列指令快速记忆法