⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 Freescale HCS12DP256 CAN通讯程序源代码
💻 C
字号:
/*
 * Copyright (c) 2004, Freescale Semiconductor
 * Freescale Willy Note
 *
 * File name   : main.c
 * Project name: MSCAN12_DP256
 *
 * Author      : Rebeca Delgado
 * Department  : RTAC Americas
 *
 * Description : a brief description of the module.
 *               Another line of the description.
 *
 * History     :
 * 21/07/2004  : Initial Development. (A10021)
 */

#include <hidef.h>              /* for EnableInterrupts macro */
#include <mc9s12dp256.h>        /* include peripheral declarations */

#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"

/* ID Definition */
#define ST_ID_100 0x20000000    /* Standard Id 0x100 formatted to be loaded 
                                 * in IDRx Registers in Tx Buffer
                                 */

/* Acceptance Code Definitions */ 
#define ACC_CODE_ID100 0x2000
#define ACC_CODE_ID100_HIGH ((ACC_CODE_ID100&0xFF00)>>8)
#define ACC_CODE_ID100_LOW (ACC_CODE_ID100&0x00FF)


/* Mask Code Definitions */
#define MASK_CODE_ST_ID 0x0007
#define MASK_CODE_ST_ID_HIGH ((MASK_CODE_ST_ID&0xFF00)>>8)
#define MASK_CODE_ST_ID_LOW (MASK_CODE_ST_ID&0xFF)

/* Error Flags Definition */
#define NO_ERR 0x00
#define ERR_BUFFER_FULL 0x80 

/* Functions Prototypes */
void CANInit(void);
unsigned char CAN0SendFrame(unsigned long id, unsigned char priority, unsigned char length, unsigned char *txdata );
void Delay(void);

void main () {    
    
    unsigned char errorflag = NO_ERR;
    unsigned char txbuff[] = "ABCDEFGH";

    CANInit();                /* Initialize MSCAN12 Module */
    
    while(!(CAN0CTL0&0x10));  /* Wait for Synchronization */
   
    CAN0RFLG = 0xC3;          /* Reset Receiver Flags
                               *  
                               *  0b11000011
                               *    ||||||||__ Receive Buffer Full Flag
                               *    |||||||___ Overrun Interrupt Flag
                               *    ||||||____ 
                               *    |||||_____>- Transmitter Status Bits
                               *    ||||______ 
                               *    |||_______>- Receiver Status Bits
                               *    ||________ CAN Status Change Interrupt Flag
                               *    |_________ Wake-Up Interrupt Flag
                               */
    
    CAN0RIER = 0x01;          /* Enable Receive Buffer Full Interrupt
                               *  
                               *  0b00000001
                               *    ||||||||__ Receive Buffer Full Int enabled
                               *    |||||||___ Overrun Int disabled
                               *    ||||||____ 
                               *    |||||_____>- Tx Status Change disabled
                               *    ||||______ 
                               *    |||_______>- Rx Status Change disabled
                               *    ||________ Status Change Int disabled
                               *    |_________ Wake-Up Int disabled
                               */
    
    
    
    
    
    EnableInterrupts;

    for (;;) {
        errorflag = CAN0SendFrame((ST_ID_100), 0x00, sizeof(txbuff)-1, txbuff);
        Delay(); 
 	}

}

/*
 * CANInit: a description of the function functionName.
 *
 * Bit Timing Definitions 
 * ----------------------
 *
 * CAN Clock Source (External oscillator) = 16Mhz 
 * BitRate = 125Khz 
 * Total Time Quanta = 16
 * Sincronization Jump Width = 4 Time Quanta
 * 1 sample  
 * Sample point at 75% of Bit Timing 
 *
 * CAN_BRP = ((CAN Clock Source)/fTq) - 1
 * fTq = (Bit Rate) * (Total Time Quanta)
 * Total Time Quanta = (SYNCH_SEG+(TSEG1+1)+(TSEG2+1))          
 * Total Time Quanta =      1    +    11   +    4     = 16 Time Quanta
 * fTq = 125Khz * 16 Time Quanta = 2Mhz
 * CAN_BRP = (16Mhz/2Mhz) - 1 = 8 - 1 = 7 
 * TSEG1 = 10
 * TSEG2 = 3
 * SJW = (Synchronization Jump Width-1) = 3        
 *
 * Another line of the description.
 *
 * Parameters: None
 *
 * Return : None
 */
 
void CANInit(void) {

    CAN0CTL0 = 0x01;          /* Enter Initialization Mode 
                               *  
                               *  0b00000001
                               *    ||||||||__ Enter Initialization Mode
                               *    |||||||___ Sleep Mode Request bit
                               *    ||||||____ Wake-Up disabled
                               *    |||||_____ Time stamping disabled
                               *    ||||______ Synchronized Status
                               *    |||_______ CAN not affected by Wait
                               *    ||________ Receiver Active Status bit
                               *    |_________ Received Frame Flag bit
                               */
                                
    while (!(CAN0CTL1&0x01)){};	/* Wait for Initialization Mode acknowledge
                                 * INITRQ bit = 1
                                 */

    CAN0CTL1 = 0xA0;          /* Enable MSCAN module and LoopBack Mode
                               *  
                               *  0b10100000
                               *    ||||||||__ Initialization Mode Acknowledge
                               *    |||||||___ Sleep Mode Acknowledge
                               *    ||||||____ Wake-up low-pass filter disabled
                               *    |||||_____ Unimplemented
                               *    ||||______ Listen Only Mode disabled
                               *    |||_______ Loop Back Mode enabled
                               *    ||________ Ext Osc/Xtal as Clock Source
                               *    |_________ MSCAN Module enabled
                               */
    
    CAN0BTR0 = 0xC7;          /* Synch Jump = 3 Tq clock Cycles 
                               *
                               *  0b11000111
                               *    ||||||||__
                               *    |||||||___\
                               *    ||||||____ |
                               *    |||||_____ |_ CAN Clock Prescaler = 7
                               *    ||||______ |
                               *    |||_______ |
                               *    ||________/
                               *    |_________>- SJW = 3
                               */
    
    CAN0BTR1 = 0x3A;	      /* Set Number of samples per bit, TSEG1 and TSEG2
                               *
                               *  0b00111010
                               *    ||||||||__
                               *    |||||||___|
                               *    ||||||____|- TSEG1 = 10
                               *    |||||_____|
                               *    ||||______
                               *    |||_______\_ TSEG2 = 3
                               *    ||________/
                               *    |_________ One sample per bit
                               */
                              
    CAN0IDAC = 0x10;          /* Set four 16-bit Filters 
                               *  
                               *  0b00010000
                               *    ||||||||__ 
                               *    |||||||___\_ Filter Hit Indicator 
                               *    ||||||____/
                               *    |||||_____ Unimplemented 
                               *    ||||______ 
                               *    |||_______>- Four 16-bit Acceptance Filters   
                               *    ||________ 
                               *    |_________>- Unimplemented 
                               */
                                 
    /* Acceptance Filters */
    CAN0IDAR0 = ACC_CODE_ID100_HIGH;    //|\    16 bit Filter 0
    CAN0IDMR0 = MASK_CODE_ST_ID_HIGH;   //| \__ Accepts Standard Data Frame Msg 
    CAN0IDAR1 = ACC_CODE_ID100_LOW;     //| /   with ID 0x100
    CAN0IDMR1 = MASK_CODE_ST_ID_LOW;    //|/
    
    CAN0IDAC  = 0x10;                   /* Set four 16-bit Filters */
    
    CAN0IDAR2 = 0x00;                   //|\    16 bit Filter 1
    CAN0IDMR2 = MASK_CODE_ST_ID_HIGH;   //| \__ Accepts Standard Data Frame Msg
    CAN0IDAR3 = 0x00;                   //| /   with ID 0x000
    CAN0IDMR3 = MASK_CODE_ST_ID_LOW;    //|/
    
    CAN0IDAR4 = 0x00;                   //|\    16 bit Filter 2
    CAN0IDMR4 = MASK_CODE_ST_ID_HIGH;   //| \__ Accepts Standard Data Frame Msg
    CAN0IDAR5 = 0x00;                   //| /   with ID 0x000
    CAN0IDMR5 = MASK_CODE_ST_ID_LOW;    //|/
    
    CAN0IDAR6 = 0x00;                   //|\    16 bit Filter 3
    CAN0IDMR6 = MASK_CODE_ST_ID_HIGH;   //| \__ Accepts Standard Data Frame Msg
    CAN0IDAR7 = 0x00;                   //| /   with ID 0x000
    CAN0IDMR7 = MASK_CODE_ST_ID_LOW;    //|/
    
    CAN0CTL0 = 0x00;            /* Exit Initialization Mode Request */
    while ((CAN0CTL1&0x01) != 0){};	/* Wait for Normal Mode */

}


/*
 * functionName: a description of the function functionName.
 * Another line of the description.
 *
 * Parameters: param1 - description
 * param2 - description
 *
 * Return : description of the value returned by functionName
 */
unsigned char CAN0SendFrame(unsigned long id, unsigned char priority, unsigned char length, unsigned char *txdata ){
    
    unsigned char index;
    unsigned char txbuffer = {0};

    if (!CAN0TFLG)              /* Is Transmit Buffer full?? */
        return ERR_BUFFER_FULL;

    CAN0TBSEL = CAN0TFLG;       /* Select lowest empty buffer */
    txbuffer = CAN0TBSEL;		/* Backup selected buffer */
    
    /* Load Id to IDR Registers */
    *((unsigned long *) ((unsigned long)(&CAN0TXIDR0)))= id;
        
    for (index=0;index<length;index++) {
        *(&CAN0TXDSR0 + index) = txdata[index];  /* Load data to Tx buffer 
                                                  * Data Segment Registers
                                                  */
    }

    CAN0TXDLR = length;                          /* Set Data Length Code */
    CAN0TXTBPR = priority;                       /* Set Priority */

    CAN0TFLG = txbuffer;	                     /* Start transmission */
                
    while ( (CAN0TFLG & txbuffer) != txbuffer);  /* Wait for Transmission 
                                                  * completion 
                                                  */
    return NO_ERR;
 }

/*
 * functionName: a description of the function functionName.
 * Another line of the description.
 *
 * Parameters: param1 - description
 * param2 - description
 *
 * Return : description of the value returned by functionName
 */
void Delay (void) {

    unsigned int counter;
    
    for (counter=0;counter<10000;counter++); 
}

/*
 * ModuleISR: a description of the function functionName.
 * Another line of the description.
 *
 * Parameters: None
 * MSCAN0 RECEIVE ISR 
 * DESCRIPTION:
 * Interrupt asserted when a message has been received and shifted into 
 * the foreground buffer of the receiver FIFO.
 *
 * Return : None
 */
#pragma CODE_SEG NON_BANKED

void interrupt CAN0RxISR(void) {
    unsigned char length, index;
    unsigned char rxdata[8];

    length = (CAN0RXDLR & 0x0F);
	for (index=0; index<length; index++)
  	    rxdata[index] = *(&CAN0RXDSR0 + index); /* Get received data */

    CAN0RFLG = 0x01;   /* Clear RXF */
}

#pragma CODE_SEG DEFAULT

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -