I'm evaluating the Renesas RX65N MCU EV Kit. In this post: Expand the Blinky example with a timer and interrupt. This time it's a real blinky. The code to poll the user button is removed and the led is controlled by one of the onboard timers. |
Modify the Project
In the configurator, navigate to the Components tab, select the Config_PORT module and uncheck PORT0.
We don't use the switch anymore. That's why we can remove our claim to that port and pin.
Then add the following Timer blocks:
- Compare Match timer: this is a compare type timer that can throw an interrupt at expiration.
- r_cmt_rc for the API functions
Click Finish.
Save the configurator and Generate code.
Write the C Code
Activate the C/C++ perspective again and open RX65_blinky.c.
The reference to SW_2 is removed.
I'm also going to use an alternative option to set gpio outputs - without a function call. This requires a different declaration.
#include "r_gpio_rx_if.h" /* Contains prototypes for the GPIO driver */ #define LED_2 PORT7.PODR.BIT.B0 void main(void); void cmt_cb(void *pdata);
The main function no longer polls the switch. It prepares the timer (2 Hz, resulting in one blink per second), sets the interrupt handler then does nothing interesting.
void main(void) { bool cmt_result; uint32_t cmt_hdl; /* Create timer0 with callback at a rate of 2Hz. */ cmt_result = R_CMT_CreatePeriodic(2, cmt_cb, &cmt_hdl); if (false == cmt_result) { nop(); //Handle your error here. } /* nothing to do, spin here */ while(1); }
In the timer interrupt handler, the led is toggled. You can compare the different way toggling with the post 2a.
void cmt_cb(void *pdata) { uint32_t channel_num = *((uint32_t *)pdata); switch(channel_num) { case 0: LED_2 = ~LED_2; // Toggle LED2 with each callback break; default: break; } }
Build and debug. The blue led blinks.
The e2 studio project is attached.