PDA

View Full Version : How to set external clock source in PBP



TurboLS
- 16th February 2005, 21:42
I am trying to use an external resonator with my circuit and I was wondering what i set

DEFINE ADC_CLOCK to to use an external 20mhz resonator.

I already set OSC to 20, but that doesn't seem to work. Also, how do I set an 8 bit data word acquired from an analog in, to output bit by bit, say through the entire PORTB. I was going to wire it up to 8 LEDs to make sure it was working and I can't find any documentation on how to set an entire port to output an 8 bit digital word. My code is as follows:

' PicBasic Pro program to perform
' 8-bit A/D conversion
'
' Connect analog input to (RA0)
' Connect LEDs to 8 pins of PORTB

include "modedefs.bas"

' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
DEFINE OSC 20 ' Sets clock speed to 20Mhz
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

adval var byte ' Create adval to store result

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00000010 ' Set PORTA analog
TRISB = %00000000 ' Set PORTB to all output
Pause 500 ' Wait .5 second

SHIFTOUT PORTB,PORTA.7,0,[adval] ' Output 8 bit word to PORTB
Pause 500 ' Wait .5 second

' Do A/D conversion after each pixel shift

loop: ADCIN 0, adval ' Read channel 0 to adval
Goto loop ' Do it forever
End

mister_e
- 16th February 2005, 21:57
1. HS oscillator is probably not set in your programmer
2. PORTB=Myvar will output your variable result in one shot using all 8 bits if your var is define as a BYTE sized one
3. and you'll need to modify your code if you want to display your results
4. Since we don't know your PIC model, you''ll probably don't need to set your PORTA as analog... More than often, it's already in analog when you boot the PIC.



' PicBasic Pro program to perform
' 8-bit A/D conversion
'
' Connect analog input to (RA0)
' Connect LEDs to 8 pins of PORTB

DEFINE OSC 20 ' Sets clock speed to 20Mhz=> speed of your crystal not a/d

' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

adval var byte ' Create adval to store result

TRISA = %11111111 ' Set PORTA to all input
TRISB = %00000000 ' Set PORTB to all output

loop:
ADCIN 0, adval ' Read channel 0 to adval
' possible that you need a pause here... few ms
PORTB=Adval ' display result on portb
PAUSE 200 ' wait 200 mSec
Goto loop ' Do it forever
End

TurboLS
- 16th February 2005, 22:51
thanks a lot man, but yeah, the HS is definitely set. I will try what you said about the advar = portb thing tho. Also the model number is a PIC18F4520

TurboLS
- 16th February 2005, 23:54
Hey, I would just like you to know that the adval=PORTB thing worked. I guess my next step is to rewire it and recode it to use the software SEROUT command. Thanks again for all your help.

mister_e
- 17th February 2005, 00:34
it has to work. I'm sure you meant PORTB=adval is working

good luck.

TurboLS
- 17th February 2005, 19:23
OK, I wired up the serial port and, using the built-in serial port com tool in MicroStudio, I am able to see information from PORTC.6; however the data (which I thought would be 1s and 0s) is coming out all garbled as weird characters and zeros with square edges.

I am using the SEROUT2 command:

SEROUT2 PORTC.6,16780,[adval]

Also, I have included the modedefs.bas file. Any ideas?

Dwayne
- 17th February 2005, 19:58
OK, I wired up the serial port and, using the built-in serial port com tool in MicroStudio, I am able to see information from PORTC.6; however the data (which I thought would be 1s and 0s) is coming out all garbled as weird characters and zeros with square edges.

I am using the SEROUT2 command:


Tie your pin to ground with a Resister. That way it is a definite "0". If the pin is left floating, you can get all kinds of garbage...

then, make sure you use a slow baud rate...2400 is good starting place. Make sure it has the right parity.

Dwayne

TurboLS
- 17th February 2005, 21:24
What would be a good resistor value to use to tie the PORTC.6 to ground with?

I already have a 1k resistor from PORTC.6 before transmitting to the PC. I tried putting another 1k resistor from PORTC.6 to ground and now i get no data, does that mean I am using the wrong speed/parity/stop bits, etc?

thanks again for all your help.

Dwayne
- 17th February 2005, 22:06
Hello Turbo LS,

TLS>>I already have a 1k resistor from PORTC.6 before transmitting to the PC. I tried putting another 1k resistor from PORTC.6 to ground and now i get no data, does that mean I am using the wrong speed/parity/stop bits, etc?<<

I would use a 4.7. 1 is a little light.

You may need to invert your data. T2400 buad if you have a scope, I would suggest scoping your output, to make sure that your signal is strong. If yoy type a "1", you should see your 1 being sent to your Serial port... Remeber it will look backwards on yourscope, but should still see the hex values of 1s and 0s.

Use that scope!... It can tell you alot! It can tell you if the 1's are high enough voltage to be 1's!... If they are not high enough voltage, you will not be able to read them as ones...

It almost sounds as if you put 2 1k resisters in Parallel, causing the voltage to drop way too low.... thus nothing.

If this is so, then your problem probably lies in inverting your signal, or your baud rate is not matched up (or both).

Dwayne

TurboLS
- 18th February 2005, 03:01
I will try the 4.7k, but i did manage to use the scope and the values coming out of the digital B port (the one still wired to the LEDs) read around 5V each.

I also added BIN8 to my SEROUT2 command line. I then started seeing 1s and 0s in the serial command window, however there appeared to be way too many 0s.

Again, this could be because the baud rate, parity, stop bits, etc. are still miscoded.

What should I use if all i can set in the PBP serial window is baud rate, parity, stop bits, and the COM port?

the settings in SEROUT2 use open/driven, inverted/true, and they also reference bits 13,14, and 15, which I don't really understand (this info from Appendix A in the manual) because I am only sending 8 data.

In any case, I think i am getting closer to what I am looking for, it's just a matter of selecting the correct settings.

thanks again for all your help.

mister_e
- 18th February 2005, 03:11
If you don't use MAX232 between you PC and PIC you'll need to select inverted mode.



SEROUT PORTB.0,N2400,["hello"] ' 2400 inverted, true driven

SEROUT2 PORTB.0,16780,["hello"] ' also 2400 bauds, inverted, true driven


under MicroCode studio serial window
Mode 2400,n,8,1 : 2400 bauds, no paritry,8 data bits, 1 stop bit

don't care about bit 14,15 and blah blah blah, just refer to the table at the end, choose you baudrate, inverted or not and, true driven. That will do the job.

TurboLS
- 18th February 2005, 03:43
what will i have to do to the inverted data once i get it to access the data that was originally coded into the 8 bit adval variable? Like once I receive it in inverted mode, how to I get the actual data from that?

mister_e
- 18th February 2005, 04:17
if you receive your data directly from your PC you'll have to use SERIN in inverted mode. The data will be automatically set as the original on the PC.

PC send and receive inverted. This is one of the reason why we use MAX232 when using the internal USART of a PIC when available.

TurboLS
- 18th February 2005, 05:20
i will only be receiving data from the PIC into the PC, so I will only be concerned with the output commands. Are MAX232 chips expensive?

mister_e
- 18th February 2005, 06:27
MAX232... 1$ or 2$ maximum.

But you don't need it if you send data Inverted... PC will re-inverted himself.

You'll need it only if you want to use the internal USART. BTW you can also use a simple transistor as inverter or 7407, 7404 or else kind of inverter. MAX232 is a standard and he's build for that purpose.

TurboLS
- 18th February 2005, 14:54
Oh ok, so that's what i was unclear about. So if I send 2400 baud, 8 data bits, 1 stop bit, no parity, and inverted from the chip, then as long as i match everything but inversion up in the serial com window, it should work, right?

BobEdge
- 18th February 2005, 16:04
Hi,

I have had the same problem. the define osc does not work.

so if you are using 8MHz osc then send serial data at 2400 to pc, but it will actually be set to 4800 baud on the pc.

Im having a nightmare with timing at the moment. If anyone knows how to get the define osc to work, please let us know

Kind Regards

Bob.....

NavMicroSystems
- 18th February 2005, 16:30
Bob

DEFINE OSC does work as epected.

There must be something else wrong with your code.

If you post your code we'll have a look at it.

NavMicroSystems
- 18th February 2005, 16:44
TurboLS


Originally posted by TurboLS
OK, I wired up the serial port and, using the built-in serial port com tool in MicroStudio, I am able to see information from PORTC.6; however the data (which I thought would be 1s and 0s) is coming out all garbled as weird characters and zeros with square edges.

I am using the SEROUT2 command:

SEROUT2 PORTC.6,16780,[adval]

Also, I have included the modedefs.bas file. Any ideas?

SEROUT2 PORTC.6,16780,[adval]

will send out the value of adval.

what your Terminal program displays is the ASCII representation of adval.

if you would like to see 1's and 0's try:

SEROUT2 PORTC.6,16780,bin [BIN10 adval,13,10]

If you are using only 8bit ADC and adval is Byte size replace BIN10 with BIN8


if you would like to see the decimal value of adval try:

SEROUT2 PORTC.6,16780,bin [DEC4 adval,13,10]

for 8 Bit ADC with adval being byte size replace DEC4 with DEC3

The ",13,10" simply adds a "Carriage Return" and a "Line Feed" after each line

TurboLS
- 18th February 2005, 21:10
Thanks a lot, I got it to work at 19200 flawlessly. Thanks again for all your help. Now I just have to get on my teammates case to get the clocking signals running.

TurboLS
- 18th February 2005, 21:21
Hey, I was wondering if you would grace me with a few more insights.

Right now I have:

DEFINE ADC_CLOCK 4 ' i know 3 means internal RC, but i want to use the external resonator, is that 4?

DEFINE OSC 20 ' i know 20 means 20mhz, but does that also work for resonators as well as oscillators?

DEFINE SAMPLEUS 50 'i eventually want to sample in the megahertz range if possible,

I'm also going to eventually put in a trigger to start the A/D process and end it after a certain number of samples. Well, off I go into the pic basic manual. Thanks again for the help.

mister_e
- 18th February 2005, 21:37
DEFINE ADC_CLOCK 4 ' i know 3 means internal RC, but i want to use the external resonator, is that 4?

you can't use directly the full speed of external clock. in fact you'll have the external clock divided by n. Refer to section 10 of your datasheet in the ADCON0 table.




DEFINE OSC 20 ' i know 20 means 20mhz, but does that also work for resonators as well as oscillators?

20 is the speed of the external oscillator, can be crystal, ceramic resonator, pins on a motor....



DEFINE SAMPLEUS 50 'i eventually want to sample in the megahertz range if possible,

I'm also going to eventually put in a trigger to start the A/D process and end it after a certain number of samples. Well, off I go into the pic basic manual. Thanks again for the help.


The fastest way is to don't use this ADCIN statement and do your sample with FOSC/2 and read/write the internal registers yourself.

Download and look this code (http://www.melabs.com/resources/samples/pbp/a2d10.bas)

with some few modification you'll do it as fast as the PIC can do for you ;)

TurboLS
- 18th February 2005, 22:27
Upon further investigation of my design criteria, I will have to find a way to do the A/D asynchronously, say when a certain pin goes to 5V. I was looking at "ON INTERRUPT" commands and there is not much in the manual on that, maybe 2 pages at the most. I am gonna look through some other posts here and see if i can find some good examples. thanks again.

TurboLS
- 18th February 2005, 23:00
I found this statement in the code you told me to look at:

if ADCON0.2 = 1 Then notdone

I was thinking i could change it to:

IF PORTA.1 = 1 THEN ADCIN 0,adval '1 meaning 5V high'

is there any way i can combine 2 lines into a conditional statement?

i.e. when porta.1 goes high, do the A/D conversion and send it out using SEROUT2 command and do those two things only when porta.1 goes high.

mister_e
- 18th February 2005, 23:29
you can but if you check a specific DIGITAL pin, you can't do the Analog conversion on the same pin without changing something.
AND what is the use of converting a digital signal ... 0v or 5volt ADC=0 or 255

BUT if your waiting for a digital signal to do a analog conversion on another pin... for sure.


If PushButtonOnPORTABIT0 = 1 then
ADCIN PORTA.1,Myvar
SEROUT PORTC.0,N2400,[#Myvar]
endif

be sure of your setting of ADCONx register

TurboLS
- 19th February 2005, 00:27
Yeah, I basically want to sample the analog signal going into PORTA.0 and transmit it out PORTC.6 when PORTA.1 goes to 5V. Also, when I do that, can i take out the DEFINE ADC_CLOCK line? And for that matter the DEFINE SAMPLEUS line? I will look over the ADCON register settings you were talking about tomorrow. thanks again.

mister_e
- 19th February 2005, 01:19
If you plan to remove those line without any manual setting into the ADCONx register... i'm pretty sure nothing will work as you want. BUT iI didn't check the default setting as i always set those OPTION_REG,TRIS, ADCON,CMCON.................. for all my application.

TurboLS
- 19th February 2005, 01:23
Oh ok, so even though I will be asynchronously triggering the samples, I should still declare the ADC clock and stuff just to get it to compile?

Also, how can I make sure that it only takes one sample each time i trigger the "Pushbutton" port?

TurboLS
- 19th February 2005, 15:56
Nevermind about the whole 1 sample per button thing, i just realized that the way it is written, it will do just that.