Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

atmega32u4 and timer interrupt

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

Bascom Member



Joined: 02 Jan 2017
Posts: 81

poland.gif
PostPosted: Tue Jun 29, 2021 10:48 am    Post subject: atmega32u4 and timer interrupt Reply with quote

Hi,

this code is similar to code I use on Uno, except that interrupts are on different pins. On Uno this code works but on Micro it seems that timer0 interrupt is not executed or should I make it different somehow? After timer0 execution I should see text informing that it's saved or read at least. Any hints?

Code:

'******** MICRO
$regfile = "m32u4def.dat"
$crystal = 16000000
$hwstack = 30
$swstack = 30
$framesize = 32
$baud = 19200

Cha Alias Pind.1                                            'INT1
Chb Alias Pind.0                                            'INT0

Config Cha = Input
Config Chb = Input
Config Int1 = Rising
Config Submode = New

Config Timer0 = Timer , Prescale = 64

Enable Interrupts
Enable Int1
Enable Timer0

On Int1 Encodera
On Timer0 Timer0_int

'-------------------------------------------
Dim Impulsy As Integer , Flaga As Bit 'zmienne do obsługi impulsatora
Dim Licznik_czasu As Word , Countera As Word 'zmienne do odliczania czasu do zapisu do eeprom

Flaga = 0
Timer0 = 6
Licznik_czasu = 0
Impulsy = 0

Sub Write_to_eeprom(value As Integer)
Local I1 As Byte , I2 As Byte
  I1 = Low(value)
  I2 = High(value)
  Writeeeprom I1 , 0
  Writeeeprom I2 , 1
  Print "zapisane " ; I1 ; "  " ; I2
End Sub

Function Read_from_eeprom() As Integer
Local I1 As Byte , I2 As Byte
  Readeeprom I1 , 0
  Readeeprom I2 , 1
  Read_from_eeprom = Makeint(i1 , I2)
Print "odczytane " ; I1 ; "  " ; I2 ; "  " ; Read_from_eeprom
End Function

Impulsy = Read_from_eeprom()
'Print Impulsy ; "   impulsy"

' Stop Timer0

Set Chb
Impulsy = 0
Call Write_to_eeprom(impulsy)

Do
 If Flaga = 1 Then
  Licznik_czasu = 0
  Print Impulsyanie timer0_int żeby zapisać stan licznika impulsatora do eepromu  
'  Start Timer0
  Flaga = 0
 End If
Loop
End

Encodera:
  If Chb = 1 Then
   Decr Impulsy
     Else
   Incr Impulsy
  End If
  Flaga = 1
Return

Timer0_int:
 Countera = Countera + 6
 Incr Licznik_czasu
 If Licznik_czasu = 4000 Then
  Licznik_czasu = 0
  Call Write_to_eeprom(impulsy)
'  Stop Timer0
 End If
Return


Regards,

(BASCOM-AVR version : 2.0.8.2 , Latest : 2.0.8.3 )
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1944

blank.gif
PostPosted: Tue Jun 29, 2021 12:17 pm    Post subject: Re: atmega32u4 and timer interrupt Reply with quote

krolikbest wrote:
Code:
  Print Impulsyanie timer0_int żeby zapisać stan licznika impulsatora do eepromu  

It would be odd if it would compile.
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 749

poland.gif
PostPosted: Tue Jun 29, 2021 12:30 pm    Post subject: Reply with quote

It can be easly tested in the simulator. HWSTACK is too low. Also "Set Chb" does nothing because you can`t write into PIN register. Try "Portd = Portd Or &B0000_0011 "

Code for simulator below. Enable "Sim timers" and hit couple of times INT1 on Interrupts tab.
If HWSTACK will be 30 then everytime "Zapisuje -1". If HWSTACK is larger then "Zapisuje -3 ... etc."
You have over >2KB SRAM on it. Why not use it for increase stacks? Very Happy

Code:
'******** MICRO
$regfile = "m32u4def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 30
$framesize = 32
$baud = 19200
$sim

Cha Alias Pind.1                                            'INT1
Chb Alias Pind.0                                            'INT0

Config Cha = Input
Config Chb = Input
Config Int1 = Rising
Config Submode = New

Config Timer0 = Timer , Prescale = 64
 Stop Timer0

On Int1 Encodera
On Timer0 Timer0_int

'-------------------------------------------
Dim Impulsy As Integer , Flaga As Bit
Dim Licznik_czasu As Word , Countera As Word

Flaga = 0
Timer0 = 6
Licznik_czasu = 0
Impulsy = 0

Sub Write_to_eeprom(value As Integer)
 Local I1 As Byte , I2 As Byte
  I1 = Low(value)
  I2 = High(value)
  Writeeeprom I1 , 0
  Writeeeprom I2 , 1
 Print "Zapisuje " ; Value
End Sub

Function Read_from_eeprom() As Integer
 Local I1 As Byte , I2 As Byte
  Readeeprom I1 , 0
  Readeeprom I2 , 1
 Print "Odczytano " ; Read_from_eeprom
  Read_from_eeprom = Makeint(i1 , I2)
End Function

Impulsy = Read_from_eeprom()
'Print Impulsy ; "   impulsy"

Set Chb : Portd = Portd Or &B0000_0011

Impulsy = 0
Call Write_to_eeprom(impulsy)

Enable Interrupts
Enable Int1
Enable Timer0


Do
 If Flaga = 1 Then
  Licznik_czasu = 0
  Print "Timer Started"
   Start Timer0
  Flaga = 0
 End If
Loop
End

Encodera:
  If Chb = 1 Then
   Decr Impulsy
     Else
   Incr Impulsy
  End If
   Print "Imp=" ; Impulsy
  Flaga = 1
Return

Timer0_int:
 Incr Licznik_czasu
 If Licznik_czasu = 2 Then
  Licznik_czasu = 0
  Call Write_to_eeprom(impulsy)
   Stop Timer0
   Print "Timer Stopped"
 End If
Return


Back to top
View user's profile Visit poster's website
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 81

poland.gif
PostPosted: Tue Jun 29, 2021 1:42 pm    Post subject: Reply with quote

Hi,

MWS - absolutely righ Smile I had to mix something during copy-paste
EDC - both versions work in simulation, but on real hardware somehow don't. I use avrdude with params
Code:
-p atmega32u4 -c avr109 -P COM49 -b 57600 -V -D -U flash:w:"C:\kody\Bascom\impulsator\SIMULATOR_enkoder BOTLAND 400ppr_czasowka_eeprom.hex":i
to programm uC. On Uno works whereas on Micro doesn't.

Regards,
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1944

blank.gif
PostPosted: Tue Jun 29, 2021 2:36 pm    Post subject: Reply with quote

krolikbest wrote:
MWS - absolutely righ Smile I had to mix something during copy-paste

I ask because it's hard to judge from mixed code to the real code, especially if there is more 'mix'.

Does the encoder Int1 fire? The current program logic has Timer0 stopped, which is started only by execution of the Int1 ISR.
Without Int1 no Timer0 interrupt.
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 749

poland.gif
PostPosted: Tue Jun 29, 2021 3:03 pm    Post subject: Reply with quote

Code slightly modified for clarity and working just fine.





Code:
'$programmer = 3                                             'External prog avr109
$programmer = 19                                            'USBasp

$regfile = "m32u4def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 30
$framesize = 32
$baud = 115200

Config Submode = New

Cha Alias Pind.1                                            'INT1
Chb Alias Pind.0                                            'INT0

Config Cha = Input
Config Chb = Input

Portd = Portd Or &B0000_0011

Config Int1 = Rising

Config Timer0 = Timer , Prescale = 64
 Stop Timer0

On Int1 Encodera
On Timer0 Timer0_int

'-------------------------------------------
Dim Impulsy As Integer , Flaga As Bit
Dim Licznik_czasu As Word , Countera As Word

Flaga = 0
Timer0 = 6
Licznik_czasu = 0
Impulsy = 0

Sub Write_to_eeprom(value As Integer)
 Local I1 As Byte , I2 As Byte
  I1 = Low(value)
  I2 = High(value)
  Writeeeprom I1 , 0
  Writeeeprom I2 , 1
 Print "Saving " ; Value
End Sub

Function Read_from_eeprom() As Integer
 Local I1 As Byte , I2 As Byte
  Readeeprom I1 , 0
  Readeeprom I2 , 1
  Read_from_eeprom = Makeint(i1 , I2)
 Print "Readed " ; Read_from_eeprom
End Function

Print "Reboot... " ;
Impulsy = Read_from_eeprom()

Enable Interrupts
Eifr.intf1 = 1                                              'clear flag after pullup
Enable Int1
Enable Timer0

Do
 If Flaga = 1 Then
  Licznik_czasu = 0
  'Print "Timer Started"
   Start Timer0
  Flaga = 0
 End If
Loop
End

Encodera:
  If Chb = 1 Then
   Decr Impulsy
     Else
   Incr Impulsy
  End If
   'Print "Imp=" ; Impulsy
  Flaga = 1
Return

Timer0_int:
 Incr Licznik_czasu
 If Licznik_czasu = 2000 Then
  Licznik_czasu = 0
  Call Write_to_eeprom(impulsy)
   Stop Timer0
   'Print "Timer Stopped"
 End If
Return
Back to top
View user's profile Visit poster's website
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 81

poland.gif
PostPosted: Tue Jun 29, 2021 3:38 pm    Post subject: Reply with quote

I found the cause but don't know yet why. After reseting Micro (button push) I have to disconnect usb (one as a power) and second usb where is my uart-usb converter. Also close Bascom terminal. Then connect both usb and open terminal. Then everything works. It seems strange this Micro to me (the way I have to deal with).
Thank you EDC and MWS.

Regards,
Back to top
View user's profile
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 81

poland.gif
PostPosted: Thu Jul 08, 2021 8:04 am    Post subject: Reply with quote

So in the end working project. Interesting is that after changing my usb-uart converter I had no more problems with the Micro. Here you can see finished machine https://www.youtube.com/watch?v=GtmhI39CMJY. My part was Micro and still i'm writing a ladder program for industrial controller for whole machine.
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