go_away

Author Topic: SQUARE WAVE QUESTUON.  (Read 2969 times)

0 Members and 1 Guest are viewing this topic.

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
SQUARE WAVE QUESTUON.
« on: January 25, 2009, 04:41:14 PM »
I have a TCS230D color sensor that I would like to use with my atmega-640, I have a little problem though. I

cannot figure out how to connect it to my controller!!!Lol ;D.Its output is a square wave (50% duty cycle) and

frequency directly proportional to light intensity (irradiance). My question is can the input pins read a square wave?

If so how do I go about doing it? If not how can I convert the square wave to a sinusoidal wave Or is there a

better way ?

Here is a link to the color sensors specs.Thanks for the help.

http://www.taosinc.com/ProductDetail.aspx?product=3

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: SQUARE WAVE QUESTUON.
« Reply #1 on: January 25, 2009, 06:06:18 PM »
You could try connecting its output straight to an a/d input. It will likely vary the value.
You could also buy a frequency to voltage ic and then sample it through the analog/digital pin.

(Quick idea~ have you tried using it through a fast transistor and then to a piezo speaker. color changes should register as different sounds out of the piezo..... could be a fun side project)

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: SQUARE WAVE QUESTUON.
« Reply #2 on: January 25, 2009, 07:22:55 PM »
Once I hook it up to the A/D converter pin how can I see exactly what the A/d converter is outputting? Thanks for your help. ;D

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: SQUARE WAVE QUESTUON.
« Reply #3 on: January 26, 2009, 10:27:50 AM »
You could try connecting its output straight to an a/d input. It will likely vary the value.

Are you sure?
If the duty cycle is constant, the voltage is also constant. In this case it will be 0.5 * Vmax

I would go with the interrupt technique and measure the period. What is the min/max frequency of the signal?

Michel.

Offline jpwalters

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
Re: SQUARE WAVE QUESTUON.
« Reply #4 on: January 26, 2009, 10:50:28 AM »
You could try connecting its output straight to an a/d input. It will likely vary the value.

Are you sure?
If the duty cycle is constant, the voltage is also constant. In this case it will be 0.5 * Vmax

I would go with the interrupt technique and measure the period. What is the min/max frequency of the signal?

Michel.
This was my thought as well.  Regardless of the actual frequency, the square wave will always have a 50% duty cycle, meaning that the analog input will always read a voltage of half the total signal voltage.

Oh, I'm on the same team with Caleb by the way, just so you guys know.  We are using the Axon, if that helps at all.

But anyway, to expand on what Caleb said, we basically have three options to read the output signal from the color sensor, which as stated is a square wave frequency that, from initial testing at least, will fall somewhere in the range of 0-30 kHz.  We can either count the number of pulses over a given period of time, or we can measure the length of a single pulse, or we can use an external frequency-to-voltage converter and then input that voltage to the ADC.  If possible, we'd like to use one of the first two options since those could be accomplished entirely in software, but we aren't sure exactly how to make that happen.

I've read another post here that goes over a technique that basically measures the length of a single pulse by resetting a timer when the pulse goes high, start counting with the timer, and then stopping and reading the timer value when the pulse goes low.  Would this be a good method, and if so, how exactly would one go about implementing this technique in software for the Axon?

Thanks in advance for everyone's help.  This site has been invaluable for our project.  All of us were pretty much complete noobs at this stuff when we started, and we still have a lot to learn, but we've come a long way thanks to all the great information provided on the website and the forums.  You have our gratitude!


Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: SQUARE WAVE QUESTUON.
« Reply #5 on: January 26, 2009, 01:16:30 PM »
I've read another post here that goes over a technique that basically measures the length of a single pulse by resetting a timer when the pulse goes high, start counting with the timer, and then stopping and reading the timer value when the pulse goes low.  Would this be a good method, and if so, how exactly would one go about implementing this technique in software for the Axon?


That's exactly how I would do it. But I might be wrong :p Check the datasheet of your MCU to see what timer do you have and the resolution.
« Last Edit: January 26, 2009, 01:17:59 PM by chelmi »

Offline jpwalters

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
Re: SQUARE WAVE QUESTUON.
« Reply #6 on: January 26, 2009, 04:38:19 PM »
With the Axon we have access to two 8-bit timers and four 16-bit timers.  The sensor also has the ability to scale down its output frequency to a 1:5 or 1:50 ratio, so resolution shouldn't be a problem I think.  The main issue we have is that we're having a hard time figuring out how to actually code what we're trying to accomplish (i.e. using a timer to measure the width of an input square wave pulse).

It might be something really simple, and I apologize if it is, but we're kind of learning all of this stuff as we go and we really appreciate the patient and helpful responses!

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: SQUARE WAVE QUESTUON.
« Reply #7 on: January 26, 2009, 05:13:45 PM »
Quote
You could try connecting its output straight to an a/d input. It will likely vary the value.


Are you sure?
If the duty cycle is constant, the voltage is also constant. In this case it will be 0.5 * Vmax

I would go with the interrupt technique and measure the period. What is the min/max frequency of the signal?

Michel.


Yep, the op said that the square wave frequency changes due to light intensity. An a/d port will take an average reading (provided that the pwm is above around 600hz). If you use a small value capacitor as well, this will act as a low pass filter and provide a more stable reading. It is a crudeish system, but is one that is used quite often and is simpler than the ccp method.

(its how systems that dont have capture compare facilities work with pwm signals instead of paying more for a frequency/voltage converter).

It is worth looking into the capture compare ports though... they are more accurate if you have them available. (if you are working with the axon then it is likely that the board admin has written a library to make using the ccp easier, try messaging him for more info.)

Offline jpwalters

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
Re: SQUARE WAVE QUESTUON.
« Reply #8 on: January 26, 2009, 06:58:10 PM »
Hmm, I'm not sure I understand CCP.  Looking through the Atmega640 Datasheet, I see no mention of ccp or capture compare ports, so I'm guessing this particular controller doesn't have that feature.  We will however try connecting it directly to the ADC and see what kind of inputs we receive.  I still don't understand how exactly it will take an average reading, since it would seem like the average would always be half of the max voltage of the square wave.  But, you guys definitely have more experience than me, so I'll take your word for it and see what happens.

Also, if anyone else has any suggestions, we'll happily take them as well.  Thanks again for the help guys.

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: SQUARE WAVE QUESTUON.
« Reply #9 on: January 26, 2009, 07:50:27 PM »
A CCP port is similar to the post mentioned above, it measures how long the pulses are and gives you a timer reading back. Some microcontrollers have dedicated ports for this to make programming easier. Having a quick look, the atmega640 has a capture pin on portD bit4.

Offline jpwalters

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
Re: SQUARE WAVE QUESTUON.
« Reply #10 on: January 26, 2009, 09:08:06 PM »
Ahh, indeed you're right.  When I searched the data sheet before I looked for "ccp" and "capture compare" and didn't find anything, but now I see it in the data sheet.  That helps a lot paul, thanks!

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: SQUARE WAVE QUESTUON.
« Reply #11 on: January 26, 2009, 10:27:15 PM »
I am trying to write a code that will measure a time period and output it on port c in ms. I know it might seam a little strange that I am outputting it in ms but If the rising edge and falling edge happen at different time intervals (frequencies) then each color will have a different time interval.Right?lol .My problem is that I need someone to look over my code and tell me if it will work Also the book that I got the idea from was using a 90s8535 and I am using the atmega640.The  90s8535   is a 4Mhz and mine is 16Mhz I need to figure out what to divide by to get the right interval for the ms. Also how can I read what my output is ? Can I use rfprint ,and if so how?Here is my code.Thanks again for all the help.

 
Code: [Select]
#define pulse_out PORTC
#define ICP PIND,6

void control(void)
{
 

TCCR1B=0X02;
timerAttach(1,timer_ovf_isr());
timerAttach(4,timer1_capt_isr());
 



}

Then here is my functions.

Code: [Select]
#include "global.h"


unsigned char ov_counter;
 unsigned int rising_edge;
 unsigned int falling_edge;
 unsigned long pulse_clocks;


void timer_ovf_isr(void)
 {
 ov_counter++;
 }

void timer1_capt_isr(void)
{
if(ICP)
{
rising_edge=ICR1;
TCCR1B=TCCR1B & 0xBF;
ov_counter=0;
}
else
{
falling_edge=ICR1;
TCCR1B=TCCR1B | 0x40;
pulse_clocks=(unsigned long)falling_edge - (unsigned long) rising_edge + (unsigned long)ov_counter * 0x10000;
pulse_out=pulse_clocks/500;
}
}

and my main
Code: [Select]
//SoR Include
#include "SoR_Utils.h" //includes all the technical stuff
#include "hardware.c" //declare hardware variables and ports
//#include "CMUcam.c"
//#include "Blackfin_Axon.c" //files for Blackfin Robot camera
#include "control.c" //your code goes in here
#include "pwm.c"
#include  "converter.c"
//#include "timer640.c"


//#include "axon_test.c" //include this is doing a function test for the Axon



int main(void)
{
//declare variables here
int i=0;//useless variable
int j=0;//useless variable


/****************INITIALIZATIONS*******************/
//other stuff Im experimenting with for SoR
uartInit();  // initialize the UART (serial port)
    uartSetBaudRate(0, 38400); // set UARTE speed, for Bluetooth
    uartSetBaudRate(1, 115200); // set UARTD speed, for USB connection
    uartSetBaudRate(2, 38400); // set UARTH speed
    uartSetBaudRate(3, 38400); // set UARTJ speed, for Blackfin
//G=Ground, T=Tx (connect to external Rx), R=Rx (connect to external Tx)

rprintfInit(uart1SendByte);// initialize rprintf system and configure uart1 (USB) for rprintf

timer0Init(); // initialize the timer system
TIMSK1=0x24;


configure_ports(); // configure which ports are analog, digital, etc.

a2dInit(); // initialize analog to digital converter (ADC)
a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage

LED_on();

rprintf("\r\nSystem Warming Up");

//let system stabelize for X time
for(i=0;i<=16;i++)
{
delay_cycles(5000);
rprintf(".");
}

delay_cycles(6000);

//read each ADC once to get it working accurately
for(i=0;i<16;i++)
j=a2dConvert8bit(i);

LED_off();
/**************************************************/


/*********ADD YOUR CODE BELOW THIS LINE **********/

rprintf("Initialization Complete \r\n");

//wait until user pushes button
while(!button_pressed());

reset_timer();
   rprintf("\r\n init");


control();//use this for your code

delay_cycles(1000);//an optional small delay to prevent crazy oscillations

/*********ADD YOUR CODE ABOVE THIS LINE **********/

//return 0;
}



« Last Edit: January 26, 2009, 10:30:20 PM by CALEB »

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: SQUARE WAVE QUESTUON.
« Reply #12 on: January 27, 2009, 09:57:23 AM »
Yep, the op said that the square wave frequency changes due to light intensity. An a/d port will take an average reading (provided that the pwm is above around 600hz). If you use a small value capacitor as well, this will act as a low pass filter and provide a more stable reading. It is a crudeish system, but is one that is used quite often and is simpler than the ccp method.

(its how systems that dont have capture compare facilities work with pwm signals instead of paying more for a frequency/voltage converter).

It is worth looking into the capture compare ports though... they are more accurate if you have them available. (if you are working with the axon then it is likely that the board admin has written a library to make using the ccp easier, try messaging him for more info.)

I still don't understand :( We are not talking about pwm here, just plain square wave with 50% duty cycle. You say that the average voltage will be different than 0.5 Vmax, is this an artifact of the A/D conversion? In theory the average voltage does not depend on the frequency.

Michel.

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: SQUARE WAVE QUESTUON.
« Reply #13 on: January 27, 2009, 12:30:43 PM »
Does anyone not have a code for this?I have been trying for hrs and cannot get it to work.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: SQUARE WAVE QUESTUON.
« Reply #14 on: January 27, 2009, 10:17:09 PM »
Someone else wrote Axon code to read PWM from a receiver. The concept is basically the same:
http://www.societyofrobots.com/robotforum/index.php?topic=3099.0

hope that helps!

 


Get Your Ad Here