0% found this document useful (0 votes)
35 views

Objectives:: Watchdog Timers (WDTS) Are Particularly Useful Because They Detect If A Microcontroller Is in

The document describes implementing a watchdog timer on a microcontroller to reset the system if it enters an invalid software state. It also describes using a pushbutton switch to control LEDs. The code implements a watchdog timer, configures GPIO pins for an LED and button, and uses the button to control the LEDs. It suggests additionally making all LEDs turn on/off for a short time when the button is pressed.

Uploaded by

ahmed shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Objectives:: Watchdog Timers (WDTS) Are Particularly Useful Because They Detect If A Microcontroller Is in

The document describes implementing a watchdog timer on a microcontroller to reset the system if it enters an invalid software state. It also describes using a pushbutton switch to control LEDs. The code implements a watchdog timer, configures GPIO pins for an LED and button, and uses the button to control the LEDs. It suggests additionally making all LEDs turn on/off for a short time when the button is pressed.

Uploaded by

ahmed shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Objectives:

Implement a code in which watchdog timer is used and also perform the function
of pushbutton switch which glow the leds.

Discussion:
 Six 16/32-bit and Six 32/64-bit Function timers.
 Twelve 16/32-bit and Twelve 32/64-bit capture/compare/PWM pins
 Timer modes:
 One-shot
 Periodic
 Input edge count or time capture with 16-bit prescaler
 PWM generation (separated only)
 Real-Time Clock (concatenated only)
 Count up or down
 Simple PWM (no deadband generation)
 Support for timer synchronization, daisy-chains, and stalling during debugging
 May trigger ADC samples or DMA transfers

Watchdog timers (WDTs) are particularly useful because they detect if a microcontroller is in
an invalid software state and initiate a reset if needed. These invalid software states can be
caused by anything from a software bug to electromagnetic interference. In general, a WDT
resets the system when it does not receive a regular pulsed signal from the host processor.
In critical systems, such as smoke detectors and other industrial applications, an invalid software
state could be catastrophic. In these instances, an external WDT would be imperative. The
external WDT has a separate clock source providing redundancy and making the system more robust.
Included in all devices (newer ones have the enhanced watchdog timer+).
 Its main function is to protect the system against malfunctions but it can instead be used
as an interval timer if this protection is not needed.
 The main purpose of the watchdog timer is to protect the system against failure of the
software, such as the program becoming trapped in an unintended, infinite loop.
 Left to itself, the watchdog counts up and resets the MSP430 when it reaches its limit.
The code must therefore keep clearing the counter before the limit is reached to prevent a
reset.

Code:

#include <stdint.h>
#include <stdbool.h>
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "inc/hw_gpio.h"
#include "inc/hw_memmap.h" // for GPIO Port Base
#include "inc/hw_types.h" // needed for HWREG for locking HW
#include "driverlib/watchdog.h"
#include "driverlib/rom.h"

// Interrupts prototype functions


#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"

// ADC functionality
#include "driverlib/adc.h"

// PWM functionality
#include "driverlib/pwm.h"

// Timer prototyope functions


#include "driverlib/timer.h"

// LEDs Pin HW Positions


#define LED_1 GPIO_PIN_1 // PN1
#define LED_2 GPIO_PIN_0 // PN0
#define LED_3 GPIO_PIN_4 // PF4

#define SW_1 GPIO_PIN_0 // PJ0

#define LED_OFF 0

volatile bool g_bFeedWatchdog = true;


// Program variables with Init values:
uint8_t LED_Command = LED_OFF;
int value=0;

void WatchdogIntHandler(void)
{
// If we have been told to stop feeding the watchdog, return immediately
// without clearing the interrupt. This will cause the system to reset
// next time the watchdog interrupt fires.
//
if(!g_bFeedWatchdog)
{
return;
}}
ROM_WatchdogIntClear(WATCHDOG0_BASE);

uint32_t TIMER_0_Period;

int flag=0; // Variable


bool flag2;

int main(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

//

//
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);

TIMER_0_Period = 40000000/4; // 250ms (2HZ) FOR TH LED 1 AND LED 2 WHEN


BOTH BUTTON ARE PRESSED

TimerLoadSet(TIMER0_BASE, TIMER_A, TIMER_0_Period); // 1 s

IntMasterEnable();
ROM_IntEnable(INT_WATCHDOG);

IntEnable(INT_TIMER0A);

TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_WDOG0);
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|
SYSCTL_XTAL_16MHZ);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // CONFIGURATION PINS A


SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // CONFIGURATION PINS B
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); // CONFIGURATION PINS C
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // CONFIGURATION PINS D
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // CONFIGURATION PINS E
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // CONFIGURATION PINS F
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ); // CONFIGURATION PINS J
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION); // CONFIGURATION PINS N
GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, LED_1|LED_2|LED_3); //
CONFIGURATION PINS N AS OUTPUT

GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, SW_1); // CONFIGURACON COMO


ENTRADA PARA LOS PINES PORTA
GPIOPadConfigSet(GPIO_PORTJ_BASE, SW_1, GPIO_STRENGTH_2MA,
GPIO_PIN_TYPE_STD_WPU);

//
// Set the period of the watchdog timer.
//
ROM_WatchdogReloadSet(WATCHDOG0_BASE, ROM_SysCtlClockGet());

//
// Enable reset generation from the watchdog timer.
//
ROM_WatchdogResetEnable(WATCHDOG0_BASE);
//
// Enable the watchdog timer.
//
ROM_WatchdogEnable(WATCHDOG0_BASE);
TimerEnable(TIMER0_BASE, TIMER_A);
// Loop forever:
while(true)
{ while(true){
int curr_time=flag;
while(GPIOPinRead(GPIO_PORTJ_BASE,SW_1))
{
if(flag-curr_time>3)
flag2=true;
}

if (GPIOPinRead(GPIO_PORTJ_BASE,SW_1))
{
value++;
SysCtlDelay(2000);
}
SysCtlDelay(20000);
if(value==2||value==4|| value==6){
break;

}
else
{
value=0;
}

}
if(flag2){

GPIOPinWrite(GPIO_PORTN_BASE,LED_1,1);
SysCtlDelay(200000);
GPIOPinWrite(GPIO_PORTN_BASE,LED_1,0);
g_bFeedWatchdog=false;
}
if(value==2)
{
GPIOPinWrite(GPIO_PORTN_BASE,LED_1,1);
value=0;
}
else if (value==4)
{
GPIOPinWrite(GPIO_PORTN_BASE,LED_2,1);
value=0;

else if (value==6)
{
GPIOPinWrite(GPIO_PORTN_BASE,LED_3,1);
value=0;

}
TimerDisable(TIMER0_BASE, TIMER_A);
}

////////////////////////////////////////////////////////////////
//////////////////////
////////////////////////
///////////////////////////////////
////////////////////////////////////////////////////////////////////

void ISR_Timer0IntHandler(void) // TIMER0


{
// ACK timer 1 by clearing the timer interrupt
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
flag ++;
return;
}
Suggestion:
We can also add some other function of switch which make all the leds on
and off for a 1 or 2 sec time.

You might also like