It’s Relax Time: Button Read

In the previous posting, we’ve learn how to make a program using DAVE IDE from zero. We have to understand the registers that we used in the program, without library, so we have to read the reference manual of XMC4500. I think it is good for beginner, like me. So, we didn’t depend on the library from the IDE. Or if the library didn’t match with what we want, we can make our own library. 🙂

Now, let’s start to use DAVE advance features. Just create a new project (Dave Poject), choose Easy Start Project.

Then open Main.c.

/* SFR declarations of the selected device */
#include <XMC4500.h>
#include "GPIO.h"

void SysTick_Handler(void);

typedef enum { OFF = 0, ON = 1} state_t ;
state_t led1_state = ON;
state_t led2_state = ON;

int main(void) {
/* Initialize LED1 */
 P1_1_set_mode(OUTPUT_PP_GP);
P1_1_set_driver_strength(STRONG);

/* Initialize LED2 */
P1_0_set_mode(OUTPUT_PP_GP);
P1_0_set_driver_strength(STRONG);

/* Initialize BUTTON1 */
P1_14_set_mode(INPUT);

/* Initialize BUTTON2 */
P1_15_set_mode(INPUT);

/* fSYS=fCPU at 120MHz */
/* Systick every 120000 cycles = 10ms */
if(SysTick_Config(SystemCoreClock / 100UL) == 0){

/* Loop forever */
for(;;) {
}
}
else{
asm("BKPT 255");
}
}

void SysTick_Handler(void) {
static uint32_t ticks = 0UL;
static state_t button1_state = OFF;

static state_t button2_state = OFF;
ticks++;

/* Read BUTTON1, update state if pressed */
if(P1_14_read() == 0UL){
 button1_state = ON;
}
else{
 if(button1_state == ON){
 if(led1_state == ON){
  led1_state = OFF;
}
else{
led1_state = ON;
}
}
button1_state = OFF;
}

/* Read BUTTON2, update state if pressed */
if(P1_15_read() == 0UL){
button2_state = ON;
}
else{
if(button2_state == ON){
if(led2_state == ON){
led2_state = OFF;
}
else{
led2_state = ON;
}
}
button2_state = OFF;
}

/* Toggle every 1s */
if(ticks == 100UL){
if(led1_state == ON){
/* Toggle LED1 */
P1_1_toggle();
}

if(led2_state == ON){
/* Toggle LED2 */
P1_0_toggle();
}
ticks = 0UL;
}
}

Check the program, there is no register name!. Yeah, it’s already write in “GPIO.h”. We just call the function. As we know that there are 2 LED in Relax Kit that connected to P1.0 and P1.1, and also there are 2 buttons connected to P1.14 and P1.15. To initialize P1.0 and P1.1 as output with strong driver mode we will write

/* Initialize LED1 */
P1_1_set_mode(OUTPUT_PP_GP);
P1_1_set_driver_strength(STRONG);

/* Initialize LED2 */
P1_0_set_mode(OUTPUT_PP_GP);
P1_0_set_driver_strength(STRONG);

And P1.14 and P1.15:

 /* Initialize BUTTON1 */
P1_14_set_mode(INPUT);

/* Initialize BUTTON2 */
P1_15_set_mode(INPUT);

The program also use System Tick Timer at 10 ms.

But, I will make a change. The program will be changed to blink the LED, and we can adjust the blink rate by push the button, increase and decrease. System tick timer will be set to 1 ms. LED toggle in system tick timer interrupt handle, and the main program will poll/scan the 2 buttons, if Button1 (P1.14) is pressed, the thedelay value will be add by 10, then the LED will blink 10 ms slower. And if Button2 (P1.15) is pressed, the thedelay value will minus by 10, and the LED will blink 10 ms faster.

And here is the souce code..


/* SFR declarations of the selected device */

#include <XMC4500.h>

#include "GPIO.h"

void SysTick_Handler(void);
void delayms(uint32_t delay_ms);

uint32_t thedelay;
uint32_t ticks = 0UL;
uint32_t currentticks;

int main(void) {

/* Initialize LED1 */
P1_1_set_mode(OUTPUT_PP_GP);
P1_1_set_driver_strength(STRONG);

/* Initialize LED2 */
P1_0_set_mode(OUTPUT_PP_GP);
P1_0_set_driver_strength(STRONG);

/* Initialize BUTTON1 */
P1_14_set_mode(INPUT);

/* Initialize BUTTON2 */
P1_15_set_mode(INPUT);

thedelay= 500;
ticks=0;
currentticks=ticks;

/* fSYS=fCPU at 120MHz */
/* Systick every 120000 cycles = 1ms */
SysTick_Config(SystemCoreClock / 1000UL);

while(1)
{
   if(P1_14_read()== 0UL)
    {
      delayms(100);  //Anti debounce
      while(P1_14_read()==0UL); //Wait until button released
      thedelay+=10;
      if(thedelay>1000)
         thedelay=1000;
    }

  if(P1_15_read()== 0UL)
   {
     delayms(100);
     while(P1_15_read()==0UL);
     thedelay-=10;
     if(thedelay<;10)
       thedelay=10;
   }
}
}

void delayms(uint32_t delay_ms)
{
  uint32_t currenttick;

  currenttick=ticks;
  while(ticks-currenttick<delay_ms);
}

void SysTick_Handler(void) {

 ticks++;
 if(ticks-currentticks>=thedelay)
 {
   P1_0_toggle();
   P1_1_toggle();
   currentticks=ticks;
 }
}

And check the video

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: