打印

FreeScale MC9S08DZ32 CAN问题

[复制链接]
2088|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
MWJ198310|  楼主 | 2008-7-18 15:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
斑竹 
请帮忙 我快疯掉了 调了将近一个月了 CAN还没有调通  现在问题是CAN不能发送出去 也不知道问题出在什么地方 望指导  
以下是电路图和程序

#ifndef CAN_C
#define CAN_C

#include "can.h"
struct can_msg msg_send;

/********************************************************************************************
function name     :  Can_Init
input parameters  :  none
output parameters :  none
description       :  MSCAN Peripheral Initialization.
********************************************************************************************/
void Can_Init(void)
{  
  CANCTL1_CANE   = 1;       //can module is enabled 
  
  if(!(CANCTL0 & 0x01))     //Active MSCAN initialization mode               
    CANCTL0 = 0x01;
  while(!(CANCTL1 & 0x01)); //Wait until the MSCAN is in initialization mode 
  
  CANCTL1_CLKSRC = 0;       //MSCAN clock source is the oscillator clock
  
  CANBTR0  = 0x13;          //Prescaler = 4
  CANBTR1  = 0x23;          //125K
  
  CANIDAC_IDAM = 0x00;      //Two 32-bit acceptance filters
  
  CANIDAR0 = 0x00;
  CANIDAR1 = 0x00;
  CANIDAR2 = 0x00;
  CANIDAR3 = 0x00;
  CANIDAR4 = 0x00;
  CANIDAR5 = 0x00;
  CANIDAR6 = 0x00;
  CANIDAR7 = 0x00;

  CANIDMR0 = 0xFF;
  CANIDMR1 = 0xFF;
  CANIDMR2 = 0xFF;
  CANIDMR3 = 0xFF;
  CANIDMR4 = 0xFF;
  CANIDMR5 = 0xFF;
  CANIDMR6 = 0xFF;
  CANIDMR7 = 0xFF;
  
  CANRIER_RXFIE = 1;
      
  CANCTL0 = 0x00;           //Active MSCAN Normal Operation
  while(CANCTL1 & 0x01);    //Wait until the MSCAN operates normally
      
  while(!(CANCTL0 & 0x10)); //Wait until MSCAN is synchronized to the CAN bus   
}

/********************************************************************************************
function name     :  Can_SendMesg
input parameters  :  the data which would be transmited through can bus
output parameters :  none
description       :  MSCAN Peripheral Initialization.
********************************************************************************************/
Bool Can_SendMesg(struct can_msg msg) 
{
  unsigned char n_tx_buf,i;

  if(msg.len > MAX_LEN)                    //Checks len validity
    return(FALSE);
  
  if(!(CANCTL0 & 0x10))                    //Checks synchronization to CAN bus
    return(FALSE);
    
  n_tx_buf = 0;
  do
  {
    CANTBSEL = CANT**;                    //Looks for a free buffer
    n_tx_buf = CANTBSEL;
  }while(!n_tx_buf); 
   
  CANTIDR0 = (unsigned char)(msg.id >> 3); //Write Identifier
  CANTIDR1 = (unsigned char)(msg.id << 5);
  
  if(msg.RTR)
    CANTIDR1 |= 0x10;                      //RTR=Recessive
    
  
  for(i = 0; i < msg.len; i++)             //Write Data Segment
    *((&CANTDSR0)+i) = msg.data;
    
  
  CANTDLR  = msg.len;                      //Write Data Length
  
  
  CANTTBPR = msg.prty;                     //Write Priority
  
  
  CANT**  = n_tx_buf;                     //Clear TXx Flag (buffer ready to transmission)
  return(TRUE);
}

interrupt 28 void Can_Receive_Interrupt(void) 
{
   PTAD_PTAD4 = 0;
   CANR** = 0x01;
}
/********************************************************************************************
function name     :  Can_TransmitTest
input parameters  :  none
output parameters :  none
description       :  can transmit routine
********************************************************************************************/
void Can_TransmitTest(void) 
{
  msg_send.id      = ID_TX;
  msg_send.data[0] = 0x7f;
  msg_send.data[1] = 0x33;
  msg_send.data[2] = 0x55;
  msg_send.data[3] = 0x00;
  msg_send.data[4] = 0x01;
  msg_send.data[5] = 0xfe;
  msg_send.data[6] = 0xfd;
  msg_send.data[7] = 0xfc;
  msg_send.len     = 8;
  msg_send.RTR     = FALSE;
  msg_send.prty    = 0;
  
  (void)(Can_SendMesg(msg_send)) ;
  
  //PTAD = CANTDSR0;
  //PTAD = CANTDSR1;
  //PTAD = CANTDSR2;
  //PTAD = CANTDSR3;
  //PTAD = CANTDSR4;
  //PTAD = CANTDSR5;
  //PTAD = CANTDSR6;
  //PTAD = CANTDSR7; 
    
}

#endif电路图

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

2

帖子

0

粉丝