Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Timer Counter Question
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 Unsupported versions
View previous topic :: View next topic  
Author Message
Sebastian.Heyn

Bascom Member



Joined: 13 Apr 2005
Posts: 86

PostPosted: Sun Jan 22, 2017 11:56 pm    Post subject: Timer Counter Question Reply with quote

Sorry for bothering. Maybe Im not that good on googling today

I need to measure a frequency.

At the moment I'm doing it like that:

There is also a overflow interrupt that increments the timer0_ov variable

Code:


do
loop until speed_in=0
enable timer0
start timer0
timer0=0
   do
   loop until speed_in=1
   do
   loop until speed_in=0
stop timer0

act_counts=256*timer0_ov
act_counts=act_counts+timer0
step_dura=duration_8*act_counts

 


now I kept reading and it should be possible to use the counter function, right?

If so I understand I can still use ovf0 interrupt for my overflow increment. But is it possible to trigger an interrupt every 2 edges?

(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
Goorman

Bascom Member



Joined: 12 Jul 2017
Posts: 13

PostPosted: Sat Jul 22, 2017 10:03 pm    Post subject: Reply with quote

Hi
I want to configure time2 as timer for overload interrupt on Atmega8.
I can’t fine any sample .
Kindly please help me?
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Jul 23, 2017 4:05 am    Post subject: Reply with quote

Goorman wrote:
I want to configure time2 as timer for overload interrupt on Atmega8.
Searching for overflow interrupt will be more successful.
Back to top
View user's profile
Goorman

Bascom Member



Joined: 12 Jul 2017
Posts: 13

PostPosted: Sun Jul 23, 2017 3:25 pm    Post subject: Reply with quote

It Was easy.
''''''''''''''''''''''''''''''''''''''
$regfile = "m8def.dat"
$crystal = 8000000
Config Portb.5 = Output
Config Timer2 = Timer , Prescale = 256
On Timer2 Timer2isr
Enable Interrupts
Enable Timer2
Timer2 = 0
Do
'main program loop
Loop

Timer2isr:
Toggle Portb.5
Return

Thanks
Back to top
View user's profile
Goorman

Bascom Member



Joined: 12 Jul 2017
Posts: 13

PostPosted: Sun Jul 23, 2017 7:31 pm    Post subject: Simple frequency meter on Atmega8 Reply with quote

Simple frequency meter on Atmega8
It's my first code.Laughing

Code:
'this program is tested on PROTEUS
'Inject pulse between  range 500 to 31k hertz  to PD4
$regfile = "m8def.dat"
$crystal = 8000000
$baud = 9600
Config Timer2 = Timer , Prescale = 256
On Timer2 Timer2isr : Enable Interrupts : Enable Timer2
Config Timer0 = Counter , Edge = Rising : Start Timer0
Dim F As Word , R As Byte

Do
If R > 0 Then : F = F * 121 : Print F : R = 0 : End If
Loop

Timer2isr:
If R = 0 Then : F = Timer0 : R = 1 : Timer0 = 0 : End If
Return
Back to top
View user's profile
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Tue Jul 25, 2017 8:05 am    Post subject: Reply with quote

Quote:
PostPosted: Sun Jul 23, 2017 8:31 pm Post subject: Simple frequency meter on Atmega8
Simple frequency meter on Atmega8
It's my first code.

And where is the measured frequency in your code?

The principle: (signal on PD5)
Code:
pseudocode
Dim frequency as word
Config timer1 as counter.

Do
   clear timer1
   waitms 1000         'timer counts pulses for 1 second
   frequency = timer1  'number of pulses after 1 sec = frequency [Hz]
   display frequency
Loop
 

This way you can measure frequency 1 - 65535 Hz with resolution 1 Hz.
Instead of "waitms 1000" you can use another timer to make an exact delay 1 second.
Back to top
View user's profile
Goorman

Bascom Member



Joined: 12 Jul 2017
Posts: 13

PostPosted: Tue Jul 25, 2017 11:14 am    Post subject: Reply with quote

Hi Visovian
Sorry I forgot to comment.
You can find frequency in “F As Word” in my code.
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Wed Jul 26, 2017 12:05 am    Post subject: Reply with quote

Since its your first code, that is fine. But as a point to consider, both for other users, and for yourself later when you have to debug some big complex program you wrote 5 years ago, its much better to comment your code with stuff about what it is intended to do, and why.

Its not necessary to comment every line, for instance there is no point in doing
Code:

A = B * C 'Multiply B times C and store result in A
 

But it is worth commenting what the multiply is for, and what the variables are.

As an adjunct to that, rather than calling variables by short names like F etc, its much better to call them things like Frequency, some meaningful name that makes it obvious what the variable is. Ancient Basic interpreters had very strict limitations about lengths of variables, and the longer the name, the longer the code took to execute. Those restrictions went away about 30 years ago. With modern compilers like Bascom, the length of a name only costs you typing time, the compiler will make exactly the same machine code, and execute in exactly the same time, regardless of how you name the variables. But you will in the end save lots of time head-scratching when you look back at your code much later.

Even better IMHO, is to prefix the name with the variable type, so F becomes wFrequency, meaning a word variable. That way you know immediately from the code what sorts of things the variable can hold, how its represented in memory, and how you can use it. Note I use what is called CamelCase, ie it has 'humps' in it where appropriate. Other people use w_Frequency. Its a matter of taste. There are useful rules about what prefixes are used, and how to apply them in different languages. Look up "coding prefix notation" on Google.

You do have to turn off the Bascom automatic case setting to use this, else the editor will capitalise the first letter of every name.

Have fun Smile

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

Bascom Member



Joined: 12 Jul 2017
Posts: 13

PostPosted: Wed Jul 26, 2017 3:59 pm    Post subject: Reply with quote

Hi, AdrianJ
Wow Applause , thank you very much for giving me a great gift. I will certainly follow it.
Back to top
View user's profile
Goorman

Bascom Member



Joined: 12 Jul 2017
Posts: 13

PostPosted: Fri Jul 28, 2017 12:30 am    Post subject: Reply with quote

The bad news is that the previous my frequency meter routine has a bug.
But I changed it and now it measures one Hz to 31kHz.
There is some inaccuracy in measuring at high frequencies. But it's not so critical to me
Dear Visovian and AdrianJ , Thank you for helping me improve

Code:
'this program is tested on PROTEUS
'Inject pulse between  range 1 to 31k hertz to PD4
$regfile = "m8def.dat"
$crystal = 8000000
$baud = 9600
$hwstack = 32       ' default use 32 for the hardware stack
$swstack = 10       ' default use 10 for the SW stack
$framesize = 40       ' default use 40 for the frame space
Config Timer2 = Timer , Prescale = 256
On Timer2 Timer2isr : Enable Interrupts : Enable Timer2
Config Timer0 = Counter , Edge = Rising : Start Timer0
'W_frq as Current value of frequency
'Frq_colector as Pulse packet collector
Dim W_frq As Word , Frq_colector As Byte
'main loop
Do
If Frq_colector = 123 Then : Print W_frq : W_frq = 0 : Frq_colector = 0 : End If
Waitms 400       'This delay actually simulates the main program's delay
Loop

Timer2isr:
If Frq_colector < 123 Then : W_frq = W_frq + Timer0 : Incr Frq_colector : End If
Timer0 = 0       'This command was not in the correct location. It caused a bug in my previous post
Return
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Fri Jul 28, 2017 12:58 am    Post subject: Reply with quote

That looks nicer ! And is easier to see what you are doing.
_________________
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: Fri Jul 28, 2017 9:54 am    Post subject: Reply with quote

Not bad for a beginner.

Quote:
There is some inaccuracy in measuring at high frequencies.
It is because you count pulses not for 1 sec, but for 122*0.8192 = 999,424 ms.
So your result is 1/1000 less than exact frequency.
You can correct it by adding freq/1000 to the result.

Clearing timer0 in every interrupt probably adds a further little error.
To avoid it, you can use interrupt also for counter0.


Code:
Config Timer2 = Timer , Prescale = 256
Enable Timer2
On Timer2 Timer2_isr

Config Timer0 = Counter , Edge = Rising
Enable Timer0
On Timer0 Counter0_isr

Dim Frequency As Word
Dim Counter0_ovf_count As Byte
Dim Timer2_ovf_count As Byte
Dim Timer2_flag As Byte

Enable Interrupts

'===========================================

Do
   If Timer2_flag = 1 Then
      Print Frequency
      Timer2_flag = 0
   End If
Loop

'===========================================

Timer2_isr:                        'every 8.192 ms
   Incr Timer2_ovf_count           'count overflows

   If Timer2_ovf_count >= 122 Then ' 1 sec elapsed (122*8.192 = 999,424 ms)
      Stop Counter0
      Frequency = Counter0_ovf_count * 16
      Frequency = Frequency + Counter0
      Timer2_flag = 1              'freq value updated

      Counter0 = 0                 'clear all
      Counter0_ovf_count = 0
      Timer2 = 0
      Timer2_ovf_count = 0

      Start Counter0
   End If

Return
'--------------------------------

Counter0_isr:
   Incr Counter0_ovf_count
Return
 


More advanced method for exact 1 sec delay is using a timer in CTC mode.
Back to top
View user's profile
Goorman

Bascom Member



Joined: 12 Jul 2017
Posts: 13

PostPosted: Fri Jul 28, 2017 2:06 pm    Post subject: Reply with quote

Visovian Applause
next time I use the CTC
I need to practice more .
And, are you testing me by 16? I changed it to 256
Thanks a lot
Back to top
View user's profile
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Fri Jul 28, 2017 8:29 pm    Post subject: Reply with quote

Hi Goorman,

You are right. Good eye.
Of course it should be
Frequency = Counter0_ovf_count * 256
I did not test it in real.

Why do you not use timer1 for counting pulses?
Back to top
View user's profile
Goorman

Bascom Member



Joined: 12 Jul 2017
Posts: 13

PostPosted: Fri Jul 28, 2017 10:51 pm    Post subject: Reply with quote

Hi Visovian
Why don't I use Timer1?
Because I need two PWMs as DAC to control voltage and current’s SMPS.
This SMPS will feed a high voltage circuit.
Well, I want to use timer2 for main program timing E.g. Debouncing keys and etc.
At end, I saw that the timer 0 was left unused. I said is it not good to read the high voltage’s frequent by it?
Now I am going to study AVR ADC and SPI because I need them too.
But finally, I must scramble on the ARM. Therefore I have to push the computer shift button and start to writ “; ++ ! {} |! & # " Instead of BASIC
Back to top
View user's profile
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 Unsupported versions 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