Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

AVR ATtiny13: Setting up Timer0?

Status
Not open for further replies.

HarveyH42

Banned
Still very new to microcontrollers, and having trouble deciphering the information in the data sheets and app notes. It's a little too generic and broad for me to adapt to my project at this point.

Basically, looking to output a 38kHz signal to drive an IR LED. It's for proximity detection with an IR reciever module.

I need to set a pin on PORTB for output with DDRB.
Select the clock source (internal oscillator) TCCR0
And TCNT0 to set the frequency.

I'm under the impression that once it's started, the timer will generate a square wave and output it to the selected pin, independent of the program.

Just having trouble figuring out which bits are which, and whether they need to be set or cleared. I'm not familiar enough with all the labels, to get this figured out.

Oh, I'm working in assembler, and don't much 'C' (not enough to be useful). O know this has to super-simpler, just need to see it.
 
TCNT0 is the actual counter value - the ugly way is to manually set this everytime it exceeds a certain value - but that's the wrong way of doing things...

Look at the table on page 70 of the data sheet - "Waveform Generation Mode Bit Description". This summarizes the 8 counting modes that the timer can be used as. The default mode is to have TCNT0 count from 0 to 0xFF and rollover to 0 again. When you set OCR0A or OCR0B to some value, you end up with the standard pulse-width modulation/DAC type setup.

Since you're trying to get a particular frequency, you need to use modes 2,5, or 7. 2 is probably good enough - in this mode TCNT0 counts from 0 to OCR0A, then gets reset to 0. So, figure out how many clock cycles in a 38KHz cycle and set the prescaler and OCR0A accordingly.

So:
DDRB = xx; set to output...
TCCR0A = 01 00 0010b ; toggle on compare match and WGM = 2
TCCR0B = 00000xxxb ; throw in prescaler values here
OCR0A = xx; use eqn on page 63

so the code looks a lot like
LDI r16, 0b01000010
STS TCCR0A, r16

or something like that. For IO registers that are close to 0, they can also be written as
OUT TCCR0A, r16 ;only takes 1 cycle to execute versus 2 for the STS

Also, snag a copy of the instruction set guide if you haven't already
https://www.electro-tech-online.com/custompdfs/2006/12/doc0856.pdf

[And download some of the sample code they include with the app notes too]
 
Thanks, this helps out a great deal. I'll give it a try and post the results. I'm mostly having trouble with the labels for registers and bits, need to make list of what's pre-defined. I've got the instruction set printed (170 pg), the Tiny13 data sheet, setting up timers/counters, hardware consideration... Basically grabbed everything related to getting started.
 
Cool, got 40 khz out on pin 5. Played around with it some, but couldn't get any closer to 38 KHz, maybe good enough. Going to hook up the IR module in a little bit to see it responds. Here's the program I used to set it up.

.include "tn13def.inc"

.def Temp = R16

.org 0x0000

RESET:
ldi Temp, 0xff
out DDRB, Temp
ldi Temp, 0b01000010
out TCCR0A, Temp
ldi Temp, 0b00000010
out TCCR0B, Temp
ldi Temp, 0b00000110
out OCR0A, Temp

Loop:
rjmp Loop
 
Suggestion. If you have any intention of using AVR's for more than about a month or so ditch your comment scheme... Each of those bits DDRB and TCCR and OCR have actual definitions for them, and using bit shifting using the defined names will make your code DRAMATICALLY more readable both to yourself and to others. The only exclusion would be for list fields, such as the bits that control the timer prescaler options, or others which follow a defined list and bit sequence. In order to make any sense of that code above you need to either be an AVR super genius or have the PDF for that EXACT chip open and the register overview one the screen.
 
I should have mentioned that I just started programming microcontrollers 6 days ago, guess it shows. My previous assembly experience was about 15 years ago, on a Commodore-64, 6502 processor. I wrote my own assembler, never used labels before this. I am try to get a handle on all this new stuff, but its a little overwhelming. Each bit in a register has a name/label, but don't know how to combine them in the program statements, so put them together in binary. I have the bit names, but don't know how to set or clear them by name yet. Figure I'll pick it up as I go along, need to lose the old habits, and learn some new ones. Comments and remarks... never could get into it, too much typing to begin with.

Just for an example, would you mind rewriting the code, as it should be? You set me on track, and I got it working. Got a little side tracked, but tomorrow I'm going to test a pin-change-interupt routine (written in a similar style as above). If it works out, the project will be completed soon after, and planning on posting it here. It's a IR proximity alarm, flashing LEDS and maybe a siren from Timer1. Should be smaller than a 9v battery.
 
[MODNOTE]This thread is too old and most of the participants are banned
Please acknowledge the date before you revive old threads...[/MODNOTE]
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top