Tiny25, timer 1 setup to output a frequency on pin PB1 (OC1A)

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

This should be extra simple --- what am I missing?  I have a factory-default (i.e., 1MHz clock) ATtiny25 and I'm trying to output a 500Hz square wave at pin PB1.  Here's the code I'm trying --- notice that pins PB0 and PB2 are connected to LEDs, and I do see those two LEDs flashing a bit faster than 1Hz (as expected).  However, I see nothing at pin PB1:

 

int main()
{
    PORTB = 0x5;
    DDRB = 0x5;

    void init();
    init();

    while (1)
    {
        PINB = 0x5;
        _delay_ms(300);
    }
    return 0;
}

void init()
{
    uint8_t tccr1 = (0x1 << 7)     // Configure timer 1 for CTC mode
                  | (0x1 << 2)     // Prescaler at CLK/8
                  | (0x1 << 4);  // Toggle OC1A on timer match

    OCR1A   = 125;
    TCCR1 = tccr1;
}

Any ideas?

 

Thanks,

Cal-linux

--

 

This topic has a solution.
Last Edited: Sat. Mar 10, 2018 - 12:42 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to make the pin an output.

 

cal-linux wrote:
uint8_t tccr1 = (0x1 << 7) // Configure timer 1 for CTC mode | (0x1 << 2) // Prescaler at CLK/8 | (0x1 << 4); // Toggle OC1A on timer match

It is easier to help if you use bit names instead of bit numbers.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

You need to make the pin an output.

 

D'oh!!  I always had the idea that setting up a pin as an output actually meant "set it up as GPIO in output mode".  In my mind, setting up the timer to toggle OC1A pin would override (or in any case, it would be an alternative) to setting it up as GPIO in output mode....  Oh well, now I know!  :-)

 

cal-linux wrote:
uint8_t tccr1 = (0x1 << 7) // Configure timer 1 for CTC mode | (0x1 << 2) // Prescaler at CLK/8 | (0x1 << 4); // Toggle OC1A on timer match

It is easier to help if you use bit names instead of bit numbers.

 

Agreed --- and my initial version of the program read:

    uint8_t tccr1 = (0x1 << CTC1)     // Configure timer 1 for CTC mode
                  | (0x1 << CS12)     // Prescaler at CLK/8
                  | (0x1 << COM1A0);  // Toggle OC1A on timer match

As I was unable to figure out what was wrong, I tried hardcoding numbers, in case I (or the compiler / include files) was getting some of the names wrong.

 

Thanks!

Cal-linux

--

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

D'oh!!  I always had the idea that setting up a pin as an output actually meant "set it up as GPIO in output mode".  In my mind, setting up the timer to toggle OC1A pin would override (or in any case, it would be an alternative) to setting it up as GPIO in output mode....  Oh well, now I know!  :-)

Some peripherals override pin function.  Others do not.  Don't guess.  Check the datasheet.

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:

Some peripherals override pin function.  Others do not.  Don't guess.  Check the datasheet.

Oops!  That was certainly right in front of my eyes --- in retrospect, it seems inexcusable that I missed it, having looked at this very item in the datasheet!  :-\

 

(I guess next time I make this same mistake I'll have experience enough to know better!  :-) )

 

Cheers,

Cal-linux

--

 

Last Edited: Sat. Mar 10, 2018 - 01:50 PM