|
本帖最后由 whxemail 于 2012-11-27 18:57 编辑
本人从微芯官网下载了CAN18CXX8的CAN通信例程,有如下问题
1、CAN初始化函数
void CANInitialize(BYTE SJW,
BYTE BRP,
BYTE PHSEG1,
BYTE PHSEG2,
BYTE PROPSEG,
enum CAN_CONFIG_FLAGS config)
{
}
问题1:关于几个值的设置,
主时钟源设置为 32M,本人想得到 250Kbps 的波特率,即位时间为 4us 本人设置如下
CANInitialize(2, 3, 5, 5, 4, CAN_CONFIG_VALID_STD_MSG);//250k波特率
T Q(μs)= (2 * (BRP + 1))/F OSC(MHz) 即 (2 * (3 + 1))/32(MHz) = 0.25 us
标称位时间 = T Q * (Sync_Seg + Prop_Seg +Phase_Seg1 + Phase_Seg2)
即 T Q * (Sync_Seg + Prop_Seg +Phase_Seg1 + Phase_Seg2) =0.25us*(2+5+5+4)= 4us
但是通过示波器测出来的波形位时间不是4us,是不是我的波特率设置有问题呢,希望知道的解答下
2、CAN发送函数
BOOL CANSendMessage(unsigned long id,
BYTE* Data,
BYTE DataLen,
enum CAN_TX_MSG_FLAGS MsgFlags)
{
BYTE i;
BYTE *ptr;
// Find the first empty transmitter.
if ( TXB0CON_TXREQ == 0 )
{
// TxBuffer0 is empty. Set WIN bits to point to TXB0
CANCON &= 0b11110001;
CANCON |= 0b00001000;
}
else if ( TXB1CON_TXREQ == 0 )
{
// TxBuffer1 is empty. Set WIN bits to point to TXB1
CANCON &= 0b11110001;
CANCON |= 0b00000110;
}
else if ( TXB2CON_TXREQ == 0 )
{
// TxBuffer2 is empty. Set WIN bits to point to TXB2
CANCON &= 0b11110001;
CANCON |= 0b00000100;
}
else
// None of the transmit buffers were empty.
return FALSE;
/*
* Now that WIN has remapped RXB0 to empty buffer, simply
* populate RXB0 buffer
*/
// Set transmit priority.
RXB0CON = MsgFlags & CAN_TX_PRIORITY_BITS;
// Populate Extended identifier information only if it is
// desired.
if ( !(MsgFlags & CAN_TX_FRAME_BIT) )
CANIDToRegs((BYTE*)&RXB0SIDH, id, CAN_CONFIG_XTD_MSG);
else
CANIDToRegs((BYTE*)&RXB0SIDH, id, CAN_CONFIG_STD_MSG);
RXB0DLC = DataLen;
if ( !(MsgFlags & CAN_TX_RTR_BIT) )
RXB0DLC |= 0b01000000;
// Populate data values.
ptr = (BYTE*)&RXB0D0;
for ( i = 0; i < DataLen; i++ )
ptr = Data;
/*
* Mark this buffer as ready to start transmit.
* We are not using C bit field structure because RXB0 registers
* are remapped to one of the empty transmit buffers and their
* bit3 is not same as RXB0CON bit3. To avoid confusion, in-line
* assembly is used to directly set bit 3 of corresponding TXBnCON
* register.
*/
#if defined(MCHP_C18)
_asm
bsf RXB0CON, 3, 0
_endasm
#endif
#if defined(HITECH_C18)
asm("bsf _RXB0CON,3");
#endif
/*
* Restore CAN buffer mapping so that subsequent access to RXB0
* buffers are to the real RXB0 buffer.
*/
CANCON &= 0b11110001;
return TRUE;
}
问题2:
上面的发送函数,为什么把一些要发送的内容赋给接收寄存器,
如
RXB0CON = MsgFlags & CAN_TX_PRIORITY_BITS;
RXB0DLC = DataLen;
bsf RXB0CON, 3, 0
而不是发送寄存器
本来以为例程错了,把接收寄存器都改为了发送寄存器,但是连波形都没了,原来不改还有的
等待指点。。。。。。。。。。。。。
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|