Continue to Site

# convert <= 5k hertz sinewave with PIC trasmit through UAR

Status
Not open for further replies.

#### icbinghui

##### Newbie level 4
baud hertz conversion

Hi,

Is there a way to convert a frequency of up to 5k hertz or below of a sinewave into digital values using the internal ADC of a PIC16F877A and transmit the values using uart to the pc. How do i make sure that once i have the correct amount of the sampled values of the sinewave, i'm able to transmit the values to the pc using the uart with the proper baudrate setting?

Currently facing a problem regarding on sampling a high frequency sinewave and finding the right baudrate to transmit the sampled data to the pc. thanks for your help.

converting 5k times

Sample 5x the maximum frequency.
So for a 5KHz waveform you'll need to sample @ 25KHz
Therefore to transmit an raw 8 bit A/D value you'll need at least 250,000baud (10bits x 25,000bytes)

This sounds exactly like a post on Electro-tech Online.

The OP over there derrick826 is having a tough time understanding the replies. :|

how to convert from baud to hertz

blueroomelectronics said:
Sample 5x the maximum frequency.
So for a 5KHz waveform you'll need to sample @ 25KHz
Therefore to transmit an raw 8 bit A/D value you'll need at least 250,000baud (10bits x 25,000bytes)

This sounds exactly like a post on Electro-tech Online.

The OP over there derrick826 is having a tough time understanding the replies. :|

yes, i am aware about sampling the waveform 5 times the maximum frequency. Just to enquire if the internal ADC of the PIC16F877A has enough maximum sampling rate which i can use to sampling that high frequency. Can the sampling rate of the internal ADC be adjusted?

how do you calculate tad for pic18f

The ADC with one channel only can sample at a maximum speed of 32TOSC (5,000,000/32) @ 20MHz OSC but your code will have to process it and that will add some overhead. But that's still pretty fast at 156K samples per second.
The A/D conversion runs in the background and sets the GO_DONE flag when complete, you could send out data on the serial port during that time.

Re: convert <= 5k hertz sinewave with PIC trasmit through

blueroomelectronics said:
The ADC with one channel only can sample at a maximum speed of 32TOSC (5,000,000/32) @ 20MHz OSC but your code will have to process it and that will add some overhead. But that's still pretty fast at 156K samples per second.
The A/D conversion runs in the background and sets the GO_DONE flag when complete, you could send out data on the serial port during that time.

32 Tosc for a 20Mhz will give a minimum Tad of 1.6us. Conversion time of 12 *Tad plus the aquisition time will give the sampling time. this is according to one of the website written by a lecturer. Through calculation, it is about 25k of sampling rate which is far off than what you have told me.

here a link to back up what i have typed
http://members.shaw.ca/hserban/Unit%204-%20Lecture%20Notes.pdf:|

My mistake, I forgot about the sampling time. But your bottleneck will probably be the serial port even at 25KHz. Just transmitting 8bit raw data via RS232 at 25KHz is a baud of 250K.

Re: convert <= 5k hertz sinewave with PIC trasmit through

Even with that 250k baudrate , the serial port can't sample that fast, maximum for a serial port is 115200 baudrate. What other methods can you suggest to transfer that amount of samples? What is the maximum baudrate for a parallel port?

Re: convert <= 5k hertz sinewave with PIC trasmit through

icbinghui said:
32 Tosc for a 20Mhz will give a minimum Tad of 1.6us. Conversion time of 12 *Tad plus the aquisition time will give the sampling time. this is according to one of the website written by a lecturer. Through calculation, it is about 25k of sampling rate which is far off than what you have told me.

Those are ideal numbers at best. Your actual in-circuit tests will be the reality and I guarantee much less than those calculations, for a number of reasons. You must still manage the ASRESH/ADRESL data after completing each conversion which can add some additional cycles. There are some tricks to minimize this impact. Consider PIC18 for faster ADC's and better RAM access.

I would suggest using RAM to store a group of samples. The 16F877A has 96 bytes in each bank... watch out for the mirrored RAM at the top of each bank. Take 96 readings, and then stop reading and send the data. Using 115K baud you can send all the data in 6 milliseconds. Then go back and do it again. Since this is likely not an application of scientific precision, a 6 millisecond window of no data acquisition may be acceptable.

Re: convert <= 5k hertz sinewave with PIC trasmit through

How do you get 6 ms for sending 96readings where each readings which i send has 10 bits (including the start and stop bit) through a 115k baudrate? Do you mean 69 readings?

69*10 = 690 bits

690/115000 = 6ms

Re: convert <= 5k hertz sinewave with PIC trasmit through

My mistake... ~8ms.

• 1/115000 = 8.7us per baud bit

96 bytes = 960 baud bits

960 x 8.7us = 8.3ms

Re: convert <= 5k hertz sinewave with PIC trasmit through

xorcise said:
icbinghui said:
32 Tosc for a 20Mhz will give a minimum Tad of 1.6us. Conversion time of 12 *Tad plus the aquisition time will give the sampling time. this is according to one of the website written by a lecturer. Through calculation, it is about 25k of sampling rate which is far off than what you have told me.

Those are ideal numbers at best. Your actual in-circuit tests will be the reality and I guarantee much less than those calculations, for a number of reasons. You must still manage the ASRESH/ADRESL data after completing each conversion which can add some additional cycles. There are some tricks to minimize this impact. Consider PIC18 for faster ADC's and better RAM access.

I would suggest using RAM to store a group of samples. The 16F877A has 96 bytes in each bank... watch out for the mirrored RAM at the top of each bank. Take 96 readings, and then stop reading and send the data. Using 115K baud you can send all the data in 6 milliseconds. Then go back and do it again. Since this is likely not an application of scientific precision, a 6 millisecond window of no data acquisition may be acceptable.

The amount of 96 readings can be quite little when you're measuring a really high frequency baudrate like 5khz sinewave. But you're saying that once the sinewave is being sampled at that amount of 96 readings, which is then saved into the RAM, stop the reading of the ADC and then send these 96 readings to the RS232, do you think the pc will plot out a nice graph with these readings? definitely it will not be a real time anymore and some timing have to be adjusted in the program. Any opinions?

Why not use a USB enabled PIC like a 18F2550?

Re: convert <= 5k hertz sinewave with PIC trasmit through

One thing overlooked is that there is a maximum processing speed regardless of the clock speed of the CPU. For 16 series PICs the maximum ADC sample rate is about 30k samples/S, beyond that the quantization is unreliable.

Re: convert <= 5k hertz sinewave with PIC trasmit through

ldanielrosa said:
One thing overlooked is that there is a maximum processing speed regardless of the clock speed of the CPU. For 16 series PICs the maximum ADC sample rate is about 30k samples/S, beyond that the quantization is unreliable.

I don't think that was overlooked...

You can apply Nyquist FFT and DFT formulas which require a minimum sampling speed of 2 per period. You can gather a quantity of samples in powers of 2, such as 64, 128, 256, etc, and determine the frequency of the signal.

What is overlooked is that the ADC input cannot read AC signals that dip below ground. The incoming p-p signal must be elevated above ground where the zero reference of the signal now sits at 2.5V, and the the AC amplitude is not greater than 5V p-p.

hi, just for my extra knowledge.. at least how many samples is needed for one cycle to plot a sinewave .

some says follow the nyquist theoram which is 2 samples/cycle, others says 5 samples/cycle. Is there any link which explains about all these?

Re: convert <= 5k hertz sinewave with PIC trasmit through

At least 2 per period - Google "Nyquist". Then Google PIC + FFT.

Re: convert <= 5k hertz sinewave with PIC trasmit through

Hello all.
I'd like to introduce another idea.
If you are sure that the wave shape is always the same, i.e., sinewave, I would propose that:
Square your input signal using for example a comparator or a smith-trigger gate as 4093 or 17132.

Then connect the square signal to one of the CCP inputs of the microcontroller.

Now you can use the referring timer to measure the period of your input square signal. If you use a 16bit timer you will be able to get approximately 16 bits precision in your frequency measurement that is much better than what you could get from an adc (10 or 12 bits in microchip microcontrollers).

After you use mathematical functions of the c compiler to calculate the frequency (inverse of the period).

Then send the result through usart.

Hope it helps
S.

Re: convert <= 5k hertz sinewave with PIC trasmit through

Cutoff (-3dB) will happen at about 2.4 samples/period, and a lot of the losses will be from aliasing errors. Your readings will be a lot more stable above 4 samples/period.

Status
Not open for further replies.