[活动专区] 【AT-START-F407测评】+ AT24C04

[复制链接]
775|0
 楼主| 比神乐 发表于 2021-1-30 10:49 | 显示全部楼层 |阅读模式
今天捣鼓了一下AT24C04
硬件I2C查询方式。
SCL:PB6
SDA:PB7
代码:
  1. #include "at32f4xx.h"
  2. #include "at32f4xx_i2c_ex.h"
  3. #include "stdio.h"       
  4. #include "string.h"       
  5. #include "at32_board.h"

  6. /** @addtogroup AT32F407_StdPeriph_Examples
  7.   * @{
  8.   */

  9. /** @addtogroup EEPROM_Poll
  10.   * @{
  11.   */

  12. /* Private typedef -----------------------------------------------------------*/
  13. /* Private define ------------------------------------------------------------*/
  14. /* Private macro -------------------------------------------------------------*/
  15. #define BUF_SIZE      12


  16. /* Private variables ---------------------------------------------------------*/
  17. u8 write_buf1[BUF_SIZE] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12};
  18. u8 write_buf2[BUF_SIZE] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0};
  19. u8 read_buf[BUF_SIZE] = {0};

  20. /* Private function prototypes -----------------------------------------------*/
  21. /* Private functions ---------------------------------------------------------*/

  22. /**
  23.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Main program
  24.   * @param  None
  25.   * @retval None
  26.   */
  27. int main(void)
  28. {
  29.   int i = 0, Flag = 0;

  30.   AT32_Board_Init();
  31.   
  32.   UART_Print_Init(115200);

  33.   I2Cx_Init(I2C_PORT);
  34.   
  35.   printf("I2C EEPROM demo(Polling).\r\n");               

  36.   while(1)
  37.   {
  38.     /* Wait for Key BUTTON_KEY1_DOWN press before starting write */
  39.     if(AT32_BUTTON_Press() == BUTTON_WAKEUP)
  40.     {
  41.       switch(Flag)
  42.       {
  43.         /* Write buffer 1*/
  44.         case 0:
  45.         {
  46.           I2C_EE_WriteBuffer(I2C_PORT, 0, write_buf1, BUF_SIZE, 1000);
  47.          
  48.           printf("Write buffer 1!\r\n");
  49.         }break;
  50.         
  51.         /* Read buffer 1*/
  52.         case 1:
  53.         {
  54.           I2C_EE_ReadBuffer(I2C_PORT, 0, read_buf, BUF_SIZE, 1000);
  55.         
  56.           for(i = 0; i < BUF_SIZE; i++)
  57.           {      
  58.             printf("%X ",read_buf[i]);
  59.           }
  60.          
  61.           printf("\r\n");
  62.         }break;
  63.         
  64.         /* Write buffer 2*/         
  65.         case 2:
  66.         {
  67.           I2C_EE_WriteBuffer(I2C_PORT, 0, write_buf2, BUF_SIZE, 1000);
  68.          
  69.           printf("Write buffer 2!\r\n");
  70.         }break;
  71.         
  72.         /* Read buffer 2*/
  73.         case 3:
  74.         {
  75.           I2C_EE_ReadBuffer(I2C_PORT, 0, read_buf, BUF_SIZE, 1000);
  76.         
  77.           for(i = 0; i < BUF_SIZE; i++)
  78.           {      
  79.             printf("%X ",read_buf[i]);
  80.           }
  81.          
  82.           printf("\r\n");
  83.         }break;  
  84.         
  85.         default:
  86.         {
  87.           Flag = 0;
  88.         }break;        
  89.       }
  90.       
  91.       Flag++;
  92.       
  93.       if(Flag == 4)
  94.       {
  95.         Flag = 0;
  96.       }
  97.     }
  98.   }   
  99. }

  100. /**
  101.   * @}
  102.   */

  103. /**
  104.   * @}
  105.   */
  106.   
效果图:
1.jpg
4.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部