Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Simple programming help

Status
Not open for further replies.

kevroy

Junior Member level 2
Joined
Feb 5, 2011
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Canada
Activity points
1,470
Hi,

I have written a program for an automatic audio record/play. Im designing a repeater which automatically records the message as it comes in, then it (supposed to) triggers the play function on the ISD1790P once the message is done. I have the record function down and working properly. In order to play the message from the ISD, you have to bring the "play" pin to Vss (ground). I need my output (named LED in my code) to drop low after the record function goes from 0 to 1. (ive provided a timing diagram of what i need).
And as a note, the record, erase, and play functions on the ISD1790P are active when the pin is brought to GROUND, not Vcc.

Any questions, please ask me.

Thank you.

**broken link removed**

My code is below.

int main()
{
unsigned int number;
int lowcount = 0;
int count = 0;
setup();

while(1)
{
//Insert Code Here

delayMs(10);
ADCON1bits.SAMP = 0; //start conversion

while(ADCON1bits.DONE == 0){}; //wait for conversion to finish

number = ADCBUF0;

if(number > 15)
{
RECORD = 0;
lowcount = 0;
}
else if(number < 15)
{
RECORD = 0;
lowcount = lowcount + 1;
}
if(lowcount > 80)
{
RECORD = 1;
count = 1;
}

if(RECORD == 1 && count == 1)
{
LED = 0;
count = 0;

}

}
}
 

I suspect the problem is to do with the signal you are reading into the ADC. Reading an AC signal at 10ms intervals and expecting to get at least 15 good readings is going to be hit-and-miss at best.
My suggestion would be to ensure the audio is at least 2V peak to peak, using an amplifier if necessary, then rectify and filter it so you measure the envelope of the sound rather than individual cycles within it. In other words, the ADC sees a rise in voltage which follows the sound volume rather than trying to pick out individual cycles of the waveform.

Brian.
 
  • Like
Reactions: kevroy

    kevroy

    Points: 2
    Helpful Answer Positive Rating
I suspect the problem is to do with the signal you are reading into the ADC. Reading an AC signal at 10ms intervals and expecting to get at least 15 good readings is going to be hit-and-miss at best.
My suggestion would be to ensure the audio is at least 2V peak to peak, using an amplifier if necessary, then rectify and filter it so you measure the envelope of the sound rather than individual cycles within it. In other words, the ADC sees a rise in voltage which follows the sound volume rather than trying to pick out individual cycles of the waveform.

Brian.

Thanks for your reply Brian.

My record part of the circuit works flawlessly as far as i can see. When there is no signal, it doesnt record. As soon as there is a signal it records perfectly. Then, once the signal is gone it stops. So im not worried about that part.

My problem is with trying to get the "play" function to drop low. I believe i need to keep track of my previous loop (the record loop) and once it goes from 0 to 1, i need a 500ms delay then drop play low for a short period of time. I tried to program it in, but it is not working, and im not sure what im doing wrong.

thanks again.
 

I can see a number of problems but first, explain what ADCBUF0 holds. For it to work I think it would have to hold the average number of samples in a set period. If it just accumulates the number of ADC readings, what makes the number drop again?

Another problem is the chance of your variables overflowing, you have no protection against them reaching maximum value and rolling over to zero or even overflowing to the next memory location.

I would be inclined to re-write the code so it is timer based rather than count based. In other words, enter the routine periodically instead of using delays in the loop to time the events. It makes it easier to produce running averages and hence detect the presence of signal to record and also makes it easier to create the longer delays needed to stop recording and produce the play control.

What processor is running this code?

Brian.
 
  • Like
Reactions: kevroy

    kevroy

    Points: 2
    Helpful Answer Positive Rating
I can see a number of problems but first, explain what ADCBUF0 holds. For it to work I think it would have to hold the average number of samples in a set period. If it just accumulates the number of ADC readings, what makes the number drop again?

Another problem is the chance of your variables overflowing, you have no protection against them reaching maximum value and rolling over to zero or even overflowing to the next memory location.

I would be inclined to re-write the code so it is timer based rather than count based. In other words, enter the routine periodically instead of using delays in the loop to time the events. It makes it easier to produce running averages and hence detect the presence of signal to record and also makes it easier to create the longer delays needed to stop recording and produce the play control.

What processor is running this code?

Brian.

I am new to this programming thing so please bare with me here.

The ADCBUF0 stores a value of the input signal level on a scale of 4.25 volts, to the number of bits the chip (dsPIC30F4011) can read which is 1024. So if 0 volts signal = 0 bits, then 4.25 volts equals 1024 bits.
If i get a signal coming in that is 500mV, that will be a small number, say 70. In this case, my input from my computer puts out less than "15" when there is no audio. As soon as i play music, it comes out of the headphone jack at greater than 15.

i am not sure how to protect variables......

thanks
 

Thank you for the additional information.

You have a standard 10-bit resolution ADC with a reference voltage of 5V, in other words, 'number' x 0.048 = the input voltage.
There is a serious danger of the method you use to detect the audio not working. What you are doing is taking a single sample of the input voltage every 10mS. That sample represents just one part of the audio waveform and could easily be zero or very low on many samples, even if audio was present. It would be far better to do as I suggested with the rectifier and filter so you sample the average voltage level, it will make the audio detection far more reliable and give better immunity from triggering on interference. At its simplest you only need one diode, one capacitor and one resistor, total cost of $0.10

Going back to your original code, you might be able to fix it by adding another variable, one that is set when you record but stays set afterward. Then after detecting the end of the sound you say "I have made a recording and now there has been a silence so wait a while and operate the play function". I would still do it timer based though!

Brian.
 
  • Like
Reactions: kevroy

    kevroy

    Points: 2
    Helpful Answer Positive Rating
Thank you for the additional information.

You have a standard 10-bit resolution ADC with a reference voltage of 5V, in other words, 'number' x 0.048 = the input voltage.
There is a serious danger of the method you use to detect the audio not working. What you are doing is taking a single sample of the input voltage every 10mS. That sample represents just one part of the audio waveform and could easily be zero or very low on many samples, even if audio was present. It would be far better to do as I suggested with the rectifier and filter so you sample the average voltage level, it will make the audio detection far more reliable and give better immunity from triggering on interference. At its simplest you only need one diode, one capacitor and one resistor, total cost of $0.10

Going back to your original code, you might be able to fix it by adding another variable, one that is set when you record but stays set afterward. Then after detecting the end of the sound you say "I have made a recording and now there has been a silence so wait a while and operate the play function". I would still do it timer based though!

Brian.

Ok,

in regards to the diode / rectifier. Since my input voltage is fairly low, when i pass the signal through the diode i will see a .7 volt drop. Will this not totally eliminate the signal voltage?

And in regards to the other variable that i need. At the bottom of my code, i put a variable in there called "count". When record goes to 1, "count" is supposeed to go to 1 as well.
Then checks if LED && RECORD are 1, and if they are, drop "play" to low, then the next line of code is supposed to bring count back to 0. I think my problem is down there somewhere.

thanks again
 

You are correct, some voltage will be dropped, that's why I suggested using an amplifier as well. Consider two things though, first, you shouldn't feed negative voltages into the ADC, it could be damaged and a diode will prevent that. Secondly, if your voltage is very low to start with, the ADC accuracy will be seriously compromised, for example, if you have a 200mV AC signal, at the moment you only use about 2% of the ADCs available range.

Brian.
 
  • Like
Reactions: kevroy

    kevroy

    Points: 2
    Helpful Answer Positive Rating
So i should amplify the signal, would an op-amp work?
 

Yes, you could use an op-amp but you would have to be careful to choose one that worked with only a 5V supply. As the quality isn't particularly important, you could just use a single transistor amplifier stage. Can you show the schematic of the present system, including the source of the sound so we can see which kind of amplifier would be best to use.

Brian.
 
  • Like
Reactions: kevroy

    kevroy

    Points: 2
    Helpful Answer Positive Rating
I have a schematic, but its in autocad, how can i post it?
 

Apologies for replying sop late.

If you can export the schematic as a JPG or PDF file it should post here. If all else fails, take a screen capture and post it, the quality may not be so good but it will give us an idea.

Brian.
 

Thanks for your help so far Brian,

Now i got the programming working, but my next problem is signal voltage. Not sure if i said it earlier, but im building a simplex repeater for two way GMRS radios. I havent tested it yet with the radios( im using music from my computer right now) but i suspect the voltage coming out of the radio to be too low to trigger the ADC as you said. Would a transistor not affect the voice quality? And what transistor circuit would work best for my application?

Thanks
 

The extra amplifier would only be between the audio path and the ADC circuit so it wouldn't change the signal to the transmitter. What levels are you seeing at the moment? A good compromise would be a 2V threshold, this would give high immunity against background noise and interference but the intended signal would still operate it reliably.

If you think a GMRS radio repeater is complicated, look at the one for TV I designed at ATV Projects Home Page !

Brian.
 

Concerning the audio level, the speaker output of a walkie-talkie type radio should be 4 V p-p with the volume knob turned up. Otherwise use an op-amp.
 

OK, so if the voltage is 4V P-P, i may not have a problem, but if i do, i will amplify it.

Now my other question.

How can i take the signal from the recording chip and get it to transmit on the radio?

Lets say somebody is talking on the radio with a headset, they have to press the PTT button to send the message. How can i avoid this? Would a transistor work to switch the circuit on? Or should i use the VOX and hope it picks up the message?

I will try and post the schematic. im having a little trouble with it right now.
 

If you are extracting the audio for retransmission from the loudspeaker connection you probably don't want to turn the volume up too high as it will introduce distortion, keeping it at a relatively low level will give better audio quality, boosting it only for the DAC to read will be the better solution. In most small radios, the level will only be a few hundred mV so a voltage gain of 20 times would be in order.

If the radio has VOX that's probably the best thing to use, otherwise you need to know what voltages are across the PTT button to decide how to automatically operate it. If one side of the PTT is ground, it should be easy to connect a transistor in parallel with it so either the switch itself or the transistor can provide the PTT function. If the switch has voltages at both sides it would be easier to use a small relay. Bear in mind that if not using VOX, you might need to time how long the audio lasts so you can hold the PTT closed for long enough to transmit it.

To feed the audio into the radio, just take the output from the recording device, put it through a volume control so you can set the best level, then connect it to the microphone input.

Brian.
 

So the radios im using are the Motorola SX700. These radios have the VOX option. I think i will use that function.

Just a basic NPN transistor amp, like the one in the link below, will work for me?


The basic transistor amplifier
 

Yes, as simple as that !

You also need to convert the AC signal to DC and provide a small 'reservoir' to hold the peak levels for a fraction of a second, this will ensure the ADC always sees something when audio is present and doesn't mistakenly sample the individual cycles when they are at or close to zero point. Doing this is very easy, all you need is a coupling capacitor from the transistor amplifier, a diode to change the AC to DC, a capacitor to hold the charge and a resistor to let it leak away slowly. The ADC measures the voltage across the capacitor holding the charge.

Brian.
 

Yes, as simple as that !

You also need to convert the AC signal to DC and provide a small 'reservoir' to hold the peak levels for a fraction of a second, this will ensure the ADC always sees something when audio is present and doesn't mistakenly sample the individual cycles when they are at or close to zero point. Doing this is very easy, all you need is a coupling capacitor from the transistor amplifier, a diode to change the AC to DC, a capacitor to hold the charge and a resistor to let it leak away slowly. The ADC measures the voltage across the capacitor holding the charge.

Brian.

In referance to the capacitor, thats what the "Cc" capacitor and "Rb" resistor do, right? (in the diagram from the link i posted above)
the diode would just be in series after the amp because of the 0.7 volt drop?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top