Slotted non-blocking delays example (L152)

Name of the project

l152_slotted_nb_delays

Description

Application will simultaneously blink 4 LEDs at 4 different speeds. This is a demonstration for the library presented in the previous article. It is a simple app, but more complex ones can be written like simultaneously scrolling a text on 4 (or more) 7seg LED displays. reading temperature sensors, sending data on serial, displaying data on an alpha numeric LCD, while “waiting” for a button to be pressed, etc. You use the VPC to set the board clocks and the four output pins. In my example I labeled them from LED1 to LED4

Code snippet: the required headers and definitions (some are already inserted by the VPC):

#include "stdint.h"
#include "main.h"
#include "my_gpio.h"
#include "my_uart.h"
#define TIM6_SNBD_FREQ 1000  // every 1 millisecond
#define TIM6_SNBD_NR_SLOTS 4 // we are using 4 different delays
#include "tim6_slotted_nb_delays.h"

Code snippet: the main code of the application (well, the user written part):

#define LED1_DELAY_IDX 0 // first delay has 0 index as is a place in an array
#define LED2_DELAY_IDX 1
#define LED3_DELAY_IDX 2
#define LED4_DELAY_IDX 3

#define LED1_DELAY 250   // delay in milliseconds
#define LED2_DELAY 500
#define LED3_DELAY 750
#define LED4_DELAY 1000 
  
int main(void){
  /* local variables */
  
  /* mandatory system initializations */
  vpc_system_init();
  //vpc_rtc_init(); /* enable if required */  
  vpc_gpio_init();
  //vpc_usart2_uart_init(); /* enable if required */ 
  /* third-party initializations */
  
  /* do your own initializations below */
  tim6_snbd_init();
  tim6_snbd_set(LED1_DELAY_IDX, LED1_DELAY);
  tim6_snbd_set(LED2_DELAY_IDX, LED2_DELAY);
  tim6_snbd_set(LED3_DELAY_IDX, LED3_DELAY);
  tim6_snbd_set(LED4_DELAY_IDX, LED4_DELAY);
  
  while(1){
    /* your forever repeating code */
    if (tim6_snbd_check(LED1_DELAY_IDX)){
      GPIO_ToggleBits(LED1_Port, LED1_Pin);
      tim6_snbd_set(LED1_DELAY_IDX, LED1_DELAY);
    }
    if (tim6_snbd_check(LED2_DELAY_IDX)){
      GPIO_ToggleBits(LED2_Port, LED2_Pin);
      tim6_snbd_set(LED2_DELAY_IDX, LED2_DELAY);
    }
    if (tim6_snbd_check(LED3_DELAY_IDX)){
      GPIO_ToggleBits(LED3_Port, LED3_Pin);
      tim6_snbd_set(LED3_DELAY_IDX, LED3_DELAY);
    }
    if (tim6_snbd_check(LED4_DELAY_IDX)){
      GPIO_ToggleBits(LED4_Port, LED4_Pin);
      tim6_snbd_set(LED4_DELAY_IDX, LED4_DELAY);
    }

  }
  return 0;
}
 

Note

This example require version 3.3.3 or newer of VPC.

Project published in repository.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.