Napisałem sobie monitor sieci LIN który może też pracować jako SLAVE.
Ramka LIN wygląda tak, że na początku nadawane jest 13 bitów o wartości zero i to normalnie w UART jest ciężko obsłużyć bo to jest typowy błąd (za długo stan niski).
Jednak w procesorach Xtiny i AVRX do UARTu dodano tryb LINAUTO który sam to sprzetowo obługuje.
Można więc podsłuchac swoje autko (nie wszędzie jest CAN) lub nawet coś sobie z tego zbudować.
W załączniku opis który stworzyłem na potrzeby forum MCS więc po angielsku. W razie pytań można pisac
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- $regfile = "AVRX128db32.dat" 'FLASH 128KB | SRAM 16KB | EEPROM 512B | USER 32B
- $crystal = 24000000 '1xTCA|3xTCB|1xTCD
- $hwstack = 64
- $swstack = 40
- $framesize = 128
- '############################################
- '# LIN MONITOR/SLAVE with AVRX or XTINY #
- '# by BARTek EDC #
- '# www.bart-projects.pl #
- '# bartek.niveasoft@gmail.com #
- '############################################
- ' BASCOM 2085 with Xtiny Addon
- Debug On
- $projecttime = 507
- Config Vregpwr = Auto
- Config Osc = Enabled , Frequency = 24mhz
- Config Sysclock = Int_osc , Prescale = 1
- Config Submode = New
- '*******************************************************
- '* CONSTANS SETTINGS *
- '*******************************************************
- Const Lin_baud = 19200
- '2x time for one byte at given baudrate (not next byte means end)
- Const Fr_timeout = _xtal /(lin_baud / 8) * 2 ' (lin_baud/8)=Bytes\s
- Const Only_answered = 1
- '*******************************************************
- '* I/O CONFIG *
- '*******************************************************
- Config Porta.3 = Output : Led Alias Porta.3
- '-------- TQFP32 ------------- ALTERNATIVE-
- 'COM1 TXD-PA0-30, RXD-PA1-31 (PA4-26/PA5-27)
- 'COM2 TXD-PC0-6 , RXD-PC1-7 ( NO REMAP )
- 'COM3 TXD-PF0-20, RXD-PF1-21 (PF4-24/PF5-25)
- '*******************************************************
- '* CONFIG COM1 BUFFERED USART FOR LIN * 'COM1 TXD-PA0-30, RXD-PA1-31 (PA4-26/PA5-27)
- '*******************************************************
- Config Com1 = Lin_baud , Mode = Asynchroneous , Parity = None , Databits = 8 , Stopbits = 1
- Open "COM1:" For Binary As #1
- Config Serialin0 = Buffered , Size = 100 , Bytematch = All
- Const Lin = 1
- Config Porta.2 = Output : Lin_en Alias Porta.2 : Set Lin_en 'enables L9631 transceiver
- '*******************************************************
- '* CONFIG COM2 BUFFERED USART FOR PC * 'COM2 TXD-PC0-6 , RXD-PC1-7 (PC4-14/PC5-17)
- '*******************************************************
- Config Com2 = 115200 , Mode = Asynchroneous , Parity = None , Databits = 8 , Stopbits = 1
- Open "COM2:" For Binary As #2
- Config Serialin1 = Buffered , Size = 100
- Const Usb = 2
- '*******************************************************
- '* TIMER SETTINGS FOR TIME BASE *
- '*******************************************************
- Config Tca0 = Normal , Prescale = 8 , Run = On
- Tca0_per = 29999 '10ms @24MHz/8
- '*******************************************************
- '* TIMER SETTINGS FOR LIN TIMEOUT *
- '*******************************************************
- Config Tcb0 = Periodic_int , Run = On , Prescale = Off
- Tcb0_ccmp = Fr_timeout
- Enable Tcb0_capt
- On Tcb0_capt Tcb0_isr Nosave
- Config Portd.6 = Output : Pulse Alias Portd.6
- '*******************************************************
- '* MULTIPURPOSE VARIABLES *
- '*******************************************************
- Dim Helpb As Byte , N As Byte
- Dim Helpw As Word
- Dim Runonce As Byte
- '*******************************************************
- '* PROGRAM VARIABLES *
- '*******************************************************
- 'time
- Dim Miliseconds As Byte
- 'programm
- Dim Char As Byte
- Dim Tflag As Byte
- Dim Raw_buff(10) As Byte , Idx As Byte
- Dim Id As Byte , Parity As Byte , Chck As Byte , Bcnt As Byte
- '*******************************************************
- '* ERAM VARIABLES *
- '*******************************************************
- '*******************************************************
- '* PROGRAM SUB`s *
- '*******************************************************
- Const Mian = _xtal * 64 'freq shifted left 6 bits (datasheet formula)
- Function Calc_2x_speed_baud(byval New_baud As Dword)as Word
- Local Tmian As Dword
- Local Result As Word
- Shift New_baud , Left , 3 'multiply by 8 samples per bit
- Tmian = Mian / New_baud
- Result = Tmian 'cast
- 'Debug #usb , "2xSpeed BAUDH=" ; Hex(high(result)) ; " BAUDL=" ; Hex(low(result))
- Calc_2x_speed_baud = Result
- End Function
- Function Calc_normal_speed_baud(byval New_baud As Dword)as Word
- Local Tmian As Dword
- Local Result As Word
- Shift New_baud , Left , 4 'multiply by 16 samples per bit
- Tmian = Mian / New_baud
- Result = Tmian 'cast
- 'Debug #usb , "Normal BAUDH=" ; Hex(high(result)) ; " BAUDL=" ; Hex(low(result))
- Calc_normal_speed_baud = Result
- End Function
- Sub Enable_linauto
- Debug #usb , "->Reconfiguring USART..."
- Debug #usb , "Actual USART0_CTRLB &B" ; Bin(usart0_ctrlb) 'RXEN,TXEN,U2X
- Debug #usb , "@" ; Lin_baud ; " ACTUAL USART0_BAUD=&H" ; Hex(usart0_baud)
- Helpb = Usart0_ctrlb And &B1111_1001
- 'Helpb = Helpb Or Bits(usart_rxmode1_bp) 'GENAUTO
- Helpb = Helpb Or Bits(usart_rxmode1_bp , Usart_rxmode0_bp) 'LINAUTO
- Usart0_ctrlb = Helpb
- Usart0_ctrld = Bits(usart_abw1_bp , Usart_abw0_bp) '25% tolerance
- Debug #usb , "New USART0_CTRLB &B" ; Bin(usart0_ctrlb) 'RXEN,TXEN,U2X
- Usart0_baud = Calc_normal_speed_baud(lin_baud)
- Debug #usb , "@" ; Lin_baud ; " NORMAL USART0_BAUD=&H" ; Hex(usart0_baud)
- End Sub
- '################# START ################
- Debug #usb , "{010}Program started"
- Call Enable_linauto()
- Enable Interrupts
- Do
- '-[10ms]-
- If Tca0_intflags.tca_single_ovf_bp = 1 Then
- Tca0_intflags.tca_single_ovf_bp = 1
- If Miliseconds < 49 Then
- Incr Miliseconds
- Else
- Miliseconds = 0
- Toggle Led
- End If
- End If
- If Tflag = 1 Then
- If Ischarwaiting(#lin) > 0 Then
- Idx = 0
- Do
- If Idx < 10 Then
- Incr Idx : Raw_buff(idx) = Inkey(#lin)
- End If
- Loop Until Ischarwaiting(#lin) = 0
- Id = Raw_buff(1) And &B0011_1111
- Parity = Raw_buff(1)
- Shift Parity , Right , 6 'only two most significant bits
- #if Only_answered = 1
- If Idx > 2 Then 'filter for terminal
- #endif
- Print #usb , "ID=" ; Hex(id) ; " "; '" P" ; Parity ;
- If Idx > 2 Then
- Bcnt = Idx - 1 ' -CRC
- For N = 2 To 9
- If N <= Bcnt Then
- Print #usb , Hex(raw_buff(n)) ; " "; 'two hex chars and space
- Else 'equal columns
- Print #usb , " " ; 'two hex chars and space
- End If
- Next N
- Decr Bcnt
- Print #usb , "Len " ; Bcnt ; " CRC=" ; Hex(raw_buff(idx))
- Else
- Print #usb , " No Answear!"
- End If
- #if Only_answered = 1
- End If
- #endif
- End If
- Tflag = 0
- End If
- Loop
- Serial0bytereceived:
- Pushall
- Set Pulse
- Tcb0_cnt = 0 'prelonge timeout
- Tcb0_ctrla.tcb_enable_bp = 1 'Start Timer
- Popall
- Return
- Tcb0_isr:
- Reset Pulse
- Tcb0_ctrla.tcb_enable_bp = 0 'Stop Timer
- Tcb0_intflags.tcb_capt_bp = 1 'reset INT flag
- Tflag = 1
- Return