Problem with timer [SOLVED]

General discussion on mikroBasic PRO for AVR.
Post Reply
Author
Message
Petr2023
Posts: 14
Joined: 07 Jun 2023 16:39

Problem with timer [SOLVED]

#1 Post by Petr2023 » 07 Jun 2023 17:05

hi
Sorry for my bad Eng

Can't find bug

This code works not correct.
Each ~30 toggles it switch port faster. Then ~30 toggles normal and 2-3 switches faster. Then ~30 normal .......
If this code move to Timer_ISR, then no bug!!! All works correct!
But in main: not.

Code: Select all

DIM Tmsec as BYTE at GTimer             'Alias èëè ïñåâäîíèì ìëàäøåãî áàéòà
DIM Tsec as WORD at GTimer              'Alias èëè ïñåâäîíèì ìëàäøåãî ñëîâà 65ñåê ìàêñèìóì

'   Main program
main:
    Dim _tsec_old as WORD
    Dim _tsec_now as WORD

    DDRD=$FF                                    ' 
    DDRB=$FF
    PORTB=%10101010
    PORTD=%01010101
    
    InitMessages()
    InitTimers()
    SREG_I_bit = 1                '

Wait:
     _tsec_now=Tsec
    If (_tsec_now-_tsec_old>400*msec) Then
       PORTD=not PORTD
       _tsec_old=Tsec
    End If
    GoTo Wait
'    While TRUE
'    wend
Please help!
Thank You!

Project:
test.zip
(38.2 KiB) Downloaded 21 times
Last edited by Petr2023 on 08 Jun 2023 19:12, edited 1 time in total.

Petr2023
Posts: 14
Joined: 07 Jun 2023 16:39

Re: Problem with timer

#2 Post by Petr2023 » 08 Jun 2023 17:12

Sorry for double Topic.

Do some code in other compiler.
All works fine!!!

MikroE - please, anybody check opcodes or asm.

It's scary to continue writing code

Algorithm and HEX from Algorithm Builder in attach

For atmega168 8Mhz int clock
Attachments
test.zip
(360 Bytes) Downloaded 18 times
AB-timer-example.png
AB-timer-example.png (17.15 KiB) Viewed 444 times

Petr2023
Posts: 14
Joined: 07 Jun 2023 16:39

Re: Problem with timer [SOLVED]

#3 Post by Petr2023 » 08 Jun 2023 19:14

Atomic Operation
atomic.png
atomic.png (15.02 KiB) Viewed 436 times
But!
I can't understand how vars same times modified if i save registers in ISR.

Petr2023
Posts: 14
Joined: 07 Jun 2023 16:39

Re: Problem with timer [SOLVED]

#4 Post by Petr2023 » 11 Jun 2023 02:17

This solution would be better:
volatile.png
volatile.png (5.91 KiB) Viewed 409 times
VOLATILE

Petr2023
Posts: 14
Joined: 07 Jun 2023 16:39

Re: Problem with timer [SOLVED]

#5 Post by Petr2023 » 21 Jun 2023 17:51

Found a bug in MY code.

Code: Select all

     IN         R27, SREG+0
     PUSH       R27
but it should be

Code: Select all

     PUSH       R27
     IN         R27, SREG+0
     PUSH       R27

Post Reply

Return to “mikroBasic PRO for AVR General”