Hi team,
I am using the RL78/G22 mcu with Fast prototyping board to develop the PWM output. In my scenario, I am using the switch as an external interrupt to trigger. But the interrupt function is not triggered when the switch was pressed, also the output duty cycle is 100% when I do a build and run the code. what is the issue here?
Hello,
Can you share your configurations/code ?
#include "r_cg_macrodriver.h"#include "r_cg_timer.h"#include "r_cg_userdefine.h"#include "iodefine.h"#include "r_cg_intc.h"
volatile bool pwm_enabled = false;#define PERIOD_TICKS 62499 // 4 seconds #define DUTY_TICKS 15624 // 1 second
void R_MAIN_UserInit(void);void R_TAU0_Channel0_Create(uint16_t period);void R_TAU0_Channel0_Start(void);void R_TAU0_Channel0_Stop(void);void R_TAU0_Channel2_Create(uint16_t duty);void R_TAU0_Channel2_Start(void);void R_TAU0_Channel2_Stop(void);
#pragma interrupt INTP1_ISR(vect=INTP1)void INTP1_ISR(void);
void R_MAIN_UserInit(void){ /* Enable TAU0 module */ PER0 |= 0x01; // Enable TAU0 PRR0 &= ~0x01; // Release TAU0 from reset /* Initialize TAU0 channels for PWM */ uint16_t period = PERIOD_TICKS; // Example period value uint16_t duty = DUTY_TICKS - 1; // Example duty value R_TAU0_Channel0_Create(period); R_TAU0_Channel2_Create(duty); R_TAU0_Channel0_Stop(); // Initially stop the PWM R_TAU0_Channel2_Stop(); // Initially stop the PWM /* Configure PWM Output Pin P0.1 (TO00) */ PMCT0 &= ~0x02; // Clear PMCT01 (Enable Timer Output Mode) PM0 &= ~0x02; // Set P0.1 as output P0 &= ~0x02; // Set P0.1 to Low PMCA0 &= ~0x02; // Clear PMCA01 (Enable TO00 function)
/* Configure switch pin as input with pull-up */ PM1 |= 0x02; // Set P1.1 as input PMCT1 &= ~0x02; // Enable INTP1 function (disable GPIO mode) PU1 |= 0x02; // Enable pull-up resistor on P1.1
/* Configure LED pin as output */ PM5 &= ~0x01; // Set P5.0 as output P5 &= ~0x01; // Initialize LED off
/* Configure external interrupt for switch */ EGN0 |= 0x02; // Enable falling edge detection on INTP1 EGP0 &= ~0x02; // Disable rising edge detection on INTP1 IF0L &= ~0x02; // Clear INTP1 interrupt flag MK0L &= ~0x02; // Enable INTP1 interrupt
/* Ensure ISC0 is cleared for normal operation */ ISC &= ~0x01;
/* Enable global interrupts */ EI();}
/* External interrupt service routine for switch press */#pragma interrupt INTP1_ISR(vect=INTP1)void INTP1_ISR(void){ pwm_enabled = !pwm_enabled; // Toggles the value each time switch pressed if (pwm_enabled) { R_TAU0_Channel0_Start(); R_TAU0_Channel2_Start(); printf("PWM Started\n"); } else { R_TAU0_Channel0_Stop(); R_TAU0_Channel2_Stop(); printf("PWM Stopped\n"); } IF0L &= ~0x02; // Clear flag}
/* Timer One (Master Timer) - Period: */void R_TAU0_Channel0_Create(uint16_t period){ TPS0 = 0x0030; // Set prescaler to divide by *** (assuming 1 MHz input clock) TMR00 = 0x0000; // Set to interval timer mode TDR00 = period; // Set period
TO0 |= _0001_TAU_CH0_OUTPUT_VALUE_1; // Set initial output value TOE0 |= _0001_TAU_CH0_OUTPUT_ENABLE; // Enable output printf("TAU0 Ch0 Configured\n"); // Set breakpoint here}
/* Timer Two (Slave Timer) - Duty Cycle: */void R_TAU0_Channel2_Create(uint16_t duty){ TPS0 = 0x0030; // Set prescaler TMR02 = 0x0000; // Set to one-count mode TDR02 = duty; // Set duty cycle
TO0 |= _0004_TAU_CH2_OUTPUT_VALUE_1; // Initial output TOE0 |= _0004_TAU_CH2_OUTPUT_ENABLE; // Enable output printf("TAU0 Ch2 Configured\n"); // Set breakpoint here}
void R_TAU0_Channel0_Start(void){ TMIF00 = 0U; /* clear INTTM00 interrupt flag */ TMMK00 = 0U; /* enable INTTM00 interrupt */ TOE0 |= _0001_TAU_CH0_OUTPUT_ENABLE; TS0 |= _0001_TAU_CH0_START_TRG_ON;}
void R_TAU0_Channel0_Stop(void){ TT0 |= _0001_TAU_CH0_STOP_TRG_ON; TOE0 &= (uint16_t)~_0001_TAU_CH0_OUTPUT_ENABLE; TMMK00 = 1U; /* disable INTTM00 interrupt */ TMIF00 = 0U; /* clear INTTM00 interrupt flag */}
void R_TAU0_Channel2_Start(void){ TMIF02 = 0U; /* clear INTTM02 interrupt flag */ TMMK02 = 0U; /* enable INTTM02 interrupt */ TOE0 |= _0004_TAU_CH2_OUTPUT_ENABLE; TS0 |= _0004_TAU_CH2_START_TRG_ON;}
void R_TAU0_Channel2_Stop(void){ TT0 |= _0004_TAU_CH2_STOP_TRG_ON; TOE0 &= (uint16_t)~_0004_TAU_CH2_OUTPUT_ENABLE; TMMK02 = 1U; /* disable INTTM02 interrupt */ TMIF02 = 0U; /* clear INTTM02 interrupt flag */}
void main(void){ R_MAIN_UserInit(); while (1U) { ; // Main loop }}
ELC does not support TAU PWM function so it cannot be used. However I have done this in a software way. Once you press the user switch the PWM output starts from P16.
G22_FPB_ext_interrupt_TAU.zip
Thanks, I have done the modification like you mentioned P16 for switch input but when I press the switch, I cannot see any PWM output?I can only see if I press the switch, it becomes high and then becomes low.
What do you see on P16 pin ?
when I press the switch it becomes high then low.
This is the output from P16:
ok but i have checked the configurations also tried to run the code but, in the simulator, it was not executing. I have selected the pin P137/INTP0 to check the duty % which was connected to the o/p LED component.
Since the application works fine I am not sure what your question is exactly at this point.
No, actually I am running this code in the Renesas online simulator. will it work on this platform? or I need a hardware setup to run this code
As far as I see from your screenshots it works with the simulator too.
yeah, now it was working fine i selected the wrong pin. Thanks for the help.