如题!
主程序
int main(void)
{
//
// Set the clocking to run directly from the crystal.
//配置系统时钟16M,不分频
//
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
//
// Initialize the UART and write status.
//串口初始化以及写状态
//
ConfigureUART();
UARTEnable(GPIO_PORTA_BASE);
//
// Enable the peripherals used by this example.
//使能定时器1时钟
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
//
// Enable the peripherals used by this example.
//使能定时器2时钟
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); //
// For this example T1CCP1 is used with port B pin 5.
// The actual port and pins used may be different on your part, consult
// the data sheet for more information.
// GPIO port B needs to be enabled so these pins can be used.
// TODO: change this to whichever GPIO port you are using.
//T1CCP1只能使用PB5口,详见pin_map.h,不同的板子另说
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//
// Configure the GPIO pin muxing for the Timer/CCP function.
// This is only necessary if your part supports GPIO pin function muxing.
// Study the data sheet to see which functions are allocated per pin.
// TODO: change this to select the port/pin you are using
//管脚配置
//
GPIOPinConfigure(GPIO_PB5_T1CCP1);
GPIOPinConfigure(GPIO_PB6_T0CCP0);
//
// Configure the ccp settings for CCP pin. This function also gives
// control of these pins to the SSI hardware. Consult the data sheet to
// see which functions are allocated per pin.
// TODO: change this to select the port/pin you are using.
//管脚类型设置为定时器外设
//
GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_5);
GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6);
//
//串口打印
//
UARTprintf("16-Bit Timer PWM ->");
UARTprintf("\n Timer = Timer1B");
UARTprintf("\n Mode = PWM");
UARTprintf("\n Duty Cycle = 可调\n");
UARTprintf("\nGenerating PWM on CCP1 (PB5) -> ");
UARTprintf("\n Timer = Timer0A");
UARTprintf("\n Mode = CAP_COUNT");
//
// Configure the two 32-bit periodic timers.
//配置定时器1:分立工作|B:PWM
//
TimerConfigure(TIMER1_BASE,TIMER_CFG_SPLIT_PAIR|TIMER_CFG_B_PWM);
TimerConfigure(TIMER0_BASE,TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_CAP_COUNT);
TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
//
//定时器预置值设定,设定占空比为50000/t
//
TimerLoadSet(TIMER1_BASE, TIMER_B, 50000);
//
//定时器预置值设定
//
TimerLoadSet(TIMER0_BASE, TIMER_A, 50000);
//
//定时器匹配值设定
//
TimerMatchSet(TIMER0_BASE,TIMER_A,0);
//
//Set the Timer1B match value to load value / 10*t.
//
TimerMatchSet(TIMER1_BASE, TIMER_B,TimerLoadGet(TIMER1_BASE, TIMER_B)/10 *t);
//
//使能定时器中断
//
IntEnable(INT_TIMER0A);
//
//定时器中断使能为捕获比较
//
TimerIntEnable(TIMER0_BASE, TIMER_CAPA_MATCH);
//
// Enable the timers.
//使能定时器
//
TimerEnable(TIMER1_BASE, TIMER_B);
TimerEnable(TIMER0_BASE, TIMER_A);
//
// Loop forever while the timers run.
//
while(1)
{
if(t>=1)t--;//调节占空比从0到1,十分之一步进
else t=10;
//
// Print out indication on the console that the program is running.
//
PrintRunningDots();
//
//Set the Timer1B match value to load value /10* t.
//
TimerMatchSet(TIMER1_BASE, TIMER_B,TimerLoadGet(TIMER1_BASE, TIMER_B)/10*t);
//
//Get the Timer1B match value.
//
timerload=TimerMatchGet(TIMER1_BASE, TIMER_B);
//
//打印定时器比较值,PWM波占空比
//UARTgetc();
sysctlvol=SysCtlClockGet();
edgecount=TimerValueGet(TIMER0_BASE,TIMER_A);
UARTgets(cp,0);
UARTprintf("\nsysctlvol:\%u\n",sysctlvol);
UARTprintf("\ntimerload:\%u\n",timerload);
UARTprintf("\n 占空比:\%u%%\n",100-t*10);
UARTprintf("\n 中断次数:\%u\n",g_ui32IntCount);
UARTprintf("\n 脉冲计数:\%u\n",edgecount);
//UARTprintf("\nuartgets:\%u\n",*cp);
}
}