Derive PWM using Timer/ Interrupts
Published: 2023-11-17 10:41:49

Hello Readers,
I'm Sana, and I'm a Junior Software Developer with Luxoft India. I'm happy to share this post, in which I relate my previous experience with Derive the PWM using timer/interrupt.

**Introduction**
PWM stands for Pulse Width Modulation. It's a technique that controls analog devices with a digital output. 
PWM is used to generate analog signals from digital devices like microcontrollers. The signal produced is a train of square wave pulses. The wave is either high or low at any given time
The objective of deriving PWM (Pulse Width Modulation) using timers and interrupts is to achieve precise control of the output pulse width and frequency. PWM is a commonly used technique in embedded systems and electronic devices to control the speed of motors, brightness of LEDs, Heating and cooling control, and various other applications where precise control of the output signal is required.

**Hardware and Software setup**

- Micro Controller:
Renesas MCU : RL78 / F12 series - R5F109GC- 48PINS

- Equipment being used:

1. Renesas E2 Lite Debugger
2. CRO
3. Power Supply
4. USB Cable
5. CRO Probes

**Hardware Details**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zo4rkmuodcokmten4zui.png)

**Operational Modes:**



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvs4vr503asvq6xc8o55.png)

- OFF Mode:
In OFF State the ECU will transition from the ON Mode to the OFF Mode
when IGN_VOLTAGE < 5-VDC

- ON Mode:
In ON State the ECU will transition from the OFF Mode to the ON Mode
when IGN_VOLTAGE > 5-VDC.

- IDLE Mode:
When Ignition Voltage is 5-VDC > IGN_VOLTAGE < 10-VDC and user is not switching (not pressing any switch) Heater or Ventilation mode can be consider as IDLE Mode.
It will default to the IDLE sub- mode when in the OPERATIONAL Mode.
In Idle Mode, ECU is in RUN condition , However all the outputs are disable.

- Normal Operation Mode :
It transits from IDLE Mode to Normal Mode when 10-VDC > IGN_VOLTAGE < 15-VDC

- Heat Mode:
The ECU will transition from the IDLE Mode to HEAT Mode when Heat Mode Switch is pressed while ECU in Normal Operation Mode.
The ECU will transition from the VENT Mode to HEAT Mode when Heat Mode Switch is pressed while ECU in Normal Operation Mode.

- Vent (Blower) Mode:
The ECU will transition from the IDLE Mode to Vent Mode when Vent Mode Switch is pressed while ECU in Normal Operation Mode.
The ECU will transition from the HEAT Mode to VENT Mode when VENT Mode Switch is pressed while ECU in Normal Operation Mode

- Degraded Mode :
It transits from NORMAL HEAT or VENT Mode state to the DEGRADED HEAT state when IGN_VOLTAGE < 8.5-VDC.
It transits from the NORMAL HEAT or VENT Mode state to the DEGRADED HEAT state when IGN_VOLTAGE > 16.5-VDC.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vfyfpg555zeqxbdb45q.png)

- Fault State:
When user disconnect the Seat Cushion HMAT / Seat Back HMAT or BLOWER, then within 10-msec of Normal operation mode will transitioning to the FAULT state.
The ECU shall disable HEATER or BLOWER Functionality within 10-msec transitioning to the FAULT state from Normal operation mode.
The ECU shall set HEAT_IND or VENT_IND to INACTIVE within 16-sec of transitioning to the FAULT state.

- Fault Protection:
The ECU shall be capable of operating after a fault is removed without incurring any physical damage or functional degradation.

**I/O Port configuration:**
The I/O Port Configuration is essential for establishing proper communication, facilitating PWM generation, interfacing with peripherals, and ensuring system compatibility in the project. It allows for precise control and validation of signals, enabling successful implementation of PWM using timers and interrupts.
In our Project as per the requirement we made I/o Configuration for all the Ports, based on the input received from the in the form of excel(I/O pin mapping).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlciqu2z0opn5yy08dr7.png)

**Timer Configuration**
Timer configuration is essential for precise timing, PWM generation, support for multiple channels, interrupt generation, efficient resource utilization, dynamic adjustments, compatibility with hardware, and system optimization. It enables the project to achieve accurate and controlled PWM signals, enhancing the functionality and performance of the overall system.
In our project we considered Renesas RL78 Microcontroller, which consists of eight 16 bit-timer, Considered channel 0 to channel7.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7me3rjpthuyrnqiw14mu.png)

**I/O Port Validation**

Validated the I/O Port using LED, by considering Port (P73) as output.
While we RUN the software, LED will be ready to glow.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frow91aez1al978r92n4.png)

**PWM Validation**
- PWM Implementation on Renesas Controller: 

 __Used Timer channels from channel0 to channel7__
                


Channel               Usage	

 0	--             _Master of Ch1_
 	
 1	--             _Slave of Ch1 (generates output TO01)_

 2	--             _Slave of Ch2 (generates output TO02)_
	
 3      --             _Interval timer Ch3_

 4	--            _ Master of Ch2 _
	
 5	--             _Slave of Ch5 (generates output TO05)_
	
 6	--             _Slave of Ch6 (generates output TO06)_
	
 7	--             Interval timer Ch7embed  



**PWM Feasibility on Evaluation Board:**
Renesas evaluation boards  have built-in hardware support for PWM generation.
The evaluation board usually includes timer modules, such as the TAU (Timer Array Unit), that can be utilized for generating PWM signals.


**PWM Validation:**

- TimerChannel1 as Slave and P16 as output, Pin in target board: channel1(CN1) 16pin, with duty cycle 50% 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0bjhmopmji7mjjr5lfj.png)

**Conclusion**
Pulse-width modulation (PWM) is a powerful technique for controlling the power delivered to a load. By varying the duty cycle of a PWM signal, the average power delivered to the load can be controlled. This technique is widely used in a variety of applications, including motor control, LED lighting, and power supplies. Renesas microcontrollers (MCUs) offer a variety of hardware and software features that can be used to generate PWM signals. The Timer/Interrupt units (TIUs) on Renesas MCUs are particularly well-suited for PWM generation, as they offer a high degree of flexibility and control.