View previous topic :: View next topic |
Author |
Message |
DJD XTREEM
Joined: 24 Jun 2004 Posts: 190 Location: 127.0.0.1
|
Posted: Mon Oct 17, 2005 11:10 am Post subject: IR Serial link @ 76KHZ |
|
|
In order to let a Lego Spybotic robot talk with an AVR i need to generate a 76 khz carryer wave.
The datastream is just a serial link on 4800baud 8N1
Now i thought to set timer1 to 76 khz with a duty cycle of 25% ..
And there is where the problems begin..
I can't get it working on 76 Khz with a duty cycle of 25%..
Serial comunication with hyper terminal is no problem.. But the 76 Khz is difficult to generate for me..
Using a attiny 2313 on 8 mhz.. (can be build to 12 if nesseary..)
Who can help me out of this on?? Need this working on 20 Oktober.. Allready a week experimenting to get it working.. But i have almost no experince with timers..
Many thanks in advance,
Daniel Wittenaar _________________ Bascom AVR 1.11.8.8 full version
STK500 Programmer
ISP Programmer |
|
Back to top |
|
|
DToolan
Joined: 14 Aug 2004 Posts: 1384 Location: Dallas / Fort Worth, Texas (USA)
|
Posted: Thu Oct 20, 2005 12:11 am Post subject: |
|
|
Quote: | i thought to set timer1 to 76 khz with a duty cycle of 25%... |
At 8 MHz, the closest you can come to 76 KHz is 76.19KHz (this may be good enough?).
Using the system clock with no prescale, 105 counts will give you the 76.19 KHz period. Using fast PWM mode, we can have the timer set the OC1A pin on a compare match and reset it at some top value. COM1A0 = 1 and COM1A1 = 1 will tell the timer to do this.
Since the total period will be 105 counts, 25% on-time leaves 75% off-time and (105 x .75 = 78.75). We will fill OCR1AL with &H4F (79) so when the counter reaches that point, it will set the OC1A pin.
Selecting mode 14 from table 46 of your AVR doc tells the timer we will use ICR1 to hold our top value (105). We will fill ICR1L with &H69 (105) so when the counter reaches that point, it will reset the OC1A pin.
None of this will be exact as 105 counts at the system clock (no prescale) is the closest you can get to 76 KHz. You can't divide 105 counts equally into a 75% / 25% ratio so you'll get "almost" 76 KHz and "almost" a 25% duty cycle.
I think the following should work (I don't have a 2313)...
Code: | $regfile = "ATtiny2313.dat"
$crystal = 8000000
Config PortB.3 = Output 'the OC1A pin
OCR1AL = &H4F 'set the OC1A pin at a count of 79
ICR1L = &H69 'reset the OC1A pin (and the counter) at a count of 105
TCCR1A = &b11000010 'compare match settings for PWM mode
TCCR1B = &b00011001 'more PWM mode selections and use system clock
'the timer is now running in PWM mode 14
'and automatically setting / resetting the OC1A pin |
|
|
Back to top |
|
|
DJD XTREEM
Joined: 24 Jun 2004 Posts: 190 Location: 127.0.0.1
|
Posted: Thu Oct 20, 2005 5:41 am Post subject: |
|
|
Thank you for your advice.
I found out that the original IR transmitter has many up and downs in the IR wave.. it goes from 74 to 78 Khz.. As it has to be 76 by the manual..
So that differents has no effect on my programm
I will try this code as soon i'm in reach of my programmer.. Should be somewhere around this afternoon..
And maybe i have to finnish this project also this afternoon.. Because somebody want to play with it..
You will hear from me if it works and what frequency it runs.. _________________ Bascom AVR 1.11.8.8 full version
STK500 Programmer
ISP Programmer |
|
Back to top |
|
|
DJD XTREEM
Joined: 24 Jun 2004 Posts: 190 Location: 127.0.0.1
|
Posted: Fri Oct 21, 2005 6:48 pm Post subject: |
|
|
After compiling this and run it on an avr.. I got an output from around 8Khz..
with a dutycylce somewhere around 80 over 20..
But because i'm not really understanding the programm it is hard to get the right frequency..
Some sugestions?? _________________ Bascom AVR 1.11.8.8 full version
STK500 Programmer
ISP Programmer |
|
Back to top |
|
|
DToolan
Joined: 14 Aug 2004 Posts: 1384 Location: Dallas / Fort Worth, Texas (USA)
|
Posted: Sat Oct 22, 2005 12:17 am Post subject: |
|
|
Quote: | But because i'm not really understanding the programm it is hard to get the right frequency... |
Ok, for a basic primer on timers... go to this thread and download the calculation tool mentioned within (if you don't already have it).
http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=545
What follows below was calculated the same as above for you, except that I have a 14.7456MHz Mega128. In the calc tool, I selected 14.746MHz as my crystal speed and entered 76000 Hz as my desired timer. The "Prescale" (TCCRxx) is a division of the system clock. "None" means to use the system clock... this is what Bascom calls "Prescale = 1".
If, for instance, you wanted to use an 8-bit timer for a 1 sec desired interrupt... the timer would overflow much faster than one second. The calc tool would tell you "Out of Timer Range". In this case, you would select other Prescale values to divide the system clock by (make the timer count slower). The lowest possible prescale value (within range of the timer and desired interrupt) will give you the greatest accuracy... i.e. will be closest to your desired interrupt time.
You'll notice a box for TCNT0 (8-bit timer 0) and two boxes for TCNT1L/TCNT1H (16-bit timer 1). In "normal" mode (discussed in the aforementioned thread), these boxes tell you what value to preload and reload your timer with. "Normal" mode operates your timer in an "interrupt me on overflow" state. Pre-loading your timer with some value leaves some other value (xx) "left over". This "left over" value determines how many counts the timer will go through until the overflow interrupt.
You'll also notice two boxes for OCR1AL/OCR1AH (Output Capture Mode). This can also be used for "Clear Timer on Compare" mode (discussed in the aforementioned thread). These boxes tell you what to put in your timer registers as a compare match value (count up to here and then give me an interrupt). In "CTC" mode, we don't have to preload and reload our timer with some value to leave xx number of counts left over. It is not operated with overflow in mind, but instead, a compare match value is the interrupt source. The timer will count up to this value, generate an interrupt, and then clear the counter (start over).
Ok, so for a 76KHz interrupt using the system clock (no prescale), my 128 should count to &HC2 (194 counts). Yours, with a slower oscillator (counting slower), only needed to count to &H69 (105). If I do something every 194 counts, I'll end up with a 76KHz frequency.
Continuing, you said you wanted a 25% positive period and a 75% low period. I know that my total period is 194 counts so 75% is 145.5. At 146 counts (on a compare match), I'll tell my timer to set the OC1A pin. This option is set with the COM1A1 and COM1A0 bits in TCCR1A (look in your ATtiny2313 doc, page 104, table 44). After the timer sets the OC1A pin at 146 counts, I want it to continue counting to 194 and then reset the OC1A pin. This is a "TOP" value to count up to, and again, is an option listed in table 44.
Since my top count will be 194 and the timer will set the pin high at 146 and low at 194, I'll have a 25% duty cycle within the 194 total count (period).
We are having Timer1 automatically generate a pulse so we want PWM mode. Page 106, table 46 of your ATtiny2313 doc, lists PWM mode 14 which nicely fits our needs. The table shows what bits to set in the various timer registers to operate in this mode. It also shows that the registers ICR1L/ICR1H will hold (be the source of) the "TOP" (count up to) value.
Code: |
'$regfile = "ATtiny2313.dat"
'$crystal = 8000000
'Config PortB.3 = Output 'the OC1A pin for an ATtiny2313
'OCR1AL = &H4F 'set the OC1A pin at a count of 79
'ICR1L = &H69 'reset the OC1A pin (and the counter) at a count of 105
'TCCR1A = &b11000010 'compare match settings for PWM mode
'TCCR1B = &b00011001 'more PWM mode selections and use system clock
$regfile = "m128def.dat"
$crystal = 14745600
Config PortB.5 = Output 'the OC1A pin for an m128
OCR1AL = &H92 'set the OC1A pin at a count of 146
ICR1L = &HC2 'reset the OC1A pin (and the counter) at a count of 194
TCCR1A = &b11000010 'compare match settings for PWM mode
TCCR1B = &b00011001 'more PWM mode selections and use system clock
DO
'the timer is now running in PWM mode 14 (as per table 46 - ATtiny2313 doc)
'and automatically setting / resetting the OC1A pin (as per table 44 - ATtiny2313 doc)
LOOP
END |
What does the above code give me? What does it produce?
Now why it works for me but doesn't work for you (gives you an 8KHz frequency), I don't know. Why you have a high period 80% and low period 20%, I don't know. My scope has an "invert" function which would display the trace opposite it's true form... giving the illusion of a longer high period, shorter low period. Maybe your scope is in this mode?
Your registers, names and options for Timer1 are exactly the same as mine. I'd make a guess that your ATtiny2313 isn't really operating at 8MHz... otherwise, you'd have a 76KHz frequency on OC1A. Check your fuse bits. I bet your ATtiny2313 is operating on an internal 1MHz clock. If this is true, either fix it or use the calc tool to recalculate for 1MHz. |
|
Back to top |
|
|
DJD XTREEM
Joined: 24 Jun 2004 Posts: 190 Location: 127.0.0.1
|
Posted: Sat Oct 22, 2005 6:15 am Post subject: |
|
|
Thanks a lot for this explanation..
I used a 90s2313 so no internal crystal is possible..
Scoping on the crystal says me that is works on 8 mhz.. (pind.3 pind.4)
I will try another crystal (16mhz Recalculated) and will try a tiny2313..
hopefully get it to work this afternoon..
Greets Daniel _________________ Bascom AVR 1.11.8.8 full version
STK500 Programmer
ISP Programmer |
|
Back to top |
|
|
DToolan
Joined: 14 Aug 2004 Posts: 1384 Location: Dallas / Fort Worth, Texas (USA)
|
Posted: Sat Oct 22, 2005 9:37 pm Post subject: |
|
|
Quote: | I used a 90s2313 so no internal crystal is possible... |
Umm... your initial post said ATtiny2313. The 90s2313 and the Tiny2313 are like Coke and Pepsi... similar but different. There are many enhancements to the timers in the ATtiny (or Mega series) vs. the 90s. The 90s2313 has fixed "TOP" values for the PWM which translates to fixed frequencies (a division of OSC). The ATtiny2313 can have variable "TOP" values set by IRC1x which translates to variable frequencies independent of OSC.
You could still produce the frequency and pulse width you want on this chip (AT90s2313) but not as an automatic (background PWM) feature of the timer (see note 1). You would have to SET/RESET the pin actively in an ISR.
You'd have to set the timer to interrupt at 79 counts of the system clock (8MHz - CTC mode). In the ISR you'd set a pin and wait 26 more counts... (no sense jumping out of the ISR for only 26 oscillator ticks). After 26 more counts, reset the pin, reset the timer value (it's count) to 0 and return. 79 low + 26 high = 105 counts which will give you the 76KHz frequency and 25% duty cycle.
The upside of producing the pulse in this fashion is that the same code can be used on both chips. The downside is it will eat up 30% of your processing power (time) producing the pulse instead of having it produced in the background via PWM.
1. An automatic (background PWM) 76KHz frequency could be achieved by choosing an oscillator frequency that could be divided by 510, 1022 or 2046 to achieve this (as per table 11, page 35, AT90S2313). |
|
Back to top |
|
|
DJD XTREEM
Joined: 24 Jun 2004 Posts: 190 Location: 127.0.0.1
|
Posted: Sun Nov 06, 2005 12:07 pm Post subject: |
|
|
Finally got an tiny2313 this weekend.. Programmed it and yes 76Khz really nice..
If i have finnished the code with sending serial commands.. I will post the code and maybe the schematic here.. _________________ Bascom AVR 1.11.8.8 full version
STK500 Programmer
ISP Programmer |
|
Back to top |
|
|
Ro-Bot-X
Joined: 16 Nov 2005 Posts: 5 Location: Right now, Indiana, USA
|
Posted: Sun Jan 22, 2006 3:27 am Post subject: 40kHz frequency using Timer2 PWM on atmega8 |
|
|
Hi guys,
I am trying to understand all this things but unfortunately is not easy for me. I am trying to generate a 36kHz or a 40kHz 50% duty cycle frequency using Timer2 PWM on a atmega8 (with a 8MHz oscilator). I use Timer1 PWM1a and PWM1b for motor driver, so I can't use them for this. I looked on the atmega8 pdf file, but did not understood how to do it. I would realy like to know how to calculate different frequency between 32kHz and 48kHz. I am trying to drive a IR LED and get the light bounce of an object and sense it with a IR demodulator IC (like the ones in TVs). I need to do a frequency sweep, cause the demodulator IC is sensitive differently around it's labeled frequency. This way I can guess the distance the object may be. If some of you guys can help me with this I will realy apreciate it.
Thank a lot,
Gabriel _________________ Build Robots! instead of playing Quake... |
|
Back to top |
|
|
Ro-Bot-X
Joined: 16 Nov 2005 Posts: 5 Location: Right now, Indiana, USA
|
Posted: Sun Jan 22, 2006 3:27 am Post subject: 40kHz frequency using Timer2 PWM on atmega8 |
|
|
Hi guys,
I am trying to understand all this things but unfortunately is not easy for me. I am trying to generate a 36kHz or a 40kHz 50% duty cycle frequency using Timer2 PWM on a atmega8 (with a 8MHz oscilator). I use Timer1 PWM1a and PWM1b for motor driver, so I can't use them for this. I looked on the atmega8 pdf file, but did not understood how to do it. I would realy like to know how to calculate different frequency between 32kHz and 48kHz. I am trying to drive a IR LED and get the light bounce of an object and sense it with a IR demodulator IC (like the ones in TVs). I need to do a frequency sweep, cause the demodulator IC is sensitive differently around it's labeled frequency. This way I can guess the distance the object may be. If some of you guys can help me with this I will realy apreciate it.
Thank a lot,
Gabriel _________________ Build Robots! instead of playing Quake... |
|
Back to top |
|
|
bzijlstra
Joined: 30 Dec 2004 Posts: 1179 Location: Tilburg - Netherlands
|
Posted: Sun Jan 22, 2006 11:08 am Post subject: Thanks |
|
|
DToolan
Thank you very much for the very good example of the timers.
Until now I haven't done much with timers but after reading this I will do some tests.
Have fun
Ben Zijlstra |
|
Back to top |
|
|
|