Is my program to generate a 1kHz square wave correct on pinb1 of ATmega32(8MHz 64presclar Timer1)?

605 Views Asked by At

The code compiles correctly but I am unable to obtain 1kHz Square Wave. I attached an LED at PINB1 to check. I used Timer1, with CTC mode and Prescalar as 64. PLease Help.

#define F_CPU 8000000L
#include <avr/io.h>
#include "avr/iom32.h"


// - - - - PROGRAM TO GENERATE A SQUARE WAVE OF 1KHz - - - - //
void _delay_();
int main(void)
{
    DDRB = 0xFF;
    OCR1AH = 0xF4;
    OCR1AL = 0x23;
    TCNT1H = 0;
    TCNT1L = 0;
    while (1) 
    {
        PORTB |= (1 << 4);
        _delay_();
        PORTB &= ~(1 << 4);
        _delay_();
    }
}

void _delay_() {
    TCCR1A = 0x00;
    TCCR1B = 0x0B;
    while(!(TIFR & (1 << 4)));
    TCCR1B = 0x00;
    TCCR1A = 0x00;
    TIFR |= 0x10;
}
1

There are 1 best solutions below

0
On

As oldtimer suggested, you should use a o-scope to verify the output. If you don't have one, or if you do and still no output, then try replacing the delay routine with a simple software delay such as this:

void _delay_() {
    // simple software delay
    for (uint32_t i = 0; i < 50000; i++);
}

The idea is to adjust the maximum count (50000) to any value that creates a long enough delay to see the LED blink. If the LED still doesn't blink, then the problem is with the other code, or the external connection to the LED. For example, you say PINB1, but isn't that at bit position 1 << 1, but your code uses 1 << 4.