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

📄 main.c

📁 STR912FW44上的ADS项目工程
💻 C
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Date First Issued  : 05/18/2006 : Version 1.0
* Description        : This file contains the software implementation for the
*                      WDG software library verification.
********************************************************************************
* History:
* 05/24/2006 : Version 1.1
* 05/18/2006 : Version 1.0
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/


/* Standard include ----------------------------------------------------------*/
#include "91x_lib.h"

/* Include of other module interface headers ---------------------------------*/
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
canmsg RxCanMsg;
canmsg CanMsg;

GPIO_InitTypeDef    GPIO_InitStructure;
CAN_InitTypeDef     CAN_InitStructure;

const char disp_ln1[] = "STR91x DEMO";
const char disp_ln2[] = "CAN NORMAL";

  enum {
    LD2,
    LD3,
    LD4,
    LD5
   };

typedef  enum {
    NA,
    Priority_1,
    Priority_2,
    Priority_3
  }VIC_Priority;

  /* buffer for receive messages */
  canmsg RxCanMsg;

  /* used message object numbers */
  enum {
	CAN_TX_MSGOBJ = 0,
	CAN_RX_MSGOBJ = 1
  };

  /* array of pre-defined transmit messages */
  canmsg TxCanMsg[2] = {
	{ CAN_STD_ID,      0x0ff, 4, { 0x22, 0x33, 0x44, 0x00 } },
	{ CAN_EXT_ID, 0x12345678, 8, { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 } }
  };

/* Private functions ---------------------------------------------------------*/
void Delay(void)
{
  u32 i;
  for(i=0;i<60000;i++);
}
void System_Setup(void)
{
  SCU_MCLKSourceConfig(SCU_MCLK_OSC);
  SCU_PCLKDivisorConfig(SCU_PCLK_Div2);
  SCU_PLLFactorsConfig(128,25,4);
  SCU_PLLCmd(ENABLE);
  while(!(SCU->SYSSTATUS&SCU_FLAG_LOCK));
  SCU_MCLKSourceConfig(SCU_MCLK_PLL);

  SCU_APBPeriphClockConfig(__CAN, ENABLE);
  SCU_APBPeriphClockConfig(__GPIO0, ENABLE);
  SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
  SCU_APBPeriphClockConfig(__GPIO3, ENABLE);
  SCU_APBPeriphClockConfig(__GPIO6, ENABLE);
  SCU_APBPeriphClockConfig(__GPIO8, ENABLE);
  SCU_APBPeriphClockConfig(__GPIO9, ENABLE);
  SCU_AHBPeriphClockConfig(__VIC, ENABLE);

  SCU_APBPeriphReset(__CAN, DISABLE);
  SCU_APBPeriphReset(__GPIO0, DISABLE);
  SCU_APBPeriphReset(__GPIO2, DISABLE);
  SCU_APBPeriphReset(__GPIO3, DISABLE);
  SCU_APBPeriphReset(__GPIO6, DISABLE);
  SCU_APBPeriphReset(__GPIO8, DISABLE);
  SCU_APBPeriphReset(__GPIO9, DISABLE);
  SCU_AHBPeriphReset(__VIC, DISABLE);
}

void IO_Init(void)
{
  GPIO_DeInit(GPIO2);
  GPIO_DeInit(GPIO8);
  
  
  
/* GPIO Configuration --------------------------------------------------------*/
  //L2
  GPIO_DeInit(GPIO9);
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
  GPIO_Init (GPIO9, &GPIO_InitStructure);
  
  //L3
  GPIO_DeInit(GPIO3);
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
  GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
  GPIO_Init (GPIO3, &GPIO_InitStructure);
  
  //L4
  GPIO_DeInit(GPIO0);
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
  GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
  GPIO_Init (GPIO0, &GPIO_InitStructure);
  
  //L5
  GPIO_DeInit(GPIO6);
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
  GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
  GPIO_Init (GPIO6, &GPIO_InitStructure);


/* P3.3 alternate input 1, CAN_RX pin 61*/
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Direction=GPIO_PinInput;
GPIO_InitStructure.GPIO_Type=GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_IPConnected=GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate=GPIO_InputAlt1;
GPIO_Init(GPIO3,&GPIO_InitStructure);

/* P3.2 alternate output 2, CAN_TX pin 60*/
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Direction=GPIO_PinOutput;
GPIO_InitStructure.GPIO_Type=GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_IPConnected=GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt2;
GPIO_Init(GPIO3,&GPIO_InitStructure);

}

void LED_OFF(u8 LED)
{
  switch (LED)
  {
    case LD2:   GPIO_WriteBit(GPIO9, GPIO_Pin_6, Bit_RESET);  break;
    case LD3:   GPIO_WriteBit(GPIO3, GPIO_Pin_7, Bit_RESET);  break;
    case LD4:   GPIO_WriteBit(GPIO0, GPIO_Pin_1, Bit_RESET);  break;
    case LD5:   GPIO_WriteBit(GPIO6, GPIO_Pin_6, Bit_RESET);  break;
    default:;
  }
}

void LED_ON(u8 LED)
{
  switch (LED)
  {
    case LD2:   GPIO_WriteBit(GPIO9, GPIO_Pin_6, Bit_SET);  break;
    case LD3:   GPIO_WriteBit(GPIO3, GPIO_Pin_7, Bit_SET);  break;
    case LD4:   GPIO_WriteBit(GPIO0, GPIO_Pin_1, Bit_SET);  break;
    case LD5:   GPIO_WriteBit(GPIO6, GPIO_Pin_6, Bit_SET);  break;
    default:;
  }
}

void delay(u32 XTime)
{
  u32 j;

  for(j=0;j<XTime;j++);
}

void Init_CAN(void)
{
  /* initialize the CAN at a standard bitrate, interrupts disabled */
  CAN_InitStructure.CAN_ConfigParameters=0x0;
  CAN_InitStructure.CAN_Bitrate=CAN_BITRATE_1M;
  CAN_Init(&CAN_InitStructure);

  /* configure the message objects */
  CAN_InvalidateAllMsgObj();
  CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_STD_ID);
  CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);

}



/*******************************************************************************
* Function Name  : main
* Description    : Main program
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
  u8 i;

  #ifdef DEBUG
    debug();
  #endif
  
  System_Setup();/*PCLK=8MHz*/

  IO_Init();
  Init_CAN();

  LED_OFF(LD2);
  LED_OFF(LD3);
  LED_OFF(LD4);
  LED_OFF(LD5);


  while(1)
  {

    Delay();
    Delay();
    Delay();
    
    if(CAN_ReceiveMessage(CAN_RX_MSGOBJ, FALSE, &RxCanMsg))
    {
      if(RxCanMsg.Dlc == TxCanMsg[0].Dlc)
	  {

	      for(i = 0; i < RxCanMsg.Dlc && RxCanMsg.Data[i] == TxCanMsg[0].Data[i]; i++);
		  if(i >= RxCanMsg.Dlc)
		  {
		    // receiving succeeded
		    //L3
			GPIO_WriteBit(GPIO3, GPIO_Pin_7,GPIO_ReadBit(GPIO3,GPIO_Pin_7)^Bit_SET);
		  }
	  }
	  CAN_ReleaseRxMessage(CAN_RX_MSGOBJ);
    }
    
    CAN_SendMessage(CAN_TX_MSGOBJ, &TxCanMsg[0]);
    while(!CAN_WaitEndOfTx());
    
    //L2
    GPIO_WriteBit(GPIO9, GPIO_Pin_6,GPIO_ReadBit(GPIO9,GPIO_Pin_6)^Bit_SET);

    CAN_ReleaseTxMessage(CAN_TX_MSGOBJ);
    Delay();
 
  }

}


/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/







⌨️ 快捷键说明

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