8051模拟IIC操作EEPROM
贴上usart.h文件的内容:
#ifndef _usart_h_
#define _usart_h_
#include
#include
extern void UART_Init(void);
extern void Send_String(unsigned char *u_string);
extern void Send_UHex(unsigned char u_tx);
#endif
#include "usart.h"
/*------------------------------------------------
串口通讯初始化
------------------------------------------------*/
void UART_Init(void)
{
SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHz
TR1 = 1; // TR1: timer 1 打开
TI = 1;
}
/*
* Send_UChar:
* 发送单个字符
*
*
*/
void Send_UChar(unsigned char u_tx)
{
SBUF = u_tx;
while(!TI);
TI = 0;
}
/*
* Send_String:
* 发送字符串
*
*
*/
void Send_String(unsigned char *u_string)
{
while(*u_string)
{
Send_UChar(*u_string);
u_string++;
}
}
void Send_UHex(unsigned char u_tx)
{
unsigned char u_high;
unsigned char u_low;
u_high = u_tx/16 ;
u_low = u_tx%16 ;
if((u_high >= 10) && (u_high <= 15))
{
u_high = (u_high - 10) + A;
}
else
{
u_high = u_high + 0;
}
if((u_low >= 10) && (u_low <= 15))
{
u_low = (u_low - 10) + A;
}
else
{
u_low = u_low + 0;
}
Send_UChar(u_high);
Send_UChar(u_low);
Send_UChar( );
}
最后就是main.c文件的内容:
/*-------------------------------------------------------------------------------
* 名称:51单片机控制EEPROM
* 晶振使用11.0592M
*
* I2C电路:
* 单片机 EEPROM
* P10--------->WP
* P11--------->SCL
* P12--------->SDA
*
* A0,A1,A2均通过硬件拉高电平
*
* copyright by wit_yuan 2016-09-15 at 龙兴园北区
-------------------------------------------------------------------------------*/
#include
#include
#include
#include "usart.h"
unsigned char g_c_continue_write[16];
unsigned char g_c_continue_read_array[16];
/*------------------------------------------------
主函数
------------------------------------------------*/
void main (void)
{
unsigned char value,i;
UART_Init();
EEPROM_Init();
Send_String("--------------------app run-----------------\n");
#if 0
EEPROM_Write(EEPROM_ADDRESS,0,0x02);
for(i = 0 ; i < 100 ; i ++);
for(i = 0 ; i < 100 ; i ++);
for(i = 0 ; i < 100 ; i ++);
value = EEPROM_Read(EEPROM_ADDRESS,0);
Send_String("\n value :");
Send_UHex(value);
Send_String("\n ");
#else
#if 1
Send_String("array :\n");
for(i = 0 ; i < 16 ; i ++)
{
g_c_continue_write[i] = i + 1;
Send_UHex(g_c_continue_write[i]);
}
Send_String("\n");
Send_String("write array : \n");
value = eeprom_continue_write(EEPROM_ADDRESS,0,g_c_continue_write,16);
Send_String("read write status : \n");
#if 1
value = read_eeprom_status(EEPROM_ADDRESS);
Send_String("\n status :");
Send_UHex(value);
Send_String("\n ");
#endif
#endif
#if 1
Send_String("read array : \n");
eeprom_continue_read(EEPROM_ADDRESS,0,g_c_continue_read_array,16);
Send_String("read:\n");
for(i = 0 ; i < 16 ; i ++)
{
Send_UHex(g_c_continue_read_array[i]);
}
Send_String("\n");
value = EEPROM_Read(EEPROM_ADDRESS,0x0D);
Send_String("\n value :");
Send_UHex(value);
Send_String("\n ");
#endif
#endif
Send_String("\n ----------app end----------------\n");
while (1) //主循环
{
}
}
更多的具体讲解,就参考stm32模拟IIC操作EEPROM那篇文章吧。
8051模拟IIC操作EEPRO 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)