最近学习stm32f103zet6,用的keil 4.6,先是看看固件库,拿程序软件仿真一下,前几天买的jlink到了,开始硬件仿真,遇到很多问题。特来求教。
一个最基本的定时器,我的板子是gpiod口,程序如下
#include "stm32f10x.h"
void RCC_cfg();
void TIMER_cfg();
void NVIC_cfg();
void GPIO_cfg();
NVIC_InitTypeDef NVIC_InitStructure;
int main()
{
RCC_cfg();
NVIC_cfg();
GPIO_cfg();
TIMER_cfg();
//?????2
TIM_Cmd(TIM2,ENABLE);
while(1);
}
void RCC_cfg()
{
//????????
ErrorStatus HSEStartUpStatus;
//?RCC???????????
RCC_DeInit();
//??????????
RCC_HSEConfig(RCC_HSE_ON);
//????????????
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
//??AHB??(HCLK)?????
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//????AHB??(APB2)?HCLK??
RCC_PCLK2Config(RCC_HCLK_Div1);
//????AHB??(APB1)?HCLK?2??
RCC_PCLK1Config(RCC_HCLK_Div2);
//??FLASH????
FLASH_SetLatency(FLASH_Latency_2);
//???????
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//??PLL??,?HSE?9?? 8MHz * 9 = 72MHz
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//??PLL
RCC_PLLCmd(ENABLE);
//??PLL????
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//??PLL??????
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//??PLL???????
while(RCC_GetSYSCLKSource() != 0x08);
}
//??TIM2???
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
//??GPIO???
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
}
void TIMER_cfg()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//???Timer??????
TIM_DeInit(TIM2);
//???????TIM2?????
TIM_InternalClockConfig(TIM2);
//??????36000-1,????????72MHz/36000 = 2kHz
TIM_TimeBaseStructure.TIM_Prescaler = 36000 - 1;
//??????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
//??????????????
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//????????,??2000???????????
TIM_TimeBaseStructure.TIM_Period = 2000 - 1;
//??????TIM2?
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
//????????
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
//??ARR??????
TIM_ARRPreloadConfig(TIM2, DISABLE);
//??TIM2???
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
}
void NVIC_cfg()
{
#ifdef VECT_TAB_RAM
/* 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
//??????1
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//??TIM2?????
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
//???????????0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//???????????0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//????
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void GPIO_cfg()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //????5
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //??????50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???????
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
仿真时,若中断函数中设置断点,则先单步运行至while循环,再f5 全速执行可进中断,然后单步至while,再f5可再次进入断点。
若中断函数中设置断点,则先单步运行至while循环,再f5 全速执行可进中断,在中断断点处若点f5则直接回到main的第一句 rcc配置句。
又在中断中和main入口rcc处设置断点,f5全速运行,到main入口rcc语句断点,再f5依旧。
弄了几天了,最简单的程序都过不去,新手求大家帮助
|