How to set external clock source in PBP


Closed Thread
Results 1 to 29 of 29
  1. #1
    TurboLS's Avatar
    TurboLS Guest

    Default How to set external clock source in PBP

    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
    Last edited by TurboLS; - 16th February 2005 at 21:46.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.

    Code:
    ' 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
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Talking

    it has to work. I'm sure you meant PORTB=adval is working

    good luck.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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?

  7. #7
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    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
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  8. #8
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  9. #9
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    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
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  10. #10
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  11. #11
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    If you don't use MAX232 between you PC and PIC you'll need to select inverted mode.

    Code:
    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.

    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  12. #12
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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?

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  14. #14
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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?

  15. #15
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  16. #16
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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?

  17. #17
    Join Date
    Feb 2005
    Location
    Holmfirth England
    Posts
    116


    Did you find this post helpful? Yes | No

    Unhappy

    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.....

  18. #18
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    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.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  19. #19
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by NavMicroSystems; - 18th February 2005 at 16:50.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  20. #20
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  21. #21
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  22. #22
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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

    with some few modification you'll do it as fast as the PIC can do for you
    Last edited by mister_e; - 18th February 2005 at 21:42.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  23. #23
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  24. #24
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  25. #25
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.
    Code:
    If PushButtonOnPORTABIT0 = 1 then
         ADCIN PORTA.1,Myvar
         SEROUT PORTC.0,N2400,[#Myvar]
    endif
    be sure of your setting of ADCONx register
    Last edited by mister_e; - 18th February 2005 at 23:31.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  26. #26
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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.
    Last edited by TurboLS; - 19th February 2005 at 00:30.

  27. #27
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  28. #28
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    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?
    Last edited by TurboLS; - 19th February 2005 at 01:27.

  29. #29
    TurboLS's Avatar
    TurboLS Guest


    Did you find this post helpful? Yes | No

    Default

    Nevermind about the whole 1 sample per button thing, i just realized that the way it is written, it will do just that.

Similar Threads

  1. HW PWM not working
    By comwarrior in forum General
    Replies: 5
    Last Post: - 31st July 2009, 17:16
  2. Microcode studio - PIC16F877A Interrupts
    By mcbeasleyjr in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th January 2009, 06:10
  3. Data Out From External Clock
    By Quin in forum Serial
    Replies: 2
    Last Post: - 25th October 2008, 09:11
  4. Wierd sleep issue
    By orca in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th March 2006, 22:06
  5. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts