Beginner in need of help !!


Closed Thread
Results 1 to 40 of 72

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi Rob,
    Welcome to the forum! As you can imaging there are several ways to do this (and most other things). Amoque shows you one, here's another (which also tries to take care of the debounce):
    Code:
    LED VAR GPIO.0
    Switch VAR GPIO.1
    
    ANSEL = 0                     ' No analog inputs
    CMCON = 7                     ' Comparators disabled
    TRISIO = %11111110            ' GPIO.0 as output
    
    Main:
      IF Switch = 1 THEN          ' If the button is pressed...
        TOGGLE LED                ' ...we toggle the LED.
        Pause 20                  ' Then we wait for contact bounce on make to die, may need tweaking
        WHILE Switch = 1 : WEND   ' Then we wait for the user to actually release the button
        Pause 20                  ' Finally we wait for contact bounce on release to die, may need tweaking
      ENDIF
    Goto Main
    /Henrik.

  2. #2
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Quote Originally Posted by HenrikOlsson View Post
    Hi Rob,
    Welcome to the forum! As you can imaging there are several ways to do this (and most other things). Amoque shows you one, here's another (which also tries to take care of the debounce):
    Code:
    LED VAR GPIO.0
    Switch VAR GPIO.1
    
    ANSEL = 0                     ' No analog inputs
    CMCON = 7                     ' Comparators disabled
    TRISIO = %11111110            ' GPIO.0 as output
    
    Main:
      IF Switch = 1 THEN          ' If the button is pressed...
        TOGGLE LED                ' ...we toggle the LED.
        Pause 20                  ' Then we wait for contact bounce on make to die, may need tweaking
        WHILE Switch = 1 : WEND   ' Then we wait for the user to actually release the button
        Pause 20                  ' Finally we wait for contact bounce on release to die, may need tweaking
      ENDIF
    Goto Main
    /Henrik.
    Thanks for this Henrik, I found this the best way. Another question, what if I wanted to run a different command to just turning on the LED, what about if I wanted to start the LED flashing upon button tap? Could I do this with the same method?

    Thanks,
    Rob

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi,

    > what if I wanted to run a different command to just turning on the LED
    Don't understand this one, sorry...

    > what about if I wanted to start the LED flashing upon button tap?
    Well, yes. But as soon as you mention blinking it starts to get a Little bit more complicated. Why?
    Because then you need delays in there, during which (if using PAUSE) the button won't be polled so it'll feel unresponsive. Of course there are ways around this but the correct one depends on what you want to do after we get that LED to blink....

    Try this (I haven't, hope it works....):
    Code:
    LEDState VAR BYTE
    Time VAR WORD
    
    LED_OFF CON 0
    LED_ON CON 1
    LED_FLASH CON 2
    
    Rate CON 250		      ' Time between LED toggles when blinking
    
    Main:
      IF Switch = 1 THEN          ' If the button is pressed...
        LEDState = LEDState + 1
        IF LEDState = 3 THEN LEDState = 0
        Pause 20                  ' Then we wait for contact bounce on make to die, may need tweaking
        WHILE Switch = 1 : WEND   ' Then we wait for the user to actually release the button
        Pause 20                  ' Finally we wait for contact bounce on release to die, may need tweaking
      ENDIF
    
      IF (LEDState = LED_OFF) or (LEDState = LED_ON) THEN
        LED = LEDState            ' Turn LED on or off, solid.
      ENDIF
    
      IF LEDState = LED_FLASH THEN
        Time = Time + 1
        IF Time = Rate THEN
          TOGGLE LED
          Time = 0
        ENDIF
        PAUSEUS 1000
      ENDIF  
      
    Goto Main
    /Henrik.

  4. #4
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Update: Ive looked in the data sheet & found out the reason for dividing by either 24 or 32, if your working out low range then : 24, high range : 32. Thats another thing understood.

    So the last thing I am struggling to understand now is, how do you get the number from th bits?

    VR<3:0> = 1111 (15). < How on earth do you get 15 from just bits 1111?

    This is the only thing that I'm struggling to understand now.

    Thanks,
    Rob
    Last edited by robbo78; - 24th January 2015 at 11:06.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,689


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    the low range internal vref gives you 15 steps of 0.208 volt steps starting at 0v
    the high range internal vref gives you 15 steps of 0.156 volt steps starting at 1.25v

    if you don't need the cout as an actual physical output CM<2:0> = 110 is a better choice
    and lets you chose pin 6 or 7 as your input (select with cmcon0.3 "CIS")
    why waste pins on a 8 pin chip

    0000=0
    0001=1
    0010=2
    0011=3
    ...................etc etc
    its called binary
    Last edited by richard; - 24th January 2015 at 11:16. Reason: binary

  6. #6
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Edit: just re-read your post. I need to be able to read a pin & output on it at the same time (obviously with pauses, so do I need to use comparator with output?

    Hi Richard, I just came back here from doing a bit of googleing. It took me about 2 mins to figure out that its just binary, maybe if I had paid more attention in Maths I would know this!

    I now understand the voltage steps as well, everything is coming together now, its actually quite simple once understood.

    I still need to know if you can use a comparator with internal reference on any pin or does it have to be the CIN- pin?

    Cheers,
    Rob
    Last edited by robbo78; - 24th January 2015 at 11:24.

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,689


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    if you don't need the cout as an actual physical output CM<2:0> = 110 is a better choice
    and lets you chose pin 6 or 7 as your input (select with cmcon0.3 "CIS")
    input to comparator is [ cin+ pin 7] or [cin- pin 6] with CM<2:0> = 110 its your choice

    there are other modes but in all cases cin is only on pins 6,7

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,689


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    if you need or can use the comparator output use CM<2:0> = 101
    it still lets you chose pin 6 or 7 as your input (select with cmcon0.3 "CIS")

  9. #9
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Okay Richard thanks for your help.

    So lets say I wanted an LED to be OFF if the voltage on cin+ is below 1.1v, & LED to be ON if the voltage is higher than 1.1v. Would I use this -

    VR3 ... VR0 = 1000 (8) with a VDD of 3.3V the Vref will be (8/24) x 3.3 = 1.1V

    I hope this is right lol

  10. #10
    Join Date
    May 2013
    Location
    australia
    Posts
    2,689


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    there is not enough info to answer

    what comparator mode ?
    what cis setting
    is vrr set
    what cinv seting
    how is led connected ?
    Last edited by richard; - 24th January 2015 at 12:42. Reason: spell

Similar Threads

  1. Beginner help!
    By Recognize in forum General
    Replies: 14
    Last Post: - 26th April 2012, 15:55
  2. pic24 beginner
    By robertpeach in forum General
    Replies: 23
    Last Post: - 13th August 2009, 12:57
  3. need help! to beginner
    By emilhs in forum mel PIC BASIC Pro
    Replies: 27
    Last Post: - 6th May 2009, 19:44
  4. Beginner at PB
    By alphahr in forum Off Topic
    Replies: 1
    Last Post: - 21st April 2008, 18:43
  5. Can anyone help a beginner in a struggle?
    By douglasjam in forum mel PIC BASIC
    Replies: 1
    Last Post: - 6th May 2005, 00:29

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