Challenge

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

/*
* PWMdcmotrCNTRL.c
*
* Created: 2/15/2023 10:39:32 AM
* Author : Employee
*/

#define F_CPU 8000000UL /* Define CPU Frequency 8MHz */
#include <avr/io.h> /* Include AVR std. library file */
#include <stdio.h> /* Include std. library file */
#include <util/delay.h> /* Include Delay header file */
#define lcdport PORTA
#define rs_high PORTD|=(1<<2); //rs display bit
#define rs_low PORTD &= ~(1<<2);
#define en_high PORTD|=(1<<3); //en pin in display
#define en_low PORTD &= ~(1<<3);
volatile uint8_t Direction = 0;
void lcd_cmd(unsigned char cmd) //setting up of lcd in command mode
{
lcdport=cmd;
rs_low;
en_low;
_delay_ms(100);
en_high;
}
void lcd_data(unsigned char data) //setting up lcd in data mode
{

lcdport=data;
rs_high;
en_low;
_delay_ms(100);
en_high;

}
void lcd_init() //initialisation of lcd.
{
lcd_cmd(0x38);_delay_ms(5);
lcd_cmd(0x02);_delay_ms(5);
lcd_cmd(0x01);_delay_ms(5);
lcd_cmd(0x0c);_delay_ms(5);
lcd_cmd(0x06);_delay_ms(5);
lcd_cmd(0x80);_delay_ms(5);
lcd_cmd(0x00);


}

void ADC_Init() /* ADC Initialization function */
{
DDRA = 0x00; /* Make ADC port as input */
ADCSRA = 0x87; /* Enable ADC, with freq/128 */
ADMUX = 0x40; /* Vref: Avcc, ADC channel: 0 */
}

int ADC_Read(char channel) /* ADC Read function */
{
ADMUX = 0x40 | (channel & 0x07);/* set input channel to read */
ADCSRA |= (1<<ADSC); /* Start ADC conversion */
while (!(ADCSRA & (1<<ADIF))); /* Wait until end of conversion */
ADCSRA |= (1<<ADIF); /* Clear interrupt flag */
_delay_ms(1); /* Wait a little bit */
return ADCW; /* Return ADC word */
}

int main(void)
{
DDRC = 0xFF; /* Make PORTC as output Port */
DDRA=0XFF; /*lcd port as outputT */
DDRB |= (1<<PB3); /* Make OC0 pin as Output */
DDRD |= (1<<3); //for lcd Enable
DDRD |= (1<<2); //for lcd rs bit

ADC_Init(); /* Initialize ADC */
lcd_init();
TCNT0 = 0; /* Set timer0 count zero */
TCCR0 = (1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00)|(1<<CS01);/* Set Fast PWM with Fosc/64 Timer0 clock */

PORTC = 2;

OCR0 = (ADC_Read(0)/4); /* Read ADC and map it into 0-255 to write in OCR0 register */
lcd_data('c');
lcd_data('l'); //Displaying clockwise
lcd_data('o');
lcd_data('c');
lcd_data('k');
lcd_data('w');
lcd_data('i');
lcd_data('s');
lcd_data('e');

}

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.