pic18f25k80 и ECAN
-
-
04.07.2023 #78047
Николай
Ключникr0cket111 04 июл 2023, 15:42Всем привет.
Пытаюсь запустить CAN на PIC18f25k80.
MPLAB X IDE v6.10 и MCC
Микруха физического уровня PCA82C251T
AN2714 прочитал. Настройки такие же ввел.
Все скомпилировалось. Заливаю в проц.
На CAN RX и CAN TX постоянно какой то сигнал висит.
Если менять скорость в настройках CAN то сигнал тоже меняет скорость.
Но канхакер на него не реагирует.
А так же сигнал как будто зациклен. А у меня однократная отправка.#include mcc_generated_files/mcc.h
uCAN_MSG txMessage,
void main(void) {
SYSTEM_Initialize(),
INTERRUPT_GlobalInterruptEnable(),
PORTBbits.RB7 = 0,
PORTBbits.RB6 = 0,
while (1)
{
txMessage.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B,
txMessage.frame.id = 0x100,
txMessage.frame.dlc = 8,
txMessage.frame.data0 = 128,
txMessage.frame.data1 = 64,
txMessage.frame.data2 = 32,
txMessage.frame.data3 = 64,
txMessage.frame.data4 = 128,
txMessage.frame.data5 = 255,
txMessage.frame.data6 = 128,
txMessage.frame.data7 = 64,
CAN_transmit(&txMessage),
PORTBbits.RB7 = 0,
PORTBbits.RB6 = 1,
__delay_ms(100),
PORTBbits.RB7 = 1,
PORTBbits.RB6 = 0,
__delay_ms(100),
while (1),
}
}Подскажите кто сталкивался.
Yoda 05 июл 2023, 12:49Вот поэтому я никогда и не использую никакие готовые библиотеки. Если не будет работать, то можно голову сломать в этих десятках и десятках файлов, когда всю суть можно в один файл в пару функций запихать.
CAN PIC18F46K80#include <,pic18f46k80.h>,
#include PIC18_ecan.h
#include main.h
unsigned long id1,id2,
unsigned char B1,B2,B3,B4,B5,B6,B7,B8,
unsigned char data18, data28,
// ===========================================
// CAN INIT
// ===========================================
void ECAN_init void
{
TRISBbits.TRISB2 = 0, // tx
TRISBbits.TRISB3 = 1, // rx
CANCON = 0x80, // REQOP<,2:0>, = 0b100
/*To ensure we dont proceed before we have changed to CONFIG mode, we check CANSTATbits.OPMODE and wait for it to change to 0b100*/
while CANSTATbits.OPMODE != 0b100 {},
// Enter CAN module into Mode 0
ECANCON = 0x00,
// JSW = 1
// PRG = 8
// SG1 = 8
// SG2 = 3
// WIDTH = 20 TQ
//BRGCON1
BRGCON1bits.SJW = 0b00, // Sync Jump Width is 1 TQ
BRGCON1bits.BRP = 0b000011, // 2 * BRP = 4
//BRGCON2
BRGCON2bits.SEG2PHTS = 1, // Makes the SEG2 fully programmable
BRGCON2bits.SAM = 1, // On reception the bit is sampled 3 times, twice before and one on at the intersection of SEG1 and SEG2, majority rules.
BRGCON2bits.SEG1PH = 8 – 1,
BRGCON2bits.PRSEG = 8 – 1,
//BREGCON3
BRGCON3bits.WAKDIS = 0, //Enable wakeup from CAN
BRGCON3bits.WAKFIL = 0, //Disable filters for wakeup
BRGCON3bits.SEG2PH = 3 – 1,
BSEL0 = 0b11111100, // ALL TX
// CLEAR REGISTERS
RXM0SIDH = 0,
RXM0SIDL = 0,
RXM0EIDH = 0,
RXM0EIDL = 0,
// SET MASK
// 1 = CHECK
// 0 = DO NOT CHECK
RXM0SIDH = MASK >,>, 21 & 0b11111111,
RXM0SIDLbits.SID = MASK >,>, 18 & 0b00000111,
RXM0SIDLbits.EID = MASK >,>, 16 & 0b00000011,
RXM0EIDH = MASK >,>, 8 & 0b11111111,
RXM0EIDL = MASK >,>, 0 & 0b11111111,
RXM0SIDLbits.EXIDEN = 1,
// **********************************
// Initialize Receive Filters.
// **********************************
// switch filter 0 ON
// disable other filters
RXFCON0 = 0b00000001,
RXFCON1 = 0b00000000,
// Assign Filters to Masks
MSEL0 = 0b11111100, //Assign Filters 0 to Mask 0
MSEL1 = 0b11111111, //Assign Filters 4-7 No mask
MSEL2 = 0b11111111, //Assign Filters 8-11 to No mask
MSEL3 = 0b11111111, //Assign Filters 12-15 to No mask
// Assign Filters to Buffers
RXFBCON0 = 0b11110000, //Assign Filter 0 to RXB0
RXFBCON1 = 0b11111111, //Assign the rest of the buffers with no filter
RXFBCON2 = 0b11111111,
RXFBCON3 = 0b11111111,
RXFBCON4 = 0b11111111,
RXFBCON5 = 0b11111111,
RXFBCON6 = 0b11111111,
RXFBCON7 = 0b11111111,
// **********************************
// Set Receive Filters.
// **********************************
// CLEAR REGISTERS
RXF0SIDH = 0,
RXF0SIDL = 0,
RXF0EIDH = 0,
RXF0EIDL = 0,
// SET MASK
// 1 = CHECK
// 0 = DO NOT CHECK
RXF0SIDH = CAN_NODE >,>, 21 & 0b11111111,
RXF0SIDLbits.SID = CAN_NODE >,>, 18 & 0b00000111,
RXF0SIDLbits.EID = CAN_NODE >,>, 16 & 0b00000011,
RXF0EIDH = CAN_NODE >,>, 8 & 0b11111111,
RXF0EIDL = CAN_NODE >,>, 0 & 0b11111111,
RXF0SIDLbits.EXIDEN = 1,
/* In this case, we are only passing messages with ID=1 and 2 through.
The SID has been split between two registers,
bits SID<,10:3>, lie in RXFnSIDH and SID<,2:0>, are in MSBs of RXFnSIDL.
Thus the whole SID is 0000000000100000.*/
RXB0CON = 0x00,
RXB0CONbits.RXM1 = 1,
RXB0CONbits.RXM0 = 0,
// Now we can set CAN back to normal mode and this allows it to actually use it.
CANCON = 0b0000000,
//Another while loop to check CANSTATbits.OPMODE,
//and wait for it to change to 0b000 before continuing.
while CANSTATbits.OPMODE != 0b000 {},
}
// ===========================================
// CAN SEND
// ===========================================
void ECAN_Transmit unsigned long EID, unsigned char d0, unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4, unsigned char d5, unsigned char d6, unsigned char d7
{
unsigned long can_timeout = 0,
//Wait for last message to clear
whileTXB0CONbits.TXREQ == 1
{
if ++can_timeout >, 10000 return,
}
TXB0SIDLbits.EXIDE = 1,
TXB0EIDLbits.EID = EID >,>, 0 // bits 0 – 7
TXB0EIDHbits.EID = EID >,>, 8 // bits 8 – 15
TXB0SIDLbits.EID = EID >,>, 16 // bits 16 – 17
TXB0SIDLbits.SID = EID >,>, 18 // bits 18 – 20
TXB0SIDHbits.SID = EID >,>, 21 // bits 21 – 28
// The TXBnDLCbits.TXRTR bit 6 is the remote transmission request bit, and tells another node of that ID to send data.
// The TXBnDLCbits.DLC bits 3:0 is an indication of how many bytes are to be sent, it can range from 0 to 8
TXB0DLC = 8,
TXB0D0 = d0,
TXB0D1 = d1,
TXB0D2 = d2,
TXB0D3 = d3,
TXB0D4 = d4,
TXB0D5 = d5,
TXB0D6 = d6,
TXB0D7 = d7,
TXB0CONbits.TXREQ = 1,
}
// ===========================================
// CAN RECEIVE
// ===========================================
void ECAN_Receivevoid
{
if RXB0CONbits.RXFUL // receive buffer 0
{
id1 = unsigned longRXB0EIDL & 0b11111111 <,<, 0,
id1 |= unsigned longRXB0EIDH & 0b11111111 <,<, 8,
id1 |= unsigned longRXB0SIDL & 0b00000011 <,<, 16,
id1 |= unsigned longRXB0SIDL & 0b11100000 <,<, 18,
id1 |= unsigned longRXB0SIDH & 0b11111111 <,<, 21,
data10 = RXB0D0,
data11 = RXB0D1,
data12 = RXB0D2,
data13 = RXB0D3,
data14 = RXB0D4,
data15 = RXB0D5,
data16 = RXB0D6,
data17 = RXB0D7,
RXB0CONbits.RXFUL = 0, // Mark the buffer as read, and no longer contains a new message
PIR5bits.RXB0IF = 0, // reset receive buffer 0 interrupt flag
}
if RXB1CONbits.RXFUL // receive buffer 1
{
id2 = unsigned longRXB1EIDL & 0b11111111 <,<, 0,
id2 |= unsigned longRXB1EIDH & 0b11111111 <,<, 8,
id2 |= unsigned longRXB1SIDL & 0b00000011 <,<, 16,
id2 |= unsigned longRXB1SIDL & 0b11100000 <,<, 18,
id2 |= unsigned longRXB1SIDH & 0b11111111 <,<, 21,
data20 = RXB1D0,
data21 = RXB1D1,
data22 = RXB1D2,
data23 = RXB1D3,
data24 = RXB1D4,
data25 = RXB1D5,
data26 = RXB1D6,
data27 = RXB1D7,
RXB1CONbits.RXFUL = 0, // Mark the buffer as read, and no longer contains a new message
PIR5bits.RXB1IF = 0, // reset receive buffer 1 interrupt flag
}
}my504 06 июл 2023, 17:35Дебаг включить не судьба?
-
Viewing 0 reply threads
- Вы должны войти в систему, чтобы ответить в этой теме.