Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Hardware 24-32bit Quadratic decoding Xmega

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Ev3658

Bascom Member



Joined: 30 Nov 2014
Posts: 32

russia.gif
PostPosted: Thu Nov 30, 2017 11:57 am    Post subject: Hardware 24-32bit Quadratic decoding Xmega Reply with quote

The code works, but there is a problem at the zero point, too many interrupts can be.
I can not count hardware in plus and minus it does not work. I ask you for help, tell me, it is possible to make a 32-bit hardware account in the positive and negative directions.

Bascom 2.0.8.0
Code:

$regfile = "xm256a3def.dat"
$crystal = 33177600
$hwstack = 200
$swstack = 128
$framesize = 235

$lib "xmega.lib" : $external _xmegafix_clear : $external _xmegafix_rol_r1014


Config Osc = Enabled , Extosc = Enabled , Pllosc = Enabled , _
Range = 2mhz_9mhz , Startup = Xtal_1kclk , Pllsource = Extclock , Pllmul = 9 , Plldiv2 = Disabled , 32khzosc = Disabled       ', 32khzpowermode = Normal
Config Sysclock = Pll , Prescalea = 1 , Prescalebc = 1_1
Config Priority = Static , Vector = Application , Lo = Enabled , Med = Enabled , Hi = Enabled

'===================== UART
Config Com7 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Config Portf.2 = Input                                      'RX
Config Portf.3 = Output                                     'TX
Open "COM7:" For Binary As #1



Config Pina.0 = Input                                       'A
Config Pina.1 = Input                                       'B
Config Xpin = Pina.0 , Sense = Rising                      
Config Xpin = Pina.1 , Sense = Rising                       '
                     
Config Event_system = Dummy , Mux0 = Porta.0 , Qd0 = Enabled , Digflt0 = 2

Tcc1_per = 9999
Config Tcc1 = Normal , Prescale = E0 , Event_source = E0 , Event_action = Qdec , Event_delay = Enabled  

On Tcc1_ovf Enc1:
Enable Tcc1_ovf

Dim En1 As Byte

Do
Waitms 300
Print #1, En1;" ";Tcc1_CNT
Loop
End


Enc1:
If Tcc1_ctrlfset.0 = 0 Then
   Incr En1
   Else
    Decr En1
End If

Return

 


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

Moderator



Joined: 24 Feb 2006
Posts: 1922
Location: Denmark

denmark.gif
PostPosted: Thu Nov 30, 2017 8:56 pm    Post subject: Reply with quote

Try use online Help (Long) check out Elementary Data Types
_________________
/ Kim
Back to top
View user's profile Visit poster's website MSN Messenger
Ev3658

Bascom Member



Joined: 30 Nov 2014
Posts: 32

russia.gif
PostPosted: Thu Nov 30, 2017 10:19 pm    Post subject: Reply with quote

kimmi wrote:
Try use online Help (Long) check out Elementary Data Types

In the help the necessary information is absent. Crying or Very sad

In the documentation for Hmega A3, there is little information.
Tracking 16bit is working.
I can not pass the subtraction to the second timer.
The above program code is a software implementation for increasing the number of bits, but there is no reliable work at the zero point.


Code:
Config Pina.0 = Input                                       'A
Config Pina.1 = Input                                       'B
Config Xpin = Pina.0 , Sense = Rising                    
Config Xpin = Pina.1 , Sense = Rising

Config Event_system = Dummy , Mux0 = Porta.0 , Qd0 = Enabled , Digflt0 = 2
Config Tcc1 = Normal , Prescale = E0 , Event_source = E0 , Event_action = Qdec , Event_delay = Enabled '------ 0-16 bit  (UP/DOWN)
 
Config Event_system = Dummy , Mux1 = Tcс1_ovf
Config Tce1 = Normal , Prescale = E1 , Event_source = E1                                                 '-------16-32bit (UP....) - No Down ((((
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Thu Nov 30, 2017 11:31 pm    Post subject: Reply with quote

Cant see what you are trying to do, but if you do
Code:

Dim En1 as long

does that do what you want ?
Read the help for the differences and ranges of byte, integer and long variables

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

Bascom Member



Joined: 30 Nov 2014
Posts: 32

russia.gif
PostPosted: Fri Dec 01, 2017 8:51 am    Post subject: Reply with quote

AdrianJ wrote:
...
does that do what you want ?

No, problem Decr Timer2
Back to top
View user's profile
Madf

Bascom Member



Joined: 13 Nov 2011
Posts: 179
Location: Moscow

russia.gif
PostPosted: Fri Dec 01, 2017 9:10 am    Post subject: Reply with quote

In the 32-bit counting mode, the subtraction does not work in the quadrature encoder.
How to set up correctly?
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: Sat Dec 02, 2017 11:53 pm    Post subject: Reply with quote

Your code does not show how or where you use Timer2.

If you are using a hardware timer, and you expect it to decrement on an input, then you misunderstand how timers work. Hardware timers only increment ( apart from certain special conditions like some PWM modes ).

Software timers, like what you show for the Timer1 overflow routine Enc1, can increment or decrement a variable, or do anything else you can put in the code.

Edit:
Actually that may be wrong in the case of an XMega. I do not know whether they can be set to decrement as well as increment. But you will still have to show your code using Timer2.

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

Bascom Member



Joined: 30 Nov 2014
Posts: 32

russia.gif
PostPosted: Sun Dec 03, 2017 9:03 pm    Post subject: Reply with quote

AdrianJ wrote:
Your code does not show how or where you use Timer2. .....


Unfortunately I can not overcome the language barrier.
The image corresponds to this part of the code:
Code:

Config Pina.0 = Input                                       'A
Config Pina.1 = Input                                       'B
Config Xpin = Pina.0 , Sense = Rising                    
Config Xpin = Pina.1 , Sense = Rising

Config Event_system = Dummy , Mux0 = Porta.0 , Qd0 = Enabled , Digflt0 = 2
Config Tcc1 = Normal , Prescale = E0 , Event_source = E0 , Event_action = Qdec , Event_delay = Enabled '------ 0-16 bit  (UP/DOWN) ----- TIMER 1
 
Config Event_system = Dummy , Mux1 = Tcс1_ovf
Config Tce1 = Normal , Prescale = E1 , Event_source = E1                                                 '-------16-32bit (UP....) - No Down ((((---------- TIMER 2
Back to top
View user's profile
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 299
Location: Berlin

germany.gif
PostPosted: Thu Dec 07, 2017 9:57 pm    Post subject: Reply with quote

Hi,
the secret is well hidden in datasheet, and only in datasheets published after 03/2012 - older datasheets do not mention that.

Quote from page 167 of actual datasheet:
Quote:
14.6.2 Event Action Controlled Operation
The event selection and event action settings can be used to control the counter from the event system. For the counter,
the following event actions can be selected:
- Event system controlled up/down counting
- Event n will be used as count enable
- Event n+1 will be used to select between up (1) and down (0). The pin configuration must be set to low level sensing
(...)


I found example code in AVRFreaks (author: ghf080), here bascom adapted example for your 32-Bit Up-Down-Timer
Code:

(...)
Config Tcc1 = Normal , Prescale = E0 , Event_source = E0 , Event_action = Qdec '------ 0-16 bit  (UP/DOWN)

tcc1_cca = 0 'We need a compare-Register for direction -> event system

Config Event_system = Dummy , Mux1 = Tcc1_ovf 'Overflow-Event
Config Event_system = Dummy , Mux2 = Tcc1_cca 'in next Channel the "compare-event"

Config Tce1 = Normal , Prescale = E1 , Event_source = E1, Event_action = UPDOWN , Event_delay = Enabled
(...)
 


I tested this in Atmel Studio 7 Simulator as I don't have any fast encoder for tests.

Keep in mind that reading a 32Bit value out of two 16bit registers is not atomic. In case the low timer is just about to overflow when reading the following read of the high timer can happen after the overflow. The result is a wrong value. To get the readings as close as possible together you can use assembler like that:
Code:

(...)
dim lEncodeCounter as long
(...)
'reading the encoder timers

'if no interupts are used in you application comment the following line
disable interrupts

$asm
  lds r16,{TCC1_CNT} 'read 16Bit: low first! Puts automaticaly high byte in timer C1's TEMP register
  lds r18,{TCE1_CNT} 'read 16Bit: low first! Puts automaticaly high byte in timer E1's TEMP register
  lds r17,{TCC1_CNT}+1 'now read the TEMP from 16 bit value
  lds r19,{TCE1_CNT}+1 'now read the TEMP from 16 bit value
  loadadr lEncodeCounter, X
  st X+, r16 'save indirect to lEncodeCounter, low byte first
  st X+, r17 'save indirect to lEncodeCounter
  st X+, r18 'save indirect to lEncodeCounter
  st X, r19 'save indirect to lEncodeCounter, high byte last
$end asm

'if no interupts are used in you application comment the following line
enable interrupts
 

This gives you a delay of only 2 clock cycles between the readings. Its up to you to decide if this is fast enough - depending on your application.
Other possibility would be to check for readings nearby overflow and discard them or multiple readings with continuity check.

Edit: After thinking about it I realize that the unwanted overflow can happen even at slowest encoder speeds. So at least two readings and a check of allowed difference should be done.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR 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