CS684: Embedded System Course
Lab 2: Speed Control using 8 bit Phase Correct PWM Mode
LEARNING RESOURCES
This lab requires understanding of motor interfacing with AVR microcontroller and basic knowledge about Pulse Width Modulation. Refer the Resources file.
AIM
In this lab, you will increase and decrease the speed of motors using Phase Correct PWM Mode. The aim of this lab is to get you familiar with one of the PWM mode available on the ATmega2560.
In this lab, you will interface the L293D motor driver and the Timer connected to the ATmega2560.
The program is provided as Eclipse project. But, the program contains few incomplete functions which you would have to complete as per the instructions present in the comments.
CONNECTIONS
-
Motors are connected to the Microcontroller through L293D Motor Driver IC.
Motors Pin Microcontroller Pin
- RB --> PC3
- RF --> PC2
- LF --> PC1
- LB --> PC0
-
PWM Pins of the Microcontroller are connected to the L293D Motor Driver IC.
PWM Pin Microcontroller Pin
- Left Motor --> PD5
- Right Motor --> PD6
PROCEDURE
Step-1: Download lab2_motor_pwm zip file. Right-click on the hyperlinks and select Save Link As... option to download. Extract the zip file. Start Eclipse, click on File --> Open Projects From File System --> Directory and browse for lab2_motor_pwm folder to open the project.
Step-2: In src/eBot_Sandbox.cpp, complete the traverse_s_shape function to achieve the following:
- Move the robot in forward direction.
- In the while loop, set the speed as complement of each other for the left and right motors and increment it by one every ten milliseconds till 255.
Note: While completing, you have to use following functions defined in eBot_MCU_Predef.h header file:
- void forward (void);
- void velocity (unsigned char, unsigned char);
Step-3: Build the project (click on Project --> Build Project). If there are no errors present in your program, the project will get build successfully and you will get the message (on console at bottom pane) as shown below,
**======== Build Finished. 0 errors, 0 warnings ========**
Step-4: Check the behaviour of the robot on CoppeliaSim.
- Download the eBot.ttm model. Start CoppeliaSim, click on File --> Load Model and browse for eBot.ttm file. This will load the robot model on the scene. (shown in Expected Output - CoppeliaSim video).
- In Eclipse, right click on lab2_motor_pwm folder (Project Explorer - left pane) and select Run As --> Local C/C++ Application. If no errors are encountered, following message will be displayed onto the console (bottom pane):
Connection Success ...
0
0
0
Please Enter Y to Start Simulation: Y
Enter 'Y' on console. This will start the simulation in CoppeliaSim. You can verify the output by referring to Expected Output - CoppeliaSim video.
Note: For better angle, you can change the perspective of the scene using page selector.
Step-5: You will notice some pre-written function stubs in src/eBot_MCU_Predef.c included for your assistance related to motor and timer. Complete the pre-written functions with the help of comments provided.
Step-6: In the Package Explorer (left pane), right click on lab2_motor_pwm folder and select Show in Local Terminal --> Terminal (bottom pane). Type the following command in the terminal:
avr_hex_file_generator -cpp src/lab2_motor_pwm_main -dcpp src/eBot_Sandbox -dc src/eBot_MCU_Predef -m atmega328p
If there are no errors present in your program, the project will get compiled correctly and you will get the message as shown below, Also, build folder will be generated in project directory that will contain lab2_motor_pwm_main.hex file along with other build files.

Step-7: Load the hex file on the SimulIDE circuit (AVR_simulator.simu) once your program gets build successfully. Save the project. Ensure that code performs as expected. NOTE: Getting Build succeeded output doesn't mean that your program will give the expected output as it can contain logical errrors.
MACROS to use
- In the skeleton code eBot_MCU_Predef.c we have included a header file named, eBot_MCU_Predef.h which consists of the declaration of the following constants (for atmega328p controller):
// Motor direction registers and pins
#define motors_dir_ddr_reg DDRC
#define motors_dir_port_reg PORTC
#define motors_RB_pin PC3 // 3
#define motors_RF_pin PC2 // 2
#define motors_LF_pin PC1 // 1
#define motors_LB_pin PC0 // 0
// Motor enable registers and pins
#define motors_pwm_ddr_reg DDRD
#define motors_pwm_port_reg PORTD
#define motors_pwm_R_pin PD6 // 6
#define motors_pwm_L_pin PD5 // 5
// Timer / Counter registers
#define TCNTH_reg TCNT0H // Timer / Counter 0 High Byte register
#define TCNTL_reg TCNT0L // Timer / Counter 0 Low Byte register
#define OCRAL_reg OCR0AL // Output Compare Register 0A Low Byte
#define OCRAH_reg OCR0AH // Output Compare Register 0A High Byte
#define OCRBL_reg OCR0BL // Output Compare Register 0B Low Byte
#define OCRBH_reg OCR0BH // Output Compare Register 0B High Byte
#define TCCRA_reg TCCR0A // Timer / Counter Control Register 0A
#define TCCRB_reg TCCR0B // Timer / Counter Control Register 0B
// Bits of compare output mode in the TCCRnA register ( Timer / Counter 'n' Control Register A, where n = 0, 1, 2, 3, 4, 5 )
#define COMA1_bit COM0A1 // 7 (Compare Output Mode bit 1 for Channel A)
#define COMA0_bit COM0A0 // 6 (Compare Output Mode bit 0 for Channel A)
#define COMB1_bit COM0B1 // 5 (Compare Output Mode bit 1 for Channel B)
#define COMB0_bit COM0B0 // 4 (Compare Output Mode bit 0 for Channel B)
// Bits of waveform generation mode in the TCCRnA and TCCRnB register ( Timer / Counter 'n' Control Register A/B, where n = 0, 1, 2, 3, 4, 5 )
#define WGM0_bit WGM00 // 0 (Waveform Generation Mode bit 0)
#define WGM1_bit WGM01 // 1 (Waveform Generation Mode bit 1)
#define WGM2_bit WGM02 // 2 (Waveform Generation Mode bit 2)
#define WGM3_bit WGM03 // 3 (Waveform Generation Mode bit 3)
// Bits of clock select mode in the TCCRnB register ( Timer / Counter 'n' Control Register B, where n = 0, 1, 2, 3, 4, 5 )
#define CS0_bit CS00 // 0 (Clock Select bit 0)
#define CS1_bit CS01 // 1 (Clock Select bit 1)
#define CS2_bit CS02 // 2 (Clock Select bit 2)
- You have to use these constants declared in eBot_MCU_Predef.h and the Masking Operators to complete the lab.
EXPECTED OUTPUT - CoppeliaSim
Software required: CoppeliaSim and Eclipse (refer Installation_Instructions provided to you)
EXPECTED OUTPUT - SimulIDE
Software required: SimulIDE AppImage (refer Installation_Instructions provided to you)