请编程实现
时间: 2024-02-13 09:07:20 浏览: 15
好的,下面是51单片机的代码实现,实现将PC端串口发送的字符串储存在AT24C02芯片中,并可以通过串口读取存储在AT24C02中的字符串。
```c
#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#define FOSC 11059200L
#define BAUD 9600
#define SDA P1_0
#define SCL P1_1
sbit LED = P2^0;
unsigned char code write_address = 0xA0; // AT24C02的写地址
unsigned char code read_address = 0xA1; // AT24C02的读地址
bit busy; // 串口发送标志位
unsigned char buffer[128]; // 串口接收缓冲区
unsigned char idx = 0; // 缓冲区索引
void delay_us(unsigned int n) // 延时函数
{
while(n--)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void delay_ms(unsigned int n) // 延时函数
{
while(n--)
{
delay_us(1000);
}
}
void init_uart() // 串口初始化
{
TMOD |= 0x20;
SCON = 0x50;
TH1 = (unsigned char)(256 - FOSC/32/BAUD);
TL1 = (unsigned char)(256 - FOSC/32/BAUD);
ES = 1;
EA = 1;
TR1 = 1;
}
void uart_send(unsigned char byte) // 串口发送函数
{
while(busy);
busy = 1;
SBUF = byte;
}
void uart_puts(unsigned char *str) // 串口发送字符串函数
{
while(*str)
{
uart_send(*str++);
}
}
void i2c_start() // I2C总线起始信号
{
SDA = 1;
SCL = 1;
delay_us(5);
SDA = 0;
delay_us(5);
SCL = 0;
}
void i2c_stop() // I2C总线停止信号
{
SDA = 0;
SCL = 1;
delay_us(5);
SDA = 1;
delay_us(5);
}
bit i2c_write_ack() // I2C总线写应答信号
{
bit ack;
SDA = 1;
SCL = 1;
delay_us(5);
ack = SDA;
SCL = 0;
delay_us(5);
return !ack;
}
void i2c_write_byte(unsigned char byte) // I2C总线写数据函数
{
unsigned char i;
for(i = 0; i < 8; i++)
{
SDA = (byte & 0x80) ? 1 : 0;
byte <<= 1;
SCL = 1;
delay_us(5);
SCL = 0;
delay_us(5);
}
}
unsigned char i2c_read_byte(bit ack) // I2C总线读数据函数
{
unsigned char byte = 0;
unsigned char i;
for(i = 0; i < 8; i++)
{
byte <<= 1;
SCL = 1;
delay_us(5);
byte |= SDA;
SCL = 0;
delay_us(5);
}
SDA = ack ? 0 : 1;
SCL = 1;
delay_us(5);
SCL = 0;
delay_us(5);
return byte;
}
void i2c_write(unsigned char address, unsigned char *data, unsigned char length) // I2C总线写函数
{
unsigned char i;
i2c_start();
i2c_write_byte(address);
if(!i2c_write_ack())
{
i2c_stop();
return;
}
for(i = 0; i < length; i++)
{
i2c_write_byte(data[i]);
if(!i2c_write_ack())
{
i2c_stop();
return;
}
}
i2c_stop();
}
void i2c_read(unsigned char address, unsigned char *data, unsigned char length) // I2C总线读函数
{
unsigned char i;
i2c_start();
i2c_write_byte(address);
if(!i2c_write_ack())
{
i2c_stop();
return;
}
i2c_start();
i2c_write_byte(address + 1);
if(!i2c_write_ack())
{
i2c_stop();
return;
}
for(i = 0; i < length; i++)
{
data[i] = i2c_read_byte(i == length - 1);
}
i2c_stop();
}
void main()
{
unsigned char data[128];
unsigned char length;
init_uart();
while(1)
{
if(idx > 0 && buffer[idx - 1] == '\n') // 接收到完整的字符串
{
idx--;
length = idx;
idx = 0;
i2c_write(write_address, buffer, length); // 将字符串写入AT24C02中
uart_puts("Data written to AT24C02: ");
uart_puts(buffer);
}
i2c_read(read_address, data, sizeof(data)); // 从AT24C02中读取字符串
if(data[0] != 0xFF)
{
uart_puts("Data read from AT24C02: ");
uart_puts(data);
i2c_write(write_address, "\xFF", 1); // 清空AT24C02中的字符串
}
}
}
void uart_isr() interrupt 4 using 1 // 串口中断服务函数
{
if(RI)
{
RI = 0;
buffer[idx++] = SBUF;
}
if(TI)
{
TI = 0;
busy = 0;
}
}
```
注意,在使用本程序前需要将AT24C02芯片连接到51单片机的I2C总线上,并根据具体的硬件电路和芯片规格进行配置。另外,本程序使用9600波特率的串口通信,如果需要修改波特率可以修改BAUD宏定义。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)