1. 程式人生 > >EEPROM—_IIC匯流排程式設計,用按鍵控制IIC寫入讀取資料並由數碼管顯示

EEPROM—_IIC匯流排程式設計,用按鍵控制IIC寫入讀取資料並由數碼管顯示

關於IIC序列匯流排的組成及工作原理
1.採用序列匯流排技術可以使系統的硬體設計大大簡化,系統的體積減小,可靠性提高,同時,系統的更改和擴充極為容易。
2.常用的序列擴充套件匯流排有:IIC(Inter IC BUS)匯流排、單匯流排,SPI匯流排及Microwire、PLUS
3.IIC是一種序列匯流排,只有兩根雙向訊號線:
一根是資料線SDA
一根是時鐘線SCL
4.標準的IIC匯流排的資料傳送有嚴格的時序要求
5.每個接到IIC總線上的器件都有唯一的地址

關於移位操作:
左移時最低位補0,最高位移入psw的cy位;
右移時最高位保持原數,最低位移除;

關於AT24Cxx系列的晶片
01、02、03、04、05晶片最大儲存位元組分別為128、256、512、1024、2048
A1、A2、A3為地址輸入口;
SDA:序列地址和資料輸入/輸出口;
SCL:序列時鐘輸入,上升沿資料寫入;
下降沿資料讀出
wp:防寫 wp = 0,允許資料正常讀寫操作;
wp = 1,防寫,只讀

k1儲存資料
K2讀取上次資料
K3資料加1
K4清零

i2c.h

#ifndef _I2C_H
#define _I2C_H

#include"reg52.h"

//typedef unsigned char uchar;

sbit SDA = P2^0;
sbit SCL = P2^1;

void At24c02Write(uchar addr,uchar dat);
uchar At24c02Read(uchar addr);

#endif


i2c.c

#include"i2c.h"

void Delay10us()
{
    uchar i,j;
    for(i=1
;i>0;i--) for(j=2;j>0;j--); } void I2cStart() { SDA = 1; Delay10us(); SCL = 1; Delay10us(); SDA = 0; Delay10us(); SCL = 0; Delay10us(); } void I2cStop() { SDA = 0; Delay10us(); SCL = 1; Delay10us(); SDA = 1; Delay10us(); } uchar I2cSendByte(uchar dat) { uchar a = 0,b = 0; for(a=0;a<8;a++){ SDA = dat>>7; dat = dat<<1; Delay10us(); SCL = 1; Delay10us(); SCL = 0; Delay10us(); } SDA = 1; Delay10us(); SCL = 1; Delay10us(); while(SDA){ b++; if(b == 300){ SCL = 0; Delay10us(); return 0; } } SCL = 0; Delay10us(); return 1; } uchar I2cReadByte() { uchar a=0,dat=0; SDA = 1; Delay10us(); for(a=0;a<8;a++){ SCL = 1; Delay10us(); dat = dat << 1; dat |= SDA; Delay10us(); SCL = 0; Delay10us(); } return dat; } void At24c02Write(uchar addr,uchar dat) { I2cStart(); I2cSendByte(0xa0); I2cSendByte(addr); I2cSendByte(dat); T2cStop(); } uchar At24c02Read(uchar addr) { uchar num; I2cStart(); I2cSendByte(0xa0); I2cSendByte(addr); I2cStart(); I2cSendByte(0xa1); num = I2cReadByte(); I2cStop(); return num; } mian.c #include "i2c.h" typedef unsigned int u16; typedef unsigned char u8; sbit LSA = P2^2; sbit LSB = P2^3; sbit LSC = P2^4; sbit key1 = P3^0; sbit key2 = P3^1; sbit key3 = P3^2; sbit key4 = P3^3; u8 code sngduan[10]={ }; u8 num =0; u8 spit[4] = {0}; void delay(u16 i) { while(i--); } void keypors() { if(key1 ==0){ delay(1000); if(key1 == 0){ IAt24c02Write(1,num); } while(!key1); } if(key2 ==0){ delay(1000); if(key2 == 0){ num = IAt24c02Read(1); } while(!key2); } if(key3 == 0){ delay(1000); if(key3 == 0){ num++; if(num>255){ num = 0; } } while(!key3); } if(key4 ==0){ delay(1000); if(key4 == 0){ num = 0; } while(!key4); } } void datapors() { spit[0] = sumduan[sum%10]; spit[1] = sumduan[sum%100/10]; spit[2] = sumduan[sum%1000/100]; spit[3] = sumduan[sum/1000]; } void display() { u8 i; for(i=0;i<4;i++){ switch(i){ case 0: LSA = 0;LSB = 0;LSC = 0;break; case 1: LSA = 1;LSB = 0;LSC = 0;break; case 2: LSA = 0;LSB = 1;LSC = 0;break; case 3: LSA = 1;LSB = 1;LSC = 0;break; } P0 = spit[i]; delay(100); P0 = 0x00; } } void main() { while(1){ kerpros(); datapros(); display(); } }
  • 加粗 Ctrl + B
  • 斜體 Ctrl + I
  • 引用 Ctrl + Q
  • 插入連結 Ctrl + L
  • 插入程式碼 Ctrl + K
  • 插入圖片 Ctrl + G
  • 提升標題 Ctrl + H
  • 有序列表 Ctrl + O
  • 無序列表 Ctrl + U
  • 橫線 Ctrl + R
  • 撤銷 Ctrl + Z
  • 重做 Ctrl + Y