PDA

View Full Version : HighSpeed A/D and Baud Rates



flyingasics
- 13th January 2010, 23:02
Long time reader first time poster.

Right now I am running a PIC 16F688 at 20Mhz. I'm taking the AD of a 1 Khz 50% duty cycle signal and sending it at 1250000 baud out to a serial terminal on a PC. Yes, my 232 converter is capable of this speed.

Could someone help me with the math involved in the approximate time it is taking to do a conversion and then send the data. This is what I have come up with:

1/1250000 = .8 us per bit

Im sending HSEROUT [ bin ADRESH,Bin ADRESL," "] Is this 3 bytes of data total or 6 bytes?

I seemed to have read somewhere in PBP manual that serout can send up to 65535 dec which would be a word.

Assuming its 6 bytes of data and a stop and start bit for each word for the serial com gives 6*18 + 3*2 = 54 bits per result sent.

54*.8 us = 43us

The AD conversion is around 20 us according to datasheet.

Now is this conversion happening at the same time as the HSEROUT is? Or is it 20 us for the conversion and then 43 us to send it? I think they can happen at the same time.......

Ok, now the 1 Khz square wave has a duty cycle of 1 ms

1ms/43us = around 23 samples per duty cycle. I seem to get around 4 or 5.

Can anyone enlighten me on where the extra ~800 something us are going each cycle? Or how my math is wrong.

Thanks!

Here is my code:

'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2010 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 1/12/2010 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
@ __config _HS_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF
DEFINE OSC 20

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_SPBRG 0 ' 1250000 @ 20MHz

GoDone VAR ADCON0.1

ADCON1 = %00100000 ; Fosc/32
ADCON0 = %10010101 ;Right Justified, ch5(AN5), A/D on

loop1:

GoDone = 1 ;start conversion

loop2:

if GoDone = 0 Then ; is conversion done?

HSEROUT [ bin ADRESH,Bin ADRESL," "] ;spit out answer

else
goto loop2 ; go back and check if A/D is done if it wasn't before

endif


goto loop1 ;do forever

tenaja
- 13th January 2010, 23:49
Im sending HSEROUT [ bin ADRESH,Bin ADRESL," "] Is this 3 bytes of data total or 6 bytes?
No.
It's actually 17 bytes. The "BIN" qualifier sends one byte for each bit, either "0" or "1". If you want 3 bytes, you need to delete the two "BIN":

HSEROUT [ ADRESH, ADRESL," "]

BTW, If you want fast a/d, then you need to start the new a/d conversion as soon as the last one was finished--don't wait until after your serial is sent.

Charles Linquis
- 14th January 2010, 01:35
The way to really get some speed is to send two bytes only. The TXregister is copied to the transmit shift register one clock cycle after you load the TX register, then the TX register can be loaded immedately again. Your routine can proceed while the bytes are being sent.

If you send more than two bytes, your routine has to wait until all but the last two bytes are shifted out.

Sending two bytes takes only microseconds (in program time), while sending more than two bytes takes those same microseconds + ((number of bytes - 2)* byte send time).

Another way to make things go fast - as Tenaja has said, don't use the ADCIN command. Instead, do it manually.

1. Set up the ADC channel and start the conversion.
2. Send some data.
3. Read the result of the conversion you started in step 1.
4. Set up the ADC channel and start the conversion.
5. Send some data.
7. Read the result of the conversion your started in step 4.
8. Go back to step 1.