PDA

View Full Version : Final Button Code



WarPony
- 19th May 2008, 02:51
Here is what I have so far.

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



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

skimask
- 19th May 2008, 13:53
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...

WarPony
- 20th May 2008, 02:23
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?



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

skimask
- 20th May 2008, 03:37
Well, I was referring to your schematic. The way you code depends on the way you wired it up...

Archangel
- 20th May 2008, 04:15
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?



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 ?

WarPony
- 20th May 2008, 14:15
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.

skimask
- 20th May 2008, 14:29
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.

WarPony
- 20th May 2008, 20:54
Why couldnt the pbp manual explain it like that?

Thanks skimask

I will finish my circuit and let you know how it went.

Acetronics2
- 20th May 2008, 21:37
Hi WP

I played a bit with the code these two days ...

that gives the following ...




'************************************************* ****************************
'************************************************* ****************************
' Thermostat F84 test sur EasyPic5
' Thermostat 16F877a + DS 18B20
'************************************************* ****************************
'************************************************* ****************************

'************************************************* ****************************
'************************************************* ****************************

@ __config _HS_OSC & _WDT_ON & _PWRTE_ON & _BODEN_ON & _LVP_OFF & _CP_OFF

'************************************************* ****************************
'* LCD Defines for EasyPic5 *
'************************************************* ****************************

DEFINE LCD_DREG PORTB ' I/O port where LCD is connected
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4 ' Register select pin
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5 ' Enable pin
DEFINE LCD_BITS 4 ' 4-bit data bus
DEFINE LCD_LINES 2 ' LCD has 2 character lines

DEFINE OSC 8
DEFINE BUTTON_PAUSE 100

'Define LCD_DREG PORTD
'Define LCD_DBIT 4
'Define LCD_RSREG PORTE
'Define LCD_RSBIT 0
'Define LCD_EREG PORTE
'Define LCD_EBIT 1


Temp_Down VAR PortA.0 'Temp down button input port
Temp_Up VAR PortA.1 'Temp up button input port
G_LED VAR PortC.0 ' Green LED output cold needed
Relay_Cold VAR PORTC.1
R_LED VAR PortC.6 ' Red LED output hot needed
Relay_hot VAR PortC.7
DQ VAR PortE.2 ' One-wire Data-Pin "DQ" on PortE.2


'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

delay VAR BYTE 'Button working variable
Dummy VAR BYTE ' Dummy for Div32
Loscale VAR BYTE
Hiscale VAR BYTE
Decal VAR BYTE

Tempeff VAR WORD
Setpoint VAR WORD
R_Temp VAR WORD ' RAW Temperature readings
TempC VAR WORD ' Temp in deg C
Float VAR WORD ' Holds remainder for + temp C display

ColdStart VAR BIT
Sign VAR BIT

Cold_Bit VAR R_Temp.Bit11 ' Sign-Bit for +/- Temp. 1 = Below 0 deg C


ADCON1 = 7 ' Set PORTD and PORTE to digital
CMCON = 7 ' Comparators OFF

PORTA = %00000011
PORTB = 0
PORTC = 0
PORTD = 0
PORTE = 0

TRISA = %00000011
TRISB = 0
TRISC = 0
TRISD = 0
TRISE = %00000010


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)


Setpoint = 200 'Default setpoint on start up
ColdStart = 1

LCDOut $FE,1 'Clear LCD
PAUSE 750

'Chargement motifs en CGRAM

LCDOUT $FE,$40,$00,$00,$00,$00,$00,$04,$04,$04 ' Cust Char #0
LCDOUT $FE,$48,$00,$00,$00,$04,$04,$04,$04,$04 ' Cust Char #1
LCDOUT $FE,$50,$04,$04,$04,$04,$04,$04,$04,$04 ' Cust Char #2
LCDOUT $FE,$58,$04,$04,$0E,$0E,$0E,$1F,$1B,$11 ' Cust Char #3
LCDOUT $FE,$60,$11,$1B,$1F,$0E,$0E,$0E,$04,$04 ' Cust Char #4


OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_10bit] 'Skip ROM search and write 10bit resolution to scratch pad

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

'************************************************* ****************************
' Check for still busy converting ( ~ 4500 fois ... )
'************************************************* ****************************

waitloop:

INPUT DQ
If NOT DQ Then waitloop


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

Convert_Temp:

IF R_Temp.15 THEN

R_Temp = ~R_Temp + 1
Sign = 1 'Signe -

ELSE

Sign = 0 'Sign = "+"

ENDIF


TempC = ( R_Temp & $7FF ) >> 4 ' Partie Entière

Float = ((R_Temp.Lowbyte & $0F ) * 25 )>> 2 ' Partie décimale 100èmes

Tempeff = TempC*10 + Float/10 'Préparation 1/10 degrés

gosub Aff

GOTO Start_Convert


Up_Loop:

If Temp_Up = 0 Then
Setpoint = Setpoint + 5
lcdout $fe, $C0, "Setpoint = ", DEC2 (Setpoint/10),".",DEC1(SetPoint // 10)," "
Gosub Show
Pause 500
Else
Goto Start_Convert
Endif
Goto Up_Loop


Down_Loop:

If Temp_Down = 0 Then
Setpoint = Setpoint - 5
lcdout $fe, $C0, "Setpoint = ", DEC2 (Setpoint/10),".",DEC1(SetPoint // 10)," "
Pause 500
Gosub Show
Else
Goto Start_Convert
Endif
Goto Down_Loop



'************************************************* ****************************
'************************************************* ****************************
' AFFICHAGE
'************************************************* ****************************
'************************************************* ****************************

'Motifs

'Small bar LCDOUT $FE,$40,$00,$00,$00,$00,$00,$04,$04,$04 ' Cust Char #0
'Mid bar LCDOUT $FE,$48,$00,$00,$00,$04,$04,$04,$04,$04 ' Cust Char #1
'Big bar LCDOUT $FE,$50,$04,$04,$04,$04,$04,$04,$04,$04 ' Cust Char #2
'Arrow LCDOUT $FE,$58,$04,$04,$0E,$0E,$0E,$1F,$1B,$11 ' Cust Char #3
'Inv Arrow LCDOUT $FE,$60,$11,$1B,$1F,$0E,$0E,$0E,$04,$04 ' Cust Char #4

'************************************************* ****************************
Aff:'Affichage temp
'************************************************* ****************************

'préparation chiffre : sign = 1 = valeur négative

Loscale = ( TempC /5 + Sign ) * 5 + Sign << 7
Decal = 0
Decal = ( TempC // 5 ) << 1

IF Sign THEN

IF Float > 25 AND Float <= 75 then Decal = Decal - 1
IF Float > 75 Then Decal = Decal - 2

ELSE

IF Float > 25 AND Float <= 75 then Decal = Decal + 1
IF Float > 75 Then Decal = Decal + 2

ENDIF

Hiscale = (TempC /5 + ( Sign ^1)) * 5 + Sign << 7

'Impression valeurs 1ère ligne

LCDOUT $FE, $81 ,SDEC2 Loscale,178,"C"," ",SDEC TempC,".",dec2 Float,$FE,$80 + 12,SDEC2 Hiscale,178,"C"

'Impression échelle 2ème ligne

LCDOUT $FE,$C0,$01,$00,$02,$00,$01,$00,$01,$00,$01,$00,$0 1,$00,$02,$00,$01,$00

'Impression index

IF Tempeff < Setpoint THEN

LCDOUT $FE, $C2 + Decal,$03 'Index Chauffage

ELSE

LCDOUT $FE, $C2 + Decal,$04 ' Index Refroidissement

ENDIF

Show:

IF Tempeff < Setpoint THEN

high R_LED ' Turn on Red LED C0 and turn off Green LED C6
low G_LED

IF Coldstart THEN Relay_hot = 1 : Relay_cold = 0 : Coldstart = 0

ELSE

High G_LED ' Turn on Green LED C6 and turn off Red LED C0
low R_LED

IF Coldstart THEN Relay_hot = 0 : Relay_cold = 1 : coldstart = 0

ENDIF

'************************************************* ****************************
Drive:
'************************************************* ****************************

IF Tempeff < (Setpoint - 5) AND Relay_hot = 0 THEN Relay_hot = 1 : Relay_cold = 0 'C7

IF Tempeff > (Setpoint + 5) AND Relay_cold = 0 THEN Relay_hot = 0 : Relay_cold = 1 'C1

RETURN

END



Some little improvements to work further on for the drive section ...

but the idea is here ...

Alain

WarPony
- 20th May 2008, 23:53
meersee buukuu alain (bad french accent)

gonna grab some of that code you worked up...this is for residential so the temps I am working with will be from about 60 degrees F to 80 degrees F for setpoints and 20 degrees F to 120 degrees F for the outside reference temp.

thanks again
Bryan

Acetronics2
- 21st May 2008, 10:06
Hi, WP

Not so bad for the accent !!!

a small forgotten thing




'************************************************* ****************************
Aff:'Affichage temp
'************************************************* ****************************

'préparation chiffre : sign = 1 = valeur négative

Loscale = ( TempC /5 + Sign ) * 5 + Sign << 7
Decal = 0
Decal = ( TempC // 5 ) << 1

IF Sign THEN

'********************
Decal = 10 - Decal
'********************
IF Float > 25 AND Float <= 75 then Decal = Decal - 1
IF Float > 75 Then Decal = Decal - 2
.
.
.

skimask
- 21st May 2008, 15:23
Why couldnt the pbp manual explain it like that?
Because PicBasicPro isn't basic digital electronics.
You get basic digital electronics from a book called 'Basic Digital Electronics'.

WarPony
- 21st May 2008, 22:47
Read that book....didnt hold my attention...maybe thats my problem...haha

skimask
- 22nd May 2008, 00:08
Read that book....didnt hold my attention...maybe thats my problem...haha

Yep, boring book...I agree... Or is it?
Which is more boring?
Asking questions to be answered here or not asking that same question which would've been answered long ago by a quick read?
I know my answer...

mister_e
- 22nd May 2008, 00:12
Which is more boring?
NONE, maybe having such reply ?
;)

Acetronics2
- 22nd May 2008, 08:30
Yep, boring book...I agree... Or is it?
Which is more boring?
Asking questions to be answered here or not asking that same question which would've been answered long ago by a quick read?
I know my answer...


Hi, Skimask

The general question is ...

is it possible to care only with programming when trying to build a full application ...

Ahhhhh ... virtual world.

I remember when I was at university ... we did some regulation exercises + applications.

After having carefully calculated all parameters, some friends went to the ... real world.

facing the control panel with its scaled pots and switches ... they were somewhat embarassed ...

a moment later, after they had stared at this strange extraterrestrian panel ... we were asked this unforgettable question :

But, Please ... what and Where is the Proportionnal, Integral and derivative actions ???


They really were gentle friends, so it was really hard not to laught at them ...

But they still were tomorrows engineers ....


"Just place the relative pots indexes on the percentage you've calculated" ... we hardly could say ...


Alain

WarPony
- 22nd May 2008, 14:00
Besides

Getting hands on experience and learning on the fly is so much more fun.

Bryan

skimask
- 22nd May 2008, 14:14
Getting hands on experience and learning on the fly is so much more fun.
And sometimes you get to let that 'all important mystery smoke' out of the parts you're messing with.
Can't do that with a sim...well, I guess you can...but...