Hi, All,
I am using Tive TM4C123GXL EVB with Keil MDK-ARM u5 to build a PWM program. Sometimes the HardFault event is triggered and enter the break point in the HardFault_Handler when the project is running. The codes are shown below:
#include <stdint.h>
#include <stdbool.h>
#include "inc\tm4c123gh6pm.h"
void Delay(uint32_t time);
int main(void)
{
int pw;
SYSCTL_RCGCPWM_R |= 0x2; // enable PWM1 with clock
SYSCTL_RCGCGPIO_R = 0x20 | 0x02;
SYSCTL_RCC_R = SYSCTL_RCC_USEPWMDIV|SYSCTL_RCC_PWMDIV_2;
SYSCTL_RCGC0_R |= SYSCTL_RCGC0_PWM0; // enable PWM1 again
// PWM1_2B - PF1 - M1PWM5 pin - Module 1 Generator 2
PWM1_2_CTL_R = 0x0; // disable PWM1_2B or M1PWM5
PWM1_2_GENB_R = 0x0000080C;
PWM1_2_LOAD_R = 3999;
PWM1_2_CTL_R = 0x1; // enable PWM1_2B or M1PWM5
PWM1_ENABLE_R = 0x20; // enable PWM1
GPIO_PORTF_DIR_R |= 0x02;
GPIO_PORTF_DEN_R |= 0x02;
GPIO_PORTF_AFSEL_R |= 0x2; // PF1 - Alternate Function: PWM1-2B
GPIO_PORTF_PCTL_R |= GPIO_PCTL_PF1_M1PWM5; // 0x00000050
GPIO_PORTB_DIR_R |= 0x03; // enable PB1, PB0 output pins
GPIO_PORTB_DEN_R |= 0x03; // enable PB1, PB0 digital function pins
GPIO_PORTB_DATA_R |= 0x01; // enable PB0 (AIN1=1) CW-rotation
while(1)
{
for (pw = 100; pw < 3999; pw += 20)
{
PWM1_2_CMPB_R = pw;
Delay(50000);
}
for (pw = 3999; pw > 100; pw -= 20)
{
PWM1_2_CMPB_R = pw;
Delay(50000);
}
GPIO_PORTB_DATA_R |= 0x01;
for (pw = 100; pw < 3999; pw += 20)
{
PWM1_2_CMPB_R = pw;
Delay(50000);
}
for (pw = 3999; pw > 100; pw -= 20)
{
PWM1_2_CMPB_R = pw;
Delay(50000);
}
GPIO_PORTB_DATA_R |= 0x01;
}
}
void Delay(uint32_t time)
{
uint32_t Loop;
for (Loop = 0; Loop < time; Loop++) {}
}