0% found this document useful (0 votes)
2 views

dcdc1

This document contains C code for controlling a DC motor using an AVR Mega32 microcontroller. It initializes PWM for motor control and defines functions to move the motor forward, backward, left, right, and stop based on button inputs. The code includes debouncing for button presses and sets the appropriate outputs on the microcontroller's ports.

Uploaded by

22145092
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

dcdc1

This document contains C code for controlling a DC motor using an AVR Mega32 microcontroller. It initializes PWM for motor control and defines functions to move the motor forward, backward, left, right, and stop based on button inputs. The code includes debouncing for button presses and sets the appropriate outputs on the microcontroller's ports.

Uploaded by

22145092
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <mega32.

h>

#include <delay.h>

#define IN1 0

#define IN2 1

#define IN3 2

#define IN4 3

#define chuky 499

#define dutyc 250

/* Timer_PWM Function */

void PWM_Init(unsigned int);

void PWM_Start(void);

/* DC_Motor Controller Function */

void Tien(unsigned int);

void Lui(unsigned int);

void Trai(unsigned int);

void Phai(unsigned int);

void Stop(void);

void main(void)

// IO init

DDRD = 0xFF; // Set Port D as output

DDRB = 0x00; // Set Port B as input

PORTB = 0xFF; // Enable pull-up resistors for Port B


PWM_Init(chuky); // Initialize PWM

while (1) // Continuous loop

if ((PINB & (1 << 0)) == 0) // Tien (Forward)

delay_ms(1); // Debounce delay

while ((PINB & (1 << 0)) == 0); // Wait for button release

Tien(dutyc); // Move forward

if ((PINB & (1 << 1)) == 0) // Lui (Backward)

delay_ms(1); // Debounce delay

while ((PINB & (1 << 1)) == 0); // Wait for button release

Lui(dutyc); // Move backward

if ((PINB & (1 << 2)) == 0) // Trai (Left)

delay_ms(1); // Debounce delay

while ((PINB & (1 << 2)) == 0); // Wait for button release

Trai(dutyc); // Turn left

if ((PINB & (1 << 3)) == 0) // Phai (Right)


{

delay_ms(1); // Debounce delay

while ((PINB & (1 << 3)) == 0); // Wait for button release

Phai(dutyc); // Turn right

if ((PINB & (1 << 4)) == 0) // Stop

delay_ms(1); // Debounce delay

while ((PINB & (1 << 4)) == 0); // Wait for button release

Stop(); // Stop the motor

void PWM_Init(unsigned int period)

TCCR1A=(1<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | (0<<COM1B0)


| (1<<WGM11) | (0<<WGM10);

TCCR1B=(0<<ICNC1) | (0<<ICES1) | (1<<WGM13) | (1<<WGM12) |


(0<<CS12) | (1<<CS11) | (0<<CS10);

TCNT1H=0x00;

TCNT1L=0x00;

ICR1H= (period >> 8) & 0xFF;;

ICR1L= period & 0xFF;;

OCR1AH=0x00;

OCR1AL=0x00;
OCR1BH=0x00;

OCR1BL=0x00;

void PWM_Start(void)

TCCR1B |= (1 << CS10); // Start the timer with no prescaler

void Tien(unsigned int duty)

PORTD |= (1 << IN1);

PORTD &= ~(1 << IN2);

PORTD |= (1 << IN3);

PORTD &= ~(1 << IN4);

OCR1B = duty; // Set duty cycle for OC1B

OCR1A = duty; // Set duty cycle for OC1A

PWM_Start(); // Start PWM

void Lui(unsigned int duty)

PORTD |= (1 << IN2);

PORTD &= ~(1 << IN1);

PORTD |= (1 << IN4);

PORTD &= ~(1 << IN3);


OCR1B = duty;

OCR1A = duty;

PWM_Start();

void Trai(unsigned int duty)

PORTD |= (1 << IN1);

PORTD &= ~(1 << IN2);

PORTD &= ~ (1 << IN3);

PORTD &= ~(1 << IN4);

OCR1B = duty;

OCR1A = 0;

PWM_Start();

void Phai(unsigned int duty)

PORTD &= ~(1 << IN1);

PORTD &= ~(1 << IN2);

PORTD |= (1 << IN3);

PORTD &= ~(1 << IN4);

OCR1A = duty;

OCR1B = 0;

PWM_Start();

}
void Stop(void)

PORTD &= ~(1 << IN1);

PORTD &= ~(1 << IN2);

PORTD &= ~(1 << IN3);

PORTD &= ~(1 << IN4);

TCCR1B &= ~(1 << CS10); // Stop the timer

You might also like