Challenge

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

#include <avr/io.h>
#define F_CPU 16000000L
#include "avr/interrupt.h"
#include <util/delay.h>
#define sw_dir (PIND & (1<<1)) // d1 pin is for direction.
#define sw_dspd (PIND & (1<<2)) // d2 pin is for speed control.
#define pw (PINC & (1<<3)) // c3 is for power ON/OFF
#define lcd_port PORTB //LCD data
#define RS_high PORTD |= (1<<5) // Register Select(RS)
#define RS_low PORTD &=~ (1<<5)
#define EN_high PORTD |= (1<<6) // Enable
#define EN_low PORTD &=~ (1<<6)
unsigned char i;
void lcd_init()
{
lcd_cmd(0x28);
lcd_cmd(0x02); //home
lcd_cmd(0x01); // clear
lcd_cmd(0x0C); // display on cursor off
lcd_cmd(0x06); // increment
lcd_cmd(0x80); // begin from 1st line
}
void lcd_cmd(char cmd) //command for lcd display
{
lcd_port = (cmd & 0xf0); //4 bits
RS_low;
EN_low;
_delay_ms(1);
EN_high; //enable the lcd
lcd_port = ((cmd<<4) & (0xf0));
RS_low;
EN_low;
_delay_ms(1);
EN_high;
}
void lcd_data (char data) //data to lcd
{
lcd_port = (data & 0xf0);
RS_high;
EN_low;
_delay_ms(1);
EN_high;
lcd_port = ((data<<4) & (0xf0));
RS_high;
EN_low;

_delay_ms(1);
EN_high;
}
void cursorpos(char x,char y)
{
if((x<1 || x>2) || (y>16 || y<1 )) //condition will checks the 2 rows and 16
columns
{
x=1; //row
y=1; //column
}
if(x==1)
{
lcd_cmd(0x7F + y); //{ 80-1 = 7F} for starting the 1st line hex decimal

value (80)
}
else
{
lcd_cmd(0xBF + y); //{0C-1 = 0B} for starting the 2nd line, 2nd row.
}
}
void lcd_out(char x, char y, char *str)
{
cursorpos(x,y);
while(*str)
{
lcd_data(*str);
str++;
}
}
int adc_read(char channel) // we r configuring the adc value
{
unsigned int a, result;
ADMUX = channel; // to select mux
ADCSRA = 0X80; //control & status register
ADCH = 0X00; // showing the value 0
ADCL = 0x00; // showing the value 0
ADCSRA |= (1<<6); // to start the conversion
while((ADCSRA & 0x40) != 0); //conversion of adc
a= ADCL; //a stored the ADCL register
result = ADCH; // Store the high value
result = result<<8; //to get proper result
result = result | a ; // ADCL + ADCH
return result;
}
int main(void)
{
DDRB = 0XFF; //output.
DDRA |= ((1<<0)| (1<<1)); //A0 & A1 output.
DDRD &=~ (1<<1); //D1 input.
DDRD &=~ (1<<2); //D2 input.
DDRD |= (1<<5); //D5 output.
DDRD |= (1<<6); //D6 output.
DDRC &=~ (1<<3); //C3 input.
PORTD |= (1<<2); //internal pull up resistor for D2.
PORTC |= (1<<3); //internal pull up resistor for D3.
i=64; // 25% duty cycle.
TCCR0=0X61; //PWM phase correct mode
GICR=(1<<INT0); //interrupt
MCUCR=0X02; //interrupt negative edge triggered
sei(); //Enable global interrupt
lcd_init(); //Initialize lcd
while (1)
{
while(pw==0) //till the push button is released.
{
if(sw_dir == 0) // direction control push button.
{
PORTA = 0b00000001;
lcd_out(1,1,"Motor ON ClockWi");

_delay_ms(5000);

}
else
{
PORTA = 0b00000010;
lcd_out(1,1,"Motor ON AntiClk");

_delay_ms(5000);

}
OCR0 = i; //i increases in ISR.
}
PORTA = 0x00;
lcd_out(1,1,"Motor is TURNOFF");
}
}
ISR(INT0_vect) //interrupt service routine
{
i=i+20;
}

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.