找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4154|回复: 0
打印 上一主题 下一主题
收起左侧

STM8S的I2C读写AT24C02源程序,实际可用

[复制链接]
跳转到指定楼层
楼主
实际可用。


单片机源程序如下:

  1. /******************** (C) COPYRIGHT  风驰电子嵌入式开发工作室 ********************
  2. * 文件名  :main.c
  3. * 描述    :I2C EPPROM 的读写     
  4. * 实验平台:风驰电子STM8开发板
  5. * 库版本  :V2.0.0
  6. * 作者    :ling_guansheng   
  7. **********************************************************************************/

  8. /* Includes ------------------------------------------------------------------*/
  9. /* Includes ------------------------------------------------------------------*/
  10. #include "stm8s.h"
  11. #include "stm8s_clk.h"
  12. #include "intrinsics.h"
  13. #include "stm8s_uart1.h"
  14. #include "uart.h"
  15. #include "i2c_ee.h"


  16. extern u8 RxBuffer[RxBufferSize];
  17. extern u8 UART_RX_NUM;
  18. u8 Tx1_Buffer[] = "Hi!STM8";
  19. /* Private variables ---------------------------------------------------------*/

  20. #define BASE_ADDRESS ((u16)0x0000)
  21. #define countof(a) (sizeof(a) / sizeof(*(a)))
  22. #define BufferSize1             (countof(Tx1_Buffer)-1)
  23. u8 Rx1_Buffer[BufferSize1];

  24. /* Private function prototypes -----------------------------------------------*/
  25. /* Private functions ---------------------------------------------------------*/
  26. void Delay(u32 nCount);

  27. int main(void)
  28. {
  29.     /* Infinite loop */
  30.   /*设置内部时钟16M为主时钟*/
  31.     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  32.   /*!<Set High speed internal clock  */
  33.     I2C_DeInit();
  34.     Uart_Init();
  35.     /* Initialize the I2C */
  36.     I2C_EEInit();
  37.     I2C_EE_PageWrite(Tx1_Buffer, BASE_ADDRESS, BufferSize1 );
  38.     Delay(2000); /* To let eeprom the time to finish the write operation */
  39.     I2C_EE_BufferRead(Rx1_Buffer, BASE_ADDRESS, BufferSize1 );
  40.     UART1_SendString("What is writed into a page of AT24C02 EEPROM:",\
  41.                      sizeof("What is writed into a page of AT24C02 EEPROM:"));
  42.     Delay(0xfff);
  43.     UART1_SendString(Tx1_Buffer,BufferSize1);
  44.     Delay(0xffff);
  45.     UART1_SendString("What is read from a page of AT24C02 EEPROM:",\
  46.            sizeof("What is read from a page of AT24C02 EEPROM:"));
  47.     Delay(0xffff);
  48.     UART1_SendString(Rx1_Buffer,BufferSize1);
  49.     Delay(0xffff);
  50.    while (1)
  51.    {

  52.       UART1_SendString("What is read from a page of AT24C02 EEPROM:",\
  53.              sizeof("What is read from a page of AT24C02 EEPROM:"));
  54.       Delay(0xffff);
  55.       UART1_SendString(Rx1_Buffer,BufferSize1);
  56.       Delay(0xffff);

  57.    }
  58. }

  59. void Delay(u32 nCount)
  60. {
  61.   /* Decrement nCount value */
  62.   while (nCount != 0)
  63.   {
  64.     nCount--;
  65.   }
  66. }



  67. #ifdef USE_FULL_ASSERT

  68. /**
  69.   * @brief  Reports the name of the source file and the source line number
  70.   *   where the assert_param error has occurred.
  71.   * @param file: pointer to the source file name
  72.   * @param line: assert_param error line source number
  73.   * @retval : None
  74.   */
  75. void assert_failed(u8* file, u32 line)
  76. ……………………

  77. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
I2C总线+AT24C02.rar (256.13 KB, 下载次数: 81)




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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