View previous topic :: View next topic |
Author |
Message |
krolikbest
Joined: 02 Jan 2017 Posts: 112
|
Posted: Tue Jun 29, 2021 10:48 am Post subject: atmega32u4 and timer interrupt |
|
|
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 |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Jun 29, 2021 12:17 pm Post subject: Re: atmega32u4 and timer interrupt |
|
|
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 |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Tue Jun 29, 2021 12:30 pm Post subject: |
|
|
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?
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 |
|
|
krolikbest
Joined: 02 Jan 2017 Posts: 112
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Jun 29, 2021 2:36 pm Post subject: |
|
|
krolikbest wrote: | MWS - absolutely righ 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 |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Tue Jun 29, 2021 3:03 pm Post subject: |
|
|
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 |
|
|
krolikbest
Joined: 02 Jan 2017 Posts: 112
|
Posted: Tue Jun 29, 2021 3:38 pm Post subject: |
|
|
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 |
|
|
krolikbest
Joined: 02 Jan 2017 Posts: 112
|
Posted: Thu Jul 08, 2021 8:04 am Post subject: |
|
|
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 |
|
|
|