Right .... I've been trying to get into Swordfish after using Proton and have been using the great code snippets from digital DIY to get to grips..
I was messing with the timer0 codes and decided to make a simple clock.
The problem I have is I am using two little sub routines to detect a button press to change hours and minutes. I need to delay the input or the hrs/minutes change way too fast. But !!!! if I add a small delay after the routine when you press and hold the button the timer stops working.
What n00b error am I making and is there a better way to delay a button press ? ( there is in proton, cant find one in Swordfish)
As usual any help greatly appreciated
Code: Select all
Device = 18F4520
Clock = 20
Dim
mS As Word,
count As Word,
sec As Word,
hr As Word,
min As Word,
TMR0ON As T0CON.7,
T08BIT As T0CON.6,
T0CS As T0CON.5,
T0SE As T0CON.4,
PSA As T0CON.3,
TMR0IF As INTCON.2,
TMR0 As TMR0L,
TMR0IE As INTCON.5,
TMR0_Event As Boolean
Interrupt TMR0_Interrupt()
Save(0) // Backup system variables
If TMR0IF = 1 Then // Check if a TMR0 Interrupt occurred
TMR0IF = 0 // Clear the interrupt
Inc(mS, 1638) // Increment the mS counter (scale of 1000)
If mS >= 10000 Then // Working with a scale of 1000, so this
count=count+1
mS = mS - 10000 // checks if 10mS has elapsed
TMR0_Event = True
EndIf
EndIf
Restore // Backup system variables
End Interrupt
Sub calctime()
If count=100 Then sec=sec+1
count=0
If sec=60 Then min=min+1
sec=0
If min=60 Then hr=hr+1
min=0
If hr=24 Then hr=0
End If
End If
End If
End If
End Sub
Sub butaddmin()
If PORTA.4=0 Then min=min+1
delayms (100)
If min=60 Then min=0
End If
End If
End Sub
Sub hraddmin()
If PORTB.0=0 Then hr=hr+1
delayms (100)
If hr=24 Then hr=0
End If
End If
End Sub
Sub TMR0_Initialize()
TMR0ON = 0 // Disable TMR0
T08BIT = 1 // Ensure TMR0 is working in 8 Bit mode
T0CS = 0 // Ensure TMR increments from internal clock
T0SE = 0 // Only used if external source is selected
PSA = 0 // Ensure the Clock source uses the Prescaler
T0CON.0 = 0 // Set the Prescaler bits
T0CON.1 = 0 //
T0CON.2 = 1 //
TMR0 = 0 // Clear the TMR0 register
TMR0IE = 1 // Enable TMR0 Interrupts
Enable(TMR0_Interrupt) // Enable the TMR0 Interrupt Handler
TMR0ON = 1 // Enable TMR0 to increment
End Sub
#option LCD_DATA = PORTD // Assign the LCD connections
#option LCD_EN = PORTA.1 //
#option LCD_RS = PORTA.3
#option LCD_RW = PORTA.2 // // import LCD library...
Include "LCD.bas"
Include "convert.bas"
Input (PORTA.4)
Input (PORTB.0)
LCD.Cls
sec=0
hr=23
min=59
count=0
// Start Of Main Program...
mS = 0 // Reset the mS counter
TMR0_Event = False // Clear the TMR0 Event Flag
TMR0_Initialize // Setup and enable tmr0
'Low(PORTB.0) // Make PORTB.0 and output and set it low
main:
While 1=1
While TMR0_Event = False // Wait for 10mS to elapse
Wend //
TMR0_Event = False // Reset the Event Flag
calctime
butaddmin
hraddmin
WriteAt(1,3, "Time: ",dectostr(hr,2), ":", dectostr(min,2), ":", dectostr(sec,2))
Wend // Loop forever