CocaColaKid
- 11th June 2008, 18:22
Ok here's what I've come across and for the life of me I can't see what the problem is. I'm trying to build a timer for cycle testing things. What's happening is when I run the code with the red lines remarked out it works fine. If the lines are included the timer never seems to activate the relay. Well it does but so quick the relay can't even response fast enough. Any ideas?
'************************ 16F628A Configuration Fuses **************************
@ __CONFIG _BOREN_ON & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_ON & _LVP_OFF & _MCLRE_OFF & _INTRC_OSC_NOCLKOUT
' Brown-Out Detect - _BODEN_OFF, _BODEN_ON, _BOREN_OFF, _BOREN_ON
' Code Protection - _CP_OFF, CP_ON
' Data Code Protect - _DATA_CP_OFF, DATA_CP_ON
' Power-On Timer - _PWRTE_OFF, _PWRTE_ON
' Watchdog Timer - _WDT_OFF, _WDT_ON
' Low-Voltage Program - _LPV_OFF, _LVP_ON
' RA5/MCLR Function - _MCLRE_OFF, _MCLRE_ON
' Oscillator Selection - _RC_OSC_CLKOUT, _RC_OSC_NOCLKOUT, _ER_OSC_CLKOUT,
' _ER_OSC_NOCLKOUT, _INTOSC_OSC_CLKOUT, _INTOSC_OSC_NOCLKOUT,
' _INTRC_OSC_CLKOUT, _INTRC_OSC_NOCLKOUT, _EXTCLK_OSC,
' _HS_OSC, _XT_OSC, _LP_OSC
'
'************************************************* ******************************
'---------------------------- Port Assignments --------------------------------
' porta.0 Output 3 LED
' porta.1 Output 1 LED
' porta.2 Run LED
' porta.3 Output 2 LED
' porta.4 Unused
' porta.5 Unused
' porta.6 Unused
' porta.7 Unused
' portb.0 Input 3
' portb.1 Input 2
' portb.2 Input 1
' portb.3 HPWM Output
' portb.4 Up Button
' portb.5 Down Button
' portb.6 Program Button
' portb.7 Debug Output
'---------------------------- EEPROM Locations --------------------------------
' 0 Output 1 Setting
' 1 Output 2 Setting
' 2 Output 3 Setting
'---------------------------- Debug Settings ----------------------------------
DEFINE DEBUG_REG PORTB ' Debug port
DEFINE DEBUG_BIT 4 ' Debug port bit
DEFINE DEBUG_BAUD 9600 ' Debug baud rate
DEFINE DEBUG_MODE 1 ' Debug mode 1 = inverted
DEFINE DEBUG_PACING 300 ' Delay 'in micro sec' between characters sent
i con 254 ' Control byte
clr con 1 ' Clear the display
line1 con 128 ' Move cursor to home position on line 1
line2 con 192 ' Move cursor to home position on line 2
'------------------------------- Variables -------------------------------------
StillPressed var bit
x var byte
Output1 var word
Output2 var word
Output3 var word
StartupDelay var byte
TimeOut var byte
Mode var byte ' Timer mode - 0 = 1-Shot Off Start
' 1 = 1-Shot On Start
' 2 = Repeating Off Start
' 3 = Repeating On Start
OffTimeUnits var byte[4]
OnTimeUnits var byte[4]
OffTime var word ' Off time dependant on units selected
OnTime var word ' On time dependant on units selected
OffTimeUnitSelect var byte ' Off time unit selector
OnTimeUnitSelect var byte ' On time unit selector
SetOnTime var word ' Programmed time to be on
SetOffTime var word ' Programmed time to be off
RelayOutput var bit ' Flag indicated relay status
'------------------------ Miscellaneous Variables ------------------------------
' porta.0
' porta.1
' porta.2
' porta.3
' porta.4
' porta.5
' portb.0
' portb.1
ProgramButton var portb.2
' portb.3
' portb.4
' portb.5
' portb.6
Relay var portb.7
'--------------------------- Port Initialization ------------------------------
TRISA = %10110000
TRISB = %11110111
CMCON = %00000111 ' Disable comparator
CCP1CON = %00000000 ' Enable PWM mode
OPTION_REG.7 = 0 ' Enable portb pull-ups
RCSTA.7 = 0 ' Disable serial port
DEFINE OSC 4
'----------------------------- Main Program Loop -------------------------------
data 65,129,194
include "Elapsed.pbp"
gosub ResetTime ' Reset Time to 0d-00:00:00.00
pause 250
clear
OffTimeUnitSelect = 0 ' 0=secs,1=mins,2=hrs,3=days,4=weeks
OnTimeUnitSelect = 0 ' 0=secs,1=mins,2=hrs,3=days,4=weeks
Mode = 2 ' 0=0-Shot Off Start
' 1=1-Shot On Start
' 2=Repeating Off Start
' 3=Repeating On Start
SetOffTime = 30 ' 30 seconds off
SetOnTime = 50 ' 50 seconds on
RelayOutput = 0 ' Relay is off
StartMenu:
Start:
gosub StartTimer ' Start the Elapsed Timer
gosub InitialState
if Mode = 1 then OneShotOnStart
if (Mode = 2) or (Mode = 3) then Repeating
OneShotOffStart:
gosub OffUnits
gosub OnUnits
if OnTime => SetOnTime then
low Relay
RelayOutput = 1
gosub StopTimer
gosub ResetTime
end
goto StartMenu
endif
goto OneShotOffStart
OneShotOnStart:
gosub OffUnits
gosub OnUnits
if OffTime => SetOffTime then
high Relay
RelayOutput = 0
gosub StopTimer
gosub ResetTime
end
goto StartMenu
endif
goto OneShotOnStart
Repeating: ' Repeating mode
gosub OffUnits
gosub OnUnits
if (OffTime => SetOffTime) and (RelayOutput = 1) then
lcdout $fe,1,"TIME LEFT ON "
gosub DisplayData
high Relay
RelayOutput = 0
gosub ResetTime
goto Repeating
endif
if (OnTime => SetOnTime) and (RelayOutput = 0) then
lcdout $fe,1,"TIME LEFT OFF "
gosub DisplayData
low Relay
RelayOutput = 1
gosub ResetTime
goto Repeating
endif
gosub DisplayData
goto repeating
InitialState:
if (Mode = 1) or (Mode = 3) then
RelayOutput = 1 ' Turn relay on
low Relay
else
RelayOutput = 0 ' Turn relay off
high Relay
endif
return
OffUnits:
select case OffTimeUnitSelect
case 0
OffTime = AccuSeconds
OffTimeUnits[0] = "S" : OffTimeUnits[1] = "E" : OffTimeUnits[2] = "C" : OffTimeUnits[3] = "S"
case 1
OffTime = AccuMinutes
OffTimeUnits[0] = "M" : OffTimeUnits[1] = "I" : OffTimeUnits[2] = "N" : OffTimeUnits[3] = "S"
case 2
OffTime = AccuHours
OffTimeUnits[0] = "H" : OffTimeUnits[1] = "R" : OffTimeUnits[2] = "S" : OffTimeUnits[3] = " "
case 3
OffTime = AccuDays
OffTimeUnits[0] = "D" : OffTimeUnits[1] = "A" : OffTimeUnits[2] = "Y" : OffTimeUnits[3] = "S"
end select
return
OnUnits:
select case OnTimeUnitSelect
case 0
OnTime = AccuSeconds
OnTimeUnits[0] = "S" : OnTimeUnits[1] = "E" : OnTimeUnits[2] = "C" : OnTimeUnits[3] = "S"
case 1
OnTime = AccuMinutes
OnTimeUnits[0] = "M" : OnTimeUnits[1] = "I" : OnTimeUnits[2] = "N" : OnTimeUnits[3] = "S"
case 2
OnTime = AccuHours
OnTimeUnits[0] = "H" : OnTimeUnits[1] = "R" : OnTimeUnits[2] = "S" : OnTimeUnits[3] = " "
case 3
OnTime = AccuDays
OnTimeUnits[0] = "D" : OnTimeUnits[1] = "A" : OnTimeUnits[2] = "Y" : OnTimeUnits[3] = "S"
end select
return
DisplayData:
if RelayOutput = 1 then
lcdout i,line2,dec (SetOnTime-OnTime)," ",OnTimeUnits[0],OnTimeUnits[1],OnTimeUnits[2],OnTimeUnits[3]
else
lcdout i,line2,dec (SetOffTime-OffTime)," ",OffTimeUnits[0],OffTimeUnits[1],OffTimeUnits[2],OffTimeUnits[3]
endif
return
'************************ 16F628A Configuration Fuses **************************
@ __CONFIG _BOREN_ON & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_ON & _LVP_OFF & _MCLRE_OFF & _INTRC_OSC_NOCLKOUT
' Brown-Out Detect - _BODEN_OFF, _BODEN_ON, _BOREN_OFF, _BOREN_ON
' Code Protection - _CP_OFF, CP_ON
' Data Code Protect - _DATA_CP_OFF, DATA_CP_ON
' Power-On Timer - _PWRTE_OFF, _PWRTE_ON
' Watchdog Timer - _WDT_OFF, _WDT_ON
' Low-Voltage Program - _LPV_OFF, _LVP_ON
' RA5/MCLR Function - _MCLRE_OFF, _MCLRE_ON
' Oscillator Selection - _RC_OSC_CLKOUT, _RC_OSC_NOCLKOUT, _ER_OSC_CLKOUT,
' _ER_OSC_NOCLKOUT, _INTOSC_OSC_CLKOUT, _INTOSC_OSC_NOCLKOUT,
' _INTRC_OSC_CLKOUT, _INTRC_OSC_NOCLKOUT, _EXTCLK_OSC,
' _HS_OSC, _XT_OSC, _LP_OSC
'
'************************************************* ******************************
'---------------------------- Port Assignments --------------------------------
' porta.0 Output 3 LED
' porta.1 Output 1 LED
' porta.2 Run LED
' porta.3 Output 2 LED
' porta.4 Unused
' porta.5 Unused
' porta.6 Unused
' porta.7 Unused
' portb.0 Input 3
' portb.1 Input 2
' portb.2 Input 1
' portb.3 HPWM Output
' portb.4 Up Button
' portb.5 Down Button
' portb.6 Program Button
' portb.7 Debug Output
'---------------------------- EEPROM Locations --------------------------------
' 0 Output 1 Setting
' 1 Output 2 Setting
' 2 Output 3 Setting
'---------------------------- Debug Settings ----------------------------------
DEFINE DEBUG_REG PORTB ' Debug port
DEFINE DEBUG_BIT 4 ' Debug port bit
DEFINE DEBUG_BAUD 9600 ' Debug baud rate
DEFINE DEBUG_MODE 1 ' Debug mode 1 = inverted
DEFINE DEBUG_PACING 300 ' Delay 'in micro sec' between characters sent
i con 254 ' Control byte
clr con 1 ' Clear the display
line1 con 128 ' Move cursor to home position on line 1
line2 con 192 ' Move cursor to home position on line 2
'------------------------------- Variables -------------------------------------
StillPressed var bit
x var byte
Output1 var word
Output2 var word
Output3 var word
StartupDelay var byte
TimeOut var byte
Mode var byte ' Timer mode - 0 = 1-Shot Off Start
' 1 = 1-Shot On Start
' 2 = Repeating Off Start
' 3 = Repeating On Start
OffTimeUnits var byte[4]
OnTimeUnits var byte[4]
OffTime var word ' Off time dependant on units selected
OnTime var word ' On time dependant on units selected
OffTimeUnitSelect var byte ' Off time unit selector
OnTimeUnitSelect var byte ' On time unit selector
SetOnTime var word ' Programmed time to be on
SetOffTime var word ' Programmed time to be off
RelayOutput var bit ' Flag indicated relay status
'------------------------ Miscellaneous Variables ------------------------------
' porta.0
' porta.1
' porta.2
' porta.3
' porta.4
' porta.5
' portb.0
' portb.1
ProgramButton var portb.2
' portb.3
' portb.4
' portb.5
' portb.6
Relay var portb.7
'--------------------------- Port Initialization ------------------------------
TRISA = %10110000
TRISB = %11110111
CMCON = %00000111 ' Disable comparator
CCP1CON = %00000000 ' Enable PWM mode
OPTION_REG.7 = 0 ' Enable portb pull-ups
RCSTA.7 = 0 ' Disable serial port
DEFINE OSC 4
'----------------------------- Main Program Loop -------------------------------
data 65,129,194
include "Elapsed.pbp"
gosub ResetTime ' Reset Time to 0d-00:00:00.00
pause 250
clear
OffTimeUnitSelect = 0 ' 0=secs,1=mins,2=hrs,3=days,4=weeks
OnTimeUnitSelect = 0 ' 0=secs,1=mins,2=hrs,3=days,4=weeks
Mode = 2 ' 0=0-Shot Off Start
' 1=1-Shot On Start
' 2=Repeating Off Start
' 3=Repeating On Start
SetOffTime = 30 ' 30 seconds off
SetOnTime = 50 ' 50 seconds on
RelayOutput = 0 ' Relay is off
StartMenu:
Start:
gosub StartTimer ' Start the Elapsed Timer
gosub InitialState
if Mode = 1 then OneShotOnStart
if (Mode = 2) or (Mode = 3) then Repeating
OneShotOffStart:
gosub OffUnits
gosub OnUnits
if OnTime => SetOnTime then
low Relay
RelayOutput = 1
gosub StopTimer
gosub ResetTime
end
goto StartMenu
endif
goto OneShotOffStart
OneShotOnStart:
gosub OffUnits
gosub OnUnits
if OffTime => SetOffTime then
high Relay
RelayOutput = 0
gosub StopTimer
gosub ResetTime
end
goto StartMenu
endif
goto OneShotOnStart
Repeating: ' Repeating mode
gosub OffUnits
gosub OnUnits
if (OffTime => SetOffTime) and (RelayOutput = 1) then
lcdout $fe,1,"TIME LEFT ON "
gosub DisplayData
high Relay
RelayOutput = 0
gosub ResetTime
goto Repeating
endif
if (OnTime => SetOnTime) and (RelayOutput = 0) then
lcdout $fe,1,"TIME LEFT OFF "
gosub DisplayData
low Relay
RelayOutput = 1
gosub ResetTime
goto Repeating
endif
gosub DisplayData
goto repeating
InitialState:
if (Mode = 1) or (Mode = 3) then
RelayOutput = 1 ' Turn relay on
low Relay
else
RelayOutput = 0 ' Turn relay off
high Relay
endif
return
OffUnits:
select case OffTimeUnitSelect
case 0
OffTime = AccuSeconds
OffTimeUnits[0] = "S" : OffTimeUnits[1] = "E" : OffTimeUnits[2] = "C" : OffTimeUnits[3] = "S"
case 1
OffTime = AccuMinutes
OffTimeUnits[0] = "M" : OffTimeUnits[1] = "I" : OffTimeUnits[2] = "N" : OffTimeUnits[3] = "S"
case 2
OffTime = AccuHours
OffTimeUnits[0] = "H" : OffTimeUnits[1] = "R" : OffTimeUnits[2] = "S" : OffTimeUnits[3] = " "
case 3
OffTime = AccuDays
OffTimeUnits[0] = "D" : OffTimeUnits[1] = "A" : OffTimeUnits[2] = "Y" : OffTimeUnits[3] = "S"
end select
return
OnUnits:
select case OnTimeUnitSelect
case 0
OnTime = AccuSeconds
OnTimeUnits[0] = "S" : OnTimeUnits[1] = "E" : OnTimeUnits[2] = "C" : OnTimeUnits[3] = "S"
case 1
OnTime = AccuMinutes
OnTimeUnits[0] = "M" : OnTimeUnits[1] = "I" : OnTimeUnits[2] = "N" : OnTimeUnits[3] = "S"
case 2
OnTime = AccuHours
OnTimeUnits[0] = "H" : OnTimeUnits[1] = "R" : OnTimeUnits[2] = "S" : OnTimeUnits[3] = " "
case 3
OnTime = AccuDays
OnTimeUnits[0] = "D" : OnTimeUnits[1] = "A" : OnTimeUnits[2] = "Y" : OnTimeUnits[3] = "S"
end select
return
DisplayData:
if RelayOutput = 1 then
lcdout i,line2,dec (SetOnTime-OnTime)," ",OnTimeUnits[0],OnTimeUnits[1],OnTimeUnits[2],OnTimeUnits[3]
else
lcdout i,line2,dec (SetOffTime-OffTime)," ",OffTimeUnits[0],OffTimeUnits[1],OffTimeUnits[2],OffTimeUnits[3]
endif
return