Timers and Interrupts

A "new fashioned" televisor, using an Arduino to drive the motor and display.

Moderators: Dave Moll, Andrew Davie, Steve Anderson

Timers and Interrupts

Postby Andrew Davie » Thu Mar 02, 2017 10:57 pm

Here's a great tutorial on getting timers going.

Looks like the Arduino Micro does not have a timer2, so that code needs to be removed.
I modified the frequency scalar to x1 and chose 1599 as the OCR (output compare register).

According to the docs...

interrupt frequency (Hz) = (Arduino clock speed 16,000,000Hz) / (prescaler * (compare match register + 1))

= 16,000,000/(1*(1599+1))
= 10kHz

So, I have a 10kHz timer running. Easy as that!
The thinking here is that I have an incoming analog signal on one of the pins, and I go and read the ADC value at 10kHz which should be about right for sampling the analogue NBTV signal. I can increase/reduce the frequency of course as I see how that goes. Interesting that the sample code shows output from an oscilloscope with a square wave generated via timer interrupt. Impressive how EASY it all is!

Anyway, I have to solder a header onto this tiny thing, and then I can connect to the LineOut from the CD player.
I have to understand how the ADC works, too and at what rate that can work.

I'll do a separate thread on the ADC
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Timers and Interrupts

Postby Andrew Davie » Thu Mar 02, 2017 11:05 pm

Seems the maximum rate the ADCs can sample is 10kHz.
So, that sits nicely with the NBTV bandwidth, but we're not going to be able to go higher. That's OK.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Timers and Interrupts

Postby Andrew Davie » Thu Mar 02, 2017 11:16 pm

Here's the test-code to toggle the on-board LED at 10kHz
You can change the pin from 13 (the LED) to some output pin and confirm on an oscilloscope.
I'll take it on faith, for now.

What I'm going to do next is insert an analogue pin read into the interrupt service routine, so we have a 10kHz sample of an incoming audio signal.
Then I can analyse that for sync pulses, separate out the signal from the 0.3V shelf (or whatever it's at) and feed the resultant signal to an output pin to drive the LEDs.
Of course the LED array will be driven by a transistor - and I need to revisit how LEDs work - they are driven by current not voltage, right? So that's a "todo" in terms of understanding what to do.




Code: Select all
//https://www.instructables.com/id/Arduino-Timer-Interrupts/


boolean toggle1 = 0;


void setup(){
 
  //set pins as outputs
  pinMode(13, OUTPUT);    // LED

  cli();//stop interrupts

  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0

  //set timer1 interrupt at 10KHz
  // set compare match register for 1hz increments
  OCR1A = 1599;//  = [ 16,000,000Hz/ (1prescaler * desired interrupt frequency) ] - 1
 
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS10 bit for NO (1X) prescaler
  TCCR1B |= (1<<CS10);    //1x
 
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);

  sei();  //allow interrupts

}//end setup


ISR(TIMER1_COMPA_vect){
  //timer1 interrupt 10kHz toggles pin 13 (LED)
  // you can output a square wave on some other pin just by changing to that pin # (!)
  //generates square wave of frequency 10kHz
  if (toggle1){
    digitalWrite(13,HIGH);
    toggle1 = 0;
  }
  else{
    digitalWrite(13,LOW);
    toggle1 = 1;
  }
}
 


void loop(){
  //do other things here
}
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Timers and Interrupts

Postby Andrew Davie » Thu Mar 02, 2017 11:21 pm

OK, looks like controlling current to LEDs with Arduino is pretty simple. Lovely that a gazillion people have gone before and done all this stuff. I'm just putting lego pieces together so far.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Timers and Interrupts

Postby Klaas Robers » Thu Mar 02, 2017 11:51 pm

If you really wat to sample an NBTV signal, you should do that with a sample frequency of about 24 kHz.
- the NBTV signal goes from 0 to 10 kHz,
- the sample frequency must be higher (say 20%) than twice the highest frequency.

I just looked back in the wave files for the CDs, the sync pulses on the NBTV club discs are 8 samples of a sample rate of 44.100 kHz. So on 10 kHz they are still 1.8 sample long, so you will not mis them. Mostly two samples, sometimes one. Yes, analog to digital conversion is a time consuming action......
User avatar
Klaas Robers
"Gomez!", "Oh Morticia."
 
Posts: 1656
Joined: Wed Jan 24, 2007 8:42 pm
Location: Valkenswaard, the Netherlands

Re: Timers and Interrupts

Postby Andrew Davie » Fri Mar 03, 2017 12:13 am

Klaas Robers wrote:If you really wat to sample an NBTV signal, you should do that with a sample frequency of about 24 kHz.
- the NBTV signal goes from 0 to 10 kHz,
- the sample frequency must be higher (say 20%) than twice the highest frequency.

I just looked back in the wave files for the CDs, the sync pulses on the NBTV club discs are 8 samples of a sample rate of 44.100 kHz. So on 10 kHz they are still 1.8 sample long, so you will not mis them. Mostly two samples, sometimes one. Yes, analog to digital conversion is a time consuming action......


Thanks.
If you want to reconstruct a signal accurately you need to be above they Nyqvist frequency, yes.
But my thinking was - we are just getting a signal that
a) indicates yes/no on sync
b) drives brightness of LEDs.
We don't suffer too much if b) isn't showing all time-based variations, just as long as the intensity is correct.
In other words, I'm not too fussed about being able to perfectly reconstruct the video signal. Just "good enough" is good enough. I hope.
That is we can afford if the sampling in time isn't the greatest - that reduces the vertical resolution of the image but i will see how that goes.

Thanks for the info/check on the sync pulse width on the CDs - very useful and good to know.
I had not considered the width of the sync pulses in terms of being missed via subsampling. So... good.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Timers and Interrupts

Postby Andrew Davie » Fri Mar 03, 2017 12:49 am

I just had an awesome idea! If I fed the incoming NBTV waveform to TWO input ADC pins, and read the values out of phase with each other then I would effectively be reading the original signal at twice the frequency. I can't see any reason why this wouldn't work.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Timers and Interrupts

Postby Klaas Robers » Fri Mar 03, 2017 12:55 am

This is what Karen also did in her NBTV to Video project in the PIC controller. It works, but you need to do some trick to read them in the correct time difference.

If you have two Analog inputs more, you can do it at 40 kHz. That is really high enough. 20 kHz is marginal, as in-band components between 10 kHz en 20 kHz will fold back.
User avatar
Klaas Robers
"Gomez!", "Oh Morticia."
 
Posts: 1656
Joined: Wed Jan 24, 2007 8:42 pm
Location: Valkenswaard, the Netherlands

Re: Timers and Interrupts

Postby Andrew Davie » Fri Mar 03, 2017 1:12 am

Klaas Robers wrote:This is what Karen also did in her NBTV to Video project in the PIC controller. It works, but you need to do some trick to read them in the correct time difference.

If you have two Analog inputs more, you can do it at 40 kHz. That is really high enough. 20 kHz is marginal, as in-band components between 10 kHz en 20 kHz will fold back.


There are 12 analog inputs :)
Seems to be only one 16-bit timer, but with just the one timer I could read all of the inputs used, with a delay between each read.
The delay could be tailored/calculated to be exact (either through a 8-bit timer, or just timed-out code).
I'm pretty sure that since Karen (wish she were still on this forum - she was such a great contributor!) has shown it's possible, then the ADC sample time limit of 10kHz is no longer an issue with this design/concept. Just use as many ADC channels as required, a single 10kHz timer, and one-shot sub-timers to read the other channel (or two).
The limit being the number of 8-bit timers (one is definitely available) so a 2-pin 20kHz ADC read seems simple with the timer, but up to 12 with delayed-reads also seems feasible, at the cost of chewing up CPU time.
Will keep it simple with just one pin/ADC read for my first efforts.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Timers and Interrupts

Postby gary » Fri Mar 03, 2017 10:11 am

This may or may not be applicable or helpful, and you may have already come across it - but for what it's worth:
https://www.instructables.com/id/Arduino-Audio-Input/
gary
 

Re: Timers and Interrupts

Postby Andrew Davie » Fri Mar 03, 2017 11:04 am

gary wrote:This may or may not be applicable or helpful, and you may have already come across it - but for what it's worth:
https://www.instructables.com/id/Arduino-Audio-Input/


Great link! There's some really useful stuff in there. Arduino is actually quite a capable little beast. I see that this solution tells the Arduino to use just one ADC to get to ~38kHz. I'm hoping I can still have two ADC running (one for the IR signal, one for the NBTV signal), but if not then I can still use some external solution to convert the IR signal into a binary 1/0 digital signal and do it that way.

Good to have in the toolbox - but I will still probably keep it simple for version 1. Maybe. Since the source code is all there, already, for this solution, it's tempting!
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia


Return to Andrew Davie's Arduino Televisor

Who is online

Users browsing this forum: No registered users and 6 guests