Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Timer0 problem
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
ecoology

Bascom Member



Joined: 07 Nov 2006
Posts: 340
Location: USA - California

usa.gif
PostPosted: Tue Jul 22, 2008 12:29 am    Post subject: Timer0 problem Reply with quote

This is probably going to be one of those "you're a dummy questions."

I want to use timer0 as an interrupt timer. This should be simple. I am using the mega324, BASCOM 1.11.9.2
Code:

Config Timer0 = Timer , Prescale = 64 : Stop Timer0
Enable Interrupts                                 'allow interrupts to occur
On Ovf0 Tim0_isr:

Timer0  = 0 : Enable Timer0 : Start Timer0  

Do
    Toggle portd.0
loop      

'************** ******
Tim0_isr:
Stop Timer0 : Disable Timer0
Reset Portd.0
Waitms 100
Goto Begin
Return
 

I set up the timer, then start it. To see where I am going, I go into an infinite loop, toggling portd.0 at about 1ms intervals. If I get to the ISR, I would see portd.0 stay low for 100ms.
I never get to the ISR. Portd.0 just keeps toggling forever at a 1ms rate.
Can some kind sole point out what I am doing wrong? I do not have much more hair to pull out!
Back to top
View user's profile Visit poster's website
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Tue Jul 22, 2008 1:39 am    Post subject: Reply with quote

You dont have a label for Begin, which might do a reset.

I would do it more like this: ( untested code ! )
Code:

Config Timer0 = Timer , Prescale = 64 : Stop Timer0
Enable Interrupts                                 'allow interrupts to occur
On Ovf0 Tim0_isr:

dim bTimerFlag as byte

Timer0  = 0 : Enable Timer0 : Start Timer0


Do
if bTimerflag.0 = 1
 reset bTimerflag.0
 disable timer0
 reset portd.0
 waitms 100
 enable timer0 'if you want it to repeat
else

    Toggle portd.0
end if

loop      

'************** ******
Tim0_isr:
set bTimerFlag.0

Return
 


The way you had it, it would probably only get the IRQ once, then stop, since you disable the timer IRQ.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Tue Jul 22, 2008 7:42 am    Post subject: Reply with quote

Hi,
Code:
Tim0_isr:
Stop Timer0 : Disable Timer0
Reset Portd.0
Waitms 100
Goto Begin
Return


When entering ISR, all interrupts are automatically disabled. You need not Disable Timer0.

And interrupts are reenabled at exit from isr (with asm command RETI).
In your example how do you reach the end of Tim0_isr?

When you jump from isr with GOTO without returning (which I do not like too much), then the subroutine should end with RETI:

Code:

Begin:
'some code
!reti


I hope I am not wrong.
Back to top
View user's profile
ecoology

Bascom Member



Joined: 07 Nov 2006
Posts: 340
Location: USA - California

usa.gif
PostPosted: Tue Jul 22, 2008 5:00 pm    Post subject: Reply with quote

Almost out of hair...
Fading fast!

I've tried the suggestions offered, plus a lot more. It seems nothing will cause the timer0 to time out and jump to the ISR. The code I currently have is:
Code:

Dim Readreg as bit
Config Timer0 = Timer , Prescale = 64 : Stop Timer0
Enable Interrupts                                 'allow interrupts to occur
On Ovf0 Tim0_isr:

Readreg = 0
Start timer0
Do
           If Readreg = 1 Then
                 Reset Portd.0        ' pull portd.0 low to see ISR effect
                 Readreg = 0
                Waitms 100
           End If
          Set Portd.0                  'toggle Portd.0 fast
         Waitms 1
         Reset Portd.0
        Waitms 1
Loop

'********************
Tim0_isr:
Stop Timer0
Readreg = 1
Reset Timer0
Start Timer0
Return
' ********************
 

I am missing something here. I hope someone can point out what I am sure is the obvious.
Back to top
View user's profile Visit poster's website
DToolan

Bascom Member



Joined: 14 Aug 2004
Posts: 1384
Location: Dallas / Fort Worth, Texas (USA)

blank.gif
PostPosted: Tue Jul 22, 2008 5:16 pm    Post subject: Reply with quote

** Removed **

Last edited by DToolan on Wed Jul 23, 2008 12:17 am; edited 1 time in total
Back to top
View user's profile Yahoo Messenger
ecoology

Bascom Member



Joined: 07 Nov 2006
Posts: 340
Location: USA - California

usa.gif
PostPosted: Tue Jul 22, 2008 5:44 pm    Post subject: Reply with quote

Using this code exactly, which I agree with, does not work. something else is going on.
I am using the mega324, and I've gone through the data sheet, but I do not see anything different from other megas. The processor works correctly in all respects except this. Now wondering if I have a defective chip - the timer0 is just bad????
This is one of those - ITS GOTTA WORK issues, but it doesn't
I am going to build up another board and see if this is an isolated case.
Back to top
View user's profile Visit poster's website
ecoology

Bascom Member



Joined: 07 Nov 2006
Posts: 340
Location: USA - California

usa.gif
PostPosted: Tue Jul 22, 2008 6:01 pm    Post subject: Reply with quote

This is an example of "don't beat your brains in!"
I spent two days trying to get a simple interrupt using timer0 to work.
I just changed over to timer1, and it works!
There is a defect in the chip - timer0 is non-functional. Everything else works, and timer1 works as advertised.
Thanks for the inputs. Seeing that I wasn't that far off track led me to look at the chip itself. They can fail. It wasn't a BASCOM issue at all.
Long Live BASCOM!
Back to top
View user's profile Visit poster's website
rileyesi

Bascom Member



Joined: 19 Dec 2006
Posts: 398

blank.gif
PostPosted: Tue Jul 22, 2008 6:19 pm    Post subject: Reply with quote

Ecoology,

This may or may not help, but years ago I designed a board using the old 4433 chip. Out of sheer dumb (bad!) luck, I used three pins on port b to dirve a D to A converter. A while later, I used the same board on a different design that needed to use timer0. I could not get it to work no matter what I did.

Then, I read the data sheet more closely and found (to my horror) that you needed port b to be able to use the timer0! (or something like that. My memory is kinda fuzzy on the details. maybe because I blocked them out!)

I did not look at the data sheet for your chip. Maybe there is a similar problem??

Hope this helps.

Pete
Back to top
View user's profile
ecoology

Bascom Member



Joined: 07 Nov 2006
Posts: 340
Location: USA - California

usa.gif
PostPosted: Tue Jul 22, 2008 6:24 pm    Post subject: Reply with quote

I'll take a look, but I don't recall seeing anything like that. These chips are so complex, that who knows? Anything is possible. All I know right now is that timer1 works perfectly, and timer0 is dead. out of a couple million transistor-equivalents, it only takes one failure to stop a function if not the whole chip.
Back to top
View user's profile Visit poster's website
rileyesi

Bascom Member



Joined: 19 Dec 2006
Posts: 398

blank.gif
PostPosted: Tue Jul 22, 2008 6:33 pm    Post subject: Reply with quote

One other thought.

The following is a code snippet from one of the sample programs that came with BASCOM:

Code:

Config Clock = Soft                                         'this is how simple it is
'The above statement will bind in an ISR so you can not use the TIMER anymore!
 


Maybe???

Pete
Back to top
View user's profile
ecoology

Bascom Member



Joined: 07 Nov 2006
Posts: 340
Location: USA - California

usa.gif
PostPosted: Tue Jul 22, 2008 6:38 pm    Post subject: Reply with quote

Interesting, but I am not using any config clock statments.
I am still in trouble because the timer1 is a 16 bit timer. Even with prescale = 1, I can't get a fast enough timout. Best I get is about 120 ms. I need a timout of about 20 ms.
Maybe a Config clock not so soft???
Back to top
View user's profile Visit poster's website
rileyesi

Bascom Member



Joined: 19 Dec 2006
Posts: 398

blank.gif
PostPosted: Tue Jul 22, 2008 6:46 pm    Post subject: Reply with quote

I have a program that needs 50 ms loops and I simply use the WAITMS command. I could not get it to run using the TIMERs.

I'm not saying that it can't be done by someone smarter than me!

Good luck.

Pete
Back to top
View user's profile
ecoology

Bascom Member



Joined: 07 Nov 2006
Posts: 340
Location: USA - California

usa.gif
PostPosted: Tue Jul 22, 2008 7:05 pm    Post subject: Reply with quote

These timers should be simple to use. They were designed for interrupt operations, and the BASCOM commands were intended to use as int generators. we are not doing anything so complicated, why would the timers be such a pain?
Timer1 does work well, just not fast enough. its timer0 that seems to be a problem.
It seems like there is something in configuring the timer.
I've had other problems setting registers directly. I prefer to manipulate the registers so I know exactly what is going on, but that has pitfalls. i've sent a message to Mark, asking for clarification on how to handle the registers. Maybe the BASCOM command is not setting something for timer0?
Back to top
View user's profile Visit poster's website
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Tue Jul 22, 2008 11:06 pm    Post subject: Reply with quote

To get timer1 to have an 8 bit timeout period, just set its hi byte to &hff, and if course its low byte to 0. Be aware that you need to set the hi and lo bytes in the correct order - write the hi byte first. Bascom handles this correctly if you just do:
Timer1 = &hff00

You certainly seem to have problems with timer0, or maybe just that chip. I dont have that specific processor handy, but I certainly use timer0 with other processors ( AtMega64, 644, 16, 32 ) without problems.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
ecoology

Bascom Member



Joined: 07 Nov 2006
Posts: 340
Location: USA - California

usa.gif
PostPosted: Tue Jul 22, 2008 11:12 pm    Post subject: Reply with quote

Thank you, Adrian.
Yes, I am having far more than reasonable issues with timer0. Others have commented that they have had problems too, so I don't feel like the Lone Ranger, but it is frustrating to waste so much time on seemingly simple things.

I'll play with your suggestion.
George
Back to top
View user's profile Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
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