'***********************************************************************
$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 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 = 2
'Print #buart_channel , "This use COM3" 'print current Uart
'Print #buart_channel , "MCU Rev. = " ; Mcu_revid 'version of the MCU
Print #buart_channel , ""
Print #buart_channel , "Started "
'***********************************************************************
Dim Encoder_tmp As Word , Encoder_prev As Word 'zmienna dla enkodera
Dim Steps As Integer
Enable Interrupts 'włacz przerwania globalne
'***********************************************************************
'config pinA.2 = input 'index pin of the encoder
Config Pina.1 = Input 'enkoder 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 = 4
'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
'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
'***********************************************************************
Do
Encoder_tmp = Tcd0_cnt
If Encoder_prev <> Encoder_tmp Then 'jesli zawartosc timera TCD0 jest różna od naszej zmiennej to
Select Case Encoder_prev
Case 0
If Encoder_tmp = 1 Then Incr Steps Else Decr Steps
Case Else
Select Case Encoder_tmp
Case Is < Encoder_prev : Decr Steps
Case Is > Encoder_prev : Incr Steps
End Select
End Select
Encoder_prev = Encoder_tmp
Print #buart_channel , Steps '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
'to możesz robić z inną częstotliwością
If 50ms = 1 Then
50ms = 0
If Motor_step_pin = 1 Then
Reset Motor_step_pin
Else
Select Case Steps
Case Is < 0
Incr Steps
'Call Ustaw_kierunek_l
Set Motor_step_pin
Case Is > 0
Decr Steps
'Call Ustaw_kierunek_r
Set Motor_step_pin
End Select
End If
End If
Loop
End 'end program