#include "stm32f2xx.h"
#include "stm32f2xx_rcc.h"
#define uchar unsigned char
USART_InitTypeDef USART_InitStructure;
uchar send[7]={'m','e','n','g','n','a','n'};
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
#define USE_CAN1
/* #define USE_CAN2 */
#ifdef USE_CAN1
#define CANx CAN1
#define CAN_CLK RCC_APB1Periph_CAN1
#define CAN_RX_PIN GPIO_Pin_0
#define CAN_TX_PIN GPIO_Pin_1
#define CAN_GPIO_PORT GPIOD
#define CAN_GPIO_CLK RCC_AHB1Periph_GPIOD
#define CAN_AF_PORT GPIO_AF_CAN1
#define CAN_RX_SOURCE GPIO_PinSource0
#define CAN_TX_SOURCE GPIO_PinSource1
#else /*USE_CAN2*/
#define CANx CAN2
#define CAN_CLK (RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2)
#define CAN_RX_PIN GPIO_Pin_5
#define CAN_TX_PIN GPIO_Pin_6
#define CAN_GPIO_PORT GPIOB
#define CAN_GPIO_CLK RCC_AHB1Periph_GPIOB
#define CAN_AF_PORT GPIO_AF_CAN2
#define CAN_RX_SOURCE GPIO_PinSource5
#define CAN_TX_SOURCE GPIO_PinSource6
#endif /* USE_CAN1 */
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
//void LED_Display(uint8_t Ledstatus);
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
//#define KEY_PRESSED 0x00
//#define KEY_NOT_PRESSED 0x01
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanTxMsg TxMessage;
CanRxMsg RxMessage;
unsigned char rcv[8]={0};
/* Private function prototypes -----------------------------------------------*/
void NVIC_Config(void);
void CAN_Config(void);
void Init_RxMes(CanRxMsg *RxMessage);
void Delay(void);
void Uart2Init(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
/*void CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
int i = 0;
int u8_RxLen = 0;
RxMessage.StdId = 0x00;
RxMessage.ExtId = 0x00;
RxMessage.IDE = 0;
RxMessage.RTR = 0;
RxMessage.DLC = 0;
RxMessage.FMI = 0;
for( i=0;i<8;i++){
RxMessage.Data=0x00;
}
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
u8_RxLen = RxMessage.DLC;
for( i=0;i<u8_RxLen; i++){
rcv[u8_RxLen] = RxMessage.Data[u8_RxLen];
CAN_ClearITPendingBit(CAN1,CAN_IT_FMP0);
CAN_FIFORelease(CAN1,CAN_FIFO0);
}
}
*/
int main(void)
{
int i;
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f2xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f2xx.c file
*/
CAN_Config();
/* NVIC configuration */
NVIC_Config();
Init_RxMes(&RxMessage);
Uart2Init(); //初始化串口
/*Configure Push button key */
/*STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO);*/
while(1){
/* 等待发送完成 */
// while((CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK));
// for(i=0;i<=8;i++)
// sendchar(TxMessage.Data);
/* 等待接收完成 */
while((CAN_MessagePending(CAN1,CAN_FIFO0) == 0))
CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);;
/* 接收数据 */
// if(CAN_MessagePending(CAN1,CAN_FIFO0)){
CAN_FIFORelease(CAN1,CAN_FIFO0);
// }
for(i=0;i<=8;i++)
sendchar(RxMessage.Data);
// printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r");
// RxMessage.IDE=1;
/* Infinite loop */
/*
RxMessage.StdId=0x00;
RxMessage.IDE=CAN_ID_STD;
RxMessage.DLC=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;
RxMessage.Data[2]=0x00;
RxMessage.Data[3]=0x00;
RxMessage.Data[4]=0x00;
RxMessage.Data[5]=0x00;
RxMessage.Data[6]=0x00;
RxMessage.Data[7]=0x00;
if (CAN_MessagePending(CAN1,CAN_FIFO0)){
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
;
}
}
// Delay();
for(i=0;i<=7;i++){
send=RxMessage.Data;
}
for(i=0;i<=7;i++)
{ sendchar(send);
}*/
}
}
/**
* @brief Configures the CAN.
* @param None
* @retval None
*/
void Uart2Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure1;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure1);
}
void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* CAN GPIOs configuration **************************************************/
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(CAN_GPIO_CLK, ENABLE);
/* Connect CAN pins to AF9 */
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT);
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT);
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN|CAN_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure);
/* CAN configuration ********************************************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
/* CAN register init */
CAN_DeInit(CANx);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
/* CAN Baudrate = 1MBps (CAN clocked at 30 MHz) */
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 2;
CAN_Init(CANx, &CAN_InitStructure);
/* CAN filter init */
#ifdef USE_CAN1
CAN_FilterInitStructure.CAN_FilterNumber = 0;
#else /* USE_CAN2 */
CAN_FilterInitStructure.CAN_FilterNumber = 14;
#endif /* USE_CAN1 */
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* Transmit Structure preparation */
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x01;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 1;
/* Enable FIFO 0 message pending Interrupt */
CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
}
/**
* @brief Configures the NVIC for CAN.
* @param None
* @retval None
*/
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef USE_CAN1
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
#else /* USE_CAN2 */
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
#endif /* USE_CAN1 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief Initializes the Rx Message.
* @param RxMessage: pointer to the message to initialize
* @retval None
*/
void Init_RxMes(CanRxMsg *RxMessage)
{
uint8_t i = 0;
RxMessage->StdId = 0x00;
RxMessage->ExtId = 0x00;
RxMessage->IDE = CAN_ID_STD;
RxMessage->DLC = 0;
RxMessage->FMI = 0;
for (i = 0;i < 8;i++)
{
RxMessage->Data = 0x00;
}
}
/**
* @brief Turn ON/OFF the dedicated led
* @param Ledstatus: Led number from 0 to 3.
* @retval None
*/
/*void LED_Display(uint8_t Ledstatus)
{
/* Turn off all leds */
/* STM_EVAL_LEDOff(LED1);
STM_EVAL_LEDOff(LED2);
STM_EVAL_LEDOff(LED3);
STM_EVAL_LEDOff(LED4);
switch(Ledstatus)
{
case(1):
STM_EVAL_LEDOn(LED1);
break;
case(2):
STM_EVAL_LEDOn(LED2);
break;
case(3):
STM_EVAL_LEDOn(LED3);
break;
case(4):
STM_EVAL_LEDOn(LED4);
break;
default:
break;
}
}
/**
* @brief Delay
* @param None
* @retval None
*/
void Delay(void)
{
uint16_t nTime = 0x0000;
for(nTime = 0; nTime <0xFFF; nTime++)
{
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
int sendchar (int c) {
if (c == '\n')
{
while (!(USART2->SR & 0x0080));
USART2->DR = 0x0D;
}
while (!(USART2->SR & 0x0080));
USART2->DR = (c & 0x1FF);
return (c);
}
程序如上 用的是飞思卡尔单片机给它发数据 但是收不到数据 求大神指教。 |
|