本人小白,刚开始自学stm32单片机,用的MDK4.72,采用J-link下载程序。
程序目的是用3路PWM控制RBG三 {MOD}灯,定时中断让灯切换颜 {MOD}。可是程序循环的一会儿就不在循环,卡住了,
于是我就开始调试,采用软件调试时,它能运行,可是用J-link调试时就跳入到了HardFault_Handler()!
查了好多资料,讲的好抽象。希望大神能帮助我一把,感激不敬!程序不复杂,如下:
#include "stm32f10x.h"
u8 num=0,cha=0;
void RCC_Confguration()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO ,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4,ENABLE);
}
void GPIO_Confguration()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void Timer_Confguration(u16 shu,u16 fenpin )
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM2);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period = shu;
TIM_TimeBaseStructure.TIM_Prescaler = fenpin;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = shu;
TIM_TimeBaseStructure.TIM_Prescaler = fenpin;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Period = 50000;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
void PWM_Confguration()
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_Cmd(TIM2, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
int main()
{
NVIC_InitTypeDef NVIC_InitStructure;
SystemInit();
RCC_Confguration();
GPIO_Confguration();
Timer_Confguration(2000,0);
PWM_Confguration();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
while(1);
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)==SET)
{
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
num++;
if(20==num)
{
num=0;
cha++;
if(8==cha)
cha=1;
if(cha==1){
TIM2->CCR2 = 1000;
TIM3->CCR3 = 1900;
TIM3->CCR4 = 1900;
}
else if(cha==2){
TIM2->CCR2 = 1900;
TIM3->CCR3 = 1000;
TIM3->CCR4 = 1900;
}
else if(cha==3){
TIM2->CCR2 = 1900;
TIM3->CCR3 = 1900;
TIM3->CCR4 = 1000;
}
else if(cha==4){
TIM2->CCR2 = 1900;
TIM3->CCR3 = 1000;
TIM3->CCR4 = 1000;
}
else if(cha==5){
TIM2->CCR2 = 1000;
TIM3->CCR3 = 1900;
TIM3->CCR4 = 1000;
}
else if(cha==6){
TIM2->CCR2 = 1000;
TIM3->CCR3 = 1000;
TIM3->CCR4 = 1900;
}
else if(cha==7){
TIM2->CCR2 = 1000;
TIM3->CCR3 = 1000;
TIM3->CCR4 = 1000;
}
}
}
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
}
此帖出自
小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>