|
TM4C123G 一个定时捕获2路pwm,总是有问题!
[复制链接]
#include
#include
#include "hw_memmap.h"
#include "hw_types.h"
#include "sysctl.h"
#include "interrupt.h"
#include "gpio.h"
#include "timer.h"
#include "hw_ints.h"
#include "pin_map.h"
#include "hw_types.h"
#include "debug.h"
#include "fpu.h"
#include "rom.h"
#include "rom_map.h"
#include "uart.h"
#include "uartstdio.h"
#include "pwm.h"
uint16_t Count=0;
uint8_t Flag1=1,Flag2=1,Flag21=0,Flag22=0;
uint32_t ReadValue1=0,ReadValue2=0;
void
ConfigureUART(void)
{
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//配置引脚功能
ROM_GPIOPinConfigure(GPIO_PB0_U1RX);
ROM_GPIOPinConfigure(GPIO_PB1_U1TX);
ROM_GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
ROM_UARTClockSourceSet(UART1_BASE, UART_CLOCK_PIOSC);
UARTStdioConfig(1, 115200, 16000000);
}
void Time_catch1_Interrupt(void)
{
uint32_t num1=0;
if(Flag1)
{
ReadValue1=TimerValueGet(WTIMER1_BASE,TIMER_A);
TimerControlEvent(WTIMER1_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
Flag1=0;
}
else
{
ReadValue2=ROM_TimerValueGet(WTIMER1_BASE,TIMER_A);
if(ReadValue2>ReadValue1)
{
num1=(ReadValue2-ReadValue1);
}
else
num1=(0xffffffff-ReadValue1+ReadValue2);
TimerControlEvent(WTIMER1_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
UARTprintf("value1=%d\n",num1);
Flag1=1;
}
}
void Time_catch2_Interrupt(void)
{
uint32_t num2=0;
if(Flag2)
{
ReadValue1=TimerValueGet(WTIMER1_BASE,TIMER_B);
TimerControlEvent(WTIMER1_BASE, TIMER_B, TIMER_EVENT_POS_EDGE);
Flag2=0;
}
else
{
ReadValue2=ROM_TimerValueGet(WTIMER1_BASE,TIMER_B);
if(ReadValue2>ReadValue1)
{
num2=(ReadValue2-ReadValue1);
}
else
num2=(0xffffffff-ReadValue1+ReadValue2);
TimerControlEvent(WTIMER1_BASE, TIMER_B, TIMER_EVENT_POS_EDGE);
UARTprintf("value2=%d\n",num2);
Flag2=1;
}
}
void Time_catch21_Interrupt(void)
{
uint32_t num21=0;
if(Flag21)
{
ReadValue1=TimerValueGet(WTIMER3_BASE,TIMER_A);
TimerControlEvent(WTIMER3_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
Flag21=0;
}
else
{
ReadValue2=ROM_TimerValueGet(WTIMER3_BASE,TIMER_A);
if(ReadValue2>ReadValue1)
{
num21=(ReadValue2-ReadValue1);
}
else
num21=(0xffffffff-ReadValue1+ReadValue2);
TimerControlEvent(WTIMER3_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
UARTprintf("value21=%d\n",num21);
Flag21=1;
}
}
void Time_catch22_Interrupt(void)
{
uint32_t num22=0;
if(Flag22)
{
ReadValue1=TimerValueGet(WTIMER3_BASE,TIMER_B);
TimerControlEvent(WTIMER3_BASE, TIMER_B, TIMER_EVENT_POS_EDGE);
Flag22=0;
}
else
{
ReadValue2=ROM_TimerValueGet(WTIMER3_BASE,TIMER_B);
if(ReadValue2>ReadValue1)
{
num22=(ReadValue2-ReadValue1);
}
else
num22=(0xffffffff-ReadValue1+ReadValue2);
TimerControlEvent(WTIMER3_BASE, TIMER_B, TIMER_EVENT_POS_EDGE);
UARTprintf("value22=%d\n",num22);
Flag22=1;
}
}
void Timer1IntHandler(void)
{
ROM_TimerIntClear(WTIMER1_BASE, TIMER_CAPA_EVENT);
Time_catch1_Interrupt();
ROM_TimerEnable(WTIMER1_BASE, TIMER_A);
}
void Timer2IntHandler(void)
{
ROM_TimerIntClear(WTIMER1_BASE, TIMER_CAPB_EVENT);
Time_catch2_Interrupt();
ROM_TimerEnable(WTIMER1_BASE, TIMER_B);
}
void Timer21IntHandler(void)
{
ROM_TimerIntClear(WTIMER3_BASE, TIMER_CAPA_EVENT);
Time_catch21_Interrupt();
ROM_TimerEnable(WTIMER3_BASE, TIMER_A);
}
void Timer22IntHandler(void)
{
ROM_TimerIntClear(WTIMER3_BASE, TIMER_CAPB_EVENT);
Time_catch22_Interrupt();
ROM_TimerEnable(WTIMER3_BASE, TIMER_B);
}
void pwm_set(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);//使能PWM0模块
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);//使能PWM0和PWM1输出所在GPIO
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);//使能PWM0和PWM1输出所在GPIO
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7);//配置PH0/PH1为PWM功能
GPIOPinConfigure(GPIO_PB6_M0PWM0); //#define GPIO_PB6_M0PWM0 0x00011804
GPIOPinConfigure(GPIO_PB7_M0PWM1); //#define GPIO_PB7_M0PWM1 0x00011C04
GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_4);
GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_5);//配置PH0/PH1为PWM功能
GPIOPinConfigure(GPIO_PC4_M0PWM6); //#define GPIO_PB6_M0PWM0 0x00011804
GPIOPinConfigure(GPIO_PC5_M0PWM7); //#define GPIO_PB7_M0PWM1 0x00011C04
SysCtlPWMClockSet(SYSCTL_PWMDIV_8); // PWM时钟配置:不分频
//配置PWM发生器0:加减计数,不同步
PWMGenConfigure(PWM0_BASE,PWM_GEN_0,PWM_GEN_MODE_UP_DOWN| PWM_GEN_MODE_NO_SYNC);
PWMGenConfigure(PWM0_BASE,PWM_GEN_3,PWM_GEN_MODE_UP_DOWN| PWM_GEN_MODE_NO_SYNC);
//设置PWM发生器1的频率,时钟频率/PWM分频数/n,100M/1/40=1mHZ
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 25000);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, 25000);
//设置PWM0/PWM1输出的脉冲宽度
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 10000);//50%占空比
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, 20000);//25%占空比
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_6, 10000);//50%占空比
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_7, 20000);//25%占空比
//使能PWM6和PWM7的输出
PWMOutputState(PWM0_BASE, (PWM_OUT_0_BIT |PWM_OUT_1_BIT|PWM_OUT_6_BIT|PWM_OUT_7_BIT), true);
//使能PWM发生器
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
PWMGenEnable(PWM0_BASE, PWM_GEN_3);
}
int main(void)
{
ROM_FPULazyStackingEnable();
ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
pwm_set();
ConfigureUART();
/*WT10,WT11输入捕获*/
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_WTIMER1);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
ROM_GPIOPinTypeTimer(GPIO_PORTC_BASE, GPIO_PIN_6);
ROM_GPIOPinTypeTimer(GPIO_PORTC_BASE, GPIO_PIN_7);
ROM_GPIOPinConfigure(GPIO_PC6_WT1CCP0);
ROM_GPIOPinConfigure(GPIO_PC7_WT1CCP1);
GPIOPadConfigSet(GPIO_PORTC_BASE, GPIO_PIN_6,GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
GPIOPadConfigSet(GPIO_PORTC_BASE, GPIO_PIN_7,GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
ROM_TimerConfigure(WTIMER1_BASE, (TIMER_CFG_SPLIT_PAIR |TIMER_CFG_A_CAP_TIME_UP|TIMER_CFG_B_CAP_TIME_UP));
ROM_TimerControlEvent(WTIMER1_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
ROM_TimerControlEvent(WTIMER1_BASE, TIMER_B, TIMER_EVENT_POS_EDGE);
ROM_IntEnable(INT_WTIMER1A);
ROM_IntEnable(INT_WTIMER1B);
ROM_TimerIntEnable(WTIMER1_BASE, TIMER_CAPA_EVENT);
ROM_TimerIntEnable(WTIMER1_BASE, TIMER_CAPB_EVENT);
ROM_TimerEnable(WTIMER1_BASE, TIMER_A);
ROM_TimerEnable(WTIMER1_BASE, TIMER_B);
IntRegister(INT_WTIMER1A,Timer1IntHandler);
IntRegister(INT_WTIMER1B,Timer2IntHandler);
/*WT30,WT11输入捕获*/
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_WTIMER3);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
ROM_GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_2);
ROM_GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_3);
ROM_GPIOPinConfigure(GPIO_PD2_WT3CCP0);
ROM_GPIOPinConfigure(GPIO_PD3_WT3CCP1);
GPIOPadConfigSet(GPIO_PORTD_BASE, GPIO_PIN_2,GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
GPIOPadConfigSet(GPIO_PORTD_BASE, GPIO_PIN_3,GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
ROM_TimerConfigure(WTIMER3_BASE, (TIMER_CFG_SPLIT_PAIR |TIMER_CFG_A_CAP_TIME_UP|TIMER_CFG_B_CAP_TIME_UP));
ROM_TimerControlEvent(WTIMER3_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
ROM_TimerControlEvent(WTIMER3_BASE, TIMER_B, TIMER_EVENT_POS_EDGE);
ROM_IntEnable(INT_WTIMER3A);
ROM_IntEnable(INT_WTIMER3B);
ROM_TimerIntEnable(WTIMER3_BASE, TIMER_CAPA_EVENT);
ROM_TimerIntEnable(WTIMER3_BASE, TIMER_CAPB_EVENT);
ROM_TimerEnable(WTIMER3_BASE, TIMER_A);
ROM_TimerEnable(WTIMER3_BASE, TIMER_B);
IntRegister(INT_WTIMER3A,Timer21IntHandler);
IntRegister(INT_WTIMER3B,Timer22IntHandler);
ROM_IntMasterEnable();
while(1);
}
|
|