Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Specific Hardware Topics > Electronics & Electrics

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 31st December 2010, 8:00 PM   #1
BeanerSA Thread Starter
Member
 
BeanerSA's Avatar
 
Join Date: Aug 2009
Location: Gateway to the Barossa
Posts: 3,531
Default AVR CTC Mode

Can I also service an ISR whilst operating a timer in CTC mode, or is it just one or the other?

I have already written my code to run an ISR as well as clear the counter, but it doesn't work, so I'm wondering if it is even possible.

Anyone have any ideas?
__________________
Quote:
Originally Posted by Madengineer View Post
If you follow the rules and use your brains. Everything works out.
BeanerSA is online now   Reply With Quote

Join OCAU to remove this ad!
Old 31st December 2010, 8:17 PM   #2
paulvk
Member
 
Join Date: Mar 2010
Posts: 816
Default

What is the code written in?
paulvk is offline   Reply With Quote
Old 31st December 2010, 8:17 PM   #3
dohzer
Member
 
dohzer's Avatar
 
Join Date: Jan 2002
Location: Melbourne
Posts: 5,282
Default

Looks like you can, according to the ATmega8 datasheet:

Quote:
Clear Timer on Compare Match (CTC) Mode

In Clear Timer on Compare or CTC mode (WGM13:0 = 4 or 12), the OCR1A or ICR1 Register are used to manipulate the counter resolution. In CTC mode the counter is cleared to zero when the counter value (TCNT1) matches either the OCR1A (WGM13:0 = 4) or the ICR1 (WGM13:0 = 12). The OCR1A or ICR1 define the top value for the counter, hence also its resolution. This mode allows greater control of the Compare Match output frequency. It also simplifies the operation of counting external events.

The timing diagram for the CTC mode is shown in Figure 37. The counter value (TCNT1) increases until a Compare Match occurs with either OCR1A or ICR1, and then counter (TCNT1) is cleared.

An interrupt can be generated at each time the counter value reaches the TOP value by either using the OCF1A or ICF1 Flag according to the register used to define the TOP value. If the interrupt is enabled, the interrupt handler routine can be used for updating the TOP value. However, changing the TOP to a value close to BOTTOM when the counter is running with none or a low prescaler value must be done with care since the CTC mode does not have the double buffering feature. If the new value written to OCR1A or ICR1 is lower than the current value of TCNT1, the counter will miss the Compare Match. The counter will then have to count to its maximum value (0xFFFF) and wrap around starting at 0x0000 before the Compare Match can occur. In many cases this feature is not desirable. An alternative will then be to use the fast PWM mode using OCR1A for defining TOP (WGM13:0 = 15) since the OCR1A then will be double buffered.

etc
Which micro are you using?
dohzer is offline   Reply With Quote
Old 31st December 2010, 8:22 PM   #4
paulvk
Member
 
Join Date: Mar 2010
Posts: 816
Default

And here is something to keep in mind.

When an interrupt is serviced no other interrupts can occur because the processor will disable all interrupts by clearing the master interrupt enable bit.
paulvk is offline   Reply With Quote
Old 31st December 2010, 8:30 PM   #5
BeanerSA Thread Starter
Member
 
BeanerSA's Avatar
 
Join Date: Aug 2009
Location: Gateway to the Barossa
Posts: 3,531
Default

I'm currently using an old 8535, but am looking to migrating to an atmega48/88.

As it works now, I have one timer producing a 2kHz output on it's output pin, and have fed this into, the input of the other timer, configured as a counter. The counter counts up to 999 (as I'm trying to time to 1/1000th of a sec), and then the CTC portion is working okay, the counter resets to zero. What isn't working is that I also have an ISR at OC1A, that should increment a variable, SECS.

I haven't used the counter in overflow, as I just want to be able to read the value of the counter at a given time and determine how many 1/1000ths have elapsed. Preloading and using overflow, I felt, was going to involve to much math.
__________________
Quote:
Originally Posted by Madengineer View Post
If you follow the rules and use your brains. Everything works out.
BeanerSA is online now   Reply With Quote
Old 31st December 2010, 10:37 PM   #6
SCwerTA
Member
 
SCwerTA's Avatar
 
Join Date: Jan 2005
Location: SE Melbourne
Posts: 234
Default

Is your code written in asm or C?

Firstly, if its C, and your SECS variable is shared with main and the ISR, check that the variable is volatile.

Also does the 2KHz timer serve another purpose other than triggering the second timer? Just asking because its a roundabout way of setting up a 1ms counter essentially.
SCwerTA is offline   Reply With Quote
Old 1st January 2011, 1:06 AM   #7
dohzer
Member
 
dohzer's Avatar
 
Join Date: Jan 2002
Location: Melbourne
Posts: 5,282
Default

Might want to post the code, or at least a minimal version of it.
dohzer is offline   Reply With Quote
Old 2nd January 2011, 9:35 PM   #8
BeanerSA Thread Starter
Member
 
BeanerSA's Avatar
 
Join Date: Aug 2009
Location: Gateway to the Barossa
Posts: 3,531
Default

Quote:
Originally Posted by paulvk View Post
And here is something to keep in mind.

When an interrupt is serviced no other interrupts can occur because the processor will disable all interrupts by clearing the master interrupt enable bit.
Yep I was aware of that, but aren't interrupt bits set in the register and then serviced sequentially?

Quote:
Originally Posted by SCwerTA View Post
Is your code written in asm or C?

Also does the 2KHz timer serve another purpose other than triggering the second timer? Just asking because its a roundabout way of setting up a 1ms counter essentially.
Last bit first; no that's all the 2kHz timer does, but it does it with no software overhead, it's just set and forget. I got the idea from the appnote for Long Delay Generation.

Secondly; sorry, only Bascom for me, already familiar with VB type languages as I'm not a real programmer

Code:
$regfile = "8535def.dat"
$crystal = 8000000

Config Lcd = 20 * 4
Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.1 , Db7 = Portc.0 , E = Portc.4 , Rs = Portc.5
Cursor Off

Dim Seconds As Byte
Dim Minutes As Byte

Config Porta = Input
'Porta = &B11111111 'uncomment for internal pullups

Config Timer2 = Timer , Async = Off , Prescale = 32 , Compare = Toggle , Clear Timer = 1
Config Timer1 = Counter , Edge = Rising , Compare 1a = Disconnect , Clear Timer = 1

Enable Interrupts
Enable Oc2
Enable Oc1a
On Oc1a Addseconds:
Compare1a = &HCE7
Ocr2 = &H7C                                                 '0.5ms Pulse / 2000Hz

Do
   Cls
   Lcd Timer2
   Lowerline
   Lcd Timer1
   Thirdline
   Lcd Seconds
   Waitms 20
Loop

Addseconds:
   Incr Seconds
   Return


End                                                         'end program
__________________
Quote:
Originally Posted by Madengineer View Post
If you follow the rules and use your brains. Everything works out.
BeanerSA is online now   Reply With Quote
Old 5th January 2011, 9:34 PM   #9
BeanerSA Thread Starter
Member
 
BeanerSA's Avatar
 
Join Date: Aug 2009
Location: Gateway to the Barossa
Posts: 3,531
Default

Anyone? (bump)
__________________
Quote:
Originally Posted by Madengineer View Post
If you follow the rules and use your brains. Everything works out.
BeanerSA is online now   Reply With Quote
Old 7th January 2011, 4:01 PM   #10
BeanerSA Thread Starter
Member
 
BeanerSA's Avatar
 
Join Date: Aug 2009
Location: Gateway to the Barossa
Posts: 3,531
Default

Quote:
Originally Posted by IntelInside View Post
You'll probably find that the experts here are more familiar with C, so it could pay you to convert over to it.
I see your point, but this is mostly a hardware level question, and surely the Bascom code isn't that cryptic, is it?
__________________
Quote:
Originally Posted by Madengineer View Post
If you follow the rules and use your brains. Everything works out.
BeanerSA is online now   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 3:01 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!