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 KingdomPosts: 5 |
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: AustraliaPosts: 3165 |
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 KingdomPosts: 8592 |
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 KingdomPosts: 5 |
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: AustraliaPosts: 1328 |
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: GermanyPosts: 1133 |
I think you could use the RTC alarm function of the DS3231. From DS3231 manual p10: 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): Michael |
||||
Phil23 Guru Joined: 27/03/2016 Location: AustraliaPosts: 1664 |
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.... Phil |
||||
Brian B Newbie Joined: 15/06/2017 Location: United KingdomPosts: 5 |
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 |