设为首页收藏本站淘宝杂货铺

从F到0 - From F to 0

 找回密码
 注册已关闭
搜索
查看: 1890|回复: 0
收起左侧

STC-Y3系列单片机 AT24C02读写程序源码+软串口发送

[复制链接]
发表于 2020-7-19 22:22:04 | 显示全部楼层 |阅读模式
本帖最后由 哒哒哒 于 2020-7-19 22:22 编辑

AT24C02.jpg
  1. /*
  2. 单片机型号:STC15F104E AT24C02读写程序 晶振频率:11.0592Mhz 波特率:9600
  3. */
  4. #include "reg51.h"
  5. #include "intrins.h"
  6. #include "stdlib.h"
  7. #define U8 unsigned char
  8. #define U16 unsigned int

  9. sbit txd=P3^1;

  10. sbit SDA = P3 ^ 2;
  11. sbit SCL = P3 ^ 3;

  12. //软串口驱动程序开始
  13. void delay_uart(){
  14.         U8 data a,b;
  15.                 for(b=3;b>0;b--)
  16.                 for(a=94;a>0;a--);
  17. }

  18. void txd_data(U8 i){        //软串口发送字节
  19.         U8 j=1;
  20.         delay_uart();
  21.         txd=0;
  22.                 do {
  23.                         delay_uart();
  24.                         txd= i&j;
  25.                         j<<=1;
  26.                 } while(j);
  27.         delay_uart();
  28.         txd=1;  
  29. }

  30. void txd_hex(U8 i) {        //软串口发送16进制字节(大写)
  31.         txd_data(((i >> 4) < 10 ? 48 : 55) + (i >> 4));
  32.         txd_data(((i & 15) < 10 ? 48 : 55) + (i & 0xF));

  33. }

  34. void txd_text(U8* text) {        //软串口发送字符串
  35.         for (; *text != 0; text++) {
  36.                 txd_data(*text);
  37.         }
  38. }

  39. //软串口驱动程序结束 I2C 驱动程序开始

  40. void I2C_Delay() {
  41.         U8 n=4;
  42.         while (n--) {
  43.                 _nop_();
  44.                 _nop_();
  45.         }
  46. }

  47. void I2C_Start() {
  48.         SDA = 1;
  49.         SCL = 1; I2C_Delay();
  50.         SDA = 0; I2C_Delay();
  51.         SCL = 0; I2C_Delay();

  52. }

  53. void I2C_Stop() {
  54.         SDA = 0;
  55.         SCL = 1; I2C_Delay();
  56.         SDA = 1; I2C_Delay();
  57.         SCL = 0; I2C_Delay();

  58. }

  59. void I2C_ACK() {
  60.         U8 i=0;
  61.         SCL = 1;I2C_Delay();
  62.         while (SDA && ++i);
  63.         SCL = 0;I2C_Delay();
  64. }

  65. void I2C_Write(U8 Byte) {
  66.         U8 i = 128;
  67.         do {
  68.                 SCL = 0; I2C_Delay();
  69.                 SDA = Byte & i; I2C_Delay();
  70.                 SCL = 1; I2C_Delay();
  71.                 i >>= 1;
  72.         } while (i);
  73.         SCL = 0;I2C_Delay();
  74.         SDA = 1;I2C_Delay();
  75.         I2C_ACK();
  76. }

  77. U8 I2C_Read() {
  78.         U8 i = 128,Byte=0;
  79.         SCL = 0; I2C_Delay();
  80.         SDA = 1; I2C_Delay();
  81.         do {
  82.                 SCL = 1; I2C_Delay();
  83.                 if (SDA) Byte |= i;
  84.                 SCL = 0; I2C_Delay();
  85.                 i >>= 1;
  86.         } while (i);
  87.         return Byte;
  88. }
  89. //I2C 驱动程序结束 24C02驱动程序开始



  90. U8 AT24C02_Read(U8 Addr) {                                //AT24C02 读字节
  91.         U8 Byte;
  92.         I2C_Start();
  93.         I2C_Write(0xA0);
  94.         I2C_Write(Addr);
  95.         I2C_Start();
  96.         I2C_Write(0xA1);        
  97.         Byte = I2C_Read();
  98.         I2C_Stop();
  99.         return Byte;
  100. }

  101. void AT24C02_Warning(U8 n) {        //写入n:(2~7)次成功 (可能出现坏道或写入时间过短,考虑更换芯片)
  102.         txd_text("AT24C02 Warning:0x");
  103.         txd_hex(n);
  104.         txd_text("\r\n");
  105. }


  106. void AT24C02_Error() {                //重复写入8次失败,找不到芯片或已损坏或写保护
  107.         txd_text("AT24C02 Error!\r\n");
  108.         while (1);
  109. }

  110. void AT24C02_Write(U8 Addr,U8 Byte) {                //AT24C02 写字节
  111.         U8 e = 0;
  112.         U16 d;
  113.         do {
  114.                 I2C_Start();
  115.                 I2C_Write(0xA0);
  116.                 I2C_Write(Addr);
  117.                 I2C_Write(Byte);
  118.                 I2C_Stop();
  119.                 d = 0xFFF;                //增加晶振频率或多次写入失败请加大此值
  120.                 while (--d);        //此处延时约5~6毫秒
  121.         } while (++e < 8 && AT24C02_Read(Addr) != Byte);
  122.         switch (e) {
  123.                 case 1:break;
  124.                 case 8:AT24C02_Error(); break;
  125.                 default:AT24C02_Warning(e); break;        
  126.         }
  127. }


  128. void AT24C02_Fill_Rand() {                //AT24C02 填充随机
  129.         U8 pc = 0;
  130.         do {
  131.                 AT24C02_Write(pc, rand());
  132.         } while (++pc);
  133. }

  134. void AT24C02_Send_Data() { //AT24C02 串口发送16进制数据
  135.         U8 pc = 0;
  136.         do {
  137.                
  138.                 txd_hex(AT24C02_Read(pc));
  139.                         txd_data(0x20);
  140.                 if ((pc & 15) == 15) {
  141.                         txd_text("\r\n");
  142.                 }
  143.         } while (++pc);
  144. }
  145. //24C02驱动程序结束

  146. void main(){        //上电先发送AT24C02原有的数据,然后写入256字节随机数据,再发送写入后的数据
  147.         txd_text("AT24C02 Data:\r\n");
  148.         AT24C02_Send_Data();
  149.         srand((int)(AT24C02_Read(0)<<8) | AT24C02_Read(1));
  150.         txd_text("AT24C02 Fill Rand Data...\r\n");
  151.         AT24C02_Fill_Rand();
  152.         txd_text("AT24C02 Data:\r\n");
  153.         AT24C02_Send_Data();
  154.         txd_text("Please Reset Chip...\r\n");
  155.         while (1);
  156. }
复制代码

相关帖子

您需要登录后才可以回帖 登录 | 注册已关闭

本版积分规则

QQ|手机版|Archiver|从F到0 ( 蒙ICP备17002595号-1 )
蒙公网安备15010402000325号

腾讯云安全认证

GMT+8, 2024-3-29 23:16 , Processed in 0.487028 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表