您的当前位置:首页I2C总线串行扩展

I2C总线串行扩展

来源:小侦探旅游网
计算机科学与工程系实验报告

课程名称:单片机原理与应用 实验题目:I2C总线串行扩展

班级: 姓名: 学号: 日期:

成绩:

一、实验目的

掌握单片机串行通信的设计与软件编程 二、实验要求

1、单片机利用I2C总线连接AT24C02,并在P1.7脚接有一个按键开关S以及P0口扩展了LCD1602。

2、要求设计的单片机系统能记录下按键开关S按下的次数并写入AT24C02,然后显示在LCD1602上。

3、当系统断电后,再次上电时,将在断电瞬间记录的S按下的次数的基础上继续记录按键开关S按下的次数。 三、实验要求

提交的实验报告中应包括:电路原理图、实验设计思路、C51源程序(含注释语句)、运行效果(含运行截图与说明)、实验小结 三、硬件电路原理图的设计

四、编程思路及C51源程序 编程思路:

程序在一开始先初始化AT24C02和LCD1602,然后从AT24C02中读取上次保存的按键次数。 开始死循环,循环内部扫描按键、将按键次数保存到AT24C02然后通过LCD1602显示出来。 源程序:

#include #include sbit SDA=P2^0; sbit SCL=P2^1; sbit RS=P2^4; sbit RW=P2^3; sbit E=P2^2; #define out P1 sbit k1=P2^7;

typedef unsigned int u16; typedef unsigned char u8;

char num=0; void I2cStart(); void I2cStop();

unsigned char I2cSendByte(unsigned char dat); unsigned char I2cReadByte();

void At24c02Write(unsigned char addr,unsigned char dat); unsigned char At24c02Read(unsigned char addr); void Keypros();

unsigned char table[]={0,0,0} ;

void delay2(unsigned int j) {

unsigned char i=250;

for (; j>0; j--) {

while(--i) ; }

i=249; while(--i); i=250 ;

}

void cherk_busy(void) {

unsigned char dt; do {

dt=0xff;

E=0;

RS=0; RW=1; E=1; dt=out;

}while(dt&0x80); E=0; }

void write_command(unsigned char com) {

cherk_busy();

E=0; RS=0; RW = 0; out=com; E = 1; _nop_();

E = 0; }

void write_data(unsigned char dat) {

cherk_busy();

delay2(10);

E = 0;

RS = 1; RW = 0;

out=dat;

E = 1;

_nop_();

E = 0; }

void lcd_initial(void) {

write_command(0x38); }

void string(unsigned char ad,unsigned char *s) {

write_command(ad); }

void delay(u16 i) { }

while(i--);

}

while(*s>0) {

write_data(*s++);

delay2(100); write_command(0x0C); write_command(0x06); write_command(0x01); delay2(1);

delay2(1);

void main() { unsigned int l; num=At24c02Read(1); lcd_initial(); while(1) { Keypros() ; write_command(0x8b); for(l=0;l<3;l++) {

write_data( '0'+table[l]) ; }

string(0x82,\"number\");

At24c02Write(1,num);

}

}

void Keypros() { if(k1==0) {

delay(100); if(k1==0) { num++;

table[0]=num/100 ; table[1]=num%100/10;

table[2]=num%10; } } }

void Delay10us() { }

void I2cStart() { }

void I2cStop() {

SDA=0; Delay10us(); SCL=1; SDA=1; Delay10us(); SCL=1; Delay10us(); SDA=0; Delay10us(); SCL=0;

unsigned char a,b; for(b=1;b>0;b--)

for(a=2;a>0;a--);

Delay10us();

}

Delay10us(); SDA=1; Delay10us();

unsigned char I2cSendByte(unsigned char dat) {

unsigned char a=0,b=0; for(a=0;a<8;a++) { } SDA=1; Delay10us(); SCL=1; while(SDA) {

b++; if(b>200) { }

SCL=0; Delay10us(); return 0; SDA=dat>>7; dat=dat<<1; Delay10us(); SCL=1; Delay10us(); SCL=0; Delay10us();

} SCL=0; Delay10us();

return 1; }

unsigned char I2cReadByte() { }

void At24c02Write(unsigned char addr,unsigned char dat) {

I2cStart(); I2cSendByte(0xa0); unsigned char a=0,dat=0; SDA=1;

Delay10us(); for(a=0;a<8;a++) { }

return dat;

SCL=1; Delay10us(); dat<<=1; dat|=SDA; Delay10us(); SCL=0; Delay10us();

}

I2cSendByte(addr); I2cSendByte(dat); I2cStop();

unsigned char At24c02Read(unsigned char addr) { }

unsigned char num; I2cStart();

I2cSendByte(0xa0); I2cSendByte(addr); I2cStart();

I2cSendByte(0xa1); num=I2cReadByte(); I2cStop(); return num;

五、仿真运行效果展示

单片机启动,从AT24C02中读取上次关闭时的按键次数,显示在LCD1602上。

按下按键S后次数加一并将次数并写入AT24C02,同时在LCD1602上显示。 六、实验小结

通过本次实验,我学习到了I2C总线通讯协议,使用I2C总线拓展了存储空间,在重新上电后还能读取之前的数据。

因篇幅问题不能全部显示,请点此查看更多更全内容