- '***********************************************************************
- 'Xmega quadrature encoder test base on MAK3 exaple
- 'Przyklad 2
- '$sim
- $regfile = "xm128a3udef.dat"
- $crystal = 32000000
- $hwstack = 64
- $swstack = 64
- $framesize = 64
- 'Enable The Osc
- Config Osc = Enabled , 32mhzosc = Enabled
- ' ***************** SYSTEM CLOCK CONFIG ********************************
- ' INTERNAL 32MHz NO PRESCALE
- '
- Config Osc = Disabled , 32mhzosc = Enabled , 32khzosc = Enabled
- Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
- '
- 'ENABLING AUTOMATIC OSCILLATOR CALIBRATION
- Osc_dfllctrl.0 = 1
- Dfllrc32m_ctrl.0 = 1
- '
- '***********************************************************************
- Config Priority = Static , Vector = Application , Lo = Enabled 'Enable Lo Level Interrupts
- '***********************************************************************
- '*******************Xmega 128A3U***********************
- '* UART *********** TX pin * RX pin *
- '* ****************************************************
- '* COM1 - UART_C0 * PORTC.3 * PORTC.2 *
- '******************************************************
- '* COM2 - UART_C1 * PORTC.7 * PORTC.6 *
- '******************************************************
- '* COM3 - UART_D0 * PORTD.3 * PORTD.2 *
- '******************************************************
- '* COM4 - UART_D1 * PORTD.7 * PORTD.6 *
- '******************************************************
- '* COM5 - UART_E0 * PORTE.3 * PORTE.2 *
- '******************************************************
- '* COM6 - UART_E1 * PORTE.7 * PORTE.6 *
- '******************************************************
- '* COM7 - UART_F0 * PORTF.3 * PORTF.2 *
- '******************************************************
- '* COM8 - UART_F1 * PORTF.7 * PORTF.6 *
- '******************************************************
- '***********************************************************************
- Dim Buart_channel As Byte '0 = COM1 ...... 7 = COM8
- Config Com5 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8 'COM3 - UART_D0 PORTD.3 PORTD.2
- Config Com4 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8 'COM3 - UART_D0 PORTD.3 PORTD.2
- Config Com3 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8 'COM3 - UART_D0 PORTD.3 PORTD.2
- Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8 'COM1 - UART_C0 PORTC.3 PORTC.2
- Buart_channel = 4
- Print #buart_channel , "This use COM3" 'print current Uart
- Print #buart_channel , "MCU Rev. = " ; Mcu_revid 'version of the MCU
- '***********************************************************************
- Dim Enkoder_tmp As Dword 'zmienna dla enkodera
- Dim Enkoder_tmp_1 As Dword
- Enable Interrupts 'włacz przerwania globalne
- '***********************************************************************
- 'config pinA.2 = input 'index pin of encoder
- Config Pina.1 = Input 'encoder B
- Config Pina.0 = Input 'enkoder A
- 'Config Xpin = PinA.2 , Outpull = pullup , Sense = low_level
- Config Xpin = Porta.1 , Outpull = Pullup , Sense = Low_level 'enable Pull up and reaction on low level
- Config Xpin = Porta.0 , Outpull = Pullup , Sense = Low_level
- 'konfigurujemy system zdarzeń
- Config Event_system = Dummy , Mux0 = Porta.0 , Qd0 = Enabled , Digflt0 = 8
- 'konfiguracja systemu zdarzeń multiplexer kanał 0 , portA.0, obsługa enkodera QDO właczona, filtr cyfrowy 8
- 'gdy używamy idndexu musimy dodać QDIO=enabled
- 'config Event_system = Dummy , Mux0 = portA.0 , QD0 = enabled , QDI0 = enabled , Digflt0 = 8
- '***********************************************************************
- ''Config Tcd0 = Normal , Prescale = E0 , Event_source = E0 , Event_action = Qdec
- Config Tcd1 = Normal , Prescale = E0 , Event_source = E0 , Event_action = Qdec
- 'konfiguracja timera TCD0 normal= bez generowania sygnałów, preskaler =e0, zródło zdarzenia=podajemy zródło czyli event system
- 'kanał 0 , event action =czyli reakcja na zdarzenie QDEC właczenie obsługi enkodera
- '***********************************************************************
- Enkoder_tmp = 0
- Do
- If Tcd1_cnt <> Enkoder_tmp Then 'jesli zawartosc timera TCD0 jest różna od naszej zmiennej to
- Enkoder_tmp = Tcd1_cnt 'przepisz nową wartość z timera TCD0 do zmiennej enkoder_tmp
- Enkoder_tmp_1 = Enkoder_tmp / 4
- Print #buart_channel , "Enkoder_temp_1 " ; Enkoder_tmp_1 'wyśli przez port szeregowy 2 (zmienna buart=2)
- End If
- 'na ekranie termianla zobaczymy liczbe impulsów policzonych przez nasz timer.
- 'na jeden przeskok enkodera przypadaja 4 impulsy
- Loop
- End 'end program
Potrzebuje aby zawsze zliczała od zera do maksimum potrzebne od obrotomierza.
pozdrawiam Jacek.