SlideShare a Scribd company logo
Microcontroller Lab.
Eng.Khaled Tamziz
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 1
PPU IUT Cachan
Mechanical Department
Mechatronic
Timers and PWM
Bibliography
Microcontroller PIC18F4550 Data Sheet
39632c.pdf - Chapters 11 to 15
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 2
MPLAB_C18 libraries documentation
MPLAB_C18_Libraries_51297f.pdf - Chapters 2.7 and 2.9
MPLAB_C18 header files
timers.h and pwm.h
MPLAB_C18 C source files
mcc18srctraditionnalpmc
Introduction
• In this course, we will talk about :
– Timers
– DC motor speed control (shortly)
– Pulse width modulation
– Basic state machine diagrams
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 3
– Basic state machine diagrams
Timers
• 4 timers are available :
– Timer0
– Timer1
– Timer2
– Timer3
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 4
• Each of them has general and special features.
• We will study general features.
• Then we’ll use special features with Timer2.
• It is necessary to understand the microcontroller
data sheet to be able to use the C hardware libraries.
Basic timer block diagram
8 or 16-bit counterClock
TMRxON bit
TMRxIF bit
set on
overflow
---
cleared by
software
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 5
Clock selection
Multiplexer
Programmable Prescaler
Sync with internal clock
Internal
or
external
clock
sources
software
Microcontroller internal data bus
read or write
the counter
register
.
.
.
Timers configuration and use
• C18 timers libraries allow :
– to access configuration registers and start the timer
(OpenTimerx functions).
– to read the value of the counter register
(ReadTimerx functions).
– to write a new value to the counter register
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 6
– to write a new value to the counter register
(WriteTimerx functions).
– to stop the timer (CloseTimerx functions).
• To test overflow, we need to read TMRxIF bit directly
in its own register.
• The Microcontroller Data Sheet gives information
about the concerned registers.
Rotation measurement
signal
LEFT_ENCODER
R1 R2
VCC
4
31
2
U3
H21A1
VCC
Simplified schematic
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 7
time
Each edge corresponds to a rotation of a known angle of the axis of the motor.
We know the reducer rate and the diameter of the wheel, so it is possible to calculate
the distance accomplished by the robot.
We have to count edges : we ‘ll use a timer with an external clock.
Example : use of Timer1 to count events
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 8
Configure and start Timer1
OpenTimer1(
TIMER_INT_OFF &
T1_16BIT_RW &
T1_SOURCE_EXT &
T1_PS_1_1 &
T1_OSC1EN_OFF &
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 9
T1_OSC1EN_OFF &
T1_SYNC_EXT_OFF
);
Disable the interrupt generation from
Timer1. We will see this later.
Use Timer1 as a 16-bit counter.
Select an external clock.
Choose the prescale value.
Disable the internal Timer1 oscillator.
Disable the synchronization with the
processor clock.
Then, clear the counter register, clear TMR1IF and start the timer.
Read and write the Timer1 value
• Read the value :
– prototype
unsigned int ReadTimer1( void );
– code example
unsigned int result;
result = ReadTimer1();
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 10
• Write a new value :
– prototype
void WriteTimer1( unsigned int timer );
– code example
WriteTimer1(0);
Test the Timer1 overflow
• TMR1IF is the bit 0 of PIR1 register.
• Code example :
if (PIR1bits.TMR1IF != 0) {
// timer1 overflow detected
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 11
// timer1 overflow detected
PIR1bits.TMR1IF = 0; // reset flag
}
Timer2 special features
• Timer2 is an 8-bit counter.
• The values of TMR2 and PR2 are compared on each clock cycle.
• When the two values are equal, the comparator generates an output
signal.
• This signal also resets the value of TMR2 to 00h on the next cycle
and drives the output counter/postscaler.
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 12
DC motor speed control
• First, we assume the motor speed depends on the average voltage. Thus,
we need to change the average voltage on the motor.
• The power electronics block contains a H-Bridge designed for motion
Power electronics
DC
motor
sign
pwm
POWER
Vmotor
from
microcontroller
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 13
• The power electronics block contains a H-Bridge designed for motion
control applications.
• The signal sign controls the direction of the rotation.
• The signal pwm controls the average voltage on the motor.
0
1
t
T
α.T
(1 - α).T
pwm
– T is the period.
– The duty cycle (α) is generally given in percent.
– The average value is α.
– For a power supply of U volts, Vmotor = α.U.
Capture/Compare/PWM (CCP) Modules
• Two CCP modules are available (CCP1 and CCP2).
• In PWM mode, the two modules use Timer2 as a time base.
Thus PWM frequency is the same for CCP1 and CCP2.
• Each module has its own duty cycle.
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 14
PWM period and duty cycle
• The main trap is that the PWM period is set by a 8-bit
value and the duty cycle is controlled by a 10-bit
value.
• Example :
– let’s assume the period value is 99, thus the TMR2
value (8-bit value) should grow from 0 to (99+1) = 100
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 15
value (8-bit value) should grow from 0 to (99+1) = 100
(reset value)
– so the TMR2 extended value (10-bit value) should
grow from 0 to (99+1) * 4 = 400
– and then the duty cycle value should vary from 0 (0%
duty cycle) to 400 (100% duty cycle).
PWM configuration and use
• C18 timers libraries allow :
– to configure Timer2 (OpenTimer2 function).
– to configure CCPx (OpenPWMx functions).
– to set the duty cycle (SetDCPWMx functions).
– to stop the PWM (ClosePWMx functions).
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 16
• The pins concerned must be configured as outputs
– by setting the appropriate TRIS register bit
– or by using SetOutputPWMx functions.
Example
• Let’s assume that we need a 20kHz PWM period.
– On our board, Fosc = 48MHz thus Fosc/4 = 12MHz.
– To have a full resolution (10-bit) we must choose the
biggest value for the 8-bit period register.
– If we choose 0xFF, the clock frequency will be divided
by 256 to obtain the Timer2 frequency.
– Now, we are able to calculate the prescaler :
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 17
– Now, we are able to calculate the prescaler :
(12.106 / 256) / 20000 = 2.34 < 4
– We have to reverse the calculus with a prescale value
of 4 and we’ll find the period register value :
PR2 = (12.106 / 4) / 20000 – 1 = 149
– Thus the duty cycle should be between 0 and 600.
Code example
#include <timers.h>
#include <pwm.h>
…
OpenTimer2( // Timer2 configuration
TIMER_INT_OFF & // No interrupt
T2_PS_1_4 & // Prescale 1:4
T2_POST_1_1 // Don’t use postscaler actually
);
OpenPWM1(149); // Use of CCP1 as PWM module
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 18
OpenPWM1(149); // Use of CCP1 as PWM module
OpenPWM2(149); // Use of CCP2 as PWM module
SetDCPWM1(0); // Set 0% duty cycle to PWM1
SetDCPWM2(0); // Set 0% duty cycle to PWM2
TRISCbits.TRISC2 = 0; // Make RC2 (PWM1) an output
TRISCbits.TRISC1 = 0; // Make RC1 (PWM2) an output
…
SetDCPWM1(150); // Set 25% duty cycle to PWM1
SetDCPWM2(450); // Set 75% duty cycle to PWM2
…
State machine
• We will use state machine diagram to program
sequential comportment of the robot.
• We will translate the diagram into C language by
using switch/case statements.
• We will study an example to understand clearly the
principles.
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 19
principles.
• We would like the two wheels to cover the same
distance.
• The simplest way to do it is exposed on the next
slide.
Example of state machine diagram
0
1
set the distance to cover
BP0 pushed
two motors on
left
distance
done
right
distance
done
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 20
32 left motor onright motor on
right
distance
done
left
distance
done
Code example
…
void main(void)
{
//local variables definition and initialization
unsigned char State = 0; // Store the actual state
char Bp0; // Store 1 if BP0 is pushed
unsigned int LeftEncoder, // Store Timer1 value
RightEncoder, // Store Timer0 value
Distance; // Store "distance" to cover
//peripheral configuration
…
while(1) { // main loop
Bp0 = !(PORTBbits.RB3); // Read BP0 status
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 21
Bp0 = !(PORTBbits.RB3); // Read BP0 status
RightEncoder = ReadTimer0(); // Read right encoder value
LeftEncoder = ReadTimer1(); // Read left encoder value
switch (State) { // State machine
case 0 : // State 0
SetDCPWM1(0); // Set 0% duty cycle to left pwm
SetDCPWM2(0); // Set 0% duty cycle to right pwm
if (Bp0) { // BP0 is pushed
State = 1; // Go to State 1
WriteTimer0(0); // Reset encoders
WriteTimer1(0);
Distance = 1000; // Set distance to cover
}
break; // end case 0
case 1 :
SetDCPWM1(150); // Set 25% duty cycle to left pwm
SetDCPWM2(150); // Set 25% duty cycle to right pwm
if (LeftEncoder >= Distance) {
// Left wheel covers the distance
State = 2; // Go to State 2
} else if (RightEncoder >= Distance) {
// Right wheel covers the distance
State = 3; // Go to State 3
}
break; // end case 1
case 2 :
SetDCPWM1(0); // Set 0% duty cycle to left pwm
SetDCPWM2(150); // Set 25% duty cycle to right pwm
if (RightEncoder >= Distance) {
// Right wheel covers the distance
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 22
// Right wheel covers the distance
State = 0; // Go to State 0
}
break; // end case 2
case 3 :
SetDCPWM1(150); // Set 25% duty cycle to left pwm
SetDCPWM2(0); // Set 0% duty cycle to right pwm
if (LeftEncoder >= Distance) {
// Left wheel covers the distance
State = 0; // Go to State 0
}
break; // end case 3
} // end switch
} // end while
} // end main
Conclusion
• We have learnt
– how to use Timers
– how to control the speed of a DC motor
– how to generate PWM signals
– how to program a simple state machine
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 23
• We’ll use this knowledge
– to manage with distance measurement
– to control the speed of the robot
– to run a special trajectory
Now, practice
• Use timer1 to count events on left encoder
• Use timer0 to count events on right encoder
• Change speed of motors using PWM
• The robot covers a circle shaped trajectory
• The robot covers a house shaped trajectory
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 24

More Related Content

What's hot

8051 serial communication-UART
8051 serial communication-UART8051 serial communication-UART
8051 serial communication-UART
Pantech ProLabs India Pvt Ltd
 
Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
Shivashankar Sawalgi
 
Interrupts for PIC18
Interrupts for PIC18Interrupts for PIC18
Interrupts for PIC18
raosandy11
 
PIC Microcontrollers
PIC MicrocontrollersPIC Microcontrollers
PIC Microcontrollers
Abdullah Saghir Ahmad
 
PIC Microcontrollers.ppt
PIC Microcontrollers.pptPIC Microcontrollers.ppt
PIC Microcontrollers.ppt
Dr.YNM
 
Interrupts in pic
Interrupts in picInterrupts in pic
Interrupts in pic
v Kalairajan
 
80486 microprocessor
80486 microprocessor80486 microprocessor
80486 microprocessor
Mihika Shah
 
3.programmable interrupt controller 8259
3.programmable interrupt controller 82593.programmable interrupt controller 8259
3.programmable interrupt controller 8259
MdFazleRabbi18
 
Serial Communication in 8051
Serial Communication in 8051Serial Communication in 8051
Serial Communication in 8051
Sudhanshu Janwadkar
 
Memory organization of 8051
Memory organization of 8051Memory organization of 8051
Memory organization of 8051Muthu Manickam
 
8051 Presentation
8051 Presentation8051 Presentation
8051 Presentation
Sayan Chakraborty
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
Shreyans Pathak
 
Microcontroller-8051.ppt
Microcontroller-8051.pptMicrocontroller-8051.ppt
Microcontroller-8051.ppt
Dr.YNM
 
Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))Ganesh Ram
 
Architecture of 8051
Architecture of 8051Architecture of 8051
Architecture of 8051
hello_priti
 
8051 MICROCONTROLLER ARCHITECTURE.pptx
 8051 MICROCONTROLLER ARCHITECTURE.pptx 8051 MICROCONTROLLER ARCHITECTURE.pptx
8051 MICROCONTROLLER ARCHITECTURE.pptx
MemonaMemon1
 
MICROCONTROLLER - INTEL 8051
MICROCONTROLLER - INTEL 8051MICROCONTROLLER - INTEL 8051
MICROCONTROLLER - INTEL 8051
DEPARTMENT OF PHYSICS
 
I o ports.ppt
I o ports.pptI o ports.ppt
I o ports.ppt
Pradeep V Dev
 

What's hot (20)

8051 serial communication-UART
8051 serial communication-UART8051 serial communication-UART
8051 serial communication-UART
 
Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
 
Interrupts for PIC18
Interrupts for PIC18Interrupts for PIC18
Interrupts for PIC18
 
PIC Microcontrollers
PIC MicrocontrollersPIC Microcontrollers
PIC Microcontrollers
 
PIC Microcontrollers.ppt
PIC Microcontrollers.pptPIC Microcontrollers.ppt
PIC Microcontrollers.ppt
 
Interrupts in pic
Interrupts in picInterrupts in pic
Interrupts in pic
 
8051 interrupts
8051 interrupts8051 interrupts
8051 interrupts
 
Timers
TimersTimers
Timers
 
80486 microprocessor
80486 microprocessor80486 microprocessor
80486 microprocessor
 
3.programmable interrupt controller 8259
3.programmable interrupt controller 82593.programmable interrupt controller 8259
3.programmable interrupt controller 8259
 
Serial Communication in 8051
Serial Communication in 8051Serial Communication in 8051
Serial Communication in 8051
 
Memory organization of 8051
Memory organization of 8051Memory organization of 8051
Memory organization of 8051
 
8051 Presentation
8051 Presentation8051 Presentation
8051 Presentation
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
Microcontroller-8051.ppt
Microcontroller-8051.pptMicrocontroller-8051.ppt
Microcontroller-8051.ppt
 
Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))
 
Architecture of 8051
Architecture of 8051Architecture of 8051
Architecture of 8051
 
8051 MICROCONTROLLER ARCHITECTURE.pptx
 8051 MICROCONTROLLER ARCHITECTURE.pptx 8051 MICROCONTROLLER ARCHITECTURE.pptx
8051 MICROCONTROLLER ARCHITECTURE.pptx
 
MICROCONTROLLER - INTEL 8051
MICROCONTROLLER - INTEL 8051MICROCONTROLLER - INTEL 8051
MICROCONTROLLER - INTEL 8051
 
I o ports.ppt
I o ports.pptI o ports.ppt
I o ports.ppt
 

Similar to Lecture 2 timers, pwm, state machine IN PIC

Timer 2 concept
Timer 2 conceptTimer 2 concept
Timer 2 concept
v Kalairajan
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Sambasiva62
 
Program, Code of Program and Screen Shot of Output (UNIVERSAL DRIVER USING µ...
Program, Code of Program and  Screen Shot of Output (UNIVERSAL DRIVER USING µ...Program, Code of Program and  Screen Shot of Output (UNIVERSAL DRIVER USING µ...
Program, Code of Program and Screen Shot of Output (UNIVERSAL DRIVER USING µ...
Er. Ashish Pandey
 
chapter 4
chapter 4chapter 4
chapter 4
GAGANAP12
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
Omar Sanchez
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
Omar Sanchez
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
Omar Sanchez
 
Lecture on PIC-1.pptx
Lecture on PIC-1.pptxLecture on PIC-1.pptx
Lecture on PIC-1.pptx
godfrey35
 
Share 'speed control_of_dc_motor_using_microcontroller.pptx'[1][1]
Share 'speed control_of_dc_motor_using_microcontroller.pptx'[1][1]Share 'speed control_of_dc_motor_using_microcontroller.pptx'[1][1]
Share 'speed control_of_dc_motor_using_microcontroller.pptx'[1][1]
akmalKhan55
 
Enhanced phase width modulation in TMS320F28335D
Enhanced phase width modulation in TMS320F28335DEnhanced phase width modulation in TMS320F28335D
Enhanced phase width modulation in TMS320F28335D
Satya Sahoo
 
Ccp
CcpCcp
Timers and Endge-aligned PWM
Timers and Endge-aligned PWMTimers and Endge-aligned PWM
Timers and Endge-aligned PWM
Ariel Tonatiuh Espindola
 
Design and development of programmable controller for air sampling machine
Design and development of programmable controller for air sampling machineDesign and development of programmable controller for air sampling machine
Design and development of programmable controller for air sampling machine
eSAT Journals
 
Week7_lpm_2015.ppt
Week7_lpm_2015.pptWeek7_lpm_2015.ppt
Week7_lpm_2015.ppt
AmitKumar7572
 
PLC PPT.ppt
PLC PPT.pptPLC PPT.ppt
PLC PPT.ppt
AyyarKandasamy1
 
bidirectional report
bidirectional reportbidirectional report
bidirectional reportHasan baig
 
Advanced insrumentation lab manual
Advanced insrumentation lab manualAdvanced insrumentation lab manual
Advanced insrumentation lab manual
Gautam sai teza
 
ie450pp10.ppt
ie450pp10.pptie450pp10.ppt
ie450pp10.ppt
AhmetTuran47
 

Similar to Lecture 2 timers, pwm, state machine IN PIC (20)

Timer 2 concept
Timer 2 conceptTimer 2 concept
Timer 2 concept
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
PLC.pdf
PLC.pdfPLC.pdf
PLC.pdf
 
Program, Code of Program and Screen Shot of Output (UNIVERSAL DRIVER USING µ...
Program, Code of Program and  Screen Shot of Output (UNIVERSAL DRIVER USING µ...Program, Code of Program and  Screen Shot of Output (UNIVERSAL DRIVER USING µ...
Program, Code of Program and Screen Shot of Output (UNIVERSAL DRIVER USING µ...
 
chapter 4
chapter 4chapter 4
chapter 4
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Lecture on PIC-1.pptx
Lecture on PIC-1.pptxLecture on PIC-1.pptx
Lecture on PIC-1.pptx
 
Share 'speed control_of_dc_motor_using_microcontroller.pptx'[1][1]
Share 'speed control_of_dc_motor_using_microcontroller.pptx'[1][1]Share 'speed control_of_dc_motor_using_microcontroller.pptx'[1][1]
Share 'speed control_of_dc_motor_using_microcontroller.pptx'[1][1]
 
Enhanced phase width modulation in TMS320F28335D
Enhanced phase width modulation in TMS320F28335DEnhanced phase width modulation in TMS320F28335D
Enhanced phase width modulation in TMS320F28335D
 
Timers and pwm
Timers and pwmTimers and pwm
Timers and pwm
 
Ccp
CcpCcp
Ccp
 
Timers and Endge-aligned PWM
Timers and Endge-aligned PWMTimers and Endge-aligned PWM
Timers and Endge-aligned PWM
 
Design and development of programmable controller for air sampling machine
Design and development of programmable controller for air sampling machineDesign and development of programmable controller for air sampling machine
Design and development of programmable controller for air sampling machine
 
Week7_lpm_2015.ppt
Week7_lpm_2015.pptWeek7_lpm_2015.ppt
Week7_lpm_2015.ppt
 
PLC PPT.ppt
PLC PPT.pptPLC PPT.ppt
PLC PPT.ppt
 
bidirectional report
bidirectional reportbidirectional report
bidirectional report
 
Advanced insrumentation lab manual
Advanced insrumentation lab manualAdvanced insrumentation lab manual
Advanced insrumentation lab manual
 
ie450pp10.ppt
ie450pp10.pptie450pp10.ppt
ie450pp10.ppt
 

More from أشرف أمجد الشريف

Single phase im-lecture_10_1
Single phase im-lecture_10_1Single phase im-lecture_10_1
Single phase im-lecture_10_1
أشرف أمجد الشريف
 
Sensors, Proximity sensors. Optical – Through-beam, Optical - Diffuse
Sensors, Proximity sensors. Optical – Through-beam, Optical - DiffuseSensors, Proximity sensors. Optical – Through-beam, Optical - Diffuse
Sensors, Proximity sensors. Optical – Through-beam, Optical - Diffuse
أشرف أمجد الشريف
 
Lecture 4 i2 c bus & interrupts
Lecture 4   i2 c bus & interruptsLecture 4   i2 c bus & interrupts
Lecture 4 i2 c bus & interrupts
أشرف أمجد الشريف
 
The Uses of Nanosensors: From Smart Dust to Nano “Mother Ships”
The Uses of Nanosensors: From Smart Dust to Nano “Mother Ships”The Uses of Nanosensors: From Smart Dust to Nano “Mother Ships”
The Uses of Nanosensors: From Smart Dust to Nano “Mother Ships”
أشرف أمجد الشريف
 
Hydraulic pumps performance and Characteristics
Hydraulic pumps performance and CharacteristicsHydraulic pumps performance and Characteristics
Hydraulic pumps performance and Characteristics
أشرف أمجد الشريف
 
Hydrolic Fluid purpose & properties (chapter 2)
Hydrolic Fluid purpose & properties (chapter 2)Hydrolic Fluid purpose & properties (chapter 2)
Hydrolic Fluid purpose & properties (chapter 2)
أشرف أمجد الشريف
 
Lecture 4 i2 c bus & interrupts
Lecture 4   i2 c bus & interruptsLecture 4   i2 c bus & interrupts
Lecture 4 i2 c bus & interrupts
أشرف أمجد الشريف
 
Lecture 1 microcontroller overview
Lecture 1   microcontroller overviewLecture 1   microcontroller overview
Lecture 1 microcontroller overview
أشرف أمجد الشريف
 
Braking performance 4
Braking  performance 4Braking  performance 4
Braking performance 4
أشرف أمجد الشريف
 
VEHICLE ROLLOVER ANALYSIS
VEHICLE ROLLOVER ANALYSISVEHICLE ROLLOVER ANALYSIS
VEHICLE ROLLOVER ANALYSIS
أشرف أمجد الشريف
 

More from أشرف أمجد الشريف (11)

Single phase im-lecture_10_1
Single phase im-lecture_10_1Single phase im-lecture_10_1
Single phase im-lecture_10_1
 
Sensors, Proximity sensors. Optical – Through-beam, Optical - Diffuse
Sensors, Proximity sensors. Optical – Through-beam, Optical - DiffuseSensors, Proximity sensors. Optical – Through-beam, Optical - Diffuse
Sensors, Proximity sensors. Optical – Through-beam, Optical - Diffuse
 
Lecture 4 i2 c bus & interrupts
Lecture 4   i2 c bus & interruptsLecture 4   i2 c bus & interrupts
Lecture 4 i2 c bus & interrupts
 
The Uses of Nanosensors: From Smart Dust to Nano “Mother Ships”
The Uses of Nanosensors: From Smart Dust to Nano “Mother Ships”The Uses of Nanosensors: From Smart Dust to Nano “Mother Ships”
The Uses of Nanosensors: From Smart Dust to Nano “Mother Ships”
 
Hydraulic pumps performance and Characteristics
Hydraulic pumps performance and CharacteristicsHydraulic pumps performance and Characteristics
Hydraulic pumps performance and Characteristics
 
Hydrolic Fluid purpose & properties (chapter 2)
Hydrolic Fluid purpose & properties (chapter 2)Hydrolic Fluid purpose & properties (chapter 2)
Hydrolic Fluid purpose & properties (chapter 2)
 
Lecture 4 i2 c bus & interrupts
Lecture 4   i2 c bus & interruptsLecture 4   i2 c bus & interrupts
Lecture 4 i2 c bus & interrupts
 
Lecture 3a analog to digital converter
Lecture 3a   analog to digital converterLecture 3a   analog to digital converter
Lecture 3a analog to digital converter
 
Lecture 1 microcontroller overview
Lecture 1   microcontroller overviewLecture 1   microcontroller overview
Lecture 1 microcontroller overview
 
Braking performance 4
Braking  performance 4Braking  performance 4
Braking performance 4
 
VEHICLE ROLLOVER ANALYSIS
VEHICLE ROLLOVER ANALYSISVEHICLE ROLLOVER ANALYSIS
VEHICLE ROLLOVER ANALYSIS
 

Recently uploaded

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 

Recently uploaded (20)

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 

Lecture 2 timers, pwm, state machine IN PIC

  • 1. Microcontroller Lab. Eng.Khaled Tamziz Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 1 PPU IUT Cachan Mechanical Department Mechatronic
  • 2. Timers and PWM Bibliography Microcontroller PIC18F4550 Data Sheet 39632c.pdf - Chapters 11 to 15 Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 2 MPLAB_C18 libraries documentation MPLAB_C18_Libraries_51297f.pdf - Chapters 2.7 and 2.9 MPLAB_C18 header files timers.h and pwm.h MPLAB_C18 C source files mcc18srctraditionnalpmc
  • 3. Introduction • In this course, we will talk about : – Timers – DC motor speed control (shortly) – Pulse width modulation – Basic state machine diagrams Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 3 – Basic state machine diagrams
  • 4. Timers • 4 timers are available : – Timer0 – Timer1 – Timer2 – Timer3 Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 4 • Each of them has general and special features. • We will study general features. • Then we’ll use special features with Timer2. • It is necessary to understand the microcontroller data sheet to be able to use the C hardware libraries.
  • 5. Basic timer block diagram 8 or 16-bit counterClock TMRxON bit TMRxIF bit set on overflow --- cleared by software Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 5 Clock selection Multiplexer Programmable Prescaler Sync with internal clock Internal or external clock sources software Microcontroller internal data bus read or write the counter register . . .
  • 6. Timers configuration and use • C18 timers libraries allow : – to access configuration registers and start the timer (OpenTimerx functions). – to read the value of the counter register (ReadTimerx functions). – to write a new value to the counter register Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 6 – to write a new value to the counter register (WriteTimerx functions). – to stop the timer (CloseTimerx functions). • To test overflow, we need to read TMRxIF bit directly in its own register. • The Microcontroller Data Sheet gives information about the concerned registers.
  • 7. Rotation measurement signal LEFT_ENCODER R1 R2 VCC 4 31 2 U3 H21A1 VCC Simplified schematic Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 7 time Each edge corresponds to a rotation of a known angle of the axis of the motor. We know the reducer rate and the diameter of the wheel, so it is possible to calculate the distance accomplished by the robot. We have to count edges : we ‘ll use a timer with an external clock.
  • 8. Example : use of Timer1 to count events Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 8
  • 9. Configure and start Timer1 OpenTimer1( TIMER_INT_OFF & T1_16BIT_RW & T1_SOURCE_EXT & T1_PS_1_1 & T1_OSC1EN_OFF & Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 9 T1_OSC1EN_OFF & T1_SYNC_EXT_OFF ); Disable the interrupt generation from Timer1. We will see this later. Use Timer1 as a 16-bit counter. Select an external clock. Choose the prescale value. Disable the internal Timer1 oscillator. Disable the synchronization with the processor clock. Then, clear the counter register, clear TMR1IF and start the timer.
  • 10. Read and write the Timer1 value • Read the value : – prototype unsigned int ReadTimer1( void ); – code example unsigned int result; result = ReadTimer1(); Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 10 • Write a new value : – prototype void WriteTimer1( unsigned int timer ); – code example WriteTimer1(0);
  • 11. Test the Timer1 overflow • TMR1IF is the bit 0 of PIR1 register. • Code example : if (PIR1bits.TMR1IF != 0) { // timer1 overflow detected Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 11 // timer1 overflow detected PIR1bits.TMR1IF = 0; // reset flag }
  • 12. Timer2 special features • Timer2 is an 8-bit counter. • The values of TMR2 and PR2 are compared on each clock cycle. • When the two values are equal, the comparator generates an output signal. • This signal also resets the value of TMR2 to 00h on the next cycle and drives the output counter/postscaler. Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 12
  • 13. DC motor speed control • First, we assume the motor speed depends on the average voltage. Thus, we need to change the average voltage on the motor. • The power electronics block contains a H-Bridge designed for motion Power electronics DC motor sign pwm POWER Vmotor from microcontroller Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 13 • The power electronics block contains a H-Bridge designed for motion control applications. • The signal sign controls the direction of the rotation. • The signal pwm controls the average voltage on the motor. 0 1 t T α.T (1 - α).T pwm – T is the period. – The duty cycle (α) is generally given in percent. – The average value is α. – For a power supply of U volts, Vmotor = α.U.
  • 14. Capture/Compare/PWM (CCP) Modules • Two CCP modules are available (CCP1 and CCP2). • In PWM mode, the two modules use Timer2 as a time base. Thus PWM frequency is the same for CCP1 and CCP2. • Each module has its own duty cycle. Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 14
  • 15. PWM period and duty cycle • The main trap is that the PWM period is set by a 8-bit value and the duty cycle is controlled by a 10-bit value. • Example : – let’s assume the period value is 99, thus the TMR2 value (8-bit value) should grow from 0 to (99+1) = 100 Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 15 value (8-bit value) should grow from 0 to (99+1) = 100 (reset value) – so the TMR2 extended value (10-bit value) should grow from 0 to (99+1) * 4 = 400 – and then the duty cycle value should vary from 0 (0% duty cycle) to 400 (100% duty cycle).
  • 16. PWM configuration and use • C18 timers libraries allow : – to configure Timer2 (OpenTimer2 function). – to configure CCPx (OpenPWMx functions). – to set the duty cycle (SetDCPWMx functions). – to stop the PWM (ClosePWMx functions). Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 16 • The pins concerned must be configured as outputs – by setting the appropriate TRIS register bit – or by using SetOutputPWMx functions.
  • 17. Example • Let’s assume that we need a 20kHz PWM period. – On our board, Fosc = 48MHz thus Fosc/4 = 12MHz. – To have a full resolution (10-bit) we must choose the biggest value for the 8-bit period register. – If we choose 0xFF, the clock frequency will be divided by 256 to obtain the Timer2 frequency. – Now, we are able to calculate the prescaler : Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 17 – Now, we are able to calculate the prescaler : (12.106 / 256) / 20000 = 2.34 < 4 – We have to reverse the calculus with a prescale value of 4 and we’ll find the period register value : PR2 = (12.106 / 4) / 20000 – 1 = 149 – Thus the duty cycle should be between 0 and 600.
  • 18. Code example #include <timers.h> #include <pwm.h> … OpenTimer2( // Timer2 configuration TIMER_INT_OFF & // No interrupt T2_PS_1_4 & // Prescale 1:4 T2_POST_1_1 // Don’t use postscaler actually ); OpenPWM1(149); // Use of CCP1 as PWM module Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 18 OpenPWM1(149); // Use of CCP1 as PWM module OpenPWM2(149); // Use of CCP2 as PWM module SetDCPWM1(0); // Set 0% duty cycle to PWM1 SetDCPWM2(0); // Set 0% duty cycle to PWM2 TRISCbits.TRISC2 = 0; // Make RC2 (PWM1) an output TRISCbits.TRISC1 = 0; // Make RC1 (PWM2) an output … SetDCPWM1(150); // Set 25% duty cycle to PWM1 SetDCPWM2(450); // Set 75% duty cycle to PWM2 …
  • 19. State machine • We will use state machine diagram to program sequential comportment of the robot. • We will translate the diagram into C language by using switch/case statements. • We will study an example to understand clearly the principles. Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 19 principles. • We would like the two wheels to cover the same distance. • The simplest way to do it is exposed on the next slide.
  • 20. Example of state machine diagram 0 1 set the distance to cover BP0 pushed two motors on left distance done right distance done Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 20 32 left motor onright motor on right distance done left distance done
  • 21. Code example … void main(void) { //local variables definition and initialization unsigned char State = 0; // Store the actual state char Bp0; // Store 1 if BP0 is pushed unsigned int LeftEncoder, // Store Timer1 value RightEncoder, // Store Timer0 value Distance; // Store "distance" to cover //peripheral configuration … while(1) { // main loop Bp0 = !(PORTBbits.RB3); // Read BP0 status Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 21 Bp0 = !(PORTBbits.RB3); // Read BP0 status RightEncoder = ReadTimer0(); // Read right encoder value LeftEncoder = ReadTimer1(); // Read left encoder value switch (State) { // State machine case 0 : // State 0 SetDCPWM1(0); // Set 0% duty cycle to left pwm SetDCPWM2(0); // Set 0% duty cycle to right pwm if (Bp0) { // BP0 is pushed State = 1; // Go to State 1 WriteTimer0(0); // Reset encoders WriteTimer1(0); Distance = 1000; // Set distance to cover } break; // end case 0
  • 22. case 1 : SetDCPWM1(150); // Set 25% duty cycle to left pwm SetDCPWM2(150); // Set 25% duty cycle to right pwm if (LeftEncoder >= Distance) { // Left wheel covers the distance State = 2; // Go to State 2 } else if (RightEncoder >= Distance) { // Right wheel covers the distance State = 3; // Go to State 3 } break; // end case 1 case 2 : SetDCPWM1(0); // Set 0% duty cycle to left pwm SetDCPWM2(150); // Set 25% duty cycle to right pwm if (RightEncoder >= Distance) { // Right wheel covers the distance Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 22 // Right wheel covers the distance State = 0; // Go to State 0 } break; // end case 2 case 3 : SetDCPWM1(150); // Set 25% duty cycle to left pwm SetDCPWM2(0); // Set 0% duty cycle to right pwm if (LeftEncoder >= Distance) { // Left wheel covers the distance State = 0; // Go to State 0 } break; // end case 3 } // end switch } // end while } // end main
  • 23. Conclusion • We have learnt – how to use Timers – how to control the speed of a DC motor – how to generate PWM signals – how to program a simple state machine Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 23 • We’ll use this knowledge – to manage with distance measurement – to control the speed of the robot – to run a special trajectory
  • 24. Now, practice • Use timer1 to count events on left encoder • Use timer0 to count events on right encoder • Change speed of motors using PWM • The robot covers a circle shaped trajectory • The robot covers a house shaped trajectory Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 24