Challenge

Project 1 - Controlling a DC motor using PWM and monitoring its Running status

Explanation:

  1. Header Files and Definitions:
    • The code starts by including necessary header files for AVR programming (avr/io.h and util/delay.h).
    • #define statements set the CPU frequency (F_CPU) and define the pins and ports for the LCD.
  2. LCD Functions:
    • Functions (LCD_Command, LCD_Char, LCD_Init, LCD_String, LCD_Clear) are provided to interact with an LCD using 4-bit mode.
    • These functions handle sending commands, characters, initialization, displaying strings, and clearing the LCD.
  3. Motor Control Functions:
    • Functions (motor_clockwise, motor_anticlockwise, motor_stop) are defined for controlling a motor connected to pins PA0 and PA1.
    • These functions manipulate the motor control pins to run the motor clockwise, anticlockwise, or stop it.
  4. Main Function:
    • The main function initializes PWM and motor pins, sets up the LCD, and enters an infinite loop (while(1)).
    • Inside the loop, it runs the motor clockwise for 2 seconds, stops the motor for 0.5 seconds, and then runs the motor anticlockwise for 2 seconds.
    • During each operation, it displays the corresponding direction on the LCD and clears the display before the next operation.
  5. PWM Setup:
    • The code assumes an 8-bit PWM setup with a 50% duty cycle (OCR1A = 128).
  6. Note for ATmega32:
    • The LCD pins D4 to D7 are connected to B4 to B7, and adjustments are made in the code to reflect this configuration.
  7. Loop Continuity:

The program runs indefinitely due to the while(1) loop, continuously rotating the motor and updating the LCD display with the motor's direction.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.