stm32之定時器徹底研究
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
4、main.c文件
#include "STM32Libstm32f10x.h"
#include "hal.h"
#include "stdio.h"
#include "string.h"
extern unsigned int cnt;
extern unsigned int flag;
#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;
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
}
return ch;
}
{
//USART_SendData(USART1, (u8) ch);
USART1->DR = (u8) ch;
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(); //片內(nèi)硬件初始化
ChipOutHalInit(); //片外硬件初始化
for(;;)
{
can_send(&msg);
if(flag)
{
flag=0;
printf("cnt is %d",cnt);
}
}
}
{
CanTxMsg msg;
msg.StdId=0x11;
msg.DLC=8;
msg.IDE=CAN_ID_STD;
msg.RTR=CAN_RTR_DATA;
memset(msg.Data,0x11,8);
ChipHalInit(); //片內(nèi)硬件初始化
ChipOutHalInit(); //片外硬件初始化
for(;;)
{
can_send(&msg);
if(flag)
{
flag=0;
printf("cnt is %d",cnt);
}
}
}
本程序使用了串口、定時器,通過串口將當(dāng)前計數(shù)值發(fā)給PC。
同時通過can總線對外發(fā)送數(shù)據(jù)
另外使用了printf,程序中有相應(yīng)的配置。
5、can.c文件
#include "STM32Libstm32f10x.h"
#include "hal.h"
#include
#include "hal.h"
#include
//CAN總線的發(fā)送接收管腳的初始化
void CAN_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
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);
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);
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_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
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);
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);
}
void CAN_Interrupt(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
// CanTxMsg TxMessage;
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM=DISABLE; //時間觸發(fā)
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_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);
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
}
//發(fā)送一個2字節(jié)的數(shù)據(jù)
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;
}
TxMessage.StdId=pTransmitBuf ->StdId;//用來設(shè)定標(biāo)準(zhǔn)標(biāo)識符(0-0x7ff,11位)
//TxMessage.ExtId=pTransmitBuf ->ExtId;
TxMessage.RTR= pTransmitBuf ->RTR;//設(shè)置RTR位為數(shù)據(jù)幀
TxMessage.IDE= pTransmitBuf ->IDE;//標(biāo)識符擴(kuò)展位,為標(biāo)準(zhǔn)幀
TxMessage.DLC= pTransmitBuf ->DLC;//設(shè)置數(shù)據(jù)長度
//根據(jù)DLC字段的值,將有效數(shù)據(jù)拷貝到發(fā)送數(shù)據(jù)寄存器
memcpy(TxMessage.Data, pTransmitBuf ->Data,pTransmitBuf ->DLC);
TransmitMailbox = CAN_Transmit(CAN1,&TxMessage);
TransmitMailbox=TransmitMailbox;//加上這句話就是防止編譯器產(chǎn)生警告
return 1;
}
#endif
在調(diào)用can_send(CanTxMsg *pTransmitBuf)發(fā)送數(shù)據(jù)之前,要對can總線進(jìn)行相應(yīng)的配置。
6、hal.c文件
//STM32F103RBT6有三個通用定時器,定時器2、3、4;操作基本一致
#include "STM32Libstm32f10x.h"
//各個內(nèi)部硬件模塊的配置函數(shù)
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 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);
extern void CAN_Interrupt(void);
void ChipHalInit(void)
{
//初始化時鐘源
RCC_Configuration();
//初始化GPIO
GPIO_Configuration();
//初始化中斷源
NVIC_Configuration();
//初始化串口
USART_Configuration();
//初始化定時器
//TIM2_Configuration();
//
//TIM3_Configuration(10000,7199);
USART_Configuration();
//初始化定時器
//TIM2_Configuration();
//
//TIM3_Configuration(10000,7199);
//TIM4_Configuration();
TIM_Configuration(10000,7199);
//初始化CAN總線
CAN_Configuration();
//初始化CAN總線接收中斷
CAN_Interrupt();
}
void ChipOutHalInit(void)
{
}
評論