Challenge

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

                                                               AVR LIVE BATCH-8

AIM:

Implement a system on the simulator for controlling a DC motor using L293 motor driver, monitor its status and print the running status of motor on the LCD Display.

OBJICTIVE:

  • To implement a system for controlling a DC motor using L293 motor driver.
  • To monitor the status of motor and print the running status of motor on the LCD Display.

PROGRAM:

#include  //input output header file
#define F_CPU 16000000L //CPU frequency
#include "avr/interrupt.h" // we are using external interrupt pin
#include  // delay function 
#define sw_dir (PIND & (1<<1)) // PORTD pin 1 for direction control.
#define sw_dspd (PIND & (1<<2)) // PORTD pin 2 for speed control.
#define pw (PINC & (1<<3)) // PORTC pin 3 for power ON/OFF
 
#define lcd_port PORTB //using PORTB pins are for LCD data
#define RS_high PORTD |= (1<<5) // PORTD pin 5 for Register Select(RS)
#define RS_low PORTD &=~ (1<<5) 
#define EN_high PORTD |= (1<<6) // PORTD pin 6 for Enable
#define EN_low PORTD &=~ (1<<6) 
unsigned char i;
 
void lcd_cmd(char cmd) //command for lcd display
{
lcd_port = (cmd & 0xf0); //4 bits for 4 bit lcd mode
RS_low;
EN_low;
_delay_ms(1); //after 1ms disable lcd
EN_high; //enable the lcd
lcd_port = ((cmd<<4) & (0xf0)); //lcd port
RS_low;
EN_low;
_delay_ms(1); //after 1ms disable lcd
EN_high; //enable the lcd
}
 
void lcd_data (char data) //send data to lcd
{
lcd_port = (data & 0xf0); //4 bits for 4 bit lcd mode
RS_high; //access data register
EN_low;
_delay_ms(1);
EN_high;
lcd_port = ((data<<4) & (0xf0)); //4 bits for 4 bit lcd mode
RS_high; //access data register
EN_low;
_delay_ms(1);
EN_high;
}
 
void lcd_init()
{
lcd_cmd(0x38);// used for 4 bit lcd mode
_delay_ms(5);
lcd_cmd(0x02); // return home
_delay_ms(5);
lcd_cmd(0x01); // clear screen
_delay_ms(5);
lcd_cmd(0x0c); // display on cursor off
_delay_ms(5);
lcd_cmd(0x06); // increment the display cursor
_delay_ms(5);
lcd_cmd(0x80); // force cursor to begin from 1st line
_delay_ms(5);
}
 
void cursorpos(char x,char y)
{
if((x<1 || x>2) || (y<1 || y>16 )) //condition will checks the 2 rows and 16 columns
{
x=1; //x= row
y=1; //y= column
}
if(x==1)
lcd_cmd(0x7F + y); //{ 80-1 = 7F} to start the 1st line hex decimal value (80)
else
lcd_cmd(0xBF + y); //{0C-1 = 0B} to start the 2nd line, 2nd row.
}
 
 
void lcd_out(char x, char y, char *str) //checking which position we want to print
{
cursorpos(x,y);
while(*str)
{
lcd_data(*str);
str++;
}
}
 
int adc_read(char channel) //configuring the ADC value
{
unsigned int a, result;
ADMUX = channel; //Selecting MUX using 2 bit
ADCSRA = 0X80; //control & status register to enable the adc conversion ,enable the interrupt flag, adc interrupt. ADEN=1
ADCH = 0X00; //clearing the adc register
ADCL = 0x00; 
ADCSRA |= (1<<6); // to start the conversion 6 bit ADSC high
while((ADCSRA & 0x40) != 0); // when the conversion become zero means conversion of adc (0x40 means interrupted pin )
a= ADCL; //dummy data type a and ais stored the ADCL register (ADC0-ADC7)
result = ADCH; // Store the high value (ADC8-ADC9)
result = result<<8; //to get proper result or aligned value we need to do left shift
result = result | a ; //adding the ADCL + ADCH to get the 16 bits
return result;
}
 
int main(void)
{
DDRB = 0XFF; //all 8-pins as output.
DDRA |= ((1<<0)| (1<<1)); //A0 & A1 pins as output.
DDRD &=~ (1<<1); //D1 pin as input.
DDRD &=~ (1<<2); // D2 pin as input.
DDRD |= (1<<5); //D5 pin as output.
DDRD |= (1<<6); //D6 pin as output.
DDRC &=~ (1<<3); //C3 pin as input.
PORTD |= (1<<2); //enabling internal pull up resistor for D2.
PORTC |= (1<<3); //enabling internal pull up resistor for D3.
i=64; //initializing i=64 because we are running the motor with 25% duty cycle.
TCCR0=0X61; //Non-Inverting mode, PWM phase correct mode,NO prescalar.
GICR=(1<

RESULT:

1.

POWER SWITCH:OFF , DIRECTION SWITCH:OFF , SPEED SWITCH:OFF; MOTOR CONDITION:OFF.

2.

POWER SWITCH:ON , DIRECTION SWITCH:OFF , SPEED SWITCH:OFF; MOTOR CONDITION:ON.

3.

POWER SWITCH:ON , DIRECTION SWITCH:ON , SPEED SWITCH:OFF; MOTOR CONDITION:ON ROTATE IN REVERSE DIRECTION.

4.

POWER SWITCH:ON , DIRECTION SWITCH:OFF , SPEED SWITCH:ON; MOTOR CONDITION:ON SPEED OF MOTOR INCREASED.

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.