- UID
- 1029342
- 性别
- 男
|
4、main.c文件
#include "STM32Lib\\stm32f10x.h"
#include "hal.h"
#include "stdio.h"
#include "string.h"
extern unsigned int cnt;
extern unsigned int flag;
int fputc(int ch, FILE *f)
{
//USART_SendData(USART1, (u8) ch);
USART1->DR = (u8) ch;
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
}
return ch;
}
int main(void)
{
CanTxMsg msg;
msg.StdId=0x11;
msg.DLC=8;
msg.IDE=CAN_ID_STD;
msg.RTR=CAN_RTR_DATA;
memset(msg.Data,0x11,8);
ChipHalInit(); //片内硬件初始化
ChipOutHalInit(); //片外硬件初始化
for(;;)
{
can_send(&msg);
if(flag)
{
flag=0;
printf("cnt is %d\n",cnt);
}
}
}
本程序使用了串口、定时器,通过串口将当前计数值发给PC。
同时通过can总线对外发送数据
另外使用了printf,程序中有相应的配置。
5、can.c文件
#include "STM32Lib\\stm32f10x.h"
#include "hal.h"
#include <string.h>
//CAN总线的发送接收管脚的初始化
void CAN_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* PA11-CAN RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*PA12-CAN TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
}
/*******************************************************************************
**CAN中断测试
*******************************************************************************/
void CAN_Interrupt(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
// CanTxMsg TxMessage;
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE; //时间触发
CAN_InitStructure.CAN_ABOM=DISABLE; //自动离线管理
CAN_InitStructure.CAN_AWUM=DISABLE; //自动唤醒
CAN_InitStructure.CAN_NART=DISABLE; //ENABLE:错误不自动重传 DISABLE:重传
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; //CAN_Mode_LoopBack,CAN_Mode_Normal
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //1-4
CAN_InitStructure.CAN_BS1=CAN_BS1_5tq; //1-16
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq; //1-8
CAN_InitStructure.CAN_Prescaler=4; //波特率为 36/(4*(1+5+3))=1000k
CAN_Init(CAN1,&CAN_InitStructure);
/* CAN 过滤器设置 */
CAN_FilterInitStructure.CAN_FilterNumber=0;
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=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* 允许FMP0中断*/
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
}/*
typedef struct
{
uint32_t StdId;
uint32_t ExtId;
uint8_t IDE;
uint8_t RTR;
uint8_t DLC;
uint8_t Data[8];
} CanTxMsg;
*/
//发送一个2字节的数据
void SendCan(u16 dat)
{
CanTxMsg TxMessage;
TxMessage.ExtId=0x01;
TxMessage.IDE=CAN_ID_EXT;
TxMessage.RTR=CAN_RTR_DATA;
TxMessage.DLC=2;
TxMessage.Data[0]=dat&0xff;
TxMessage.Data[1]=dat>>8;
CAN_Transmit(CAN1,&TxMessage);
}
#if 1
int can_send(CanTxMsg *pTransmitBuf)
{
u8 TransmitMailbox=0;
CanTxMsg TxMessage;
if(pTransmitBuf -> DLC > 8)
{
return 1;
}
/* transmit */
TxMessage.StdId=pTransmitBuf ->StdId;//用来设定标准标识符(0-0x7ff,11位)
//TxMessage.ExtId=pTransmitBuf ->ExtId;
TxMessage.RTR= pTransmitBuf ->RTR;//设置RTR位为数据帧
TxMessage.IDE= pTransmitBuf ->IDE;//标识符扩展位,为标准帧
TxMessage.DLC= pTransmitBuf ->DLC;//设置数据长度
//根据DLC字段的值,将有效数据拷贝到发送数据寄存器
memcpy(TxMessage.Data, pTransmitBuf ->Data,pTransmitBuf ->DLC);
TransmitMailbox = CAN_Transmit(CAN1,&TxMessage);
TransmitMailbox=TransmitMailbox;//加上这句话就是防止编译器产生警告
return 1;
}
#endif
在调用can_send(CanTxMsg *pTransmitBuf)发送数据之前,要对can总线进行相应的配置。
6、hal.c文件
/***************************************************
**HAL.c
**主要用于芯片硬件的内部外围和外部外围的初始化,两大INIT函数
**在MAIN中调用,使MAIN函数中尽量与硬件库无关
***************************************************/
//STM32F103RBT6有三个通用定时器,定时器2、3、4;操作基本一致
#include "STM32Lib\\stm32f10x.h"
//各个内部硬件模块的配置函数
extern void GPIO_Configuration(void); //GPIO
extern void RCC_Configuration(void); //RCC
extern void USART_Configuration(void); //串口
extern void NVIC_Configuration(void); //NVIC
extern void TIM2_Configuration(void);
extern void TIM3_Configuration(u16 p,u16 psc);
extern void TIM4_Configuration(void);
extern void TIM_Configuration(u16 p,u16 psc);
extern void CAN_Configuration(void);
extern void CAN_Interrupt(void);
/*******************************
**函数名:ChipHalInit()
**功能:片内硬件初始化
*******************************/
void ChipHalInit(void)
{
//初始化时钟源
RCC_Configuration();
//初始化GPIO
GPIO_Configuration();
//初始化中断源
NVIC_Configuration();
//初始化串口
USART_Configuration();
//初始化定时器
//TIM2_Configuration();
//
//TIM3_Configuration(10000,7199);
//TIM4_Configuration();
TIM_Configuration(10000,7199);
//初始化CAN总线
CAN_Configuration();
//初始化CAN总线接收中断
CAN_Interrupt();
}
/*********************************
**函数名:ChipOutHalInit()
**功能:片外硬件初始化
*********************************/
void ChipOutHalInit(void)
{
}
|
|