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

📄 candemo.c

📁 stm32初级例程
💻 C
字号:
/*----------------------------------------------------------------------------
QQ: 958664258
21IC用户名:banhushui
交流平台:http://blog.21ic.com/user1/5817/index.html
淘宝店铺:http://shop58559908.taobao.com
旺旺:半壶水电子
编译器版本:MDK4.12
 *---------------------------------------------------------------------------*/
#include <stm32f10x_lib.h>                        // STM32F10x Library Definitions
#include "STM32_Reg.h"                            // STM32 register and bit Definitions
#include "STM32_Init.h"                           // STM32 Initialization
#include "CAN.h"                                  // STM32 CAN adaption layer
#include "MyType.h"
#include "config.h"

//为了保证CAN通信的效率,本例子只有CAN初始化配置函数才使用ST官方库函数
//
/*----------------------------------------------------------------------------
  insert a delay time.
 *----------------------------------------------------------------------------*/
void delay(unsigned int nCount)
{
   for (; nCount != 0; nCount--)
      ;
}


/*----------------------------------------------------------------------------
  initialize CAN interface
 *----------------------------------------------------------------------------*/
void can_Init(void)
{
   CAN_setup();                                   // setup CAN interface
   CAN_wrFilter(33, STANDARD_FORMAT);             // Enable reception of messages

   /* COMMENT THE LINE BELOW TO ENABLE DEVICE TO PARTICIPATE IN CAN NETWORK   */
   //CAN_testmode(CAN_BTR_SILM | CAN_BTR_LBKM);      // Loopback, Silent Mode (self-test)

   //软件仿真使用环回模式
   //CAN_testmode(CAN_BTR_SILM | CAN_BTR_LBKM); /* Loopback and           */
   //硬件仿真使用正常模式
   CAN_testmode(0); //正常模式

   CAN_start();                                   // leave init mode

   CAN_waitReady();                               // wait til mbx is empty
}



/*----------------------------------------------------------------------------
  MAIN function
 *----------------------------------------------------------------------------*/
int main(void)
{
   uint32 i;
   //uint8 flag=0;

   stm32_Init();
   can_Init();                                    // initialise CAN interface


   CAN_TxMsg.id = 33;                              // initialise message to send
   for (i = 0; i < 8; i++)
      CAN_TxMsg.data[i] = 0;
   CAN_TxMsg.len = 4;
   CAN_TxMsg.format = STANDARD_FORMAT;
   CAN_TxMsg.type = DATA_FRAME;

   i = 0;
   while (1)
   {
      if (CAN_TxRdy)
      {
         if (++i == 60000)
         {
            //i=0;
            CAN_TxMsg.data[0] = 0;
            CAN_TxMsg.data[1] = 0;
            CAN_TxMsg.data[2] = 0;
            CAN_TxMsg.data[3] = 0;

            CAN_TxRdy = 0;
            CAN_wrMsg(&CAN_TxMsg);
         }
         else if (i == 120000)
         {
            i = 0;

            CAN_TxMsg.data[0] = 1;
            CAN_TxMsg.data[1] = 1;
            CAN_TxMsg.data[2] = 1;
            CAN_TxMsg.data[3] = 1;

            CAN_TxRdy = 0;
            CAN_wrMsg(&CAN_TxMsg);
         }
      }


      if (CAN_RxRdy)
      {
         CAN_RxRdy = 0;

         if (CAN_RxMsg.data[0] == 0)////PC8状
            PC8 = 0;
         else
            PC8 = 1;

         if (CAN_RxMsg.data[1] == 0)////PC9状
            PC9 = 0;
         else
            PC9 = 1;

         if (CAN_RxMsg.data[2] == 0)////PC10状
            PC10 = 0;
         else
            PC10 = 1;

         if (CAN_RxMsg.data[3] == 0)////PC11状
            PC11 = 0;
         else
            PC11 = 1;
      }
   } // end while
} // end main

⌨️ 快捷键说明

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