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

📄 main.c

📁 st7单片机关于can总线设计的源程序,使用ST7单片机的很有用
💻 C
字号:
/*
*******************************************************************************
COPYRIGHT 2003 STMicroelectronics
Source File Name : main.c                      
Group            : MicroController Division
Author           : MCD Application Team
Date First Issued:
********************************Documentation**********************************
General Purpose - User Main Program 
********************************RevisionHistory********************************
_____________________________________________________________________________
Date : 			Release:       

******************************************************************************/
//#define PWMART_OCMP

#include "ST7lib_config.h"  /* Configuration file */
#include "user.h"
void App_Tx_Copy_Data(canuint8 tx_handle,canuint8 *data_ptr);

typedef struct
{
  canuint16 stdid;
  canuint16 extid;
  canuint8  dlc;
  canuint8  data[8];
}data_buff;

data_buff user_rx_struct;     /* user receive structure */
tCanMsgOject user_tx_struct;  /* user transmmit structure using CanMsgTransmit()
                                 service */   

void main(void)
{
  canuint8 cnt,rx_handle,tx_handle=0;
  canuint8 user_tx_buffer[8]; /* user transmit buffer */
  CanInitPowerOn();           /* Call CaninitPowerOn() service */
  CanInit(0);                 /* Call Caninit() service */
  while(1)
  {
    /* User Receive Part */ 
    for(rx_handle=0;rx_handle<NO_OF_RX_HANDLES;rx_handle++)
    {
      if(Can_Ind_Flags[rx_handle/8] & Ind_Mask[rx_handle%8]) /* check for 
                                                             message received*/
      {
        CanCanInterruptDisable();  /* disable the CAN interrupt */
        if(Can_Ovf_Flags[rx_handle/8] & Ovf_Mask[rx_handle%8])  /* check for
                                                                overflow */
        {
           Can_Ovf_Flags[rx_handle/8] &= (canuint8)(~(Ovf_Mask[rx_handle%8]));
          /* User specific action */
        }
        /*data copy from message global buffer into the user appli buffer */
        /*User may implement queue structure for storing the received message*/
        user_rx_struct.stdid = Rx_Stdid[rx_handle]; 
        user_rx_struct.extid = Rx_Extid[rx_handle];
        user_rx_struct.dlc = Rx_Dlc[rx_handle];
        for(cnt=0;cnt<Rx_Dlc[rx_handle];cnt++)
          user_rx_struct.data[cnt] = (canuint8)((Rx_Data_Ptr[rx_handle][cnt]));
        /*clear the indication flag */
        Can_Ind_Flags[rx_handle/8] &= (canuint8)(~(Ind_Mask[rx_handle%8])); 
        CanCanInterruptRestore();  /* enable the CAN interrupts */
      }
    }
   /* User Transmit Part using CanTransmit() service */
    if(!CanGetStatus())  /* not to transmit when the controller is into 
                         sleep/busoff state */
    {
      App_Tx_Copy_Data(tx_handle,user_tx_buffer); /* User function copying 
                                                     the user data into Global 
                                                     Tx Buffer for TxMessage0*/
      if(CanTransmit(tx_handle)!= KCANTXOK)       /* Call CanTransmit() service
                                                     for message 0 */ 
      {
        /* user specific action incase transmission request is not successful*/
      }
      for(cnt=0;cnt<240;cnt++);
      while(CanSleep() != KCANOK);
      CanWakeup();
    }  
    if(!CanGetStatus())
    {
      /* User Transmit part using CanMsgTransmit() service */  
      user_tx_struct.stdid = MAKE_STD_ID(0x500); /* fill the stdid part */
		    user_tx_struct.extid = 0x0000;
		    user_tx_struct.dlc = 8; /* set the dlc */
      for(cnt=0;cnt<user_tx_struct.dlc;cnt++)
		     user_tx_struct.data[cnt]= cnt; /* copy dummy data into the tx buffer*/
		  if(CanMsgTransmit(&user_tx_struct)!= KCANTXOK)
      {
         cnt =0;
         /* user specific action incase transmission request is not successful*/
      }
      for(cnt=0;cnt<255;cnt++);
      CanSleep();
      CanWakeup();
    }
  }
}

void App_Tx_Copy_Data(canuint8 tx_handle,canuint8 *data_ptr)
{
  canuint8 cnt;
  CanGlobalInterruptDisable();
  for(cnt=0;cnt<Tx_Dlc[tx_handle];cnt++)
  {
    Tx_Data_Ptr[tx_handle][cnt] = *data_ptr++; /* copying the user tx data into 
                                                  global tx buffer */ 
  }
  CanGlobalInterruptRestore();
}

/******************** (c) 2003  ST Microelectronics *************END OF FILE***/

⌨️ 快捷键说明

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