该程序里包含了iic总线的一系列驱动,并通过lcd显示出读到的iic总线的发送信号和应答信号
#include <reg52.h>
#include "./delay/delay.h"
#include "./delay/delay.h"
// #define SEGPORT P0
#define ERROR 1
#define SUCCESS 0
#define ERROR 1
#define SUCCESS 0
sbit SCL = P2^7;
sbit SDA = P2^6;
sbit SDA = P2^6;
bit ack = 0;
// sbit bit_sel = P2^0;
// sbit seg_sel = P2^1;
// sbit seg_sel = P2^1;
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;
sbit RW = P2^5;
sbit E = P2^6;
#define LCDPORT P0
#define LCD_WRITE_DATA 1
#define LCD_WRITE_COM 0
#define LCD_WRITE_DATA 1
#define LCD_WRITE_COM 0
unsigned char segdata[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char disdata[8] = {0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char disdata[8] = {0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
// void display()
// {
// static unsigned char i = 0;
//
// SEGPORT = 0x0;
// seg_sel = 1;
// seg_sel = 0;
//
// SEGPORT = 0xff;
// bit_sel = 1;
// bit_sel = 0;
//
// SEGPORT = disdata[i];
// seg_sel = 1;
// seg_sel = 0;
//
// SEGPORT = bitdata[i];
// bit_sel = 1;
// bit_sel = 0;
//
// i++;
// if(i == 8)
// {
// i = 0;
// }
// }
// {
// static unsigned char i = 0;
//
// SEGPORT = 0x0;
// seg_sel = 1;
// seg_sel = 0;
//
// SEGPORT = 0xff;
// bit_sel = 1;
// bit_sel = 0;
//
// SEGPORT = disdata[i];
// seg_sel = 1;
// seg_sel = 0;
//
// SEGPORT = bitdata[i];
// bit_sel = 1;
// bit_sel = 0;
//
// i++;
// if(i == 8)
// {
// i = 0;
// }
// }
void lcd_write(unsigned char byte, unsigned char flag)
{
if(flag)
{
RS = LCD_WRITE_DATA;
}
else
{
RS = LCD_WRITE_COM;
}
RW = 0;
E = 1;
LCDPORT = byte;
delay_us(7);
E = 0;
}
{
if(flag)
{
RS = LCD_WRITE_DATA;
}
else
{
RS = LCD_WRITE_COM;
}
RW = 0;
E = 1;
LCDPORT = byte;
delay_us(7);
E = 0;
}
void lcd_init()
{
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x08,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x01,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x06,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x0C,LCD_WRITE_COM);
}
{
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x08,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x01,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x06,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x0C,LCD_WRITE_COM);
}
void lcd_display(unsigned char x,unsigned char y,unsigned byte)
{
if(y == 1)
{
lcd_write(0x80 + (unsigned int)x*0x01,LCD_WRITE_COM);
lcd_write(byte,LCD_WRITE_DATA);
}
else
{
lcd_write(0x80 + 0x40 + (unsigned int)x*0x01,LCD_WRITE_COM);
lcd_write(byte,LCD_WRITE_DATA);
}
}
{
if(y == 1)
{
lcd_write(0x80 + (unsigned int)x*0x01,LCD_WRITE_COM);
lcd_write(byte,LCD_WRITE_DATA);
}
else
{
lcd_write(0x80 + 0x40 + (unsigned int)x*0x01,LCD_WRITE_COM);
lcd_write(byte,LCD_WRITE_DATA);
}
}
void lcd_write_str(unsigned char x,unsigned char y,unsigned char *s)
{
while(*s != '\0')
{
lcd_write(0x80 + x + y,LCD_WRITE_COM);
lcd_write(*s ,LCD_WRITE_DATA);
s++;
}
}
{
while(*s != '\0')
{
lcd_write(0x80 + x + y,LCD_WRITE_COM);
lcd_write(*s ,LCD_WRITE_DATA);
s++;
}
}
void lcd_str_display(unsigned char x,unsigned char y,unsigned char *s)
{
while(*s != '\0')
{
lcd_display(x++,y,*s);
s++;
}
}
{
while(*s != '\0')
{
lcd_display(x++,y,*s);
s++;
}
}
void iic_start()
{
SDA = 1;
SCL = 1;
delay_us(1);
SDA = 0;
delay_us(1);
SCL = 0;
}
{
SDA = 1;
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;
}
{
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;
SCL = 1;
delay_us(1);
SCL = 0;
byte <<= 1;
}
SCL = 1;
SDA = 1;
delay_us(1);
if(0 == SDA)
{
ack = 1;
}
else
{
ack = 0;
}
SCL = 0;
return ack;
}
{
unsigned char i;
for(i = 0; i < 8; i++)
{
SDA = byte & 0x80;
SCL = 1;
delay_us(1);
SCL = 0;
byte <<= 1;
}
SCL = 1;
SDA = 1;
delay_us(1);
if(0 == SDA)
{
ack = 1;
}
else
{
ack = 0;
}
SCL = 0;
return ack;
}
unsigned char iic_recv_byte()
{
unsigned char temp = 0;
unsigned char i;
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;
}
{
unsigned char temp = 0;
unsigned char i;
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;
}
{
SDA = 0;
SCL = 1;
delay_us(1);
SCL = 0;
}
void iic_noack()
{
SDA = 1;
SCL = 1;
delay_us(1);
SCL = 0;
}
{
SDA = 1;
SCL = 1;
delay_us(1);
SCL = 0;
}
unsigned char iic_AT2402_send_str(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 ERROR;
}
iic_send_byte(romaddr);
if(0 == ack)
{
return ERROR;
}
for(i = 0; i < num; i++)
{
iic_send_byte(*s);
if(0 == ack)
{
return ERROR;
}
s++;
}
iic_stop();
return SUCCESS;
}
{
unsigned char i;
iic_start();
iic_send_byte(devaddr);
if(0 == ack)
{
return ERROR;
}
iic_send_byte(romaddr);
if(0 == ack)
{
return ERROR;
}
for(i = 0; i < num; i++)
{
iic_send_byte(*s);
if(0 == ack)
{
return ERROR;
}
s++;
}
iic_stop();
return SUCCESS;
}
unsigned char iic_AT2402_recv_str(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 ERROR;
}
iic_send_byte(romaddr);
if(0 == ack)
{
return ERROR;
}
iic_start();
iic_send_byte(devaddr + 1);
for(i = 0; i < num-1; i++)
{
*s = iic_recv_byte();
iic_ack();
s++;
// delay_s(1);
}
*s = iic_recv_byte();
iic_noack();
iic_stop();
return SUCCESS;
}
{
unsigned char i;
iic_start();
iic_send_byte(devaddr);
if(0 == ack)
{
return ERROR;
}
iic_send_byte(romaddr);
if(0 == ack)
{
return ERROR;
}
iic_start();
iic_send_byte(devaddr + 1);
for(i = 0; i < num-1; i++)
{
*s = iic_recv_byte();
iic_ack();
s++;
// delay_s(1);
}
*s = iic_recv_byte();
iic_noack();
iic_stop();
return SUCCESS;
}
// void timer1_init()
// {
// EA = 1;
// TH1 = (65536 - 1000)/256;
// TL1 = (65536 - 1000)%256;
// TMOD |= 0x10;
// ET1 = 1;
// TR1 = 1;
// }
// {
// EA = 1;
// TH1 = (65536 - 1000)/256;
// TL1 = (65536 - 1000)%256;
// TMOD |= 0x10;
// ET1 = 1;
// TR1 = 1;
// }
// void timer1_isr() interrupt 3
// {
//
// TH1 = (65536 - 1000)/256;
// TL1 = (65536 - 1000)%256;
//
// lcd_display();
// }
void main()
{
// unsigned char i;
unsigned char test[10] = {'p',0x30,':',0x30,0x30,':',0x30,0x30,'\0'};
unsigned char result[10];
// unsigned char i;
// unsigned char temp;
// timer1_init();
lcd_init();
iic_AT2402_send_str(0xae,100,test,10);
// {
//
// TH1 = (65536 - 1000)/256;
// TL1 = (65536 - 1000)%256;
//
// lcd_display();
// }
void main()
{
// unsigned char i;
unsigned char test[10] = {'p',0x30,':',0x30,0x30,':',0x30,0x30,'\0'};
unsigned char result[10];
// unsigned char i;
// unsigned char temp;
// timer1_init();
lcd_init();
iic_AT2402_send_str(0xae,100,test,10);
delay_ms(200);
iic_AT2402_recv_str(0xae,100,result,10);
iic_AT2402_recv_str(0xae,100,result,10);
delay_ms(20);
lcd_str_display(0,0,result);
while(1);
}
lcd_str_display(0,0,result);
while(1);
}