Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Lcd update every second?

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
lasse2t

Bascom Member



Joined: 14 Jan 2005
Posts: 95
Location: Sweden

sweden.gif
PostPosted: Sun Feb 23, 2020 9:10 pm    Post subject: Lcd update every second? Reply with quote

Hello. I need to update the lcd for maybe every 1 sec without affecting the int0 isr routine.
Int0 is from 10 to 200 hz. Not found a solution yet. Have searched the forum an internet.









Code:
$regfile = "m328pdef.dat" 'ATmega328p
$crystal = 8000000 '1MHz

'-----------------------------------------------------------------
Config Lcdpin = Pin , Db4 = Portc.0 , Db5 = Portc.1 , Db6 = Portc.2 , Db7 = Portc.3 , E = Portc.4 , Rs = Portc.5
config LCD=16x2
cursor=off
cls



config timer1=TIMER,prescale=1




config int0=falling
on int0 int0_sub
enable int0
enable interrupts


dim timer1_value as Word



do
loop




int0_sub:
timer1_value=timer1
timer1=0
locate 1,8
lcd timer1_value;"   "
return


(BASCOM-AVR version : 2.0.8.2 )
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Sun Feb 23, 2020 11:40 pm    Post subject: Reply with quote

Create another timer ISR which sets a flag every second.
In your main loop, read the flag, if its not set, continue the loop.
If it is set, clear the flag, then update the display.

Code:

main:

do
... do stuff
if displayflag = 1 then
 displayflag = 0
 update_display
end if
... do other stuff
loop

isr1: 'every second
displayflag = 1
return


 

While you can put the LCD update routine inside that ISR, its not good practice. LCD updates take a long time, and if you do that inside an ISR, you may miss some other vital interrrupt.
Its up to you to keep the main loop running as fast as reasonable, certainly at least 1 per second, but the time jitter in updating the display is almost always acceptable - humans cannot usually tell.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Mon Feb 24, 2020 10:02 am    Post subject: Reply with quote

if you don't use a timer, as Adrian suggests...

if you use a RTC in your program like DS3231 or similar you can read the second in your main loop
do
if oldsec <>_sec then
update.....
else
---
---
endif
oldsec=_sec
loop

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
lasse2t

Bascom Member



Joined: 14 Jan 2005
Posts: 95
Location: Sweden

sweden.gif
PostPosted: Mon Feb 24, 2020 10:43 pm    Post subject: Reply with quote

Ok, tested like this and it is working ok, the value change litle but maybe that because INT0 has higher priority than ON timer0?


Code:
$regfile = "m328pdef.dat" 'ATmega328p
$crystal = 8000000 '1MHz

'-----------------------------------------------------------------
Config Lcdpin = Pin , Db4 = Portc.0 , Db5 = Portc.1 , Db6 = Portc.2 , Db7 = Portc.3 , E = Portc.4 , Rs = Portc.5
config LCD=16x2
cursor=off
cls



config timer1=TIMER,prescale=8
config timer0=TIMER,prescale=1024



config int0=falling
on int0 int0_sub
on timer0 timer0_update
enable timer0
enable int0
enable interrupts


dim timer1_value as Word
dim lcd_bit as bit
dim count as byte


do
locate 1,6
if lcd_bit=1 then lcd timer1_value;"      "
reset lcd_bit
loop




int0_sub:
timer1_value=timer1
timer1=0
return


timer0_update:
incr count
if count>=20 then set lcd_bit
if count>=20 then count=0
return
 
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Mon Feb 24, 2020 11:33 pm    Post subject: Reply with quote

That looks right. And yes, there will always be some jitter. As you make the main loop longer, it will get more. But unless there is some good programming reason, timing jitter on an LCD update should not matter.

Just one comment on your code
Quote:

do
locate 1,6
if lcd_bit=1 then lcd timer1_value;" "
reset lcd_bit
loop


Its better to do the reset lcd_bit within the if statement, else the bit could be set sometime during the LCD write, then cleared by the reset after the update, and so not seen on the next loop. You also have that LOCATE instruction done every time within the main loop, but it does nothing until the LCD update is done.

I would do it this way:
Code:

do

if lcd_bit=1 then
 reset lcd_bit
 locate 1,6
 lcd timer1_value;"      "
end if

loop
 

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
lasse2t

Bascom Member



Joined: 14 Jan 2005
Posts: 95
Location: Sweden

sweden.gif
PostPosted: Mon Feb 24, 2020 11:39 pm    Post subject: Reply with quote

If i do like that in do loop i get some errors.

"If then expected
interrupts need return
end if expected"
Back to top
View user's profile
lasse2t

Bascom Member



Joined: 14 Jan 2005
Posts: 95
Location: Sweden

sweden.gif
PostPosted: Mon Feb 24, 2020 11:41 pm    Post subject: Reply with quote

Oppss, forgott end if.
working now.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum