0

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
    If you blink an LED at 1KHz, it should just look like it's always on, but maybe a little dim. 5Hz is about as fast as you can blink it, and clearly see that it's blinking. – user3386109 May 06 '21 at 18:51
  • what frequency is it generating (you need a scope of course for this) – old_timer May 06 '21 at 23:54

1 Answers1

0

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.

icodeplenty
  • 252
  • 1
  • 6