PDA

View Full Version : DS1820 with 16f688



jessey
- 25th April 2009, 23:32
Hello Everyone,

I've been trying to get my DS1820 to work on my 16f688 and can't figure out why it won't display the proper temperature but the exact same code on my 16f877 works good. This code will only display 31 degrees F. Would anyone have any suggestions as to why it's not functioning properly? It's a real stumper for me and any help would be much appreciated.

Thanks
jessey



'************************************************* ********************************
'* Name : Test Program for DS1820 & 4 x 20 LCD *
'* Author : Jessey Montgomery *
'* Includes: Darrel Taylor's EE_Vars.PBP *
'* Date : April 20/th 2009 *
'* Version : Using PicBasic Pro Ver 2.50, MPASM Ver 5.20, Using MicroCode Studio *
'* : 3.0.0.5, PICkit 2 programmer - Application Version 2.50.02 - Device *
'* : File Version 1.5100 - OS Firmware Version 2.30.01. Using a 16F688. *
'************************************************* ********************************


'(1) Comments & Objectives
' =====================
' this program is just a test program for checking out the new 4 x 20 lcd and
' also displaying temperature using a DS1820. This program communicates with a
' 16f877 to turn On/Off X10 appliance modules using a fireCracker.

' --------------
' | 16f688 |
' VDD+ (--->) |1 14| (<---) VSS-
' | |
' RA5/TICKI/OSC1/CLKIN (<-->) |2 13| (<-->) RA0/AN0/C1IN+/ICSPDAT/ULPWU
' | |
'RA4/AN3/TIG/OSC2/CLKOUT (<-->) |3 12| (<-->) RA1/AN1/C1IN-/Vref/ICSPCLK
' | |
' RA3/MCLR/Vpp(--->) |4 11| (<-->) RA2/AN2/TOCKI/INT/C1OUT
' | |
' RC5/RX/DX (<-->) |5 10| (<-->) RC0/AN4/C2IN+
' | |
' RC4/C2OUT/TX/CX (<-->) |6 9| (<-->) RC1/AN5/C2IN-
' | |
' RC3/AN7 (<-->) |7 8| (<-->) RC2/AN6
' | |
' --------------


' Clear Each Port Before Setting The TRIS Registers
' =================================================


Clear ' Set all ram registers to zero
PORTA = 0
PORTC = 0

' VSS VDD MCLR Ect. Pins
' ======================

'VDD + '(pin 1)
'VSS - '(pin 14)

' SET THE TRIS
' ============

' PORTA PINS
' ==========
TRISA.0 = 0 '(pin 13) ............................Out_To_877_Pin_37 VAR PORTA.0
TRISA.1 = 0 '(pin 12) ............................Out_To_877_Pin_38 VAR PORTA.1
TRISA.2 = 0 '(pin 11) ............................Out_To_877_Pin_39 VAR PORTA.2
TRISA.3 = 1 '(pin 4) .......................................... DQ VAR PORTA.3
TRISA.4 = 0 '(pin 3) ..........................................Led VAR PORTA.4
TRISA.5 = 1 '(pin 2) .................................... Button_5 VAR PORTA.5

' PORTC PINS
' ==========
TRISC.0 = 0 '(pin 10) Blue........... Lcd Data bit 4
TRISC.1 = 0 '(pin 9) White.......... Lcd Data bit 5
TRISC.2 = 0 '(pin 8) Green.......... Lcd Data bit 6
TRISC.3 = 0 '(pin 7) Orange......... Lcd Data bit 7
TRISC.4 = 0 '(pin 6) Orange/Black... Lcd Register select
TRISC.5 = 0 '(pin 5) Blue/White..... Lcd Enable bit


'(2) PIC Config Fuse Definitions for 16f688
' ======================================
@ __CONFIG _INTOSCIO & _WDT_ON & _MCLRE_OFF & _CP_OFF
@ ERRORLEVEL -306; ' turns off cross page boundary message alerts

'(3) PIC Hardware Definitions (ie Pin-Out & Port Assignments)
' ================================================== ======
ANSEL = 0
CMCON0 = 7 ' turn off comparators

DEFINE LCD_RSREG PORTC' LCD Register Select Port
DEFINE LCD_RSBIT 4 ' LCD Register Select
DEFINE LCD_EREG PORTC ' LCD Enable Port
DEFINE LCD_EBIT 5 ' LCD Enable
DEFINE LCD_DREG PORTC ' LCD Data Port
DEFINE LCD_DBIT 0 ' LCD data starting bit 0 or 4

Out_To_877_Pin_37 VAR PORTA.0 ' pin 13
Out_To_877_Pin_38 VAR PORTA.1 ' pin 12
Out_To_877_Pin_39 VAR PORTA.2 ' pin 11
Button_5 VAR PORTA.5 ' pin 2
Led VAR PORTA.4 ' pin 3

a VAR BIT ' a always equals zero, used in IF-THEN's instead of using GOTO's
a = 0
Timer VAR byte
Timer = 0

temp VAR WORD ' Temperature storage
count_remain VAR BYTE ' Count remaining
count_per_c VAR BYTE ' Count per degree C
Sign VAR BIT ' bit determines id - sign is needed
DQ VAR PORTA.3 ' One-wire data pin (pin 4)


Is_Pressed CON 0
Is_Not_Pressed CON 1
Is_Turned_On CON 1
Is_Turned_Off CON 0


'4a. EEPROM Assignments
' ==================
INCLUDE "EE_Vars.PBP"

' 'Declare The EE Variable' 'Load A Default Setting For The EE Variable'
X VAR BYTE : @ EE_var _X, BYTE,0
Y VAR BYTE : @ EE_var _Y, BYTE,0
Z VAR BYTE : @ EE_var _Z, BYTE,0

' To write to the eeprom @ EE_write_var _X
' ============================================


' Reset all three PORT outputs to the states they were before the power-loss
' ================================================== ========================

IF X = 1 THEN
Out_To_877_Pin_37 = 1
ELSE ' PORTA.0 pin 13
Out_To_877_Pin_37 = 0
ENDIF

IF Y = 1 THEN ' pin 12
Out_To_877_Pin_38 = 1
ELSE ' PORTA.1 pin 12
Out_To_877_Pin_38 = 0
ENDIF

IF Z = 1 THEN 'pin 11
Out_To_877_Pin_39 = 1
ELSE ' PORTA.2 pin 11
Out_To_877_Pin_39 = 0
ENDIF
Led = Is_Turned_Off

LCDOUT 254,64,14,10,14,0,0,0,0,0 ' Cust Char #0 - degree sign

PAUSE 500 ' For LCD

IF a = 0 THEN MainLoop ' a always equals zero, use instead of GOTO...


Turn_On_Or_Off_3:
if Y = 0 then ' Y toggles the #3 X10 module On/Off with each button press
Y = 1 ' Turn On #3 X10 module
ELSE
Y = 0 ' Turn Off #3 X10 module
ENDIF
@ EE_write_var _Y ' remember Y in case of a power failure
IF Y = 0 THEN Out_To_877_Pin_38 = 0 ' #3 X10 module is Off
if Y = 1 then Out_To_877_Pin_38 = 1 ' #3 X10 module is On
RETURN


Turn_On_Or_Off_4:
if X = 0 then ' X toggles the #4 X10 module On/Off with each button press
X = 1 ' Turn On #4 X10 module
ELSE
X = 0 ' Turn Off #4 X10 module
ENDIF
@ EE_write_var _X ' remember X in case of a power failure
IF X = 0 THEN Out_To_877_Pin_37 = 0 ' #4 X10 module is Off
if X = 1 then Out_To_877_Pin_37 = 1 ' #4 X10 module is On
RETURN


Turn_On_Or_Off_5:
if Z = 0 then ' Z toggles the #5 X10 module On/Off with each button press
Z = 1 ' Turn On #5 X10 module
ELSE
Z = 0 ' Turn Off #5 X10 module
ENDIF
@ EE_write_var _Z ' remember Z in case of a power failure
IF Z = 0 THEN Out_To_877_Pin_39 = 0 ' #5 X10 module is Off
if Z = 1 then Out_To_877_Pin_39 = 1 ' #5 X10 module is On
RETURN


Check_Temperature:
OWOut DQ, 1, [$CC, $44] ' Start Temp conversion
waitloop:
OWIn DQ, 4, [count_remain] ' Check for still busy converting
IF count_remain = 0 THEN waitloop

OWOut DQ, 1, [$CC, $BE] ' Read the Temp
OWIn DQ, 0, [temp.LOWBYTE, temp.HIGHBYTE, _
Skip 4, count_remain, count_per_c]
' Calculate temp in degrees Celsius to 2 decimal places
Sign = temp.15
temp = ((ABS(temp) >> 1) * 100)
if Sign then temp = -temp
temp = temp - 25 _
+ (((count_per_c - count_remain) * 100) / count_per_c)

' Convert Celsius to Fahrenheit before returning
Sign = temp.15
temp = (ABS(temp) */ 461)
if Sign then temp = -temp
temp = temp + 3200
RETURN


MainLoop:

GOSUB Check_Temperature

IF Button_5 = Is_Pressed THEN
Timer = 0
WHILE Button_5 = Is_Pressed
Led = Is_Turned_On
Timer = Timer + 1
IF Timer = 4 THEN Timer = 1
lcdout $fe, 1, "Release Sw When ? #",DEC Timer
LCDOut $fe, $c0, "If # = 1 then X10-3 "
lcdout $fe,$94, "If # = 2 then X10-4 "
lcdout $fe,$d4,"If # = 3 then X10-5 "
PAUSE 1000
WEND
Led = Is_Turned_Off
PAUSE 100 ' Debounce Push Button
IF Timer = 1 THEN GOSUB Turn_On_Or_Off_3
IF Timer = 2 THEN GOSUB Turn_On_Or_Off_4
IF Timer = 3 THEN GOSUB Turn_On_Or_Off_5
ENDIF

LCDOut $fe, 1, "X10Temp Demo Program"
LCDOut $fe, $c0," Jessey Montgomery "
lcdout $fe,$94,"#3 = ",DEC Out_To_877_Pin_38, _
" #4 = ",DEC Out_To_877_Pin_37," #5 = ",DEC Out_To_877_Pin_39
lcdout $fe,$d4
LCDOut " Temp = "
if temp.15 = 1 then LCDOut "-"
temp = ABS(temp)
LCDOut DEC temp / 100,0
LCDOut "F "

Pause 100

IF a = 0 THEN MainLoop ' a always equals zero, use instead of GOTO...

End

Darrel Taylor
- 26th April 2009, 04:14
Hi Jessey,

On the 16F688, RA3 is Input Only when MCLR is disabled.

The Pin needs to have output capability to talk to a DS1820.

hth,

jessey
- 26th April 2009, 05:09
Hi Darrel,

I didn't realize that, thanks for the heads up. That might also explain why I couldn't get my FireCracker code to work on the 688 a while back.

Thanks Again
jessey

jessey
- 28th April 2009, 06:56
Hi Darrel,

When I try and compile the firecracker code below using your EE_Vars.PBP I get an error saying:
" 3.asm 141: Overwriting previous address contents (2100) "

I'd sure like to be able to use your EE_Vars.PBP because it has some neat features and is really easy to implement. Possibly the reason it won't compile has something to do with declaring the Data statements for the firecracker taking up some of the same address's as EE_Vars.PBP is using? I tried using EEPROMData100 var BYTE for X and EEPROMData101 var BYTE for Y and EEPROMData102 var BYTE for Z and it seems to be working good but I like your EE_Vars way better.

Also the firecracker code won't work on the 16f688 when I do get it to compile without using your EE_Vars.PBP but the exact same code works perfectly on my 16f877 without your EE_Vars.PBP? I'll try changing the dtr & rts port pins but I can't really see them causing a problem. Any help would really be appreciated.

Thanks
jessey




'************************************************* ******************************
'* Name : Test Program for the Firecracker Interface *
'* Author : Jessey Montgomery *
'* Includes: Darrel Taylor's EE_Vars.PBP & Al Williams Firecracker Interface *
'* Date : April 24/th 2009 *
'* Version : Using PicBasic Pro Ver 2.50, MPASM Ver 5.20, Using MicroCode *
'* : Studio 3.0.0.5, PICkit 2 programmer - Application Version *
'* : 2.50.02 - Device File Version 1.5100 - OS Firmware Version *
'* : 2.30.01. Using a Pic16F688-I/P 04470AY. *
'************************************************* ******************************


'(1) Comments & Objectives
' =====================
' this program is just a test program for checking the firecracker

' Clear Each Port Before Setting The TRIS Registers

Clear ' Set all ram registers to zero
PORTA = 0
PORTC = 0

' VSS VDD MCLR Ect. Pins

'VDD + '(pin 1)
'VSS - '(pin 14)

' SET THE TRIS

' PORTA PINS

TRISA.0 = 0 '(pin 13) connect green wire of firecracker - dtr VAR PORTA.0
TRISA.1 = 0 '(pin 12) connect red wire of firecracker - rts VAR PORTA.1
TRISA.2 = 0 '(pin 11) Not Used VAR PORTA.2
TRISA.3 = 1 '(pin 4) Button_5 VAR PORTA.3
TRISA.4 = 0 '(pin 3) Not Used VAR PORTA.4
TRISA.5 = 0 '(pin 2) Led VAR PORTA.5

' PORTC PINS

TRISC.0 = 0 '(pin 10) Blue Lcd Data bit 4
TRISC.1 = 0 '(pin 9) White Lcd Data bit 5
TRISC.2 = 0 '(pin 8) Green Lcd Data bit 6
TRISC.3 = 0 '(pin 7) Orange Lcd Data bit 7
TRISC.4 = 0 '(pin 6) Orange/Black Lcd Register select
TRISC.5 = 0 '(pin 5) Blue/White Lcd Enable bit

'(2) PIC Config Fuse Definitions for 16f688
@ __CONFIG _INTOSCIO & _WDT_ON & _MCLRE_OFF & _CP_OFF
@ ERRORLEVEL -306; ' turns off cross page boundary message alerts after compile

'(3) PIC Hardware Definitions (ie Pin-Out & Port Assignments)
ANSEL = 0 ' all pins are digital
CMCON0 = 7 ' turn off comparators

' Data for house and unit codes
housetbl data $60,$70,$40,$50,$80,$90,$A0,$B0,$E0,$F0,$C0,$D0
data $00,$10,$20,$30
unittbl data 0,$10,$8,$18,$40,$50,$48,$58

DEFINE LCD_RSREG PORTC' LCD Register Select Port
DEFINE LCD_RSBIT 4 ' LCD Register Select
DEFINE LCD_EREG PORTC ' LCD Enable Port
DEFINE LCD_EBIT 5 ' LCD Enable
DEFINE LCD_DREG PORTC ' LCD Data Port
DEFINE LCD_DBIT 0 ' LCD data starting bit 0 or 4

Button_5 VAR PORTA.3 ' pin 4
Led VAR PORTA.5 ' pin 2
' The 3 connections to the firecracker are below...
' Wire Ground (DB9 pin 5) black
dtr VAR PORTA.0 ' pin 13 connects to DB9 pin 7 of the firecracker (green wire)
rts VAR PORTA.1 ' pin 12 connects to DB9 pin 4 of the firecracker (red wire)

a VAR BIT
a = 0
X VAR BYTE
X = 0
Y VAR BYTE
Y = 0
Z VAR BYTE
Z = 0
Timer VAR byte
Timer = 0

' byte to send
byt var byte
tmp var byte
i var BYTE 'was a nib
state var bit(4)
house var byte '0=A 1=B...
unit var BYTE ' 0-15 (must be 0 for bright/dim) 'was a nib
cmd var BYTE ' 0=off 1=on 2=bright 3=dim 'was a nib

Is_Pressed CON 0
Is_Not_Pressed CON 1
Is_Turned_On CON 1
Is_Turned_Off CON 0
' The Firecracker header (2 bytes)
h1 con $D5
h2 con $AA
' the Firecracker footer (1 byte)
foot con $AD

'4a. EEPROM Assignments

' INCLUDE "EE_Vars.PBP"

' Declare The EE Variable - Load A Default Setting For The EE Variable

' X VAR BYTE ': @ EE_var _X, BYTE,0
' Y VAR BYTE ': @ EE_var _Y, BYTE,0
' Z VAR BYTE ': @ EE_var _Z, BYTE,0

' To write to the eeprom @ EE_write_var _X
' @ EE_write_var _Y
' @ EE_write_var _Z

gosub resetfirecracker

IF X = 1 THEN
house = 0 : unit = 4 : cmd = 1 'when unit = 4 in code, set X10 receiver to 5
gosub sendcmd
ELSE
house = 0 : unit = 4 : cmd = 0 'when unit = 4 in code, set X10 receiver to 5
gosub sendcmd
ENDIF

IF Y = 1 THEN
house = 0 : unit = 2 : cmd = 1 'when unit = 2 in code, set X10 receiver to 3
gosub sendcmd
ELSE
house = 0 : unit = 2 : cmd = 0 'when unit = 2 in code, set X10 receiver to 3
gosub sendcmd
ENDIF

IF Z = 1 THEN
house = 0 : unit = 3 : cmd = 1 'when unit = 3 in code, set X10 receiver to 4
gosub sendcmd
ELSE
house = 0 : unit = 3 : cmd = 0 'when unit = 3 in code, set X10 receiver to 4
gosub sendcmd
ENDIF
Led = Is_Turned_Off

PAUSE 500 ' For LCD

IF a = 0 THEN MainLoop ' a always equals zero, use instead of GOTO...


' Firecracker Interface (Al Williams)
' http://www.awce.com/firecracker.htm or http://www.al-williams.com/awce
sendcmd:
byt=h1
gosub sendbyte
byt=h2
gosub sendbyte
read housetbl+house,byt
if unit<9 then lowunit
byt=byt+4
lowunit:
gosub sendbyte
byt=$20
if cmd=0 then addunit
byt=0
if cmd=1 then addunit
byt=$88

if cmd=2 then nounit
byt=$98
if cmd=3 then nounit
' huh???

addunit:
read unittbl+(unit//8),tmp
byt=byt+tmp
nounit:
gosub sendbyte
byt=foot
gosub sendbyte
return

' Send 1 raw byte
sendbyte:
debug hex byt," "
for i=0 to 7
if (byt & $80) = $80 then xmit1
gosub send0
nextbit:
pause 1
byt=byt*2
next
return

' Send a 1
xmit1:
gosub send1
IF a = 0 THEN nextbit

' Send bits (0 or 1)
send0:
low rts
pause 1
high rts
return

send1:
low dtr
pause 1
high dtr
return

'Reset firecracker first
resetfirecracker:
low rts
low dtr
pause 50
high rts
high dtr
pause 50
return


Turn_On_Or_Off_4:
if X = 0 then ' X toggles the #4 X10 module On/Off with each button press
X = 1 ' Turn On #5 X10 module
house = 0 : unit = 4 : cmd = 1 'when unit=4 in code,set X10 receiver to 5
gosub sendcmd : pause 1000
ELSE
X = 0 ' Turn Off #5 X10 module
house = 0 : unit = 4 : cmd = 0 'when unit=4 in code,set X10 receiver to 5
gosub sendcmd : pause 1000
ENDIF
' @ EE_write_var _X ' remember X in case of a power failure
RETURN

Turn_On_Or_Off_3:
if Y = 0 then ' Y toggles the #3 X10 module On/Off with each button press
Y = 1 ' Turn On #3 X10 module
house = 0 : unit = 2 : cmd = 1 'when unit=2 in code,set X10 receiver to 3
gosub sendcmd : pause 1000
ELSE
Y = 0 ' Turn Off #3 X10 module
house = 0 : unit = 2 : cmd = 0 'when unit=2 in code,set X10 receiver to 3
gosub sendcmd : pause 1000
ENDIF
' @ EE_write_var _Y ' remember Y in case of a power failure
RETURN

Turn_On_Or_Off_5:
if Z = 0 then ' Z toggles the #4 X10 module On/Off with each button press
Z = 1 ' Turn On #4 X10 module
house = 0 : unit = 3 : cmd = 1 'when unit=3 in code,set X10 receiver to 4
gosub sendcmd : pause 1000
ELSE
Z = 0 ' Turn Off #4 X10 module
house = 0 : unit = 3 : cmd = 0 'when unit=3 in code,set X10 receiver to 4
gosub sendcmd : pause 1000
ENDIF
' @ EE_write_var _Z ' remember Z in case of a power failure
RETURN


MainLoop:

IF Button_5 = Is_Pressed THEN
Timer = 0
WHILE Button_5 = Is_Pressed
Led = Is_Turned_On
Timer = Timer + 1
IF Timer = 4 THEN Timer = 1
lcdout $fe, 1, "Release Sw When ? #",DEC Timer
LCDOut $fe, $c0, "If # = 1 then X10-3 "
lcdout $fe,$94, "If # = 2 then X10-4 "
lcdout $fe,$d4,"If # = 3 then X10-5 "
PAUSE 1000
WEND
Led = Is_Turned_Off
PAUSE 100 ' Debounce Push Button
IF Timer = 1 THEN GOSUB Turn_On_Or_Off_3
IF Timer = 2 THEN GOSUB Turn_On_Or_Off_4
IF Timer = 3 THEN GOSUB Turn_On_Or_Off_5
ENDIF

LCDOut $fe, 1, "X10 FireCracker Demo"
LCDOut $fe, $c0," Jessey Montgomery "
lcdout $fe,$94,"#3 = ",DEC Y, _
" #4 = ",DEC X," #5 = ",DEC Z

Pause 100

IF a = 0 THEN MainLoop ' a always equals zero, use instead of GOTO...

End

Darrel Taylor
- 28th April 2009, 08:17
... Possibly the reason it won't compile has something to do with declaring the Data statements for the firecracker taking up some of the same address's as EE_Vars.PBP is using?
Yup, I think that's it too.

$2100 is the first physical address in EEPROM.
When you housetbl data $60, ..., it stores data at location 0. But it's really at 2100h.
So does EEPROM EE_Start, [0]

Both your program and EE_Vars are trying to define the same location in EEPROM.

If you open the EE_Vars.pbp file ...
The first line is ...


EE_start CON 0 ; Starting address for EE_Vars

Change that to somewhere past the locations used for the existing data.
Then they shouldn't conflict anymore.
Or you can comment that line, and copy it to your main program. That'll keep it from using the wrong location the next time you use the include file.

As for the firecracker not working.
All those EEPROM locations would have been overwritten by EE_Vars.

If they don't overlap, that won't happen.
<br>

jessey
- 28th April 2009, 09:55
Hi Darrel,

I opened EE_Vars.PBP in MicroCodeStudio and changed the CON 0 TO CON 91 then saved the file and closed it. Then I compiled the firecracker code with the INCLUDE "EE_Vars.PBP" which compiled fine. I then programmed the code into the 688 and although the X, Y and Z variables are being saved when power is removed and re-applied to the circuit the firecracker code still isn't communicating with the firecracker transmitter and the X10 modules still won't turn On/Off?

Also, I tried commenting out the first line in EE_Vars.PBP then pasted it into my program with the starting address set to 91 as you suggested and when I try to compile I get an " ERROR Line 24: Redefinition of CON EE_start.(EE_Vars.PBP) ".

I'll try programming a new 688 micro tomorrow, could it be that my eeprom is corrupted now?

Thanks
jessey

jessey
- 5th May 2009, 09:09
Hi Darrel,

I tried programming a new chip with ee_vars.pbp and I got the same results as before but I got the firecracker code working good using the saving routines below:

EEPROMData91 var BYTE
X VAR EEPROMData91
READ 91, EEPROMData91

'' To write to the eprom
'' ================
'EEPROMData91 = X
'Write 91, EEPROMData91
'-----------------------

EEPROMData92 var BYTE
Y VAR EEPROMData92
READ 92, EEPROMData92

'' To write to the eprom
'' ================
'EEPROMData92 = Y
'Write 92, EEPROMData92
'-----------------------

EEPROMData93 var BYTE
Z VAR EEPROMData93
READ 93, EEPROMData93

'' To write to the eprom
'' =================
'EEPROMData93 = Z
'Write 93, EEPROMData93
''-----------------------

I tried changing the starting address in EE_Vars.pbp from CON 0 to CON 91 and although it compiles it doesn't work but when I comment out your eeprom saving routine and use the program below, the X10 code turns On and Off the X10 appliance modules and saving to the eeprom works like a charm.

As I mentioned in my previous post, when I comment out the con 0 in EE_Vars.pbp and include it in my program as con 91 I get " ERROR Line 24: Redefinition of CON EE_start.(EE_Vars.PBP) " when I try to compile.

It was only a guess on my part that the firecracker code was using some of the same address's as EE_Vars.PBP was using. I still don't quite understand mathematically how the address's of the various eeprom and other registry locations work using the Decimal, Hex and Binary number Systems. I should have stayed in school instead of being a juvenile delinquent then this would be a lot easier but I guess it's better late than never to get a little savy and this is interesting! Any ideas on what's happening with this?

Thanks
jessey




'* Name : Test Program for the Firecracker Interface
'* Author : Jessey Montgomery
'* Includes: Al Williams Firecracker Interface
'* Date : April 24/th 2009
'* Version : Using PicBasic Pro Ver 2.50, MPASM Ver 5.20, Using MicroCode
'* : Studio 3.0.0.5, PICkit 2 programmer - Application Version
'* : 2.50.02 - Device File Version 1.5100 - OS Firmware Version
'* : 2.30.01. Using a Pic16F688-I/P 04470AY.


'(1) Comments & Objectives
' =====================
' this program is just a test program for checking the firecracker

' Clear Each Port Before Setting The TRIS Registers

Clear ' Set all ram registers to zero
PORTA = 0
PORTC = 0

' VSS VDD Pins

'VDD + '(pin 1)
'VSS - '(pin 14)

' SET THE TRIS

' PORTA PINS

TRISA.0 = 0 '(pin 13) Not Used VAR PORTA.0
TRISA.1 = 0 '(pin 12) ..........................................Led VAR PORTA.1
TRISA.2 = 1 '(pin 11) ..................................Push_Button VAR PORTA.2
TRISA.3 = 0 '(pin 4) Not Used VAR PORTA.3
TRISA.4 = 0 '(pin 3) .....connect to red wire of firecracker - dtr VAR PORTA.4
TRISA.5 = 0 '(pin 2) ...connect to green wire of firecracker - rts VAR PORTA.5

' PORTC PINS

TRISC.0 = 0 '(pin 10) Not Used VAR PORTC.0
TRISC.1 = 0 '(pin 9) Not Used VAR PORTC.1
TRISC.2 = 0 '(pin 8) Not Used VAR PORTC.2
TRISC.3 = 0 '(pin 7) Not Used VAR PORTC.3
TRISC.4 = 0 '(pin 6) Not Used VAR PORTC.4
TRISC.5 = 0 '(pin 5) Not Used VAR PORTC.5


'(2) PIC Config Fuse Definitions for 16f688
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF & _BOD_ON
@ ERRORLEVEL -306;

'(3) PIC Hardware Definitions (ie Pin-Out & Port Assignments)
ANSEL = 0 ' all pins are digital
CMCON0 = 7 ' turn off comparators
OSCCON.3 = 1 ' Use internal clock
OSCCON = %1110111 ' Use Internal OSC at 8MHz

Push_Button VAR PORTA.2 ' pin 11
Led var PORTA.1 ' pin 12

' Firecracker Interface (Al Williams)
rts VAR PORTA.5 ' pin 2 connects to DB9 pin 4 of the firecracker (green wire)
dtr VAR PORTA.4 ' pin 3 connects to DB9 pin 7 of the firecracker (red wire)

a VAR BYTE
a = 0
Timer VAR byte
Timer = 0

' byte to send
byt var byte
tmp var byte
i var BYTE 'was a nib
state var bit(4)
house var byte '0=A 1=B...
unit var BYTE ' 0-15 (must be 0 for bright/dim) 'was a nib
cmd var BYTE ' 0=off 1=on 2=bright 3=dim 'was a nib

Is_Pressed CON 0
Is_Not_Pressed CON 1
Is_Turned_On CON 1
Is_Turned_Off CON 0

h1 con $D5
h2 con $AA
foot con $AD

'4a. EEPROM Assignments
' ==================
' Data for house and unit codes
housetbl data $60,$70,$40,$50,$80,$90,$A0,$B0,$E0,$F0,$C0,$D0
data $00,$10,$20,$30
unittbl data 0,$10,$8,$18,$40,$50,$48,$58

EEPROMData91 var BYTE
X VAR EEPROMData91
READ 91, EEPROMData91
'' To write to the eprom
'' =====================
'EEPROMData91 = X
'Write 91, EEPROMData91
'------------------------
EEPROMData92 var BYTE
Y VAR EEPROMData92
READ 92, EEPROMData92
'' To write to the eprom
'' =====================
'EEPROMData92 = Y
'Write 92, EEPROMData92
'------------------------
EEPROMData93 var BYTE
Z VAR EEPROMData93
READ 93, EEPROMData93
'' To write to the eprom
'' =====================
'EEPROMData93 = Z
'Write 93, EEPROMData93
''-------------------------

gosub resetfirecracker

' Reset the three X10 modules to the states they were before the power-loss

IF X = 1 THEN
house = 0 : unit = 4 : cmd = 1 'when unit = 4 in code, set X10 receiver to 5
gosub sendcmd : PAUSE 1000
ELSE
house = 0 : unit = 4 : cmd = 0
gosub sendcmd : PAUSE 1000
ENDIF

IF Y = 1 THEN
house = 0 : unit = 2 : cmd = 1 'when unit = 2 in code, set X10 receiver to 3
gosub sendcmd : PAUSE 1000
ELSE
house = 0 : unit = 2 : cmd = 0
gosub sendcmd : PAUSE 1000
ENDIF

IF Z = 1 THEN
house = 0 : unit = 3 : cmd = 1 'when unit = 3 in code, set X10 receiver to 4
gosub sendcmd : PAUSE 1000
ELSE
house = 0 : unit = 3 : cmd = 0
gosub sendcmd : PAUSE 1000
ENDIF
Led = Is_Turned_On
PAUSE 200
Led = Is_Turned_Off

IF a = 0 THEN MainLoop ' a always equals zero, use instead of GOTO...

' Start Of Subroutines Area Below
' ===============================

' Firecracker Interface by (Al Williams)
' http://www.awce.com/firecracker.htm
sendcmd:
byt=h1
gosub sendbyte
byt=h2
gosub sendbyte
read housetbl+house,byt
if unit<9 then lowunit
byt=byt+4
lowunit:
gosub sendbyte
byt=$20
if cmd=0 then addunit
byt=0
if cmd=1 then addunit
byt=$88

if cmd=2 then nounit
byt=$98
if cmd=3 then nounit
' huh???

addunit:
read unittbl+(unit//8),tmp
byt=byt+tmp
nounit:
gosub sendbyte
byt=foot
gosub sendbyte
return

' Send 1 raw byte
sendbyte:
debug hex byt," "
for i=0 to 7
if (byt & $80) = $80 then xmit1
gosub send0
nextbit:
pause 1
byt=byt*2
next
return

' Send a 1
xmit1:
gosub send1
IF a = 0 THEN nextbit

' Send bits (0 or 1)
send0:
low rts
pause 1
high rts
return

send1:
low dtr
pause 1
high dtr
return

' Always reset firecracker first
resetfirecracker:
low rts
low dtr
pause 50 ' reset
high rts
high dtr
pause 50
return
'============'

Turn_On_Or_Off_4:
if X = 0 then ' X toggles the #4 X10 module On/or/Off with each button press
X = 1 ' Turn On
house = 0 : unit = 4 : cmd = 1 'when unit=4 in code,set X10 receiver to 5
gosub sendcmd
ELSE
X = 0 ' Turn Off
house = 0 : unit = 4 : cmd = 0
gosub sendcmd
ENDIF
EEPROMData91 = X
Write 91, EEPROMData91
'@ EE_write_var _X
RETURN

Turn_On_Or_Off_3:
if Y = 0 then ' Y toggles the #3 X10 module On/or/Off with each button press
Y = 1 ' Turn On
house = 0 : unit = 2 : cmd = 1 'when unit=2 in code,set X10 receiver to 3
gosub sendcmd
ELSE
Y = 0 ' Turn Off
house = 0 : unit = 2 : cmd = 0
gosub sendcmd
ENDIF
EEPROMData92 = Y
Write 92, EEPROMData92
'@ EE_write_var _Y
RETURN

Turn_On_Or_Off_5:
if Z = 0 then ' Z toggles the #4 X10 module On/or/Off with each button press
Z = 1 ' Turn On
house = 0 : unit = 3 : cmd = 1 'when unit=3 in code,set X10 receiver to 4
gosub sendcmd
ELSE
Z = 0 ' Turn Off
house = 0 : unit = 3 : cmd = 0
gosub sendcmd
ENDIF
EEPROMData93 = Z
Write 93, EEPROMData93
'@ EE_write_var _Z
RETURN

'XXXXXXXXXXX'

MainLoop:

IF Push_Button = Is_Pressed THEN
Timer = 0
WHILE Push_Button = Is_Pressed
Timer = Timer + 1
IF Timer = 1 THEN Led = Is_Turned_On
IF Timer = 2 THEN Led = Is_Turned_Off
IF Timer = 3 THEN Led = Is_Turned_On
IF Timer = 4 THEN Led = Is_Turned_Off
IF Timer = 5 THEN Led = Is_Turned_On
IF Timer = 6 THEN Led = Is_Turned_Off
PAUSE 500
WEND
Led = Is_Turned_Off
PAUSE 100 ' Debounce
IF Timer = 1 THEN GOSUB Turn_On_Or_Off_3
IF Timer = 2 THEN GOSUB Turn_On_Or_Off_3
IF Timer = 3 THEN GOSUB Turn_On_Or_Off_4
IF Timer = 4 THEN GOSUB Turn_On_Or_Off_4
IF Timer = 5 THEN GOSUB Turn_On_Or_Off_5
IF Timer >= 6 THEN GOSUB Turn_On_Or_Off_5
ENDIF

IF a = 0 THEN MainLoop

End

Darrel Taylor
- 6th May 2009, 04:50
Well, since brittons (in the EE_vars thread) appeared to have a similar problem changing the EE_start location ... I figured I better take another look.

So I did.
I've checked it starting at the default 0 location in EEPROM, and changed EE_start to several other values, and added DATA statements to duplicate your situation ... all with the same results.
I does exactly what it's supposed to do. The EE_var locations move to the appropriate places and everything works fine.

I don't have a Firecracker, so can't test that part.

The only thing I can see in your code, is that you've set the internal oscillator to 8Mhz, but didn't put a DEFINE OSC 8 in there. PBP will default to 4Mhz and all those pauses will only be half as long.

That doesn't explain ANY of the described problems, but it needs to be fixed before you'll be able to see the REAL problem.
<br>

jessey
- 11th May 2009, 15:05
Thanks Darrel,

I did notice that the pauses weren't quite right and after adding the define it works good now. I still couldn't get the EE_vars to work, what starting address did you use in your test code?

Regards
jessey

Darrel Taylor
- 12th May 2009, 07:42
what starting address did you use in your test code?
I tried lots of different address (including 91).
As long as the data statements came before the include (yours does), and the areas didn't overlap, everything was fine.

Of course, I'm using a 16F877.
I don't see any reason why the 688 should be different.

But anyhow, as long as what you have works, why bother looking any further.
<br>

jessey
- 18th May 2009, 05:56
Hi Darrel,

Yes that is strange that it works on the 877 but not on the 688. One of the things that I really like about your EEvars is that you can declare a default value for a variable that is initiated on the first power-up. It's nice to know what the value of a variable is at first power-up especially if it's used to execute a subroutine but I guess I could do some sort of a work a round for that in the code.

Is there a way to declare a default value using EEPROMData?

Thanks
jessey

Darrel Taylor
- 18th May 2009, 06:15
Is there a way to declare a default value using EEPROMData?


Not directly.

But if your data can NEVER include the value 255 ($FF), then yes.

On power up, read the value from EEPROM.
If it's 255, write the "default" value.
Then if you ever need to restore the default value, set the location to $FF and reset.

Unfortunately, some bootloaders have chosen to "erase" EEPROM values to 0 instead of the $FF done by hardware. This can be problematic.
As long as you know which way your programmer works, it's a definite possibility.

ADDED: You can also handle it the same way EE_vars does and create a location that indicates which EEPROM locations have been restored. If the value of that location is less than or equal to (<=) the address being tested, or $FF, restore the default value and only increment the counter AFTER a successful and verified WRITE.
This way 0, $FF and anything less than the address being tested indicates the location needs to be restored.

jessey
- 19th May 2009, 06:56
Unfortunately, some bootloaders have chosen to "erase" EEPROM values to 0 instead of the $FF done by hardware. This can be problematic. As long as you know which way your programmer works, it's a definite possibility.


Hi Darrel,

Thanks for your most welcomed input as it got me thinking on the right path (I'm hoping). I haven't checked yet if my pickit 2 programmer erases the EEPROM values to 0 or 255 when programming but with the code below it wouldn't really matter. I haven't tried this work a round yet but I can't think of any reasons why it wouldn't work.

Thanks Again
jessey



'(2) PIC Config Fuse Definitions for 16f688
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF & _BOD_ON
@ ERRORLEVEL -306

' Data for house and unit codes
housetbl data $60,$70,$40,$50,$80,$90,$A0,$B0,$E0,$F0,$C0,$D0
data $00,$10,$20,$30
unittbl data 0,$10,$8,$18,$40,$50,$48,$58

EEPROMData94 var BYTE
Set_Point VAR EEPROMData94
READ 94, EEPROMData94

IF Set_Point = 255 THEN
Set_point = 150 ' Set a default value
EEPROMData94 = Set_Point
Write 94, EEPROMData94
ENDIF

IF Set_Point = 0 THEN
Set_point = 150 ' Set a default value
EEPROMData94 = Set_Point
Write 94, EEPROMData94
ENDIF

GOTO Mainloop


My set point doesn't ever need to be set to 0 or 255 in my program and this routine would prevent the user from setting either of the two there.


Adjust_The_Set_Point:
Loop1:
IF Push_Button1 = Is_Pressed THEN
IF Set_Point < 255 THEN Set_Point = Set_Point + 1 : Save_Var = Yes
IF Set_Point > 254 THEN Set_Point = 254 ' 255 is not allowed
ENDIF

IF Push_Button2 = Is_Pressed THEN
Set_Point = Set_Point - 1 : Save_Var = Yes
IF Set_Point < 2 THEN Set_Point = 1 ' 0 is not allowed
ENDIF

IF Push_Button1 = Is_Not_Pressed THEN
IF Push_Button2 = Is_Not_Pressed THEN
RETURN
ENDIF
ENDIF

Pause 100

GOSUB Update_The_Lcd ' just in case a button is held down

IF a = 0 THEN Loop1 ' a always equals zero, use instead of GOTO...

RETURN

Darrel Taylor
- 23rd May 2009, 05:07
Hey jessey,

I guess it's just a matter of semantics, but you may find this interesting.

<font color="#000000"><b>SetPoint </b><font color="#008000"><b>VAR BYTE </b></font>: <b>SetPoint_DEFAULT </b><font color="#008000"><b>CON </b></font><font color="#800000"><b>150
</b></font><b>Threshold </b><font color="#008000"><b>VAR BYTE </b></font>: <b>Threshold_DEFAULT </b><font color="#008000"><b>CON </b></font><font color="#800000"><b>5

</b></font><font color="#008000"><b>DATA </b></font>@<font color="#800000"><b>94 </b></font><font color="#0000FF"><b><i>; Start this block of data at EE addr 94
</i></b></font><b>EE_SetPoint </b><font color="#008000"><b>DATA </b></font><b>SetPoint_DEFAULT
EE_Threshold </b><font color="#008000"><b>DATA </b></font><b>Threshold_DEFAULT

</b><font color="#008000"><b>READ </b></font><b>EE_SetPoint</b>, <b>SetPoint </b><font color="#0000FF"><b><i>; read EE values on powerup
</i></b></font><font color="#008000"><b>READ </b></font><b>EE_Threshold</b>, <b>Threshold
</b><font color="#008000"><b>GOTO </b></font><b>Main

</b><font color="#0000FF"><b><i>;----[Save/Restore EEPROM values]------------------------------------------
</i></b></font><b>Save_SetPoint</b>: <font color="#0000FF"><b><i>; write current SetPoint to EEPROM
</i></b></font><font color="#008000"><b>WRITE </b></font><b>EE_SetPoint</b>, <b>SetPoint
</b><font color="#008000"><b>RETURN
</b></font><b>Default_SetPoint</b>: <font color="#0000FF"><b><i>; restore default SetPoint value
</i></b></font><font color="#008000"><b>WRITE </b></font><b>EE_SetPoint</b>, <b>SetPoint_DEFAULT
</b><font color="#008000"><b>READ </b></font><b>EE_SetPoint</b>, <b>SetPoint
</b><font color="#008000"><b>RETURN
</b></font><font color="#0000FF"><b><i>;-----------------
</i></b></font><b>Save_Threshold</b>: <font color="#0000FF"><b><i>; write current Threshold to EEPROM
</i></b></font><font color="#008000"><b>WRITE </b></font><b>EE_Threshold</b>, <b>Threshold
</b><font color="#008000"><b>RETURN
</b></font><b>Default_Threshold</b>: <font color="#0000FF"><b><i>; restore default Threshold value
</i></b></font><font color="#008000"><b>WRITE </b></font><b>EE_Threshold</b>, <b>Threshold_DEFAULT
</b><font color="#008000"><b>READ </b></font><b>EE_Threshold</b>, <b>Threshold
</b><font color="#008000"><b>RETURN
</b></font><font color="#0000FF"><b><i>;-----------------
</i></b></font><b>Save_ALL</b>: <font color="#0000FF"><b><i>; save ALL values to EEPROM
</i></b></font><font color="#008000"><b>GOSUB </b></font><b>Save_SetPoint
</b><font color="#008000"><b>GOSUB </b></font><b>Save_Threshold
</b><font color="#008000"><b>RETURN
</b></font><b>Default_ALL</b>: <font color="#0000FF"><b><i>; restore ALL default values
</i></b></font><font color="#008000"><b>GOSUB </b></font><b>Default_SetPoint
</b><font color="#008000"><b>GOSUB </b></font><b>Default_Threshold
</b><font color="#008000"><b>RETURN

</b></font><b>Main</b>: