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 }}
Have you tried the sample code provided with the app-note using the FPB:
RL78/G22 Timer Array Unit (PWM output)
If you auto-generated the code, make sure the global interrupts are enabled (i.e. execute the "EI" instruction).
Are you using the generated functions from the Smart Configurator ? I can share a demo project for an external interrupt if it would be helpful.
Yeah sure please share it.
Please find the attached project on G22 FPB which triggers an external interrupt on press of the user switch.
G22_FPB_ext_interrupt.zip
yeah ok thanks. I have seen the code and I have a doubt only the timer function is started how it will be linking to the switch which will be acting as an external interrupt?
Do you want the the external interrupt to trigger the start of PWM timer ?
Yes, I have to configure like that. I have to develop a PWM output signal with high time as 1 second and low time as 3 second using PWM peripheral of mcu and it should start producing PWM signals when a switch is pressed.
Ok, I will try to create a demo project with this functinality.