Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Weird Atmega88 Behaviour

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

Bascom Member



Joined: 06 Aug 2015
Posts: 3

austria.gif
PostPosted: Wed Aug 12, 2015 11:42 am    Post subject: Weird Atmega88 Behaviour Reply with quote

Hello there,
i'm currently working on a project based on interrupts, powersave mode and softclock and i experienced a very odd bug of some sort.
I'm using an ATmega88PA-PU running on it's internal 8Mhz RC-Oscillator, an EA-DOG-LCD and a 32kHz crystal connected to TOSC1 and TOSC2.

This is my initialisation:

Code:

'init lcd
Config Lcd = 16 * 2 , Chipset = Dogm162v5 , Contrast = &H74
Config Lcdpin = Pin , Db4 = Portc.2 , Db5 = Portc.3 , Db6 = Portc.4 , Db7 = Portc.5 , E = Portc.1 , Rs = Portd.0
Initlcd
Cursor Off
Cls

'init timer
Config Clock = Soft , Gosub = Sectic
Enable Interrupts

Dim Secs As Byte
Dim Test As Byte
 


And this is the code snippet in question:

Code:

'main loop
Do

If Secs = 2 Then
Secs = 0
Incr Test
Cls
Upperline
Lcd Test
Lowerline
Lcd _sec
End If

Powersave
Loop
End

'timer2 isr
Sectic:
Incr Secs
Return
 


So what this should do: refresh the LCD every two seconds, the upper value (Test) should be half the value of _sec, displayed on bottom of the LCD.
But what it does: nothing. nothing at all. nothing is ever displayed.

BUT - and that is weird - it works perfectly well in the simulation, and it also works on an ATmega8!
And a more elegant implementation 'temp = secs MOD 2 : if temp = 0 then ..' works as well.

I also found a few workarounds for the M88 which i don't understand:
If i insert 'waitms 200' before the 'do' of the main loop it displays the correct numbers but it runs at double speed!
So the LCD is updated every second and the _secs value is two times higher than it should be!
If the waiting time is 'too short' (~100ms) it does not work..
If i then insert another "waitms 1" in the timer2 ISR everything works just as it should and it runs at correct speed, displaying the correct numbers!

And it also works fine without the waiting but by removing the 'Powersave' instruction from the loop!

Can anyone explain what's happening there? I already replaced the M88 and the crystal so the problem doesn't seem to be limited to my hardware.
Am i missing something? Is this a bug in the BASCOM software or is the M88 not as backwards compatible to the M8 as it should be?

I would be very happy about any help because i need this to work in this hard- and software configuration!
Furthermore this bug already made worthless the code i already wrote and i don't need any further surprises of this kind!

Best Regards!
Nicolai

(BASCOM-AVR version : 2.0.7.5 )
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 182
Location: Japan

japan.gif
PostPosted: Wed Aug 12, 2015 2:10 pm    Post subject: Reply with quote

Hi,

It is not a bug.
Look at the ATmega88PA Datasheet.
http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf

Excerpt from ATMEL of data sheet.
-----------------------------------------------
18.9 Asynchronous Operation of Timer/Counter2

- When entering Power-save or ADC Noise Reduction mode after having written to TCNT2, OCR2x, or TCCR2x, the user must wait until the written register has been updated if Timer/Counter2 is used to wake up the device.
Otherwise, the MCU will enter sleep mode before the changes are effective.
This is particularly important if any of the Output Compare2 interrupt is used to wake up the device, since the Output Compare function is disabled during writing to OCR2x or TCNT2.
If the write cycle is not finished, and the MCU enters sleep mode before the corresponding OCR2xUB bit returns to zero, the device will never receive a compare match interrupt, and the MCU will not wake up.

- If Timer/Counter2 is used to wake the device up from Power-save or ADC Noise Reduction mode, precautions must be taken if the user wants to re-enter one of these modes:
If re-entering sleep mode within the TOSC1 cycle, the interrupt will immediately occur and the device wake up again.
The result is multiple interrupts and wake-ups within one TOSC1 cycle from the first interrupt.
If the user is in doubt whether the time before re-entering Power-save or ADC Noise Reduction mode is sufficient, the following algorithm can be used to ensure that one TOSC1 cycle has elapsed:

a. Write a value to TCCR2x, TCNT2, or OCR2x.
7. Wait until the corresponding Update Busy Flag in ASSR returns to zero.
8. Enter Power-save or ADC Noise Reduction mode.
-----------------------------------------------
Back to top
View user's profile Visit poster's website
menguinponkey

Bascom Member



Joined: 06 Aug 2015
Posts: 3

austria.gif
PostPosted: Thu Aug 13, 2015 10:38 am    Post subject: Reply with quote

Thank you for your help and response! But i'm not sure if i understand the problem.
What you are implying is, that if i put the device to sleepmode before any write operation on TCNT2, TCCR2 or OCR2A/B is finished,
the write operation can not be completed, so the device never wakes up again. That makes sense of course, but is this really the culprit in this case?
Because i never seem to access these registers after the initialisation of the softclock!
Also an absurd long waiting time is required after the initialisation, for the code to run.
As i understand it, Timer2 runs asynchronously, clocked by the external 2^15Hz crystal, whose signal is fed through a 1/128-Prescaler and as soon als the timer overflows TOV2 is triggered which wakes up the MCU, once every second. Am i right so far?
Then the internal routine updates _sec, _min and so on, and my sectic-routine increments my own secs variable.
I'm not sure what else BASCOM does in the background but i don't think any access to these registers is needed or beeing done!

I'd be very happy about further help!
Best Regards
Nicolai
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 182
Location: Japan

japan.gif
PostPosted: Thu Aug 13, 2015 12:53 pm    Post subject: Reply with quote

Hi,

1. Until the AVR is to stabilize 32.768kHz oscillator of Timer2 to power-on, you must wait one second.
Code:

'init timer
Config Clock = Soft , Gosub = Sectic
Wait 1                                                      '32.768kHz oscillator waiting time to stabilize.
Enable Interrupts
 


2. 32.768kHz is 30.5uS So, you need to wait 30.5uS until you complete the interruption of Timer2.

Perhaps, if you drop the system clock of your device to 1MHz, the longer the processing time by the number of instructions of the interrupt routine, double interrupt such a problem should also be solved.

If the system clock is 8MHz, you must wait until Timer2 has completed processing the prophecy as the data sheet.
This is because, as you put the Wait of 1mS to the interrupt routine, is the same even Waitus 30.

If you follow the example of the data sheet, it will be in the following instruction.
Code:

'timer2 isr
Sectic:
Incr Secs
'Waitus 30
OCR2B = 0                                                   'Wait for the write cycle of Timer2.  
Do
Loop Until ASSR.OCR2BUB = 0
Return
 
Back to top
View user's profile Visit poster's website
menguinponkey

Bascom Member



Joined: 06 Aug 2015
Posts: 3

austria.gif
PostPosted: Thu Aug 13, 2015 6:30 pm    Post subject: Reply with quote

Quote:
Until the AVR is to stabilize 32.768kHz oscillator of Timer2 to power-on, you must wait one second.

Ahh of course - my bad - i even read this in the datasheet Very Happy

And now, with the modified ISR in place everything works just fine! (it takes ~50ÁS for the ISR to return)

I really need to study the datasheet more often, BASCOM sometimes lulles you into flase security with it's simple commands and built in functions! Wink

Thank you very much for your help!
Best Regards
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