Here's a little mystery that has cost me about 6 days of grief. the following code uses pwm to fade an LED up and down. when i organize the code using the "main(void){....}" declaration, it works as written. But when i divide the code into two segments with "void setup(){....}" and "void loop(){....}" the LED doesn't fade in and out. It just comes on 100% and stays that way.
any ideas about why this is? thanx
#include <avr/io.h>
// fastPWM on timer1, non-invert mode, no prescale, output on pin B2, Duty cycle = (x mod 50)/50
volatile unsigned int t, u, x = 0;
void wait(unsigned int base);
//void setup() {
int main(void) {
ICR1 = 50;
TCCR1A |= (1 << COM1B1) | (1 << WGM11);
TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10);
DDRB |= (1 << DDB2);
//} void loop() {
while(1){ x += 4; OCR1B = x%50; wait(600); }
return 0;
}
void wait(unsigned int base){ for (t = 0; t < base; t++){ for (u = 0; u < base; u++){} } }
any ideas about why this is? thanx
#include <avr/io.h>
// fastPWM on timer1, non-invert mode, no prescale, output on pin B2, Duty cycle = (x mod 50)/50
volatile unsigned int t, u, x = 0;
void wait(unsigned int base);
//void setup() {
int main(void) {
ICR1 = 50;
TCCR1A |= (1 << COM1B1) | (1 << WGM11);
TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10);
DDRB |= (1 << DDB2);
//} void loop() {
while(1){ x += 4; OCR1B = x%50; wait(600); }
return 0;
}
void wait(unsigned int base){ for (t = 0; t < base; t++){ for (u = 0; u < base; u++){} } }
Last edited: