Challenge

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

Hello,

I have attached the code for the project as shown below along with the necessary screenshots from the simulide showing the simulation is working.

I have also attached the src file "main.c" which contains the code for the project.

Thank You!

/*

* AVR_Project_01_motor_control.c

*

* Created: 9/14/2024 7:28:26 PM

* Author : Lareb Khan

*/

#include <avr/io.h>

#include "avr/interrupt.h"

#include <util/delay.h>

/*----- MACROS----- */

/* Define the crystal frequency */

#define F_CPU 16000000L

/* d1 pin is declared as push button for direction control */

#define sw_dir (PIND & (1<<1))

/* d2 pin is declared as push button for speed control */

#define sw_dspd (PIND & (1<<2))

/* c3 is declared as push button for power ON/OFF */

#define pw (PINC & (1<<3))

/* PORTB pins are for LCD data */

#define lcd_port PORTB

/* d5 pin is used for Register Select(RS) */

#define RS_high PORTD |= (1<<5)

/* d5 pin is used for RS */

#define RS_low PORTD &=~ (1<<5)

/* d6 pin is used for Enable */

#define EN_high PORTD |= (1<<6)

/* d6 pin is used for En. */

#define EN_low PORTD &=~ (1<<6)

unsigned char i;

/* command for lcd display */

void lcd_cmd(char cmd)

{

/* 4 bits for 4 bit lcd mode */

lcd_port = (cmd & 0xf0);

RS_low;

EN_low;

/* after 1ms disable lcd */

_delay_ms(1);

/* enable the lcd */

EN_high;

lcd_port = ((cmd<<4) & (0xf0));

RS_low;

EN_low;

_delay_ms(1);

/* enable the lcd */

EN_high;

}

/* send the data to lcd */

void lcd_data (char data)

{

/* 4 bits for 4 bit lcd mode */

lcd_port = (data & 0xf0);

/* access data register */

RS_high;

EN_low;

_delay_ms(1);

EN_high;

lcd_port = ((data<<4) & (0xf0));

RS_high;

EN_low;

_delay_ms(1);

EN_high;

}

void lcd_init()

{

/* used for 4 bit lcd mode */

lcd_cmd(0x28);

_delay_ms(5);

/* return home */

lcd_cmd(0x02);

_delay_ms(5);

/* clear screen */

lcd_cmd(0x01);

_delay_ms(5);

/* display on cursor off */

lcd_cmd(0x0C);

_delay_ms(5);

/* increment the display cursor */

lcd_cmd(0x06);

_delay_ms(5);

/* force cursor to begin from 1st line */

lcd_cmd(0x80);

_delay_ms(5);

}

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; //x= row

y=1; //y= column

}

if(x==1)

{

lcd_cmd(0x7F + y); //{ 80-1 = 7F} for starting the 1st line hex decimal value (80) {y we r using the 7F+y means we need to dtart from that column}

}

else //(x==2)

{

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++;

}

}

/* Reading the ADC channels values */

int adc_read(char channel)

{

unsigned int a, result;

/* here we are going to use which channel we need to select in MUX, here using 2 bit */

ADMUX = channel;

/* control & status register to enable the ADC conversion ,enable the interrupt flag, ADC interrupt. ADEN=1 */

ADCSRA = 0X80;

/* clearing the ADC registers */

ADCH = 0X00;

ADCL = 0x00;

/* Start the conversion 6 bit ADSC high */

ADCSRA |= (1<<6);

/* while loop till the conversion become zero means conversion of ADC (0x40 means interrupted pin ) */

while((ADCSRA & 0x40) != 0);

/* Dummy data type a and is stored the ADCL register (ADC0-ADC7) */

a= ADCL;

/* Store the high value (ADC8-ADC9) */

result = ADCH;

result = result<<8;

/* Adding the ADCL + ADCH to get the 16 bits */

result = result | a ;

return result;

}

int main(void)

{

/* Declaring all 8-pins of PORT B as output. */

DDRB = 0XFF;

/* Declaring A0 & A1 pins as output. */

DDRA |= ((1<<0)| (1<<1));

/* Declaring D1 pin as input. */

DDRD &=~ (1<<1);

/* Declaring D2 pin as input. */

DDRD &=~ (1<<2);

/* Declaring D5 pin as output. */

DDRD |= (1<<5);

/* Declaring D6 pin as output. */

DDRD |= (1<<6);

/* Declaring C3 pin as input. */

DDRC &=~ (1<<3);

/* Enabling internal pull up resistor for D2. */

PORTD |= (1<<2);

/* Enabling internal pull up resistor for D3. */

PORTC |= (1<<3);

/* Initializing to 64 because we are running the motor with 25% duty cycle. */

i=64;

/* Using the Non-Inverting mode, PWM phase correct mode,NO prescalar. */

TCCR0=0X61;

/* Using INT0 for interrupt */

GICR=(1<<INT0);

/* Make interrupt negative edge triggered */

MCUCR=0X02;

/* Enable global interrupt */

sei();

/* Initialize lcd */

lcd_init();

while (1)

{

/* Infinite loop untill the push button is released. */

while(pw==0)

{

/* Check if the direction control push button is released. */

if(sw_dir == 0)

{

/* 1 0 to respective 1A 2A pins to rotate in clockwise direction. */

PORTA = 0b00000001;

/* Running clock wise direction */

lcd_out(1,1,"Motor ON- CL ");

}

else

{

/* 1 0 to respective 1A 2A pins to rotate in Anti-Clockwise direction. */

PORTA = 0b00000010;

lcd_out(1,1,"Motor ON- ACL");

}

/* i increases in ISR, the corresponding value is to be loaded into OCR0. */

OCR0 = i;

}

/* 0 0 to respective 1A 2A pins to Stop rotation. */

PORTA = 0x00;

lcd_out(1,1,"Motor is TURNOFF");

}

}

/* Interrupt service routine */

ISR(INT0_vect)

{

/* as i increases the duty cycle also increases. i increases by 20 in each push */

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.