新聞中心

LM3S9B96 定時(shí)器的配置

作者: 時(shí)間:2016-11-11 來源:網(wǎng)絡(luò) 收藏
定時(shí)器是任何一款MCU最重要的一部分。很多初學(xué)者認(rèn)為定時(shí)器會(huì)占用CPU資源,這是錯(cuò)誤的。其實(shí),定時(shí)器和CPU是分開工作的,誰(shuí)都不影響誰(shuí),只是當(dāng)定時(shí)的時(shí)間到了,CPU會(huì)按照用戶寫的代碼,做一些事情而已。
lm3s9b96這款處理器的定時(shí)器,使用起來還是比較方便的。
最重要的就是設(shè)對(duì)定時(shí)器的工作模式(32位工作模式還是16位工作模式),定時(shí)周期。
下面以lm3s9b96的timer0為例,進(jìn)行定時(shí)周期為1s的配置

#include "inc/lm3s9b96.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/sysctl.h"


/* 用于調(diào)試 PF1 <-> LED -----------------------------------------------------*/
#define LED_PERIPH SYSCTL_PERIPH_GPIOF
#define LED_PORT GPIO_PORTF_BASE
#define LED_PIN GPIO_PIN_1
#define LED_OFF 1 << 1
#define LED_ON ~(1 << 1) // 低電平點(diǎn)亮LED


//*****************************************************************************
//
// 延時(shí)函數(shù)
//
//*****************************************************************************
void Delay(volatile signed long nCount)
{
for(; nCount != 0; nCount--);
}

//*****************************************************************************
//
// LED初始化函數(shù),用于調(diào)試timer, watchdog等
//
//*****************************************************************************
void LED_Init(void)
{
// 使能LED所在的GPIO端口
SysCtlPeripheralEnable(LED_PERIPH);

// 設(shè)置LED所在管腳為輸出
GPIOPinTypeGPIOOutput(LED_PORT, LED_PIN);

// 熄滅LED(默認(rèn)LED是點(diǎn)亮的,低電平點(diǎn)亮LED)
GPIOPinWrite(LED_PORT, LED_PIN, LED_OFF);
}

//*****************************************************************************
//
// timer0設(shè)置為1s溢出一次; TIMER_A初值:0x00F42400,向下遞減計(jì)數(shù)
//
//*****************************************************************************
void Timer0_Init(void)
{
// 使能Timer0模塊所在的GPIO端口
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

// 配置Timer0為32位周期定時(shí)器
TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER);

// 設(shè)置計(jì)數(shù)器為:(16000000 / 1)個(gè)時(shí)鐘周期,即1s
TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet() / 1);

// 使能Timer0的A通道中斷
IntEnable(INT_TIMER0A);

// 使能Timer0A超時(shí)中斷
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

// 啟動(dòng)Timer0計(jì)數(shù)
TimerEnable(TIMER0_BASE, TIMER_A);
}

//*****************************************************************************
//
// 主函數(shù)
//
//*****************************************************************************
int main(void)
{
// Set the clocking to run directly from the crystal.
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

LED_Init();
Timer0_Init();
IntMasterEnable(); // 開總中斷

while (1)
{
}
}

//*****************************************************************************
//
// 定時(shí)器0中斷處理函數(shù). 1s溢出一次
//
//*****************************************************************************
void Timer0AIntHandler(void)
{
// 清除中斷狀態(tài),重要!
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

GPIOPinWrite(LED_PORT, LED_PIN, (GPIOPinRead(LED_PORT, LED_PIN) ^ LED_PIN));
}

本文引用地址:http://butianyuan.cn/article/201611/317009.htm
按照前面講的中斷映射表的配置,將startup_ewarm.c文件中添加兩處代碼。編譯、運(yùn)行即可。


關(guān)鍵詞: LM3S9B96定時(shí)

評(píng)論


技術(shù)專區(qū)

關(guān)閉