Not understanding something about timers

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

I think I understand what is going on in Dean's timer tutorial but I am running into problems going out on my own.

The long term goal is to detect the frequency on on audio channel and set a pin high depending on that frequency.

The short term goal is to use an external signal and LIGHT THE DAMNED LED!!!!!!

Sorry, who would have thought that a simple LED could cause that kind of frustration. :lol:

I have looked for a DTMF decoder but came up short. Every decoder I have found uses a dedicated IC for the task. If I was actually decoding a DTMF signal, I wouldn't think twice and go buy the IC. I found a pile of signal generator projects but I couldn't find anything going the other way.

Project specs:
Part: atmega168
Frequencies: currently sweeping from 500Hz to 1500Hz in 60 seconds
Compiler: avr-gcc

#include  
#include  

int main (void) 
{ 
    DDRB |= (1 << 0); // Set LED as output 
    
    TIMSK1 |= (1 << ICIE1); // Enable input capture interrupt 
    
    sei(); //  Enable global interrupts 
    
    OCR1A   = 1000; // Set compare value 
    
    TCCR1B |= ((1 << CS11) | (1 << CS12)); // Start clock on falling edge
    
    while (1) 
    {   
    }   
}

ISR(TIMER1_COMPA_vect) 
{ 
       PORTB ^= (1 << 0); // Toggle the LED 
}

What I THINK is going on:
- Set up the output pin for the STUPID LED!
- Make sure the timer interrupt mask is set for input capture.
- Turn on global interrupts.
- Set the number of cycles to wait before firing the interrupt.
- Start the clock
- Do nothing for a long time.
-- When the interrupt fires, toggle PORTB pin 0.

The audio is wired into pin 11 and an LED (that blinks on other projects) on pin 14.

Am I missing something stupid? Does anyone know of a good example I can rip apart? Is this way too difficult for a "just past blinking LED" project? What is the airspeed velocity of an unladen swallow?

You can have my mac when you pry my cold dead fingers off of it.

Kevin McEnhill -- mcenhillk@gmail.com

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

Other way around... audio goes into Input Capture... pin 14.... led goes to Output Compare 0 B pin 11.....

Imagecraft compiler user

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

You have enabled the input capture interrupt, but you provided an ISR for the output compare A interrupt. As soon as you provide a signal for input capture, your program will crash.

Quote:
Other way around... audio goes into Input Capture... pin 14.... led goes to Output Compare 0 B pin 11.....

For the purposes of the code provided, you have it correct: a frequency signal on T1 to drive the timer, and the LED manually toggled on PB0 (but the frequency won't change the brightness of the LED, only the rate at which it flashes). However, once you actually use an audio signal and want to determine its frequency, Bob's suggestion is what you want.

Regards,
Steve A.

The Board helps those that help themselves.

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

And regarding DTMF decoding - the signal consists of *two* different tones at any one time. Decoder chips will usually either use analogue filters or DSP techniques to filter for each of the eight tones and see which two are present.

There *may* be enough horsepower in an AVR at high speed to manage the DSP way of doing this but I'd hate to guarantee it without a fair amount of thinking about it.

If you try and decode it using zero crossings, you'll have to deal with an extremely complex signal which will be further involved by the transmission path and generation method of the signal (e.g. square waves/sine waves). I really really would not want to have to write code that could decode DTMF in that fashion.

An external DTMF decoder chip will almost certainly be the way to go (and I'm a bloke who decided to decode FSK at 1200 baud just to see if it was possible!)

Neil

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

Check this page for a decoding algorithm... http://en.wikipedia.org/wiki/Goe...

Whether the AVR has the horsepower I don't know...

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

Kevin,
Are you trying to find ONE frequency in an audio channel that is channeling realtime audio, or are you trying to pick up on a frequency in an audio channel that is slowly sweeping?

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

There is a dtmf decoder on simtel.com that uses fft in c by some French fellow... it compiles on my pc... I've been trying to get a version to run on a mega32... tricky... I'll work on it some more and show you what I've got if I get it to sort of work....

Imagecraft compiler user

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

What I am doing is playing the 12 Days of Chistmas on the right channel and a control tone on the left channel. There is no other sound on the left so I don't have to filter anything out. The signals I am trying to detect are 1kHz to 12kHz in 1kzHz increments. I don't have millisecond timing requirements so a little slop is ok.

The plan was to listen to the control tone for 250ms and base the output on the number of times the input fell during that quarter second. If it fell 250 times, the signal must have been the 1kHz signal which means turn on pin 0.

The only reason I was looking for DTMF projects is it was the only thing I could think of that reacted to the frequency fed into it. I still think it would be an interesting project but I'll wait till I am past the blinkin' light phase.

Thanks everyone for the help so far. I think I am back on the right track.

You can have my mac when you pry my cold dead fingers off of it.

Kevin McEnhill -- mcenhillk@gmail.com

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

What kind of masochist are you? THe twelve days of christmas? ok, now I really am interested, WHY the twelve days of christmas? you trying to shoot the partridge?

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

jgmdesign wrote:
What kind of masochist are you? THe twelve days of christmas? ok, now I really am interested, WHY the twelve days of christmas? you trying to shoot the partridge?

Jim

If you just cut the pear tree down, the Partridge will simply leave...No need to shoot the poor thing!

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

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

Umm... I have driven from Omaha to Kansas City to get a programmer for a uC that will drive a Christmas decoration. That fits my definition of a masochist! :twisted:

There will be 12 sections that light up and/or animate in sync with the music. Other songs will be loaded but the lighting will be synced differently.

I am using the Blizzard version of the song (http://www.blizzard.com/misc/xmas/) during development. I can only take so much Bing Crosby without shooting someone. :)

You can have my mac when you pry my cold dead fingers off of it.

Kevin McEnhill -- mcenhillk@gmail.com

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

mcenhillk wrote:
I can only take so much Bing Crosby without shooting someone. :)

Well, maybe shooting the Partridge is a better idea after all!!!

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

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

http://en.wikipedia.org/wiki/The_Partridge_Family

Ok, how much ammo can I use? :twisted:

You can have my mac when you pry my cold dead fingers off of it.

Kevin McEnhill -- mcenhillk@gmail.com

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

mcenhillk wrote:
http://en.wikipedia.org/wiki/The_Partridge_Family

Ok, how much ammo can I use? :twisted:

All of it!

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

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

Ah Christmas! Soon be time for more of this...

http://www.lightorama02.com/2005...

including the globally famous performance of "Wizards in Winter" by the Trans Siberian Orchestra and implemented by Carson Williams using this:

http://www.lightorama.com/produc...

That's possibly the best use of microcontrollers ever devised!

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

Going back to the original question... it might be simpler just to introduce a continuous synchronous or asynchronous bitstream using, say, biphase mark. Easy to code, easy to decode, and with so few commands required, loads of room to have unambiguous sync bytes and still leave space for expansion. Or even normal UART NRZ codes, though the DC offset might be a problem if your line is capacitively coupled.

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

Wow! :shock: I swear you are speaking English there. Can you expand on 'biphase mark'?

You can have my mac when you pry my cold dead fingers off of it.

Kevin McEnhill -- mcenhillk@gmail.com

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

mcenhillk wrote:
Can you expand on 'biphase mark'?

Can't help thinking that the very top link (to Wikipedia) that you get when you type those two words into Google may be quite useful. ;)

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

Yeah I read that but what I am missing is how that would be simpler than adding an audio signal to one channel. If I am to try to add the bitstream to the control channel of the audio track, won't I run into a problem with the phase of the biphase clock and the the system clock?

You can have my mac when you pry my cold dead fingers off of it.

Kevin McEnhill -- mcenhillk@gmail.com

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

No. Biphase mark uses a phase change at the end of every bit, and a further phase change in the middle of the '1' bit. If you detect these edges - easy to do with the edge-sensitive interrupt on the AVR as you can change the polarity of the edge with every fresh interrupt - then all you need to know is how long it was since the last edge. If you have a fixed bit rate, you need only know whether the time since the last edge is more than 3/4 of the expected bit period (in which case it's a 0) or less (in which case, it's half a 1). Slide the decoded 0 and 1s into a shift register (in software, of course) and watch wht comes out; you can even encode normal non-return to zero serial data complete with continuous '1's for the idle period, start and stop bits as required... and *that* you can output to a random pin wired to the RxD pin for an automatic decode of incoming serial stream[1].

The advantages?

- the signal is self clocking - it will work over an incredible range of frequencies[2] if you compare the with the length of the last bit rather than a fixed length, so you're not dependent on closely matched clocks.
- the DC averages zero, so it works over long lines, or capacitively coupled circuits, like audio outputs.
- there's very little code overhead to decode it, just a timer reset every edge and a small state machine
- it's fun trying to get your head around how it works[3]

Neil
[1] I wonder if you can achieve the same aim by enabling the uart and writing directly to the serial input pin?
[2] This is the system used on analogue video tapes to store timecode. With a 1000 bit per second signal recorded on an audio track, it will work from one tenth normal speed through to ten times normal speed or more.
[3] If it's not fun, why are you here?

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

http://pdfserv.maxim-ic.com/en/e...

Page 17 + ...may help.

JChristoff
Illinois

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

Quote:
The plan was to listen to the control tone for 250ms and base the output on the number of times the input fell during that quarter second. If it fell 250 times, the signal must have been the 1kHz signal which means turn on pin 0.

I think it is sufficient to measure time of halfperiod like in this pseudocode.

crystal = 10 MHz

Do
clear Timer
wait_until_bit_is_set(portx.x)
wait_until_bit_is_clear(portx.x)
start Timer
wait_until_bit_is_set(portx.x)
stop Timer
if Timer<5000 then LED1 (freq. reached 1000 Hz)
if Timer<2500 then LED2 (freq. reached 2000 Hz)
...
...
Loop 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks barnacle. How would you generate the signal to embed into the audio file? Right now, I am using a program to generate my control tones but that software will only work for single waves. Since the audio will be coming from an MP3 player, will the compression muck with the signal?

Biphase marking does sound interesting but I think I will work on a failsafe (though not as interesting) method before I jump into figuring this out. If I get it before the holidaze are over, a quick fix will be in order.

jgrunt, thanks for the document. That is what I was looking for as an example.

Visovian, when I figure out how to get the signal in, I'll try it.

I have some work to do, I think I'll have to take a couple of day off of work and play in my dungeon for a while. :-)

You can have my mac when you pry my cold dead fingers off of it.

Kevin McEnhill -- mcenhillk@gmail.com

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

Ah, more data :mrgreen:

To be honest, I haven't tried encoding/decoding a biphase mark signal through MP3 - frankly, I don't think it will work well. MP3 coding converts short frames of audio (24ms, I think) into frequency/amplitude form, has a good look at the resultant, and uses a technique called 'psycho-acoustic masking' - knowing that you can't actually hear some frequencies in the presence of others - to remove some of the audio data. when it's put back together, the data you couldn't hear - but which might be significant - isn't there. Also, this method preserves the amplitude information of the remnants you *can* hear but not their phase information. I'd expect it to get delightfully garbled...

You have interested me now in the process - I may have a play this afternoon. I'll post results in the off-topic forum , probably.

Regarding your original problem, MP3 will happily code single frequencies and if your pulse train length is long compared to the MP3 frame, the phase glitches at the end of the frames shouldn't be an issue. You will probably want to arrange things so that as long as a single frequency is detected the appropriate output is high; only when the signal is absent for several cycles or has been detected as a new frequency for several cycles should you change the output, to cope with the framing from the MP3 coding.

One change to your original proposal I would make though is to alter the spacing and frequencies of your control frequencies. As planned - 1kHz spacing up to 12kHz, the difference between the top two or three frequencies is only a few microseconds so it doesn't give you long to detect centre crossings etc., plus it's lot of high energy audio which might leak onto your desired audio. Also, the MP3 cares less about higher frequencies...

I'd start with a much lower frequency - probably (to keep it musical :) ) the A two octaves below concert A - 110Hz - and maybe go up in fourths:

Note    Frequency    Signal
A2      110          1
D3#     155          2
A3      220          3
D4#     310          4
A4      440          5
D5#     610          6
A5      880          7
D6#     1220         8
A6      1760         9
D7#     2440         10
A7      3520         11
D8#     4880         12

That gives you the whole range slap in middle of the frequency bands that MP3 is most interested in, and therefore least likely to get minced up; allows you to sample your D/A converter at 16k samples per second (or even as low as about 10k samples, though I wouldn't recommend it) if you decide to feed audio straight in without any conditioning circuit (e.g. an external comparator to generate a square wave); has frequencies which are easily distinct from each other both to the human ear and to a comparison routine; and - should you wish - will even allow you to whistle at your receiver to switch things manually, if you have a reasonable ear for music :D

One thought - if you do sample and convert the audio input directly, you can use the sample frequency 'sample complete' interrupts to do your timing count for you. No need for a separate timer routine.

For interest, the musical 'fourth' is the gap between the first two notes of 'On Ilkley Moor 'Baht 'At' or 'Oh Tannenbaum'.

Neil (glad to have found something to distract him this afternoon - too many night shifts!)

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

OMG! I think I accidently activated your coolProblem() interrupt!

I just took a quick scan of your "War and Peace" sized tutorial on biphase mark encoding. That is amazing! When I get a chance to run though it I'll let you know how things go.

Status on current problem: I was using my mega168 completely wrong. I think I am straight now. I am still working toward measuring time between edges to determine the frequency but I think I have a better algorithm to accomplish this.

BTW, what chip has the most alternative functions assigned to one pin? I picked up the mega168 based on a recommendation somewhere on this forum. Every pin on this thing has at least three different functions and sometimes four. The simulavr program doesn't support the mega168 so I picked up a mega16 just to play with. Now that I have it, it looks like a much simpler chip for a n00b to figure out. Lots of pins to spread the same functionality over, fewer control register decisions to make (i.e. this or that).

You can have my mac when you pry my cold dead fingers off of it.

Kevin McEnhill -- mcenhillk@gmail.com

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

Afraid you did, Kevin :D

Keep an eye on it - it hasn't stopped growing yet...

Neil

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
coolProblem() 
  
  rti

I've stopped. You're on your own, now :mrgreen:

Neil

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

Thanks for your help. I have been neck deep in this all weekend and having a good time.

You can have my mac when you pry my cold dead fingers off of it.

Kevin McEnhill -- mcenhillk@gmail.com