LPC2138微控制器之定时器、看门狗、VIC实例

时间:2022-01-05 18:33:52

本实例使用LPC2138微控制器,Keil Proteus模拟实现。

本实例使用定时器计数,当计数值到达时触发定时器中断,在定时器中断程序中喂狗,涉及模块包括晶振、PLL、定时器、看门狗和VIC。

每次喂狗的同时,将P0.1 GPIO输出电平取反,外接一个LED灯作为Active信号灯。

 

直接贴代码:

main.c

int main(void)
{
    int ret = 0;
    unsigned char reason = 0;
    unsigned char byte = 0;

    /* Fosc & CCLK is 60MHz */
    /* FPCLK is 30MHz */
    VPBDIV = 0x02;

    watch_dog_init();

    timer0_init();

    while(1);

    return 0;
}

watchdog.c

#include <lpc213x.h>
#include "watchdog.h"

#define WDOG_INTREVAL_TIME 0x01FFFFFF

void watch_dog_init(void)
{
    /* Configure WDOG Timeout Interval */
    WDTC = WDOG_INTREVAL_TIME;

    /* Enable WDOG, reset the CPU while WDOG timeout */
    WDMOD = 0x3;
}

void watch_dog_feed()
{
    /* reload WDOG by WDTC */
    WDFEED = 0xAA;
    WDFEED = 0x55;
}

timer.c

#include <lpc213x.h>
#include "timer.h"
#include "watchdog.h"

void timer0_isr(void) __irq
{
    if ((VICIRQStatus & 0x10) && (T0IR & 0x01))
    {
        /* Clear MR0 Interrupt */
        T0IR = 0x01;

        /* ACT LED Blink */
        if (IOSET0 & 0x01)
        {
            IOCLR0 = 0x01;
        }
        else
        {
            IOSET0 = 0x01;
        }

        /* feed the WDOG */
        watch_dog_feed();

    }

}

void timer0_init(void)
{
    /* ACT LED Initialization */
    IODIR0 |= 0x1;
    IOSET0 |= 0x1;

    /* Timer0 Timer Mode */
    T0CTCR = 0x00;

    /* Clear Timer0 Counter & Prescale Counter */
    T0TC = 0x00;
    T0PC = 0x00;
    
    /* Interrupt & Reset on MR0 */
    T0MCR = 0x03;
    T0MR0 = 0x5FF;
    T0PR = 0x5FF;

    /* Enable Timer0 */
    T0TCR = 0x01;

    /* Timer0 VIC */
    VICIntSelect = 0x00;
    VICVectCntl0 = 0x20 | 4;
    VICVectAddr0 = (unsigned int)timer0_isr;

    /* Enable Timer0 VIC */
    VICIntEnable |= 0x10;

}