/*************************************************************************
*
* Used with ICCARM and AARM.
*
* (c) Copyright IAR Systems 2011
*
* File name : main.c
* Description : Define main module
* author: yang yongzhen
* qq:534117529
**************************************************************************/
#include <intrinsics.h>
#include "stm32l1xx.h"
//总共4096Bytes,即4K
#define EEPROM_START_ADDR 0x08080000
#define EEPROM_END_ADDR 0x08080FFF
void LEDInit()
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the GPIO_LED Clock */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
/* Configure the GPIO_LED pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// STM32_LEDOff(Led);
GPIO_SetBits(GPIOB,GPIO_Pin_7);
}
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
void write_eeprom(uint32_t address,uint8_t data)
{
if(IS_FLASH_DATA_ADDRESS(address))
{
FLASH_UnlockProgram();
while(FLASH_GetStatus()!=FLASH_COMPLETE);
FLASH_WriteDataByte(address, data);
FLASH_LockProgram();
}
}
uint8_t read_eeprom(uint32_t address)
{
uint8_t tmp=0;
if(IS_FLASH_DATA_ADDRESS(address))
{
FLASH_UnlockProgram();
while(FLASH_GetStatus()==FLASH_BUSY);
tmp=*(__IO uint8_t*)address;
FLASH_LockProgram();
}
return tmp;
}
/*************************************************************************
* Function Name: main
* Parameters: none
*
* Return: none
*
* Description: main
*
*************************************************************************/
void main(void)
{
//NVIC_InitTypeDef NVIC_InitStructure;
//TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
__disable_interrupt();
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();
// NVIC init
#ifndef EMB_FLASH
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
// LEDs Init
LEDInit();
GPIO_SetBits(GPIOB,GPIO_Pin_7);
Delay(0xFFFF);
GPIO_ResetBits(GPIOB,GPIO_Pin_7);
for(int i=0;i<16;i++)
{
write_eeprom(EEPROM_START_ADDR+i,i);
}
__enable_interrupt();
char temp,temp1;
temp=read_eeprom(EEPROM_START_ADDR);
temp1=read_eeprom(EEPROM_START_ADDR+15);
if((temp==0)&&(temp1==15))
{
GPIO_SetBits(GPIOB,GPIO_Pin_7);
while(1);
}
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_7);
Delay(0x0FFFF);
GPIO_ResetBits(GPIOB,GPIO_Pin_7);
Delay(0x0FFFF);
}
}
//说明:看到开发板上LED3,黄灯闪烁,说明写入和读取不成功,否则黄灯一直亮,说明确实读写成功了。 |