【STM8-SO8-DISCO】——10、串口打印EEPROM数据
本贴在前贴的基础上,添加了串口打印EEPROM数据。
前贴链接:
【STM8-SO8-DISCO】——9、字节方式读写EEPROM
https://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=619717&fromuid=307297
串口打印功能请参考:
【STM8-SO8-DISCO】——8、测试 UART1_Printf
https://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=619620&fromuid=307297
主函数main()实现。
- void main(void)
- {
- uint8_t i = 0;
- uint8_t dir = 0;
- STARTUP_SWIM_DELAY_5S;
- /* Clock configuration -----------------------------------------*/
- CLK_Config();
- /* GPIO Configuration ------------------------------------------*/
- GPIO_Config();
- /* FLASH Configuration ------------------------------------------*/
- FLASH_Config();
-
- UART1_DeInit();
- /* UART1 configuration ------------------------------------------------------*/
- /* UART1 configured as follow:
- - BaudRate = 115200 baud
- - Word Length = 8 Bits
- - One Stop Bit
- - No parity
- - Receive and transmit enabled
- - UART1 Clock disabled
- */
- UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
- UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
- /* Output a message on Hyperterminal using printf function */
- printf("\n\rUART1 Example :retarget the C library printf()/getchar() functions to the UART\n\r");
- printf("EEPROM Test!\n\r");
-
- /* ********************************************************************* */
- printf("Read from EEPROM Start:\n\r");
- /* Fill the buffer with EEPROM Data */
- for (i=0;i<128;i++)
- {
- /* Read byte at a specified address i */
- GBuffer[i] = FLASH_ReadByte(EEPROM_BASE_ADDR+i);
- }
-
- if (GBuffer[0] == 0x00)
- dir = 1;
- else
- dir = 0;
-
- for (i = 0; i < 128; i++)
- {
- printf("0x%02x,", GBuffer[i] );
- if ((i+1)%16 == 0)
- printf("\n\r");
- }
- printf("Reading End!\n\r");
-
- /* ********************************************************************* */
- printf("Write EEPROM Start:\n\r");
- for (i=0;i<128;i++)
- {
- if (dir == 0)
- /* Write byte at a specified address i */
- FLASH_ProgramByte((EEPROM_BASE_ADDR+i),i);
- else
- /* Write byte at a specified address i */
- FLASH_ProgramByte((EEPROM_BASE_ADDR+i),i+128);
- }
-
- /* Fill the buffer with EEPROM Data */
- for (i=0;i<128;i++)
- {
- /* Read byte at a specified address i */
- GBuffer[i] = FLASH_ReadByte(EEPROM_BASE_ADDR+i);
- }
-
- for (i = 0; i < 128; i++)
- {
- printf("0x%02x,", GBuffer[i] );
- if ((i+1)%16 == 0)
- printf("\n\r");
- }
- printf("Writting End!\n\r");
-
- /* ********************************************************************* */
-
- while (1)
- {
- GPIO_WriteReverse(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS);
- Delay(0xFFFF);
- }
- }
复制代码
程序功能与前贴相似,只不过原来由打断点观察内存及变量,改为了从上位机串口助手观察。
实验连线如下图
第一次上电,串口打印内容。
第二次上电,串口打印内容。
首先读取全部128字节EEPROM中的数据,打印到串口;然后修改EEPROM内容,再次从EEPROM读取全部数据,并打印。
程序中判断EEPROM中第一个字节是否等于0x00,如果等于0x00,则往EEPROM中写入内容0x80到0xFF;如果不等于0x00,则往EEPROM写入0x00到0x7F。每次上电,EEPROM中内容都会修改一次。
至此,通过串口打印EEPROM内容实验完成。
本贴测试工程附上:
STM8S_StdPeriph_Flash_EEPROM_Printf.rar
(268.3 KB, 下载次数: 26)
|