www.pudn.com > CAN.rar > CANRcv_snd.c, change:2010-05-17,size:9535b


#include "config.h" 
 
const CANFRAME GtCanFrameTest={ 
    0, 
    8, 
    0x123, 
    {0x55,0xaa,0x55,0xaa,0x03,0x02,0x01,0x08} 
}; 
 
const CANFRAME GtCanFrameTime={ 
    0, 
    8, 
    0x123, 
    {0x20,0x10,0x05,0x15,0x15,0x09,0x23,0x00} 
}; 
  const CANFRAME GtCanFrameX={ 
    0, 
    8, 
    0x123, 
    {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88} 
    }; 
  const CANFRAME GtCanFrameY={ 
    0, 
    8, 
    0x123, 
    {0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0} 
    }; 
   
unsigned char Xflag=0; 
 
CANFRAMEBUF  *GpReFrameBuf;   //定义帧接收缓冲区指针 .CANFRAMEBUF发送接受的数据缓冲区结构体         
CANFRAMEBUF  *GpTxFrameBuf;                                             /* 定义帧发送缓冲区指针         */ 
CANNODEINFO  *GpCanNodeInfo;                                            /* 定义CAN节点数据信息指针      */ 
 
BOOLEAN GbCANTI;                                                        /* CAN发送成功中断标志拷贝      */ 
unsigned long  GulSysClock  =  12000000UL;                              /* 记录处理器时钟频率           */ 
 
/********************************************************************************************************* 
将较长的标识符定义成较短的形式 
*********************************************************************************************************/ 
#define  SysCtlPeriEnable       SysCtlPeripheralEnable 
#define  SysCtlPeriDisable      SysCtlPeripheralDisable 
#define  GPIOPinTypeIn          GPIOPinTypeGPIOInput 
#define  GPIOPinTypeOut         GPIOPinTypeGPIOOutput 
#define  GPIOPinTypeOD          GPIOPinTypeGPIOOutputOD 
 
/********************************************************************************************************* 
定义KEY 
*********************************************************************************************************/ 
#define  KEY1                   GPIO_PIN_2                              /* KEY1、KEY2 PE2、PE3          */ 
#define  KEY2                   GPIO_PIN_3 
#define  KEY3                   GPIO_PIN_4                              /* KEY3、KEY4 PB4、PB5          */ 
#define  KEY4                   GPIO_PIN_5 
#define  KEYSEG1                SYSCTL_PERIPH_GPIOE,GPIO_PORTE_BASE,(KEY1|KEY2) 
#define  KEYSEG2                SYSCTL_PERIPH_GPIOB,GPIO_PORTB_BASE,(KEY3|KEY4)  
 
unsigned char  keyScan(unsigned long ulKeyPeri, unsigned long ulKeyBase, unsigned char ucKeyMark); 
 
/********************************************************************************************************* 
防止JTAG失效 
*********************************************************************************************************/ 
void  jtagWait(void) 
{ 
    if ((keyScan(KEYSEG1) & KEY1) == 0)//上电时按下key1则进入Jtag等待  
    {                                
        while (1)  
        {                                                     // 等待Jtag连接                  
            ; 
        } 
    }     
} 
 
void  delay(unsigned long  ulVal) 
{ 
    while ( --ulVal  !=  0 ); 
} 
int main(void) 
{ 
   
    CANFRAME     tCanFrame; 
    CANNODEINFO  tCanNodeInfo; 
    CANFRAMEBUF  tReFrameBuf;                                           /* 定义帧接收缓冲区             */ 
    CANFRAMEBUF  tTxFrameBuf;                                           /* 定义帧发送缓冲区 */ 
     
    jtagWait();                                                         /* 防止Jtag失效                 */ 
    SysCtlClockSet(SYSCTL_SYSDIV_8 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ); 
    GulSysClock  =  SysCtlClockGet();                                   /* 获取系统时钟,单位:Hz       */ 
    GpCanNodeInfo = &tCanNodeInfo; 
    GpReFrameBuf  = &tReFrameBuf; 
    GpTxFrameBuf  = &tTxFrameBuf; 
    GbCANTI       = 0;                                                  /* CAN发送成功中断标志拷贝清零  */ 
    /* 创建软件CAN节点              */ 
    //RX_MSG_SN:接收报文对相编号为31,TX_MSG_SN为发送报文对象编号为1, 
    canNodeCreate(GpCanNodeInfo, CAN0_BASE, CANBAUD_500K, RX_MSG_SN,TX_MSG_SN,GpReFrameBuf, GpTxFrameBuf);                                                                         
 
    canControllerInit(GpCanNodeInfo);                                   /* 初始化CAN控制器,设置节点波   */ 
                                                                        /* 特率                         */  
    canIntEnable(GpCanNodeInfo);                                        /* 使能CAN中断,并退出初始化模式 */  
     
    /****接收验收过滤*************/ 
    canMsgObjSet(GpCanNodeInfo, 0x01, UNMASK, EXT_ID_FILTER,MSG_OBJ_TYPE_RX); 
    // UNMAX:接收帧屏蔽码0x00000000 
    // 配置"接收报文对象",用于接收   
    // 数据帧    MSG_OBJ_TYPE_RX  接收报文对象,将配置pNodeInfo->ulRxMsgObjNr 
     
     /****接收验收过滤*************/       
    canMsgObjSet(GpCanNodeInfo, 0x01, MASK, EXT_ID_FILTER, MSG_OBJ_TYPE_RX_REMOTE); 
    //MSG_OBJ_TYPE_RX_REMOTE   接收远程帧报文对象 
    // 配置"发送报文对象",用于接收   
    // 远程帧   
    
 while(1) 
  {   
       if(Xflag==0) 
       { 
            if(GpReFrameBuf->ucSize > 0) 
            { 
                while(canReadByteBuf(GpReFrameBuf,&tCanFrame)!=EMPTY) 
                { 
                    GbCANTI=false; 
                    canFrameSend(GpCanNodeInfo,&tCanFrame); 
                    while(GbCANTI!=true) 
                    { 
                        delay(10 * (GulSysClock / 4000)); 
                    } 
                } 
                Xflag=1; 
//                break; 
            }  
            else 
            { 
                canWriteByteBuf(GpTxFrameBuf,&GtCanFrameTest); 
                canWriteByteBuf(GpTxFrameBuf,&GtCanFrameTime); 
                if(GpTxFrameBuf->ucSize > 0)  
                {                               
                    while (canReadByteBuf(GpTxFrameBuf, &tCanFrame) != EMPTY) 
                    {        
                        GbCANTI = false; 
                        canFrameSend(GpCanNodeInfo, &tCanFrame); 
                        delay(10 * (GulSysClock / 4000)); 
                    } 
               
                    while (GbCANTI != true) 
                    { 
                        delay(10 * (GulSysClock / 4000));                   
                    }                 
                 
                    canMsgObjSet(GpCanNodeInfo, 0x01, MASK, EXT_ID_FILTER, MSG_OBJ_TYPE_RX_REMOTE);                                                                                         
                } 
            }   
       } 
       else 
       { 
           if(GpReFrameBuf->ucSize>0) 
            { 
                while(canReadByteBuf(GpReFrameBuf,&tCanFrame)!=EMPTY) 
            { 
                GbCANTI=false; 
                canFrameSend(GpCanNodeInfo,&tCanFrame); 
                delay(10*(GulSysClock/4000)); 
            } 
                while (GbCANTI != true) 
                { 
                    delay(10 * (GulSysClock / 4000));                   
                }                 
                canMsgObjSet(GpCanNodeInfo, 0x01, MASK, EXT_ID_FILTER, MSG_OBJ_TYPE_RX_REMOTE); 
            } 
            canWriteByteBuf(GpTxFrameBuf,&GtCanFrameX); 
            canWriteByteBuf(GpTxFrameBuf,&GtCanFrameY); 
            if(GpTxFrameBuf->ucSize > 0)  
            {                               
                while (canReadByteBuf(GpTxFrameBuf, &tCanFrame) != EMPTY) 
                {        
                    GbCANTI = false;  
                    canFrameSend(GpCanNodeInfo, &tCanFrame); 
                    delay(10 * (GulSysClock / 4000)); 
                } 
           
                while (GbCANTI != true) 
                { 
                    delay(10 * (GulSysClock / 4000));                   
                }                 
                canMsgObjSet(GpCanNodeInfo, 0x01, MASK, EXT_ID_FILTER, MSG_OBJ_TYPE_RX_REMOTE);                                                                                         
            } 
        } 
    } 
} 
/********************************************************************************************************* 
** Function name:          keyScan        
** Descriptions:           扫描键盘  键盘引脚为弱上拉 
** input parameters:       ulKeyPeri:按键所在端口的时钟使能掩码 
** input parameters:       ulKeyBase:按键所在端口的基址地址 
** input parameters:       ucKeyMark:按键的PIN掩码 
** output parameters:      无       
** Returned value:         返回键盘所在端口的电平状态  
** Created by:             张展威 
** Created Date:           2008.08.28 
**-------------------------------------------------------------------------------------------------------- 
** Modified by:          
** Modified date:          
**-------------------------------------------------------------------------------------------------------- 
*********************************************************************************************************/ 
unsigned char  keyScan(unsigned long ulKeyPeri, unsigned long ulKeyBase, unsigned char ucKeyMark) 
{ 
    unsigned char ucKeyValue;   
    SysCtlPeriEnable(ulKeyPeri);                                           
    GPIOPadConfigSet(ulKeyBase, ucKeyMark,                              /* 配置key的驱动类型            */ 
    GPIO_STRENGTH_4MA, 
    GPIO_PIN_TYPE_STD_WPU); 
    GPIODirModeSet(ulKeyBase, ucKeyMark, GPIO_DIR_MODE_IN);             /* 设置key为输入                */  
    ucKeyValue = HWREGB(ulKeyBase | ucKeyMark<<2); 
    SysCtlPeriDisable(ulKeyPeri);                                     /* 禁止KEY所在的GPIO端口        */ 
   return ucKeyValue; 
}