Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

xmega frequency meter with 32bit counter

 
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
Debu

Bascom Member



Joined: 06 Jan 2008
Posts: 21
Location: Dhaka

bangladesh.gif
PostPosted: Thu May 29, 2014 11:57 am    Post subject: xmega frequency meter with 32bit counter Reply with quote

Hi,
All

I'm trying to make a frequency meter with xmega32a4 here is the code.
I only put 1KHz square wave to PORTC.0. Display show 32976.
I want to count pulse in every second.
I connect 32.768KHz Cristal with TOSC1&2.
I want measure at list 16MHz with more accuracy & less error.
No multiply coz error will be multiply also no capture pulse width.
In this system I want to make "Start TC>1Hz RTC interrupt> Stop TC> Read 32bit > Reset TC> start next count".
Or every interrupt read value CCA&CCB>Restart count> add two 16bit> Display.
A block diagram in attach file. don't know it is right or wrong.
My problem is in code. I get wrong result in LCD display(32976Hz).

Please help !!!!!!!!

Sorry for my bad English.


Code:






$regfile = "Xm32A4def.dat"
$crystal = 32000000
$hwstack = 84
$swstack = 80
$framesize = 80
$lib "xmega.lib" : $external _xmegafix_clear : $external _xmegafix_rol_r1014

Dim A As Long
Dim Dig As Bit,update_display as  Bit
Dig = 0
Dim Dig_1 As Bit
Dig_1 = 0
Dim Result_1 As Single , Result_2 As Single

'Config Osc = Enabled , 32mhzosc = Enabled
'Config Sysclock = 32mhz

Config Osc = Disabled , 32mhzosc = Enabled , 32khzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1

'Config LCD 4-Bit Mode 3.3 Volt and R/W --> GND
Config Lcdpin = Pin , Db4 = Porta.3 , Db5 = Porta.2 , Db6 = Porta.1 , Db7 = Porta.0 , E = Porta.4 , Rs = Porta.5
Config Lcd = 16x2 , Chipset = Dogm162v3

'Config PIN as input, PORTC.0 as input for capture
Config Xpin = Portc.0 , Invertio = Enabled , Slewrate = Disabled , Outpull = Buskeeper , Sense = Rising

'Config EVENT
Config Event_system = Dummy , _
Mux0 = Portc.0 , _
Mux1 = Rtc_cmp , _
Mux2 = Tcc0_ovf , _
Mux3 = Tcc0_ccb

Config Clock = Soft , GOSUB = SECTIC,Rtc = 1KHZ_INT32KHZ_ULP '{rcosc}  Select the internal 1 KHz clock from the 32KHz internal oscillator ULP
Config Priority = Static , Vector = Application , Lo = Enabled       ' the RTC uses LO priority interrupts

Config Tcc0 = Normal , Prescale = 1 , Comparea = Enabled , Compareb = Enabled , Comparec = Enabled , Compared = Enabled , Event_source = E0 , Event_action = Freq
Config Tcc1 = Normal , Prescale = 1 , Comparea = Enabled , Compareb = Enabled , Comparec = Enabled , Compared = Enabled , Event_source = E2 , Event_action = Freq
Config Portc = Input

Enable Interrupts


Cls
Cursor Off, NoBlink
Locate 1 , 1 : Lcd "TIMRE TEST#2a EX"
Locate 2 , 1 : Lcd "TEST  COUNTER#2a"
Wait 1
Cls
'----------------------------------Main---------------------------------------------

Do
If update_display = 1 Then
Toggle Dig                     'for program run check
'(
'for add need shift Result_2
Result= Result_1+ Result_2          (32976????)
Locate 1 , 1 : Lcd "F=" ; result ; " Hz    "
')

Locate 1 , 1 : Lcd "F=" ; Tcc0_ccabuf ; " Hz  LSB"
Locate 2 , 1 : Lcd Tcc1_ccbbuf ; Result_1 ; " MSB  "
Locate 2 , 13 : Lcd Dig ; RTC_INTCTRL ; Dig_1
update_display=0
End If


Loop
End                                                         'end program


'-------------------------------------------------------------------------------

SECTIC:
update_display=1
Result_1= Tcc0_ccabuf
Result_2= Tcc0_ccbbuf
Toggle Dig_1          'for program run check
Tcc0_ccabuf = 0       'Clear the Tcc0_ccabuf Or reset timer0
Tcc0_ccbbuf = 0
return
'------------------------------------------END--------------------------------


(BASCOM-AVR version : 2.0.7.6 , Latest : 2.0.7.7 )

Please login to see attach file
Back to top
View user's profile AIM Address Yahoo Messenger MSN Messenger
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 943
Location: France

france.gif
PostPosted: Sat May 31, 2014 10:51 am    Post subject: Reply with quote

Hello,
just some tips, of an old man!
1) Never let too much code in a interruption, you could loss some informations, let a flag :
dim flag as byte
flag=0

Sectic:
flag=1
return

and in the main code:
if flag=1 then
'your action
---
---
flag=0
endif

2) dim a bit is not necessary, Byte is easier to use : your ÁP work with word of 1 byte, it compare word of 8bits to find a bit inside a room place (a byte) it need more time

3) for your variable forget name as "dim A as byte" , when you debug you could search A and then the editor will stop for each A, prefer : dim Abyte as byte, Aword as word, Ainteger as integer

and enjoy Bascom
JP Wink
Back to top
View user's profile Visit poster's website
Debu

Bascom Member



Joined: 06 Jan 2008
Posts: 21
Location: Dhaka

bangladesh.gif
PostPosted: Sat May 31, 2014 12:54 pm    Post subject: Reply with quote

Thank you !!!!!
Duval JP

for your advice.
I change the code in interrupt but result is same.
Also change bit to byte but when I write toggle then that variable toggle 1 to 255.
That is not important I write those for check program run or not. I can erase those.

but I can't get the result yet.
I think something wrong in config but I can't find that.
Code:


SECTIC:
update_display=1
return

 
Back to top
View user's profile AIM Address Yahoo Messenger MSN Messenger
Debu

Bascom Member



Joined: 06 Jan 2008
Posts: 21
Location: Dhaka

bangladesh.gif
PostPosted: Mon Jun 02, 2014 12:23 pm    Post subject: Reply with quote

When I put low frequency the display value is high also
when I increase frequency the display shown low value.
So it is actually measure the pulse width, I want count the number of pulse.

Have you any idea ????????
Back to top
View user's profile AIM Address Yahoo Messenger MSN Messenger
Debu

Bascom Member



Joined: 06 Jan 2008
Posts: 21
Location: Dhaka

bangladesh.gif
PostPosted: Wed Jun 18, 2014 11:02 am    Post subject: Reply with quote

closer to success
Now I put 1KHz from external RTC but display show F= 1006 Hz.
Here the system count extra 6 Hz.
If I change clock source for internal {rtc Rtc = 1khz_int32khz_rcosc} to {Rtc = 1khz_int32khz_ulp} then frequency show F= 995 Hz.
For this error when I put 4 MHz the system show F= 4024000Hz Same error in ULP


Code:





$regfile = "Xm32A4def.dat"
$crystal = 32000000
$hwstack = 84
$swstack = 80
$framesize = 80
$lib "xmega.lib" : $external _xmegafix_clear : $external _xmegafix_rol_r1014

Dim Dig As Byte , Update_display As Byte
Dig = 0
Dim Dig_1 As Byte
Dig_1 = 0
Dim Result_1 As Single , Result_2 As Single , Result_f As Single


Config Osc = Disabled , 32mhzosc = Enabled , 32khzosc = Enabled , Startup = 32khz
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1

'Config LCD 4-Bit Mode 3.3 Volt and R/W --> GND
Config Lcdpin = Pin , Db4 = Porta.3 , Db5 = Porta.2 , Db6 = Porta.1 , Db7 = Porta.0 , E = Porta.4 , Rs = Porta.5
Config Lcd = 16x2 , Chipset = Dogm162v3

'Config EVENT
Config Event_system = Dummy , _
Mux0 = Portc.0 , _
Mux1 = Rtc_ovf , _
Mux2 = Tcc0_ovf , _
Mux3 = Tcc0_ccb


'Config PIN as input, PORTC.0 as input for capture
Config Portc.0 = Input
Config Xpin = Portc.0 , Invertio = Enabled , Slewrate = Disabled , Outpull = Buskeeper , Sense = Rising

                              '1khz_int32khz_ulp,1KHZ_32KHZ_CRYSTOSC ,1KHZ_INT32KHZ_RCOSC,32KHZ_32KHZ_CRYSTOSC
Config Clock = Soft , Gosub = Sectic , Rtc = 1khz_int32khz_rcosc       '{rcosc}  Select the internal 1 KHz clock from the 32KHz internal oscillator ULP
Config Priority = Static , Vector = Application , Lo = Enabled , Med = Enabled       ' the RTC uses LO priority interrupts
'rtc_ctrl=&b011

Config Tcc0 = Normal , Prescale = E0 ,  Event_source = E0 , Event_action = Capture
Config Tcc1 = Normal , Prescale = E2 , Event_source = E2 , Event_action = Freq
Config Portc = Input

'Tcc0_per = 50
Enable Interrupts


Cls
Cursor Off, NoBlink
Locate 1 , 1 : Lcd "TIMRE TEST#3b EX"
Locate 2 , 1 : Lcd "TEST  COUNTER#4a"
Wait 1
Cls
'--------------------------Main-----------------------------------------------------

Do


If update_display = 1 Then
   Shift Result_2 , Left , 16
   Result_f = Result_2 + Result_1
   Locate 1 , 1 : Lcd "F=" ; Result_f ; "Hz       "
   Locate 2 , 1 : Lcd Result_2 ; "+" ; Result_1             'for show Tcc0_cnt and Tcc1_cnt vallue
   Update_display = 0
    Incr Dig
    If Dig > 8 Then Dig = 0
End If

Locate 2 , 16 : Lcd Dig

Loop
End                                                         'end program
'------------------------------ 1 Sec INT-------------------------------------------

SECTIC:
Update_display = 1
  Result_1 = Tcc0_cnt
  Result_2 = Tcc1_cnt
   Tcc0_cnt = 0
   Tcc1_cnt = 0

return

 
Back to top
View user's profile AIM Address Yahoo Messenger MSN Messenger
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
Page 1 of 1

 
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