View previous topic :: View next topic |
Author |
Message |
carrhd
|
Posted: Sun Apr 16, 2023 1:13 am Post subject: Help with Timer/Counter on ATTINY85 |
|
|
Greetings.
I am used to working on a MEGA2560. I've switched to an ATTINY85 for a new project. The code that I normally use to get a one second tick isn't working. COMPARE0A, OC0A, COMPARE1A, or OC1A to work. I see them in the XML file but they aren't recognized as keywords. I'm probably doing something dumb but I can't figure out what it is. It doesn't like the PRESCALE in the config either.
This is the code I normally use:
' +------------------------------------+
' | INTERRUPT SETUP - 1 SECOND TICK |
' +------------------------------------+
declare Sub Compare1a_int()
config Timer1 = Timer , Prescale = 1024 , Clear Timer = 1
on Compare1a Compare1a_int Saveall
enable Compare1a
stop Timer1
Timer1 = 0
Compare1a = 15625 ' 1 Second
start Timer1
enable interrupts
' +------------------------------------+
' | INTERRUPT SERVICE ROUTINE |
' +------------------------------------+
Sub Compare1a_int()
doIt = true
end sub
(BASCOM-AVR version : 2.0.8.2 , Latest : 2.0.8.5 ) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2289
|
Posted: Sun Apr 16, 2023 11:25 am Post subject: Re: Help with Timer/Counter on ATTINY85 |
|
|
carrhd wrote: | COMPARE0A, OC0A, COMPARE1A, or OC1A to work. I see them in the XML file but they aren't recognized as keywords. |
I have to disagree.
Output pins are called OC0/1A, it would make no sense to name compare registers this way, but COMPARE0A works for example.
Quote: | I see them in the XML file but they aren't recognized as keywords. |
You can check the ATtiny85.DAT file in Bascom's program directory and you'll find:
Code: | OCR0A = $29
PWM0A= $29
COMPARE0A= $29 |
Different naming for the same register OCR0A and register address &H29.
Quote: | I'm probably doing something dumb but I can't figure out what it is. |
You have truly a very bouquet of issues.
Quote: | It doesn't like the PRESCALE in the config either. |
Really?
Quote: | This is the code I normally use: |
I suggest you post real code and not lookalike one.
This is real code and it compiles:
Code: | $Regfile="attiny85.dat"
$Crystal=1000000 ' internal RC, CKDIV8 fuse set
$hwstack=40
$swstack=16
$framesize=32
declare Sub Compare1a_int()
Dim doIt as Byte
Const True = 1
Const False = 0
config Timer1 = Timer , Prescale = 16384 , Clear Timer = 1
on Compare1a Compare1a_int Saveall
enable Compare1a
stop Timer1
Timer1 = 0
Compare1a = 60 ' approx 1 second, can be adjusted with OSCCAL
start Timer1
enable interrupts
Do
Loop
Sub Compare1a_int()
doIt = true
end sub |
Some hints:
- without processor and stack declaration within code, settings of the IDE apply, which may be different.
- without declaring true/false, for example by a Const, the compiler won't know them.
- without dimensioning variables, it won't compile.
- without main loop you won't have much fun.
- Timer0 and Timer1 in ATTiny 85 are 8-bit timers, assigning a 16-bit value as TOP may return odd results.
- to achieve 1-sec interrupts with internal RC-Osc 8MHz and max. Timer1-prescale, the CKDIV8 needs to be set (low).
- for doing CTC (Clear Timer = 1), the TOP value is set by OCR0A in Timer0, in Timer1 by OCR1C.
- be aware that ATTiny85 sports an ATtiny15 compatibility mode, which is enabled by a certain clock setting.
- read the data sheet.
- read the data sheet.
- read the data sheet.
ATTiny85 is an interesting, as well a special processor.
Especially Timer1, which may serve as high frequency PLL timer and sports things like dead time generation for motor controllers. |
|
Back to top |
|
|
carrhd
|
Posted: Sun Apr 16, 2023 12:05 pm Post subject: |
|
|
Thank you for the reply.
It appears that part of my problem may have been that I was using an older version of Bascom. I have updated to the latest version and am getting closer.
When I compiled your code I got the error:
Error : 46 Line : 23 Assignment error, unknown variable (DIM) [COMPARE1A: 0 60: 112] , in File : G:\My Drive\Embedded\SapMaster 2021\rom-switcher-101.bas
It likes COMPARE0A but not COMPARE1A.
Hank. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2289
|
Posted: Sun Apr 16, 2023 12:34 pm Post subject: |
|
|
Look up the dat-file for COMPARE1A, if not declared add it, or use OCR1A instead.
Edit:
It compiled ok with ATTiny45, but by just checking again, not with ATTiny85.
Difference is, ATTiny45.DAT:
Code: | OCR1A =$2e
COMPARE1A=$2E
PWM1A=$2E
OCR1C = $2d
COMPARE1C=$2D
PWM1C=$2D |
ATTiny85.DAT:
Code: | OCR1A =$2e
OCR1C = $2d |
Replace the block in T85 against the one in T45 and you'll be good.
Backup the original ATTiny85.DAT first. |
|
Back to top |
|
|
carrhd
|
Posted: Sun Apr 16, 2023 1:40 pm Post subject: |
|
|
Awesome. Thanks.
I just finished moving my ducks and am going to make breakfast for my son then I will try it.
Did I mention that prescale is not working?
I'll get back to you shortly.
Hank |
|
Back to top |
|
|
carrhd
|
Posted: Sun Apr 16, 2023 3:01 pm Post subject: |
|
|
Now it works. Many thanks!
BTW...I've written tens of thousands of lines of Bascom AVR over the years. Interrupts still cause me problems but that's a me problem, not a Bascom problem.
Bascom runs this entire room.
https://www.youtube.com/watch?v=-Gz0KUlNmJ8
Have a great day.
Hank. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2289
|
Posted: Sun Apr 16, 2023 8:18 pm Post subject: |
|
|
carrhd wrote: | Now it works. Many thanks! |
You're welcome.
Quote: | BTW...I've written tens of thousands of lines of Bascom AVR over the years. |
I wasn't able to assume this the way you've put your post.
Quote: | Interrupts still cause me problems but that's a me problem, not a Bascom problem. |
Interrupts are not difficult, if one observes a few basic rules.
Quote: | Bascom runs this entire room. |
Great.
Have fun. |
|
Back to top |
|
|
oi18ct
Joined: 08 Mar 2022 Posts: 22 Location: North Branch, Minnesota
|
Posted: Mon Apr 17, 2023 5:59 am Post subject: |
|
|
Watched your YouTube video. Nice!!! Cant say I am shocked a Canadian is automating maple tree sap processing... I suppose your a curler too, like me. Nice system, looks like it was fun _________________ Lee |
|
Back to top |
|
|
|
|
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
|
|