LED, INTERRUPTS
IDE : keil uvision
language : ARM
Microcontroller : TM4c123G
references : https://microcontrollerslab.com/category/tiva-launchpad/
Question - When right switch is pressed the blue onboard LED should be light on for N seconds (default 5 seconds), and each time the left switch is pressed the delay(N) will cycles through 2 and 5 seconds.
If right switch is pressed while the LED is on the time will reset (i.e. if we press switch at and led is on before the N seconds end we press the right switch again the LED will stay on for N more seconds starting the moment it was last pressed)
#define DELAY 16000000
#define SYSCTL_RCGCGPIO_R (*((volatile unsigned long *)0x400FE608))
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
void GPIOF_Handler(void);
void BLUELIGHT(int Nseconds);
#include "TM4C123.h"
int main(void)
{
SYSCTL_RCGCGPIO_R = 0x20;
/* PORTF0 has special function, need to unlock to modify */
GPIOF->LOCK = 0x4C4F434B; /* unlock commit register */
GPIOF->CR = 0x01; /* make PORTF0 configurable */
GPIOF->LOCK = 0; /* lock commit register */
/*Initialize PF3 as a digital output, PF0 and PF4 as digital input pins */
GPIOF->DIR &= (1<<4)|(1<<0); /* Set PF4 as a digital input pins */
GPIOF->DIR |= (1<<2); /* Set PF2 as digital output to control blue LED */
GPIOF->DEN |= (1<<4)|(1<<0)|(1<<2) ; /* make PORTF4-0 digital pins */
GPIOF->PUR |= (1<<4)|(1<<0); /* enable pull up for PORTF4, 0 */
/* configure PORTF4, 0 for falling edge trigger interrupt */
GPIOF->IS &= (1<<4)|(1<<0); /* make bit 4, 0 edge sensitive */
GPIOF->IBE &=(1<<4)|(1<<0); /* trigger is controlled by IEV */
GPIOF->IEV |= (1<<4)|(1<<0); /* rising edge trigger */
GPIOF->ICR |= (1<<4)|(1<<0); /* clear any prior interrupt */
GPIOF->IM |= (1<<4)|(1<<0); /* unmask interrupt */
/* enable interrupt in NVIC and set priority to 3 */
NVIC->IP[30] = 3 << 5; /* set interrupt priority to 3 */
NVIC->ISER[0] |= (1<<30); /* enable IRQ30 (D30 of ISER[0]) */
while(1)
{
}
}
void GPIOF_Handler(void)
{
int Nseconds =5 ;
SYSCTL->RCGCGPIO |= (1<<5); /* Set bit5 of RCGCGPIO to enable clock to PORTF*/
if (GPIOF->MIS & 0x01) /* check if interrupt causes by PF0 (riight)/SW1*/
{
while(1)
{
BLUELIGHT(Nseconds);
if (GPIOF->MIS & 0x10) /*left switch*/
{
if(Nseconds == 5)
{
Nseconds=2;
}
else
{
Nseconds=5;
}
GPIOF->ICR |= 0x10;
}
GPIOF->ICR |= 0x01;
}
}
}
void BLUELIGHT(int Nseconds)
{
int temp;
volatile unsigned long ulLoop ;
GPIOF->DATA |= (1<<2);
for(temp=0;temp<Nseconds;temp++)
{
for ( ulLoop = 0; ulLoop < DELAY; ulLoop++)
{
if (GPIOF->MIS & 0x01) /*right switch*/
{
Nseconds=5;
GPIOF_Handler();
}
}
}
GPIOF->DATA &= ~(1<<2);
}
TIMER
IDE: Keil uvision
1 second delay
/* This is a timer interrupt example code of TM4C123 Tiva C Launchpad https://microcontrollerslab.com/introduction-tiva-tm4c123g-launchpad/ */
/* Generates a delay of one second using Timer1A interrupt handler routine */
#include "TM4C123.h" // Device header file for Tiva Series Microcontroller
#define Green (1<<3) // PF3 pin of TM4C123 Tiva Launchpad, Green LED
void Time1A_1sec_delay(void);
int main(void)
{
/*Initialize PF3 as a digital output pin */
SYSCTL->RCGCGPIO |= 0x20; // turn on bus clock for GPIOF
GPIOF->DIR |= Green; // set Green pin as a digital output pin
GPIOF->DEN |= Green; // Enable PF2 pin as a digital pin
Time1A_1sec_delay();
while(1)
{
// do nothing wait for the interrupt to occur
}
}
/* Timer1 subtimer A interrupt service routine */
void TIMER1A_Handler()
{
if(TIMER1->MIS & 0x1)
GPIOF->DATA ^= Green; /* toggle Green LED*/
TIMER1->ICR = 0x1; /* Timer1A timeout flag bit clears*/
}
void Time1A_1sec_delay(void)
{
SYSCTL->RCGCTIMER |= (1<<1); /*enable clock Timer1 subtimer A in run mode */
TIMER1->CTL = 0; /* disable timer1 output */
TIMER1->CFG = 0x4; /*select 16-bit configuration option */
TIMER1->TAMR = 0x02; /*select periodic down counter mode of timer1 */
TIMER1->TAPR = 125-1; /* TimerA prescaler value */
TIMER1->TAILR = 128000-1 ; /* TimerA counter starting count down value */
TIMER1->ICR = 0x1; /* TimerA timeout flag bit clears*/
TIMER1->IMR |=(1<<0); /*enables TimerA time-out interrupt mask */
TIMER1->CTL |= 0x01; /* Enable TimerA module */
NVIC->ISER[0] |= (1<<21); /*enable IRQ21 */
}
LED, SWITCH, INTERRUPTS
IDE : Keil uvision 5
language : ARM
PORTF PF0 and PF4 fall edge interrupt example
This GPIO interrupt example code controls green LED with switches SW1 and SW2 external interrupts */
/Program 2
/*PORTF PF0 and PF4 fall edge interrupt example*/
/*This GPIO interrupt example code controls green LED with switches SW1 and SW2 external interrupts */
#define DELAY 5000000
#define SYSCTL_RCGCGPIO_R (*((volatile unsigned long *)0x400FE608))
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
#include "TM4C123.h" // Device header
int main(void)
{
SYSCTL_RCGCGPIO_R = 0x20;
/* PORTF0 has special function, need to unlock to modify */
GPIOF->LOCK = 0x4C4F434B; /* unlock commit register */
GPIOF->CR = 0x01; /* make PORTF0 configurable */
GPIOF->LOCK = 0; /* lock commit register */
/*Initialize PF3 as a digital output, PF0 and PF4 as digital input pins */
GPIOF->DIR &= (1<<4)|(1<<0); /* Set PF4 and PF0 as a digital input pins */
GPIOF->DIR |= (1<<3); /* Set PF3 as digital output to control green LED */
GPIOF->DIR |= (1<<1);
GPIOF->DIR |= (1<<2);
GPIOF->DEN |= (1<<4)|(1<<3)|(1<<0)|(1<<1) |(1<<2) ; /* make PORTF4-0 digital pins */
GPIOF->PUR |= (1<<4)|(1<<0); /* enable pull up for PORTF4, 0 */
/* configure PORTF4, 0 for falling edge trigger interrupt */
GPIOF->IS &= (1<<4)|(1<<0); /* make bit 4, 0 edge sensitive */
GPIOF->IBE &=(1<<4)|(1<<0); /* trigger is controlled by IEV */
GPIOF->IEV &= (1<<4)|(1<<0); /* falling edge trigger */
GPIOF->ICR |= (1<<4)|(1<<0); /* clear any prior interrupt */
GPIOF->IM |= (1<<4)|(1<<0); /* unmask interrupt */
/* enable interrupt in NVIC and set priority to 3 */
NVIC->IP[30] = 3 << 5; /* set interrupt priority to 3 */
NVIC->ISER[0] |= (1<<30); /* enable IRQ30 (D30 of ISER[0]) */
while(1)
{
GPIOF->DATA |= (1<<1);
}
}
/* SW1 is connected to PF4 pin, SW2 is connected to PF0. */
/* Both of them trigger PORTF falling edge interrupt */
void GPIOF_Handler(void)
{
volatile unsigned long ulLoop ;
SYSCTL->RCGCGPIO |= (1<<5); /* Set bit5 of RCGCGPIO to enable clock to PORTF*/
if (GPIOF->MIS & 0x10) /* check if interrupt causes by PF4/SW1*/
{while(1){
GPIOF->DATA &= ~(1<<1);//clear red
GPIOF->DATA |= (1<<3);//green
for ( ulLoop = 0; ulLoop < DELAY; ulLoop++)
{
}
GPIOF->DATA &= ~(1<<3);// clear green
GPIOF->DATA &= ~(1<<1);//clear red
GPIOF->DATA |= (1<<1);//red
for ( ulLoop = 0; ulLoop < DELAY; ulLoop++)
{
}
GPIOF->DATA &= ~(1<<3);// clear green
GPIOF->DATA &= ~(1<<1);//clear red
GPIOF->ICR |= 0x10; /* clear the interrupt flag */
if (GPIOF->MIS & 0x01) break;
}}
else if (GPIOF->MIS & 0x01) /* check if interrupt causes by PF0/SW2 */
{ while(1){
GPIOF->DATA &= ~(1<<1);
GPIOF->DATA &= ~(1<<3);// clear green
GPIOF->DATA &= ~(1<<2);//clear red
GPIOF->DATA |= (1<<2);//blue
for ( ulLoop = 0; ulLoop < DELAY; ulLoop++)
{
}
GPIOF->DATA &= ~(1<<1);
GPIOF->DATA &= ~(1<<3);// clear green
GPIOF->DATA &= ~(1<<2);//clear red
GPIOF->DATA |= (1<<3);//blue
for ( ulLoop = 0; ulLoop < DELAY; ulLoop++)
{
}
GPIOF->DATA &= ~(1<<2);
GPIOF->ICR |= 0x01; /* clear the interrupt flag */
if (GPIOF->MIS & 0x10) break;
}
}
}
LED, SWITCH
Using GPIO with interrupt and timers with interrupt, write a program and load it on the board, such that:
1.When the user presses the left switch, the green onboard LED lights on for 2 seconds, then the red LED lights for 4 seconds in an infinite loop.
2.when the user presses the right switch, the sequence in reversed ,ie, the red LED lights on for 2 seconds and green LEDs for 4 seconds.
/*PORTF PF0 and PF4 fall edge interrupt example*/
/*This GPIO interrupt example code controls green LED with switches SW1 and SW2 external interrupts */
#define DELAY 16000000
#define SYSCTL_RCGCGPIO_R (*((volatile unsigned long *)0x400FE608))
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
#include "TM4C123.h" // Device header
int main(void)
{
SYSCTL_RCGCGPIO_R = 0x20;
/* PORTF0 has special function, need to unlock to modify */
GPIOF->LOCK = 0x4C4F434B; /* unlock commit register */
GPIOF->CR = 0x01; /* make PORTF0 configurable */
GPIOF->LOCK = 0; /* lock commit register */
/*Initialize PF3 as a digital output, PF0 and PF4 as digital input pins */
GPIOF->DIR &= (1<<4); /* Set PF4 as a digital input pins */
GPIOF->DIR |= (1<<3); /* Set PF3 as digital output to control green LED */
GPIOF->DIR |= (1<<1); /* Set PF1 as digital output to control red LED */
GPIOF->DIR |= (1<<2); /* Set PF2 as digital output to control blue LED */
GPIOF->DEN |= (1<<4)|(1<<3)|(1<<1) |(1<<2) ; /* make PORTF4-0 digital pins */
GPIOF->PUR |= (1<<4); /* enable pull up for PORTF4, 0 */
/* configure PORTF4, 0 for falling edge trigger interrupt */
GPIOF->IS &= (1<<4); /* make bit 4, 0 edge sensitive */
GPIOF->IBE &=(1<<4); /* trigger is controlled by IEV */
GPIOF->IEV |= (1<<4); /* rising edge trigger */
GPIOF->ICR |= (1<<4); /* clear any prior interrupt */
GPIOF->IM |= (1<<4); /* unmask interrupt */
/* enable interrupt in NVIC and set priority to 3 */
NVIC->IP[30] = 3 << 5; /* set interrupt priority to 3 */
NVIC->ISER[0] |= (1<<30); /* enable IRQ30 (D30 of ISER[0]) */
while(1)
{
}
}
/* SW1 is connected to PF4 pin, SW2 is connected to PF0. */
/* Both of them trigger PORTF falling edge interrupt */
void GPIOF_Handler(void)
{
volatile unsigned long ulLoop ;
volatile unsigned long red_light_seconds ;
volatile unsigned long green_light_seconds ;
SYSCTL->RCGCGPIO |= (1<<5); /* Set bit5 of RCGCGPIO to enable clock to PORTF*/
while(1)
{
GPIOF->DATA &= ~(1<<1);//clear red
GPIOF->DATA |= (1<<3);//green
for(green_light_seconds=0;green_light_seconds<2;green_light_seconds++)
{
for ( ulLoop = 0; ulLoop < DELAY; ulLoop++)
{
}
}
GPIOF->DATA &= ~(1<<3);// clear green
GPIOF->DATA &= ~(1<<1);//clear red
GPIOF->DATA |= (1<<1);//red
for(red_light_seconds=0;red_light_seconds<4;red_light_seconds++)
{
for ( ulLoop = 0; ulLoop < DELAY; ulLoop++)
{
}
}
GPIOF->DATA &= ~(1<<3);// clear green
GPIOF->DATA &= ~(1<<1);//clear red
GPIOF->ICR |= 0x10; /* clear the interrupt flag */
if (GPIOF->MIS & 0x01) break;
}
}