EEPROM写入一数组并显示【51单片机】

** EEPROM写入有个时间TWR要求,一般是5ms,如果写的间隔过小可能写不进去。为留有一定余量,最好设成10ms**
以后要好好看数据手册啊,这个点卡了一下午…

IIC.h

#ifndef _IIC_H
#define _IIC_H

#include 
#include 

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

sbit LE = P1^0;//
sbit a = P2^2;
sbit b = P2^3;
sbit c = P2^4;
void delay1s(void);
void delay5us(void);
void delay1ms(void);
void delay10ms(void);   //误差 0us

void AT24C02Write(unsigned char addr,unsigned char dat);
unsigned char AT24C02Read(unsigned char addr);
#endif

main.c

#include "iic.h"
#include 

unsigned char code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
void display(unsigned char i,unsigned char num)
{
	switch(i)
	{
		//R->L
		case 1:
			a = 1;b = 1;c = 1;	break;
		case 2:
			a = 0;b = 1;	c = 1;break;
		case 3:
			a = 1;b = 0;c = 1;	break;
		case 4:
			a = 0;b = 0;c = 1;	break;
		case 5:
			a = 1;	b = 1;c = 0;break;
		case 6:
			a = 0;b = 1;c = 0;break;
		case 7:
			a = 1;b = 0;c = 0;break;
		case 8:
			a = 0;b = 0;	c = 0;			break;
		default:break;
	}
	P0 = table[num];
}

void main()
{
	unsigned char dat,flag=0,i=0,j=0,num[6] = {9,5,0,8,2,3},key[6]={0,0,0,0,0,0};
	
	//write 
	for(i = 0;i < 6;++i){
		AT24C02Write(i,num[i]);
		delay10ms();
	}
	
	//read 
	for(i = 0;i < 6;++i){
		dat = AT24C02Read(i);
		key[i]=dat;
	
	}
	
	//show 
	while(1){	
		display(j+1,key[j]);
		++j;
		delay1ms();
		if(j == 6){
			j = 0;
		   	P0 = 1;
			delay1ms();
		}
		
	}
}

IIC.c

#include "iic.h"
void delay1s(void)   //误差 0us
{
    unsigned char a,b,c;
    for(c=167;c>0;c--)
        for(b=171;b>0;b--)
            for(a=16;a>0;a--);
    _nop_();  //if Keil,require use intrins.h
}
void delay5us(void)   //误差 0us
{
    unsigned char a;
    for(a=1;a>0;a--);
}
void delay1ms(void)   //误差 0us
{
    unsigned char a,b,c;
    for(c=1;c>0;c--)
        for(b=142;b>0;b--)
            for(a=2;a>0;a--);
}
void delay10ms(void)   //误差 0us
{
    unsigned char a,b,c;
    for(c=1;c>0;c--)
        for(b=38;b>0;b--)
            for(a=130;a>0;a--);
}
void I2C_Init()
{
	SCL = 1;
	_nop_();
	SDA = 1;
	_nop_();
}
void I2C_Start()
{
	SDA = 1;
	_nop_();
	SCL = 1;
	delay5us();
	SDA = 0;
	delay5us();
	SCL = 0;
	delay5us();
}
void I2C_Stop()
{
	SDA = 0;
	_nop_();
	SCL = 1;
	delay5us();
	SDA = 1;
	delay5us();
}
unsigned char I2C_SentByte(unsigned char dat)
{
	unsigned char i=0,j=0;
	for(i = 0;i < 8;i++)
	{
		SCL = 0;
		delay5us();
		SDA = dat >> 7;
		dat = dat<<1;
		delay5us();
		SCL = 1;
		delay5us();
	}
	SCL = 0;
	SDA = 1;
	_nop_();
	SCL = 1;
	_nop_();
	while(SDA)
	{
		++j;
		if(j > 200)
		{
			SCL = 0;
			_nop_();
			I2C_Stop();
			return 0;
		}
	}
	SCL = 0;
	delay5us();
	return 1;
}
unsigned char I2C_ReadByte()
{
	unsigned char dat = 0,i = 0;
	SCL = 0;
	delay5us();
	SDA = 1;
	delay5us();
	for(i = 0; i < 8;++i)
	{
		SCL = 1;
		delay5us();
		dat <<= 1;
		dat |= SDA;
		delay5us();
		SCL = 0;
		delay5us();
	}
	return dat;
}

void AT24C02Write(unsigned char addr,unsigned char dat)
{
	I2C_Init();
	I2C_Start();
	I2C_SentByte(0xa0);
	I2C_SentByte(addr);
	I2C_SentByte(dat);
	I2C_Stop();
}

unsigned char AT24C02Read(unsigned char addr)
{
	unsigned char dat;
	I2C_Init();
	I2C_Start();
	I2C_SentByte(0xa0);
	I2C_SentByte(addr);
	I2C_Start();
	I2C_SentByte(0xa1);
	dat = I2C_ReadByte();
	I2C_Stop();
	return dat;
}

你可能感兴趣的:(STM32单片机开发)