Challenge

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

1)

#include <avr/io.h>//standard input output library
#define F_CPU 16000000L//micro controller frequency
#include <avr/interrupt.h>//library for interrupt
#include <util/delay.h>//library for delay function
#include <string.h>//library for string function
#define lcdport PORTC//for display
#define en_high PORTA |=(1<<PA1);//for enable
#define en_low PORTA &=~(1<<PA1);//for disable
#define rs_high PORTA |=(1<<PA0);//for register select (data)
#define rs_low PORTA &=~(1<<PA0);//for register select(command)
static unsigned int a=0;//global variable for counting switch
ISR (INT0_vect)//external interrupt for speed increment
{
OCR0=OCR0+10;//increment ocr0
a++;//incrementing counter
if(a>20)//checking the counter value
{
a=0;//when a over flows roll back to zero
}
}
ISR(INT1_vect)//external interrupt 1 for changing the direction
{
PORTB=(1<<PB5)|(0<<PB6);//reversing the polarity
lcd_print(1,1,"CW direction");//printing clockwise direction
_delay_ms(500);//calling 500 ms delay
}
void lcd_cmd(unsigned char cmd)//function for lcd command
{
rs_low;
lcdport=cmd;//writting the command on lcd
en_high;
_delay_ms(1);
en_low;
}
void lcd_data(unsigned char data)//function for writting data for lcd
{
rs_high;
lcdport=data;
en_high;
_delay_ms(1);
en_low;
}
void lc_init()//function for initialising lcd
{
lcd_cmd(0x38);_delay_ms(1);
lcd_cmd(0x02);_delay_ms(1);
lcd_cmd(0x01);_delay_ms(1);
lcd_cmd(0x0c);_delay_ms(1);
lcd_cmd(0x06);_delay_ms(1);
lcd_cmd(0x80);_delay_ms(1);
}
void cursor_pos(unsigned int x,unsigned int y)//setting the cursor position
{
if((x>2||x<1) || (y>16||y<1))
{
x=1;
y=1;
}
if(x==1)
lcd_cmd(0x7F+y);
else
lcd_cmd(0xBF+y);
}
void lcd_print(unsigned int x,unsigned int y,unsigned char*str)//function for printing string data on lcd
//with row and coloumn as argument
{
unsigned int i=0;
if(x==0||y==0)
{
cursor_pos(1,1);
}
else
{
cursor_pos(x,y);
}
while(str[i]!=0)
{
lcd_data(str[i]);
i++;
}
}
//////////////
int main(void)
{
int i=0;
TCCR0=((1<<WGM01)|(1<<WGM00)|(1<<CS00)|(1<<COM01)|(0<<COM00));//configuring the tccro registerfor pwm mode
DDRD=(0<<PD2)|(0<<PD3);//making the interrupt pins as input
PORTD=(1<<PD2)|(1<<PD3);//internal pulling up
DDRA=(1<<PA0)|(1<<PA1);//making the PA0 and PA1 as output
DDRC=0xFF;//making all the portc as ouptput
DDRB=(1<<PB5)|(1<<PB6)|(1<<PB3);//making porb PB6,PB5,PB3 as output
OCR0=64;//assigning the ocro
PORTB=(1<<PB6)|(0<<PB5);//setting the polarity ogf the motor to run in CCW direction
GICR=0b11000000;//configuring the GIFR for the two interrupts INT0,INT1
MCUCR=0b00000101;//configuring MCUCR for predicting any state change
sei();//enabling all the interrupt
lc_init();//calling the lcd initializtion for lcd
lcd_print(1,1,"CCW direction");//printing CCW direction on the lcd
lcd_print(2,1,"speed=10m/s");//printing speed=10m/s on lcd
while(1)
{
switch(a)//switch case for selecting the speed to be displayed on the lcd by the counter value
{
case 1:lcd_print(2,1,"speed=50m/s");//
break;
case 3:lcd_print(2,1,"speed=100m/s");
break;
case 5:lcd_print(2,1,"speed=150m/s");
break;
case 7:lcd_print(2,1,"speed=200m/s");
break;
case 9:lcd_print(2,1,"speed=250m/s");
break;
case 11:lcd_print(2,1,"speed=300m/s");
break;
case 13:lcd_print(2,1,"speed=350m/s");
break;
case 15:lcd_print(2,1,"speed=400m/s");
break;
case 17:lcd_print(2,1,"speed=450m/s");
break;
case 19:lcd_print(2,1,"speed=0m/s");
break;
}
}
return 0;
}
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.