Hello everyone I am new to programming. May I ask for your help and advice.
I was trying to enable an LED by PWM by toggling a switch on PortC.
I am using the fast PWM, Cleasr on compare OC0B function.
I removed the while(1) statement since I wanted the LED to get activated only once and not repeatedly.
Then I want the LED to turn-off after toggling the switch.
However it is not working. May I ask what is wrong with this code.
Thank you for your ideas
#include <avr/io.h> #define F_CPU 16000000UL #include <util/delay.h> void PWM_init(void) { TCCR0A|=(1<<COM0B1)|(1<<WGM01)|(1<<WGM00); //Clear OC0B on Compare Match, set OC0B at BOTTOM, (non-inverting mode). //set to fast PWM TCCR0B|=(1<<CS02)|(1<<CS00); //set prescale to 1024 } int main(void) { DDRD=0x20; //set OC0B to output DDRC=0x00; PWM_init(); if(PORTC&(1<<5)) { for(int count=0; count<1; count++) //set counter to 1 { for(int duty_cycle=0; duty_cycle<256; duty_cycle++) { OCR0B=duty_cycle; //increase duty cycle _delay_ms(1); //delay of duty cycle } } } else { OCR0B=0; //deactivate the LED } }
h an input pin.