在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于STM32的CAN總線通信程序

            基于STM32的CAN總線通信程序

            作者: 時(shí)間:2016-11-24 來(lái)源:網(wǎng)絡(luò) 收藏

            void USB_HP_CAN_TX_IRQHandler (void)
            {
            // request completed mbx 0
            //郵箱0請(qǐng)求完成 當(dāng)發(fā)送完成后硬件對(duì)該位置1407
            if (CAN->TSR & (1<<0)) //檢測(cè)是否置1來(lái)判斷發(fā)送已完成
            {
            // reset request complete mbx 0
            //復(fù)位郵箱0請(qǐng)求,對(duì)該位寫1可以清0該位 407
            CAN->TSR |= 1<<0;
            // disable TME interrupt

            CAN->IER &= ~(1<<0); //禁止發(fā)送郵箱空中斷

            CAN_TxRdy = 1;
            }
            }

            本文引用地址:http://www.biyoush.com/article/201611/320933.htm


            void USB_LP_CAN_RX0_IRQHandler (void) {
            // message pending ?
            //接收到CAN報(bào)文,通過(guò)判斷報(bào)文數(shù)目判斷是否有報(bào)文
            if (CAN->RF0R & (1<<0))
            {
            // read the message
            //接收?qǐng)?bào)文
            CAN_rdMsg (&CAN_RxMsg);

            CAN_RxRdy = 1; // set receive flag
            }
            }


            ////////////////////////CAN初始化////////////////////////////////////////////////////////////////////////
            void can_Init (void)
            {

            CAN_setup (); // setup CAN interface
            CAN_wrFilter (33, STANDARD_FORMAT); // Enable reception of messages,filter set


            //CAN_testmode(CAN_BTR_SILM | CAN_BTR_LBKM); // Loopback, Silent Mode (self-test)

            //軟件仿真使用環(huán)回模式
            //CAN_testmode(CAN_BTR_SILM | CAN_BTR_LBKM);
            //硬件仿真使用正常模式
            CAN_testmode(); //正常模式
            // leave init mode
            //進(jìn)入正常模式
            CAN_start ();
            // wait til mbx is empty
            //等待CAN就緒
            CAN_waitReady ();
            }

            以下是.H文件
            #ifndef _CAN_H_
            #define _CAN_H_

            #define STANDARD_FORMAT 0
            #define EXTENDED_FORMAT 1

            #define DATA_FRAME 0
            #define REMOTE_FRAME 1

            #define CAN_ID_STD ((u32)0x00000000)
            #define CAN_ID_EXT ((u32)0x00000004)


            typedef struct {
            unsigned int id; // 29 bit identifier
            unsigned char data[8]; // Data field
            unsigned char len; // Length of data field in bytes
            unsigned char format; // 0 - STANDARD, 1- EXTENDED IDENTIFIER
            unsigned char type; // 0 - DATA FRAME, 1 - REMOTE FRAME
            } CAN_msg;


            void CAN_setup (void);
            //void CAN_init (void);
            void CAN_start (void);
            void CAN_waitReady (void);
            void CAN_wrMsg (CAN_msg *msg);
            void CAN_rdMsg (CAN_msg *msg);
            void CAN_wrFilter (unsigned int id, unsigned char filter_type);
            void can_Init (void);
            void CAN_testmode (void);

            extern CAN_msg CAN_TxMsg; // CAN messge for sending
            extern CAN_msg CAN_RxMsg; // CAN message for receiving
            extern u8 CAN_TxRdy; // CAN HW ready to transmit a message
            extern u8 CAN_RxRdy; // CAN HW received a message

            #endif // _CAN_H_

            以下是主程序部分:
            int main(void) //主函數(shù),int類型,無(wú)參數(shù)
            {
            u32 i=0;
            Stm32_Clock_Init(9);// 調(diào)用系統(tǒng)時(shí)鐘初始化子程序
            delay_init(72);// 調(diào)用延時(shí)初始化子程序
            IOSET_init();// 調(diào)用io口初始化子程序
            PWM_Init(9000,0);// 定時(shí)器4初始化,設(shè)定PWM頻率=72MHz/9000=8KH

            can_Init (); // initialise CAN interface

            // initialise message to send
            for (i = 0; i < 8; i++) CAN_TxMsg.data[i] = 0;







            while(1)
            {
            delay_us(160);


            if (CAN_TxRdy)
            {
            if(++i==600)
            {

            CAN_TxMsg.id = 33;
            CAN_TxMsg.len = 4;
            CAN_TxMsg.format = STANDARD_FORMAT;//使用標(biāo)準(zhǔn)幀
            CAN_TxMsg.type = DATA_FRAME;//數(shù)據(jù)幀


            //i=0;
            //下面是數(shù)據(jù)報(bào)文
            CAN_TxMsg.data[0] = 1;
            CAN_TxMsg.data[1] = 2;
            CAN_TxMsg.data[2] =3;
            CAN_TxMsg.data[3] =4;

            //發(fā)送CAN報(bào)文
            CAN_TxRdy = 0;
            CAN_wrMsg (&CAN_TxMsg);
            }
            else if(i==1200)
            {
            i=0;
            //下面是數(shù)據(jù)報(bào)文
            CAN_TxMsg.data[0] = 0;
            CAN_TxMsg.data[1] = 1;
            CAN_TxMsg.data[2] = 1;
            CAN_TxMsg.data[3] = 1;

            //發(fā)送CAN報(bào)文
            CAN_TxRdy = 0;
            CAN_wrMsg (&CAN_TxMsg);
            }

            }

            if (CAN_RxRdy)
            {
            CAN_RxRdy = 0;

            if(CAN_RxMsg.data[0]==1)////PC8狀態(tài)
            LED1=0;
            else
            LED1=1;

            if(CAN_RxMsg.data[1]==2)////PC9狀態(tài)
            LED2=0;
            else
            LED2=1;

            if(CAN_RxMsg.data[2]==3)////PC10狀態(tài)
            LED3=0;
            else
            LED3=1;

            if(CAN_RxMsg.data[3]==4)////PC11狀態(tài)
            LED4=0;
            else
            LED4=1;

            }

            }
            }


            上一頁(yè) 1 2 下一頁(yè)

            關(guān)鍵詞: STM32CAN總線通信程

            評(píng)論


            技術(shù)專區(qū)

            關(guān)閉