Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 17:28 25 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MM-Do While Loop

Author Message
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1873
Posted: 04:19am 02 Apr 2012
Copy link to clipboard 
Print this post

I have a program I use with a Mini MM that displays Temperature, Humidity, Dewpoint and Tide times. I needed it to be portable so I needed a way to set the time, hence the following program.
It uses push button switches to interrupt to the 'Set' label and then to increment or decrement the time and date values.
When I push the Set switch it branched to the Set routine OK. I was able to set the Day, Month, Year and Hour, then when I pushed the switch to advance to minutes it completely jumped this 'Do While - Loop' and went to the line after Loop and then returned to the main Display. I deleted the minutes set routine and then it jumped the Hour set. The trace function is virtuously useless without line nos. so I had to put Print statements after every line ,Print 1, Print 2 etc to see where the program was going. Finally (after 3 days and nights)I inexplicably put a Pause at the beginning of the Minutes set routine and it works OK.
Can anyone explain.

The program:- I know I don't really need the two Display routines it's just the way it evolved.

' The Input pins are tied to Ground with 100K resistor
' and pulled up with a 10K resistor and P/B switch

Main: SetPin 1,6,Set 'Interrupt to Set
SetPin 2,2 'Increment up
SetPin 3,2 'Increment down
SetPin 20,2 'Select setting

' Display the current Time and Date

L001: GoSub L002 ' Initialise the LCD
Pause 100
LCD_line1$ = "Time "+Time$+" "
LCD_line2$ = "Date "+Date$+" "
GoSub L003 ' send to the LCD
Pause 1000
GoTo Main
'
'
'
L002: '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''
' Initialise the LCD
'
For i = 12 To 17 : SetPin i, 9 : Next i ' all open collector
_LCD_byte = &B0011 : GoSub L005 : Pause 5 ' reset
_LCD_byte = &B0011 : GoSub L005 : Pause 5 ' reset
_LCD_byte = &B0011 : GoSub L005 : Pause 5 ' reset
_LCD_byte = &B0010 : GoSub L005 : Pause 2 ' 4 bit mode
_LCD_byte = &B00101100 : GoSub L004 ' select 4 bit, 2 lines
_LCD_byte = &B00001100 : GoSub L004 ' display on, no cursor
_LCD_byte = &B00000110 : GoSub L004 ' increment on write
_LCD_byte = &B00000001 : GoSub L004 ' clear the display
Return
'
'
L003: ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''
' send the two lines to the LCD
' the text is in LCD_Line1$ and LCD_Line2$
'
_LCD_byte = &H80 : GoSub L004 ' select the 1st line
For _LCD = 1 To 16
_LCD_byte = Asc(Mid$(LCD_Line1$, _LCD, 1))
Pin(17) = 1 : GoSub L004 ' send the character
Next _LCD
_LCD_byte = &B11000000 : GoSub L004 ' select the 2nd line
For _LCD = 1 To 16
_LCD_byte = Asc(Mid$(LCD_Line2$, _LCD, 1))
Pin(17) = 1 : GoSub L004 ' send the character
Next _LCD
Return
'
'
L004: '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''
' Send a byte to the LCD
' the data to be sent is in _LCD_byte
'
Pin(13) = _LCD_byte And &B00010000 ' output the 1st 4 bits
Pin(14) = _LCD_byte And &B00100000
Pin(15) = _LCD_byte And &B01000000
Pin(16) = _LCD_byte And &B10000000
Pin(12) = 1 : Pin(12) = 0 ' tell lcd to accept data
L005: ' Entry point to send just 4 bits to the LCD
Pin(13) = _LCD_byte And &B00000001 ' output the 2nd 4 bits
Pin(14) = _LCD_byte And &B00000010
Pin(15) = _LCD_byte And &B00000100
Pin(16) = _LCD_byte And &B00001000
Pin(12) = 1 : Pin(12) = 0 : Pin(17) = 0 ' tell lcd to accept data
Return

' Set the time and Date.

Set: ' Load the variables with the current values

D$=Left$(Date$,2):M$=Mid$(Date$,4,2):Y$=Right$(Date$,4)
H$=Left$(Time$,2):MI$=Mid$(Time$4,2)
D=Val(D$):M=Val(M$):Y=Val(Y$):H=Val(H$):MI=Val(MI$)
Set1: If flag=0 Then GoTo Day
If flag=1 Then GoTo Month
If flag=2 Then GoTo Year
If flag=3 Then GoTo Hour
If flag=4 Then GoTo Min

'When the select switch is pressed Pin(20) goes high

Day: Do While Pin(20)=0
flag=1
If Pin(2)=1 Then D=D+1
If D>31 Then D=1
If Pin(3)=1 Then D=D-1
If D<1 Then D=31
X$="Day ":Z$=Str$(D)
GoSub disp
Loop
GoTo Set1
Month: Do While Pin(20)=0
flag=2
If Pin(2)=1 Then M=M+1
If M>12 Then M=1
If Pin(3)=1 Then M=M-1
If M<1 Then M=12
X$="Month ":Z$=Str$(M)
GoSub disp
Loop
GoTo Set1
Year: Do While Pin(20)=0
flag=3
If Pin(2)=1 Then Y=Y+1
If Pin(3)=1 Then Y=Y-1
X$="Year ":Z$=Str$(Y)
GoSub disp
Loop
GoTo Set1
Hour: Do While Pin(20)=0
flag=4
If Pin(2)=1 Then H=H+1
If H>23 Then H=1
If Pin(3)=1 Then H=H-1
If H<0 Then H=23
X$="Hour ":Z$=Str$(H)
GoSub disp
Loop
GoTo Set1
Min: Pause 1000
Do While Pin(20)=0
If Pin(2)=1 Then MI=MI+1
If MI>59 Then MI=1
If Pin(3)=1 Then MI=MI-1
If MI<0 Then MI=59
X$="Minute":Z$=Str$(MI)
GoSub disp
Loop

' Load the variables with the correct settings
' and return to the main display.

H$=Str$(H):MI$=Str$(MI):D$=Str$(D):M$=Str$(M):Y$=Str$(Y)
S$=Str$(S)
Time$=H$+":"+MI$+":"+S$
Date$=D$+"-"+M$+"-"+Y$
IReturn

'Display for setting the Time and Date.

Disp:
GoSub L007 ' Initialise the LCD
Pause 100
LCD_line1$ = X$+" "+Z$+" "
LCD_line2$ = " "
GoSub L008 ' send to the LCD
Pause 500
Return
'
'
'
L007: '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''
' Initialise the LCD
'
For i = 12 To 17 : SetPin i, 9 : Next i ' all open collector
_LCD_byte = &B0011 : GoSub L0010 : Pause 5 ' reset
_LCD_byte = &B0011 : GoSub L0010: Pause 5 ' reset
_LCD_byte = &B0011 : GoSub L0010: Pause 5 ' reset
_LCD_byte = &B0010 : GoSub L0010: Pause 2 ' 4 bit mode
_LCD_byte = &B00101100 : GoSub L009 ' select 4 bit, 2 lines
_LCD_byte = &B00001100 : GoSub L009 ' display on, no cursor
_LCD_byte = &B00000110 : GoSub L009 ' increment on write
_LCD_byte = &B00000001 : GoSub L009 ' clear the display
Return
'
'
L008: ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''
' send the two lines to the LCD
' the text is in LCD_Line1$ and LCD_Line2$
'
_LCD_byte = &H80 : GoSub L009 ' select the 1st line
For _LCD = 1 To 16
_LCD_byte = Asc(Mid$(LCD_Line1$, _LCD, 1))
Pin(17) = 1 : GoSub L009 ' send the character
Next _LCD
_LCD_byte = &B11000000 : GoSub L009 ' select the 2nd line
For _LCD = 1 To 16
_LCD_byte = Asc(Mid$(LCD_Line2$, _LCD, 1))
Pin(17) = 1 : GoSub L009 ' send the character
Next _LCD
Return
'
'
L009: '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''
' Send a byte to the LCD
' the data to be sent is in _LCD_byte
'
Pin(13) = _LCD_byte And &B00010000 ' output the 1st 4 bits
Pin(14) = _LCD_byte And &B00100000
Pin(15) = _LCD_byte And &B01000000
Pin(16) = _LCD_byte And &B10000000
Pin(12) = 1 : Pin(12) = 0 ' tell lcd to accept data
L0010: ' Entry point to send just 4 bits to the LCD
Pin(13) = _LCD_byte And &B00000001 ' output the 2nd 4 bits
Pin(14) = _LCD_byte And &B00000010
Pin(15) = _LCD_byte And &B00000100
Pin(16) = _LCD_byte And &B00001000
Pin(12) = 1 : Pin(12) = 0 : Pin(17) = 0 ' tell lcd to accept data
Return


"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1873
Posted: 04:31am 02 Apr 2012
Copy link to clipboard 
Print this post

Forgive the layout of the program it went awry when I pasted it and makes it a bit hard to follow.
Paul.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
BobDevries

Senior Member

Joined: 08/06/2011
Location: Australia
Posts: 266
Posted: 04:37am 02 Apr 2012
Copy link to clipboard 
Print this post

Hi Palcal,

as far as I can see, you're using the command version of the PIN(). There's two ways to use PIN():

1:

variable=PIN(x): REM gets the value of PIN x into a variable
or

2:

PIN(x)=value: REM sets PIN X to value

With your line:

DO WHILE PIN(20)=0

you are using the second one, which was not what you intended, I think.

Better to use this:

some_variable = PIN(20)
DO WHILE some_variable = 0
.
.
.
.
Some_variable = PIN(20)
LOOP


My $0.02

Regards,
Bob Devries
Dalby, QLD, Australia
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1873
Posted: 05:49am 02 Apr 2012
Copy link to clipboard 
Print this post

Bob, thanks, I tried your suggestion but exactly the same thing happens.

Paul.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024