Final Button Code


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    May 2008
    Location
    Texas
    Posts
    16

    Cool Final Button Code

    Here is what I have so far.

    Do my momentary NO switches need to pull the inputs low?

    Code:
    Define	    LCD_DREG	PORTD
    Define	    LCD_DBIT	4
    Define	    LCD_RSREG	PORTE
    Define	    LCD_RSBIT	0
    Define	    LCD_EREG	PORTE
    Define	    LCD_EBIT	1
    
    ADCON1 = 7			          ' Set PORTD and PORTE to digital
    
    Setpoint      var word
    delay         VAR Byte        'Button working variable
    Temp_Down     Var PortB.3     'Temp down button input port
    Temp_Up       VAR PortB.2     'Temp up button input port
    R_LED         VAR PortB.0     ' Red LED output
    G_LED         VAR PortB.1     ' Green LED output
    DQ            VAR PortC.0     ' One-wire Data-Pin "DQ" on PortC.0
    Busy          VAR BIT         ' Busy Status-Bit
    Sign          VAR BYTE        ' +/- sign for temp display
    Dummy         VAR BYTE        ' Dummy for Div32
    R_Temp        VAR WORD        ' RAW Temperature readings
    TempC         VAR WORD        ' Temp in deg C
    TempF         VAR WORD        ' Temp in deg F
    Float         VAR WORD        ' Holds remainder for + temp C display
    Cold_Bit      VAR R_Temp.Bit11' Sign-Bit for +/- Temp. 1 = Below 0 deg C
    Real_Cold     CON 1           ' Define Real_Cold = 1 
    DS18B20_9bit  CON %00011111   ' 93.75ms, 0.5°C
    DS18B20_10bit CON %00111111   ' 187.5ms, 0.25°C  <-- My favorite
    DS18B20_11bit CON %01011111   ' 375ms,   0.125°C
    DS18B20_12bit CON %01111111   ' 750ms,   0.0625°C  (default)
    
    OPTION_REG = $7f        
    
    
        OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_10bit]    'Skip ROM search and write 10bit resolution to scratch pad
        LCDOut  $FE                                     'Clear LCD
        Setpoint = 20                                   'Default setpoint on start up
        
    Start_Convert:
        OWOUT   DQ, 1, [$CC, $44]                       ' Skip ROM search & do temp conversion
        delay = 0
        Button Temp_Up, 0, 255, 0, delay, 1, Up_Loop
        button Temp_Down, 0, 255, 0, delay, 1, Down_Loop
        If TempC < Setpoint Then
            high R_LED                                   ' Turn on Red LED  and turn off Green LED
            low G_LED                    
        Else                                            
            High G_LED                                   ' Turn on Green LED and turn off Red LED
            low R_LED                     
        EndIf
        
    Wait_Up:
        OWIN    DQ, 4, [Busy]                           ' Read busy-bit
        IF      Busy = 0 THEN Wait_Up                   ' Still busy..?, Wait_Up..!
        OWOUT   DQ, 1, [$CC, $BE]                       ' Skip ROM search & read scratchpad memory
        OWIN    DQ, 2, [R_Temp.Lowbyte, R_Temp.Highbyte]' Read two bytes / end comms
        GOSUB   Convert_Temp
        GOTO    Start_Convert
        
    Convert_Temp:                                        ' +32.0 to +257 F
        'Sign  = "+"
        Dummy = 625 * R_Temp                            ' Multiply to load internal registers with 32-bit value
        TempC = DIV32 10                                ' Use Div32 value to calculate precise deg C
        Dummy = 1125 * R_Temp
        'TempF = DIV32 100
        'IF TempF >6795 THEN                             ' Over 99.5 deg F..?
            'TempF = TempF + 3200
            'Lcdout $FE, 1,Sign,DEC TempF DIG 4,dec TempF Dig 3,dec TempF Dig 2,".",dec2 TempF,"   F"
        'ELSE
            'TempF = TempF + 3200
            'lcdout $FE, 1,Sign,Dec TempF Dig 3,Dec TempF Dig 2,".",Dec2 TempF,"   F"
        'ENDIF
        TempC  = (R_Temp & $0FF0) >> 4                  ' Mask middle 8-bits, shift into lower byte
        Float = ((R_Temp.Lowbyte & $0F) * 625)          ' Lower 4-bits of result * 625
        LCDOut $FE, 1,DEC TempC,".",dec Float," C"
        lcdout $fe, $c0, "Setpoint = ", Setpoint
        RETURN                                                             
    Up_Loop:
        If Temp_Up = 0 Then
            Setpoint = Setpoint + 1
            lcdout $fe, 1, "Setpoint = ", Setpoint
            Pause 1000
        Else
            Goto Start_Convert
        Endif
        Goto Up_Loop
    Down_Loop:                
        If Temp_Down = 0 Then
            Setpoint = Setpoint - 1
            lcdout $fe, 1, "Setpoint = ", Setpoint
            Pause 1000
        Else
            Goto Start_Convert
        Endif
        Goto Down_Loop    
        
        
        END

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by WarPony View Post
    Do my momentary NO switches need to pull the inputs low?
    Well, without a schematic, makes it a bit hard to tell...
    But, if your switches are connected to +5v when pressed and open when not pressed, what voltage is present at the PIC's pin?
    If it floats, the yes, you could probably use a pulldown.
    OR...since you are using PortB, you might be able to use the in-chip weak pull ups and change the code to watch for a switch going low...

  3. #3
    Join Date
    May 2008
    Location
    Texas
    Posts
    16


    Did you find this post helpful? Yes | No

    Default

    I was refering to my button command. Should I start the port high and pull it low or start low and pull high based on this bit of code?

    Code:
    delay = 0
        Button Temp_Up, 0, 255, 0, delay, 0, Up_Loop
        button Temp_Down, 0, 255, 0, delay, 0, Down_Loop
    Up_Loop:
        If Temp_Up = 0 Then
            Setpoint = Setpoint + 1
            lcdout $fe, 1, "Setpoint = ", Setpoint
            Pause 1000
        Else
            Goto Start_Convert
        Endif
        Goto Up_Loop
    Down_Loop:                
        If Temp_Down = 0 Then
            Setpoint = Setpoint - 1
            lcdout $fe, 1, "Setpoint = ", Setpoint
            Pause 1000
        Else
            Goto Start_Convert
        Endif
        Goto Down_Loop

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Well, I was referring to your schematic. The way you code depends on the way you wired it up...

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by WarPony View Post
    I was refering to my button command. Should I start the port high and pull it low or start low and pull high based on this bit of code?

    Code:
    delay = 0
        Button Temp_Up, 0, 255, 0, delay, 0, Up_Loop
        button Temp_Down, 0, 255, 0, delay, 0, Down_Loop
    Up_Loop:
        If Temp_Up = 0 Then
            Setpoint = Setpoint + 1
            lcdout $fe, 1, "Setpoint = ", Setpoint
            Pause 1000
        Else
            Goto Start_Convert
        Endif
        Goto Up_Loop
    Down_Loop:                
        If Temp_Down = 0 Then
            Setpoint = Setpoint - 1
            lcdout $fe, 1, "Setpoint = ", Setpoint
            Pause 1000
        Else
            Goto Start_Convert
        Endif
        Goto Down_Loop
    Hello WarPony,
    You have written your code, If X=0 true, then do something, so you would pull the ports up to supply + with a resistor or WPUs, and the if / then loop would be true when the switch grounds it. Did I answer the right question or are you asking which way is better ?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  6. #6
    Join Date
    May 2008
    Location
    Texas
    Posts
    16


    Did you find this post helpful? Yes | No

    Default

    I am getting all twisted up with the button code and loop. I havent added my switches to my circuit yet. I just want 1 to be added to or subtracted from my setpoint when one of the two switches is pressed.

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by WarPony View Post
    I am getting all twisted up with the button code and loop. I havent added my switches to my circuit yet. I just want 1 to be added to or subtracted from my setpoint when one of the two switches is pressed.
    When we say pullup/pulldown, generally we're referring to the state of the pin when the switch isn't being actuated.
    Usually, a person will put a resistor (4.7K-10K, whatever), from the pin to Vdd, and the switch is wired in to ground that pin. Since the resistor runs from the pin to Vdd, the pin is high with the switch open, low with the switch closed.
    So, if you want to use the button command, that's great...but why not play around with it without using the button command so you can understand it's inner working a bit better.
    i.e.
    loop:
    if switch1 = 0 then setpoint = setpoint - 1
    if switch2 = 0 then setpoint = setpoint + 1
    ......blah blah blah....
    Yes, a loop like this will likely execute entirely too fast, and will most likely cause the setpoint value to 'wrap around'...but that's what 'pause' can be used for.

Similar Threads

  1. TSA5512/5511 code for PIC16F877/84
    By Marin in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th August 2013, 06:16
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  5. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43

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