Write a program to monitor bit PD7 connected to a switch. If PD7 is HIGH create
ID: 2268316 • Letter: W
Question
Write a program to monitor bit PD7 connected to a switch. If PD7 is HIGH create a square wave of 50% duty cycle on bit 0 of Port B and if PD7 is LOW create a square wave of 66% duty cycle on bit 3 of Port B. The 50% duty cycle means that the “on” and “off" states (or the high and low portions of the pulse) have the same length. Therefore, we toggle PBO with a time delay between each state. VCC | ATmega 4.7k 3 PD7 Switch 50% Duty Cycle On Time = Off Time PB0 A 66% duty cycle means that the “on” state is twice the "off" state. VCC 4.7k ATmega PD7 66% means On Time = 2 x Off time Switch PB3Explanation / Answer
#include <avr/io.h>
#include <util/delay.h>
void pwm_init()
{
// initialize TCCR0 as per requirement, say as follows
TCCR0 |= (1<<WGM00)|(1<<COM01)|(1<<WGM01)|(1<<CS00);
// make sure to make OC0 pin (pin PB3 for atmega32) as output pin
DDRB |= (1<<PB3);
// PD7 as a input
DDRD |= (0<<PD7);
}
void main()
{
uint8_t duty;
duty = 128; // duty cycle = 50% of 255 = 127.5 = 128
duty1=170; //dutycycle = 66,66% of 255 = 169.98=170
// initialize timer in PWM mode
pwm_init();
if (PIND & (1<<PD7)) //check condition of switch
{
// run forever
while(1)
{
OCR0 = duty;
}
}
else
{
// run forever
while(1)
{
OCR0 = duty;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.