Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 01:00 06 May 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 : Micromite LCD Backpack

Author Message
Brian B
Newbie

Joined: 15/06/2017
Location: United Kingdom
Posts: 5
Posted: 02:14am 20 Jun 2017
Copy link to clipboard 
Print this post

I'm what you might say a senior citizen newbie to this project, but have an electronics background.
I fly model gliders at club competitions level and at two different timings, one at ten minutes and the other at ten minutes thirty seconds, and we require a countdown timer for both of them. I have tried to produce a very rough (and I mean rough) program using the pause statement but find that after ten minutes it is fifteen seconds slow.
Any advice on how to achieve a working timer using perhaps the SETTICK command would be appreciated and also how the touch command is implemented to start and reset the timer.

Although I have downloaded the Micromite MMBasic file, is there any other recommended in depth reading available on how to program in MMBasic.

Many thanks

Brian
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 02:33am 20 Jun 2017
Copy link to clipboard 
Print this post

Internally the Micromite uses a simple RC oscillator for timing and that can vary by 1% or even more. But your inaccuracy seems to be greater so it is probably built into your program - perhaps adjusting the length of time in the pause statements would correct the issue.

I can recommend Getting Started with the Micromite for beginners. Download it from: http://geoffg.net/Downloads/Micromite/Getting%20Started%20with%20the%20Micromite.pdf. It is into its third edition and covers the Micromite and programming in MMBasic for beginners.

Geoff
Geoff Graham - http://geoffg.net
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8592
Posted: 02:34am 20 Jun 2017
Copy link to clipboard 
Print this post

All time related functions on the Micromite are dependent on the accuracy (or lack of) of a simple RC oscillator in the chip. This can be tuned using OPTION CLOCKTRIM but will still be affected by changes in temperature. I've played with this in this thread and there are lots of other threads about this (search for "OPTION CLOCKTRIM")

I can't see any reason why PAUSE would be less accurate then SETTICK or just polling TIMER

Geoff's "Getting started with the Micromite" and the Micromite manual are the best docs available
 
Brian B
Newbie

Joined: 15/06/2017
Location: United Kingdom
Posts: 5
Posted: 03:12am 20 Jun 2017
Copy link to clipboard 
Print this post

Many thanks for the quick response to my problem and the advice on the best reading matter for programming in MMBasic.
I omitted to say before that I have attached an RTC module to the Micromite which sets the internal clock at start up.
I will now go and read the Geoff's "Getting Started" manual.

Brian
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1328
Posted: 04:04am 20 Jun 2017
Copy link to clipboard 
Print this post

Brian,
Three years ago I built a Micromite UV exposure box that used a 4x4 keypad for setting the 'ON' time required and 4x20 digit LCD display for the countdown - the code is below and uses a SETTICK interrupt. You're using the LCD GUI backpack so a lot of it is not wanted but you can get some ideas from it, especially for the countdown code (sub countdown). It's reasonably well self-documented.

Greg


'UVTimer - Author: Greg Yaxley with contributions
'from David Mortimer. 2nd May 2014

'Program to enter, display & control exposure time for
'a UV lamp box using a 4x4 keypad & 4x20 LCD display.
'MicroMite Basic V4.5_28 pin.
'----------------------------------------------
OPTION AUTORUN ON 'run on power-up
dutycycle = 80 'default dutycycle for PWM (LCD backlight)
pwm 2,50,dutycycle 'LCD backlight cntrl: Ch2A 50Hz 80% pin26
LCD INIT 2,3,4,5, 23,24 'd4-d7 pins 2-5, Reset pin23, Enable pin24
LCD clear
Keypad KeyCode, Key_Int, 15,16,17,18,6,7,9,10
led = 14 'pin no. for lamps-ON led
buzz = 21 'pin no. for buzzer
timeset = 0 'flag: exposure time is not set
pin(led)=0: pin(buzz)=0 'ensure led, lamps and buzzer start OFF
setpin led,DOUT
setpin buzz,DOUT

display_menu:
keycode = 99 'reset keypad
settick 0,0 'turn OFF timer interrupt
lcd cmd 12 'make cursor invisible (turn off)
if timeset = 0 then Mins$ = "00": secs$ = "00" 'default time
lcd 1,1, " Time set = "+ mins$ +":"+ secs$
lcd 2,1, " Brightness * or # "
'lcd 2,c20, "" 'clear LCD line 2
LCD 3,1, "Reset time - Press A"
LCD 4,1, "Lamps ON - Press B"
do
if keycode = 20 goto set_time 'key A
if keycode = 21 goto lamps_ON 'key B
if keycode = 23 goto display_menu 'key D
if keycode = 10 or keycode = 11 then brightness 'keys * and #
loop

set_time:
settick 0,0
mins$= "": secs$= "" 'null default time strings
timeset = 0 're-zero time-set flag
lcd 2,c20,"" 'clear LCD line 2 (after a countdown)
lcd 3,1, " Enter set time "
lcd 4,1, " MENU - Press D "
lcd 1,13, "" 'set cursor at 'mins' position
lcd cmd 15 'turn cursor on

for count = 1 to 2 'get the two minutes digits
get_keycode
if keycode = 23 then goto display_menu
if keycode > 9 then goto set_time
mins$ = mins$ + keycode$ 'build mins$
lcd 1,13, mins$ 'display current mins$ string
next
lcd cmd 20 'move cursor past ":"

for count = 1 to 2 'get the two seconds digits
get_keycode
if keycode = 23 then goto display_menu
if keycode > 9 then goto set_time
if keycode > 5 and count = 1 then
beep (2,100,100) 'out-of-bounds secs entry
goto set_time
endif
secs$ = secs$ + keycode$ 'build secs$
lcd 1,16, secs$ 'display current secs$ string
next
timeset = 1 'flag: time correctly set
lcd cmd 12 'make cursor invisible (turn off)
'----------end of set_time:------------------

lamps_ON: 'If time is set, turns lamps ON, counts down
if timeset = 0 then beep (2,50,50) '& then turns OFF.
if timeset = 1 then 'Exits if requested.
LCD 3,1, "Reset time - Press A" 'keycode 20
LCD 4,1, "Lamps ON - Press B" 'keycode 21
if keycode = 20 then goto set_time
if keycode = 21 then 'legitimite request for "lamps ON"
setpin led,DOUT 'make lamp/relay pin digital out
setpin buzz,DOUT 'make buzzer pin digital out
lcd 2,1, "Countdown = " +mins$ +":" +secs$ +" "
lcd 3,1, " - LAMPS ON - "
lcd 4,1, "Lamps OFF - Press C "
beep (3,200,200) 'buzz "lamps-ON" - 3 short beeps
mins = val(mins$): secs = val(secs$)
totsecsrem = (mins*60)+secs 'total secs for countdown
pin(led) = 1 'turn ON lamp LED & relay
settick 1000,countdown 'interrupt each second for countdown
do while totsecsrem > 0
if keycode > 21 then exit do 'exit if C or D pressed
loop
'-------lamps_OFF------
settick 0,0 'stop countdown interrupt
beep (1,800,0) 'one long beep = "lamps OFF"
pin(led) = 0 'turn OFF lamp LED & relay
endif
endif
goto display_menu

Key_Int: 'key press detected
keycode$ = str$(keycode)
ireturn

sub get_keycode
keycode$ = "99"
do
loop until keycode$ <> "99"
end sub

sub countdown
totsecsrem = totsecsrem - 1 'decrement 1 sec each interrupt
remmins = totsecsrem/60 'convert to mins & mins fraction
dispmins = fix(remmins) 'extract the mins
dispsecs = cint((remmins-dispmins)*60) 'convert fraction to secs
lcd 2,13, str$(dispmins,2,0,"0") +":"+ str$(dispsecs,2,0,"0")
end sub 'display 2 digits for mins & secs, & any leading zeros

sub beep (beeps,ontime,offtime)
for i=1 to beeps
pin(buzz)=1: pause ontime
pin(buzz)=0
if i < beeps then pause offtime
next
End sub

sub brightness
lcd 2,c20,""
if keycode = 11 then dutycycle = dutycycle + 10
if keycode = 10 then dutycycle = dutycycle - 10
if dutycycle < 0 then dutycycle = 0
if dutycycle > 100 then
dutycycle = 100
lcd 2,1, " MAXIMUM BRIGHTNESS "
endif
pwm 2,50,dutycycle
keycode = 99
end sub
'------------------Finish---------------------
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 05:28am 20 Jun 2017
Copy link to clipboard 
Print this post

  Brian B said  I omitted to say before that I have attached an RTC module to the Micromite which sets the internal clock at start up.

I think you could use the RTC alarm function of the DS3231.

From DS3231 manual p10:
  Quote  Alarms
The DS3231 contains two time-of-day/date alarms. Alarm 1 can be set by writing to registers 07h to 0Ah. Alarm 2 can be set by writing to registers 0Bh to 0Dh. The alarms can be programmed (by the alarm enable and INTCN bits of the control register) to activate the INT/SQW output on an alarm match condition. Bit 7 of each of the time-of-day/date alarm registers are mask bits (Table 2). When all the mask bits for each alarm are logic 0, an alarm only occurs when the values in the timekeeping registers match the corresponding values stored in the time-of-day/date alarm registers. The alarms can also be programmed to repeat every second, minute, hour, day, or date. Table 2 shows the possible settings. Configurations not listed in the table will result in illogical operation.
The DY/DT bits (bit 6 of the alarm day/date registers) control whether the alarm value stored in bits 0 to 5 of that register reflects the day of the week or the date of the month. If DY/DT is written to logic 0, the alarm will be the result of a match with date of the month. If DY/DT is written to logic 1, the alarm will be the result of a match with day of the week.
When the RTC register values match alarm register settings, the corresponding Alarm Flag ‘A1F’ or ‘A2F’ bit is set to logic 1. If the corresponding Alarm Interrupt Enable ‘A1IE’ or ‘A2IE’ is also set to logic 1 and the INTCN bit is set to logic 1, the alarm condition will activate the INT/SQW signal. The match is tested on the once-per-second update of the time and date registers.

I have never used this function.

The DS3231 is VERY precise (Accuracy ±2ppm from 0°C to +40°C)! The MMBasic offers commands for setting the DS3231 registers (MMBasic Manual V5.0404 p68):
  Quote  The RTC SETREG and GETREG commands can be used to set or read the contents of registers within the chip. 'reg' is the register's number, 'value' is the number to store in the register and 'var' is a variable that will receive the number read from the register. These commands are not necessary for normal operation but they can be used to manipulate special features of the chip (alarms, output signals, etc).


MichaelEdited by twofingers 2017-06-21
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1664
Posted: 10:53am 20 Jun 2017
Copy link to clipboard 
Print this post

  Brian B said  I omitted to say before that I have attached an RTC module to the Micromite which sets the internal clock at start up.


Even though you have the RTC, it only sets the clock on startup.

After that it drifts and as mentioned, TRIMCLOCK will minimise this.

One other idea is to repeatedly read the RTC so the time the MM stores is more accurate.

This won't improve the accuracy of the internal timer values or intervals of interrupts. but if you then use the system time in the code it will be based on an accurate time.

I read the RTC every hour in most code.
You could use an interrupt to reset it every minute, or 30 seconds even.

Then I use a couple of functions to get seconds from the time string....

And Yes, I know 60x60=3600; just wrote it like that, intending the get back & edit it....


  Quote   '----------------------------------------------------------------------
'Functions to turn Time String to Seconds & Seconds back into a String
'----------------------------------------------------------------------

Function TimeSecs(TimeStr as String) as integer

TimeSecs=
Val(left$(TimeStr,2))*60*60+Val(Mid$(TimeStr,4,2))*60+Val(Right$(TimeStr,2))

End Function


Function SecsTime(TimeInt as integer) as String

Local integer HH,MM,SS
HH=TimeInt\
3600
MM=(TimeInt-HH*
3600)\60
SS=TimeInt-HH*
3600-MM*60
SecsTime=
Str$(HH,2,0,"0")+":"+Str$(MM,2,0,"0")+":"+Str$(SS,2,0,"0")

End Function



Phil
 
Brian B
Newbie

Joined: 15/06/2017
Location: United Kingdom
Posts: 5
Posted: 11:34am 22 Jun 2017
Copy link to clipboard 
Print this post

Many thanks for all the advice from you all. I'm still reading and inwardly digesting Geoff's MMBasic manual, makes life a little easier when you have the right information and insight into how to program. I have been able to trim the pause statement to correct the timing error I was getting. Hopefully in the not too distant future I will have the timer working and will be able to give it a test run at one of the competitions later in the year.
 
Print this page


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

© JAQ Software 2024