随机读取数据1,通过数码管显示
随机读取的数据最小为1,最大为255
eeprom.h
#ifndef __EEPROM_H
#define __EEPROM_H
/*
PB6 SCL
PB7 SDA
*/
#include "stm32f10x_conf.h"
extern void eeprom_init(void);
extern void eeprom_byte_write(u8 dev_addr,u8 data_addr,u8 data);
extern void eeprom_page_write(u8 dev_addr,u8 data_addr,u8 *buf,u8 size);
extern u8 eeprom_cur_read(u8 dev_addr);
extern u8 eeprom_rand_read(u8 dev_addr,u8 data_addr);
extern void eeprom_sequ_read(u8 dev_addr,u8 data_addr,u8 *buf,u8 size);
#endif
eeprom.c
#include "eeprom.h"
#include "iic.h"
void eeprom_init(void)
{
IIC_Init ();
}
void eeprom_byte_write(u8 dev_addr,u8 data_addr,u8 data)
{
IIC_Start ();
IIC_Send_Byte (dev_addr);
IIC_Wait_ACK ();
IIC_Send_Byte (data_addr);
IIC_Wait_ACK ();
IIC_Send_Byte (data);
IIC_Wait_ACK ();
IIC_Stop ();
}
void eeprom_page_write(u8 dev_addr,u8 data_addr,u8 *buf,u8 size)
{
u8 i =0;
IIC_Start ();
IIC_Send_Byte (dev_addr);
IIC_Wait_ACK ();
IIC_Send_Byte (data_addr);
IIC_Wait_ACK ();
for(i = 0;i < size ;i++)
{
IIC_Send_Byte (buf[i]);
IIC_Wait_ACK ();
}
IIC_Stop ();
}
u8 eeprom_cur_read(u8 dev_addr)
{
u8 data = 0;
IIC_Start ();
IIC_Send_Byte (dev_addr|0x01);
IIC_Wait_ACK ();
data = IIC_Recv_Byte (0);
IIC_Stop ();
return data;
}
u8 eeprom_rand_read(u8 dev_addr,u8 data_addr)
{
u8 data = 0;
IIC_Start ();
IIC_Send_Byte (dev_addr);
IIC_Wait_ACK ();
IIC_Send_Byte (data_addr);
IIC_Wait_ACK ();
IIC_Start ();
IIC_Send_Byte (dev_addr|0x01);
IIC_Wait_ACK ();
data = IIC_Recv_Byte (0);
IIC_Stop ();
return data;
}
void eeprom_sequ_read(u8 dev_addr,u8 data_addr,u8 *buf,u8 size)
{
u8 i =0;
u8 data = 0;
IIC_Start ();
IIC_Send_Byte (dev_addr);
IIC_Wait_ACK ();
IIC_Send_Byte (data_addr);
IIC_Wait_ACK ();
IIC_Start ();
IIC_Send_Byte (dev_addr|0x01);
IIC_Wait_ACK ();
for(i = 0;i < size ;i++)
{
if(i ==size-1)
buf[i] = IIC_Recv_Byte (0);
else
buf[i] = IIC_Recv_Byte (1);
}
IIC_Stop ();
}
main.c
#include"iwdg.h"
#include"led.h"
#include"fmq.h"
#include"key.h"
#include"delay.h"
#include"inte.h"
#include"hc138.h"
#include"dht11.h"
#include"iic.h"
#include"eeprom.h"
void h0(void)
{
Led_On(0);
Led_On(1);
Led_On(2);
}
void h1(void)
{
Fmq_On();
}
void h2(void)
{
Led_Off(0);
Fmq_Off();
}
int main(void)
{
char buff[5]={0};
int dht_data=0;
int i=0,j=0;
int ep=0;
inte_init();
Led_Init();
Key_Init();
Fmq_Init();
delay_init();
hc138_init();
dht11_init();
eeprom_init();
eeprom_byte_write(0xA0,0x00,254);//写入
//EEP_Page_Write(0xA0,0x00,arr,1);
set_inte_handler(h0,h1,h2);
while(1)
{
ep=eeprom_rand_read(0xA0,0x00);
//EEP_Page_Write(0xA0,0x00,arr,1);
show_digital_Out(ep);
}
return 0;
}