New to PICBASIC Pro and have simple question


Closed Thread
Results 1 to 18 of 18
  1. #1
    Join Date
    Nov 2007
    Posts
    42

    Default New to PICBASIC Pro and have simple question

    I am using Picbasic Pro to program a 16F684. What I am trying to do it get pins on portA other than porta.3 to be a input and see a high or low signal. I don't know why this isn't working and I am SURE something will point out the obvious reason it's not. Anyway here is the code below. When I put "high" to porta.0 it doesn't flash the led. I know everything working right because if I just set portc to outputs and set it to flash the same led its fine.

    Any help would be great. Thanks!

    TRISA=%111111
    TRISC=%000000
    PORTC=%000000

    MAIN:
    IF PORTA.0 = 1 THEN
    PORTC.0 = 1
    PAUSE 60
    PORTC.0 = 0
    PAUSE 60
    ENDIF
    GOTO MAIN

  2. #2
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91


    Did you find this post helpful? Yes | No

    Default Digital Input

    What you're trying to do is read a digital input on RAx. This note from page 33 of the manual points you in the right direction. The RAx pin must be configured as a digital input
    first.
    "Note: The ANSEL and CMCON0 registers must
    be initialized to configure an analog
    channel as a digital input. Pins configured
    as analog inputs will read ‘0’."




    Hope this helps...
    "It will never happen here!" just happened here.
    My thoughts and prayers for Sandy Hook victims and families.

  3. #3
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default Digital Input?

    Is "high" or "low" considered a digital signal? I assumed it was analog.

  4. #4
    Join Date
    Nov 2007
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    yes
    high = 1
    low = 0

    Thats digital 10101010

  5. #5
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default It works.

    I knew it was simple. It works fine now. Guess I should have tried setting it for digital before posting..

    Anyone know of a good thread or link to a site that has a good explanation of the "button" command, how it works, and examples?

    I would really like to use this command instead of using high and low. That way I don't have to put resistors to a ground for every input pin in my project.

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,651


    Did you find this post helpful? Yes | No

    Default

    [QUOTE=plyrathrt;46409]

    Anyone know of a good thread or link to a site that has a good explanation of the "button" command, how it works, and examples?

    > plenty of downloads Here !!! everything is good to learn as it's PbP fully compatible !!! :

    http://www.parallax.com/Education/Pr...5/Default.aspx

    I would really like to use this command instead of using high and low. That way I don't have to put resistors to a ground for every input pin in my project

    > High and low apply to Outputs ... no link to "Button" function !!!

    You'll always have to use pull up/down resistors for inputs whatever the way you read them .... yesss, that's it !!!

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  7. #7
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default 1 more question

    I didn't want to start another thread for this question.

    Is there any way to take a signal coming in on one pin and output that same signal on another?

    I don't know if the signal is digital or analog so it's not like I can just set it to high, low, or any puls commands.

    Any idea how this can be done?

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Hi plyrathrt. Yes. There is a way for digital signals. You can change the ports to your liking.

    LOOP:
    INPUT PORTB.0
    OUTPUT PORTB.1
    LET PORTB.1 = PORTB.0
    GOTO LOOP

  9. #9
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default It works. Sort of.

    Ok that works but I think it's a pulse width signal. Just looping it works however there is a issue when I want to let the signal through and then wait (pause or pauseus) and then let a signal from another pin through. It will go crazy.

    I tried to read the signal as if it was a pulse but it didn't work right. Example of how I wrote the code is below.

    pulse var word
    pulsin 3,1,pulse

    main:
    if gpio.5 = 1 then
    pulsout 0,pulse
    endif
    goto main

    This does not work. Could it be the pulse width is too big for a 4mhz osc? I defined it to 8 and still had the issue.

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,651


    Did you find this post helpful? Yes | No

    Exclamation

    Hi, Ply rat

    PULSIN has a time-limit ... ( see DEFINE PULSIN_MAX ) and returns 0 if this time limit is reached.

    So, you must stay within those limits ... (default 65535 counts = 0.65535 sec @ 4 Mhz)

    Take care : that applies to the High OR Low state of your pulse !!!

    What pulse length ( or signal ) do you want to catch ???

    Could you explain a bit more what you want to build as a device ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  11. #11
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default pulse

    Basicly I am trying to tie in a microcontroller do automate some functions on a piece of equipment. Each "button" has two contacts. 1 side of each tie in together. I originally though maybe this was a shared ground. It is aparently not.

    So that shared trace goes to the is where a signal is sent to tell the equipment what to do. The other trace is the one with the signal on it.

    When I use "let gpio.3 = gpio.1" and then tell it "high 1" it works but I can't get it to "pause" to try and send another function to it. I am basicly trying to create a macro. When I try and use the "pause" command it does functions that I never tied in to. This is why I think it is a pulse. ( I was cutting the signal off too early or too late)


    Isn't the default for Pulsin's max 65535 @4mhz? If anything I would need to give it a high max not a lower one right?

  12. #12
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default Help

    Still looking for some much needed help with the above question.

    Thank you

  13. #13
    Join Date
    Apr 2006
    Location
    New Hampshire USA
    Posts
    298


    Did you find this post helpful? Yes | No

    Default Relay...

    Quote Originally Posted by plyrathrt View Post
    I didn't want to start another thread for this question.

    Is there any way to take a signal coming in on one pin and output that same signal on another?

    I don't know if the signal is digital or analog so it's not like I can just set it to high, low, or any pulse commands.

    Any idea how this can be done?
    Hi plyrathrt,
    If you want to switch an unknown (low frequency AC) or (DC signal), with a PIC, you could use a relay.
    Your unknown signal would come in one contact, and you could chose when to switch it to the other contact(s).

    <img SRC="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2280&stc=1&d=120095554 4">
    Attached Images Attached Images  
    Ohm it's not just a good idea... it's the LAW !

  14. #14
    Join Date
    Jan 2008
    Location
    Vassar, Michigan USA
    Posts
    9


    Did you find this post helpful? Yes | No

    Default Pull up may still be needed

    I think you will probably still need a pull-up resistor in your design to get the PIC to see a high-to-low and low-to-high transition to get any of these commands to work.

    If you wanted, for example portb.0 to be your input and portb.1 to be an output, then you'd:

    'program the port so the correct pins are in or outs
    'this makes bit 1 an input, all other pins are outputs
    trisb = 1

    START:
    portb.1 = portb.0
    GOTO START


    If you wanted to do this across ports:

    trisb = 255 'all of port b is inputs
    trisd = 0 'all of port d is outputs

    START:
    portd = portb
    GOTO START


    or if you just want to do a single bit on each port:

    START:
    portd.0 = portb.1 'this makes bit 0 on port D reflect what is going on on portb bit 1


    You could also flip-flop the bits of a port using a variable:

    portBflipval VAR BYTE

    START:
    portBflipval.bit0 = portb.7
    portBflipval.bit1 = portb.6
    portBflipval.bit2 = portb.5
    portBflipval.bit3 = portb.4
    portBflipval.bit4 = portb.3
    portBflipval.bit5 = portb.2
    portBflipval.bit6 = portb.1
    portBflipval.bit7 = portb.0

    portd = portBflipval 'port d now has the value that is on port b, but flipped!!!



    Also, if you wanted to track the status of an input, so your code doesn't, for example send out multiple codes:

    buttonState VAR BIT

    START:
    if portb.0 <> buttonState then
    buttonState = portb.0
    if buttonState = 0 then HSEROUT ["Button 0 released and it's a zero!"]
    if buttonState = 1 then HSEROUT ["Button 0 pressed and it's a one!"]
    endif
    goto START

    I've found that this method is excellent for be-bouncing a button press... I don't know why but it works great, even with a hundred lines of code...


    If your aim is to read an analog voltage and then send that out on one of the 8 bit ports, that should be easy. Although I havent worked with this yet myself.

  15. #15
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default Relay

    I used transistors to make it work but was hoping there was a way to use it with only the pic.

    Once you use the "let" command how do you cancel it? That would make it so I would only need to use 1 transistor for the entire project. Example

    PIC12F683

    I could say
    LET GPIO.0 = GPIO.1 'GPIO.0 IS INPUT AND GPIO.1 AS OUTPUT TO TRANSISTOR
    HIGH 2 'SEND HIGH TO TRANSISTOR TO LET SIGNAL THROUGH
    PAUSE 100 'WAIT 100MS
    LOW 2 'STOP TRANSISTOR FROM LETTING SIGNAL THROUGH

    NOW HOW DO i CANCEL THE "LET" COMMAND SO I CAN CONTINUE LIKE THIS

    LET GPIO.3 = GPIO.1
    HIGH 2 'SEND HIGH TO TRANSISTOR TO LET SIGNAL THROUGH
    PAUSE 100 'WAIT 100MS
    LOW 2 'STOP TRANSISTOR FROM LETTING SIGNAL THROUGH

  16. #16
    Join Date
    Jan 2008
    Location
    Vassar, Michigan USA
    Posts
    9


    Did you find this post helpful? Yes | No

    Default RE: Relay

    I would probably just turn the port on, wait and then turn if off:

    if portb.0 = 1 then
    portd.0 = 1 'turn on port d bit 0
    PAUSE 100
    portd.0 = 0 'turn it back off after 100 ms
    endif

    modifiying this idea with my variable makes it a one-shot deal, so the output only pulses once on the low-going transistion, but does nothing when the user releases the button.

    buttonState VAR BIT

    START:
    if portb.0 <> buttonState then
    buttonState = portb.0
    if buttonState = 0 then 'user is pushing on button
    portd.0 = 1
    PAUSE 100
    portd.0 = 0
    endif
    endif

    Unfortunately all of the bits on a PIC are latching. So if you want them to be a one-shot blink, then you have to code them to do it. I would try to avoid using the HIGH and LOW commands, unless you intend to use the basic stamp compatability files. These can add some overhead to your compiled program that goes onto your chip.

    The pause command also affects how the PIC performs in real-time as the world has to stop inside the pic for this command to execute.
    Last edited by Jasonce65; - 22nd January 2008 at 00:12. Reason: update

  17. #17
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default Thanks,

    That's what I thought the best option would be but wanted to make sure.

    Yeah I alway's use it by bit and create variables because sometimes Mouser runs out of chips I need and I have to get a near compatible one.

    I also only use the PAUSEUS so that the pic doesn't go to sleep.

    Thank you for your help. I will try and do it this way and maybe, just maybe, it will function as desired.

  18. #18
    Join Date
    Jan 2008
    Location
    Vassar, Michigan USA
    Posts
    9


    Did you find this post helpful? Yes | No

    Default logical operators

    keep in mind that if your're working with more than one input, one that is an "enable", for example, that you can use a logical operator to make some magic...


    if portb.0 <> buttonState AND portd.3 = 1 THEN... etc.

    In one case I needed to validate what was going on with a variable that came from the HSERIN and had to use some intense logic...

    If (BtVar = 48 AND porte = 0) OR (BtVar = 49 AND porte = 1) OR (BtVar = 50 AND porte = 2) ... THEN...

    This let me match the port e value to the character that came in on the serial port. Since Port e on an 16F877A is 3 bits, I needed to compare for all 8 values using this if-then. It's incredible what tis complier and the chips, for that matter can do!

    good luck.
    J.-

Members who have read this thread : 0

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