STM8-SO8-DISCO】+ EEPROM读写
【STM8-SO8-DISCO】+ EXIT中断中做按键去点灯帖子后 继续玩EEPROM
EEPROM是地址0X4000开始的128字节
1,工程以上面的。
2,EEPROM 写程序- void write_eeprom(uint32_t addr,uint8_t *pDat,uint8_t num)
- {
- uint8_t i;
- /* Define FLASH programming time */
- FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);
- /* Unlock Data memory */
- FLASH_Unlock(FLASH_MEMTYPE_DATA);
- for(i=0;i<num;i++)
- FLASH_ProgramByte(addr++, pDat);
- FLASH_Lock(FLASH_MEMTYPE_DATA);
- }
[color=rgb(51, 102, 153) !important]复制代码
3,EEPROM 读程序
- void read_eeprom(uint32_t addr,uint8_t *pDat,uint8_t num)
- {
- uint8_t i;
- //eeprom_unlock();
- for(i=0;i<num;i++)
- pDat = FLASH_ReadByte(addr++);
- }
[color=rgb(51, 102, 153) !important]复制代码
4,把TIM4关掉(不知道为啥要这样)
5,主程序调用
- #include "stm8s.h"
- #define LED_GPIO_PORT (GPIOA)
- #define LED_GPIO_PINS (GPIO_PIN_3)
- #define USER_BUTTON_GPIO_PORT (GPIOC)
- #define USER_BUTTON_GPIO_PINS (GPIO_PIN_3) //这里可以设置为PC3,PC4,PC5中任何一个。
- #define TIM4_PERIOD 124
- #define EEPROM_BEGIN 0x4000
- void Delay(uint16_t nCount);
- static void TIM4_Config(void);
- void LED1_Toggle(void);
- void Exit_Init();
- void eeprom_unlock(void);
- void write_eeprom(uint32_t addr,uint8_t *pDat,uint8_t num);
- void read_eeprom(uint32_t addr,uint8_t *pDat,uint8_t num);
- uint32_t addr= EEPROM_BEGIN;
- uint8_t test1[8],test2[8];
- void main(void)
- {
- /* Initialize I/Os in Output Mode */
- GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
- /* Initialize I/O in Input Mode with Interrupt */
- Exit_Init();
- /* TIM4 configuration -----------------------------------------*/
- //TIM4_Config();
- while (1)
- {
- uint8_t j;
- for(j=0;j<8;j++)
- test1[j] =10+j;
- write_eeprom(EEPROM_BEGIN,&test1[0],8);
- Delay(10);
- read_eeprom(EEPROM_BEGIN,&test2[0],8);
- Delay(10);
- }
[color=rgb(51, 102, 153) !important]复制代码
6,结果:
本文转载于STM8-SO8-DISCO】+ (3)EEPROM读写
http://www.stmcu.org.cn/module/forum/thread-619256-1-1.html
|