Help modify below code using the timer Timer_A to toggle two LEDs in periodic ti
ID: 3859712 • Letter: H
Question
Help modify below code using the timer Timer_A to toggle two LEDs in periodic time intervals of A)10 sec B)30 sec C)1 sec
the original code toggled red LED when an overflow occured:
#include <ms430.h>
#define RedLED BIT0
#define RedLEDToggle (P1OUT ^= RedLED)
void main (void)
{
WDTCTL = WDTPW|WDTHOLD;
P1DIR = RedLED;
P1OUT = RedLED;
TECTL = TASSEL_2|ID_3|MC_3|TAIE;
TACCR0 = 62500;
_enable_interrupts();
LPM1; //enter low power mode
}
#pragma vector=TIMER_A1_VECTOR
__interrupt void Timer_A(void)
{
switch(TAIV)
{
case 0x02: break;
case 0x04: break;
case 0x0A; RedLEDToggle;
break;
}
}
Explanation / Answer
Even though the toggle can be done by 'millis' that I have handled previously in many cases. Here I have made some modifications in the code but unable to test the toggle on board but I am sure the code I have given will work. Do make some modifications if required.
#include <ms430.h>
#include <mbed.h>
#define RedLED BIT0
#define RedLEDToggle (P1OUT ^= RedLED)
const unsigned long LED1_interval = 10000;
const unsigned long LED2_interval = 30000;
const unsigned long LED3_interval = 1000;
unsigned long LED1_timer;
unsigned long LED2_timer;
unsigned long LED3_timer;
void main (void)
{
WDTCTL = WDTPW|WDTHOLD;
P1DIR = RedLED;
P1OUT = RedLED;
TECTL = TASSEL_2|ID_3|MC_3|TAIE;
TACCR0 = 62500;
LED1_timer = millis ();
LED2_timer = millis ();
LED3_timer = millis ();
_enable_interrupts();
LPM1; //enter low power mode
}
#pragma vector=TIMER_A1_VECTOR
__interrupt void Timer_A(void)
{
switch(TAIV)
{
case 0x02:
if ( (millis () - LED1_timer) >= LED1_interval)
RedLEDToggle;
break;
case 0x04:
if ( (millis () - LED2_timer) >= LED2_interval)
RedLEDToggle;
break;
case 0x0A;
if ( (millis () - LED3_timer) >= LED3_interval)
RedLEDToggle;
break;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.