Question: Modify the PWM motor control code so that motor RPM changes smoothly between 1 0 0 % and 5 0 % and back. You can

Modify the PWM motor control code so that motor RPM changes smoothly between 100% and 50% and back. You can modify the case statement and add a subroutine to set the PWM through OCR1 register.
1-main.cpp
2-short explanation of how your code works.
#include
#define PWM_100255
#define PWM_90230
#define PWM_50128
#define PWM_00
uint32_t uMsCnt, start_time, current_time;
void setup()
{
// Leds: RED Uno #7(PD7), GREEN Uno #6(PD6)
DDRD |=(1<< PD6); // GREEN output (Uno #6)<=> DDRD
DDRD |=(1<< PD7); /* TBD */// RED output (Uno #7)<=> DDRD
PORTD |=(1<< PD6); // GREEN off
PORTD |=(1<< PD7); // RED off...
// Timer1 setup(PWM output)
DDRB |=(1<< PB1); // PWM output on pinOC1A (Uno #9)
TCCR1A =(1<< COM1A1)|(1<< WGM10); // toggle OC1A on compare match, 8-bit phase corrected PWM
OCR1A = PWM_0; // start with PWM duty cycle 0%(output low)
TCCR1B =(1<< CS11); // prescaler=8(PWM 16MHz/8*(2*255)=3,9 kHz, T=255 us), starttimer
uMsCnt =0; // reset ms counter
start_time = millis(); // reset ms counter start_time
}
void loop()
{
// put your main code here, to run repeatedly:
switch (uMsCnt)
{
// uMsCnt goesfrom 0 to 7500 ms, to be reset on maxvalue
case 0:
OCR1A = PWM_100; // PWM 100%
// OCR1A = PWM_90; // PWM 90%
PORTD |=(1<< PD6); // GREEN off
PORTD &= ~(1<< PD7); // RED on
break;
case 2500:
OCR1A = PWM_50; // PWM 50%
PORTD &= ~(1<< PD6); // GREEN on
PORTD |=(1<< PD7); // RED off
break;
case 7500: // after 7.5 s back to beginning(PWM 100%)
uMsCnt =0; // reset ms counter
start_time = millis(); // reset ms counter start_time
break;
default:
break;
}// endswitch()
current_time = millis(); // get current time in ms
if (current_time > start_time)
{
// if start_time or more ms passed
uMsCnt +=(current_time - start_time);
// update ms count
start_time = current_time; // reset start_time
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!