//------------------------------------------------------------------
//函數(shù)名:void Time_Init()
//輸入?yún)?shù):null
//返回參數(shù):null
//說明:定時(shí)器初始化函數(shù)
//------------------------------------------------------------------
void Time_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;本文引用地址:http://butianyuan.cn/article/201611/320971.htm TIM_DeInit(TIM3);//復(fù)位TIM3定時(shí)器
TIM_TimeBaseStructure.TIM_Period =7999; //設(shè)置自動(dòng)重裝載寄存器鎖存值,1ms溢出
TIM_TimeBaseStructure.TIM_Prescaler = 8;//9分頻
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //時(shí)鐘分頻因子
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//計(jì)數(shù)器向上計(jì)數(shù)模式
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);//寫TIM3各寄存器參數(shù)
TIM_ClearFlag(TIM3,TIM_FLAG_Update);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
}
文件:usart.h
#ifndef _USART_H
#define _USART_H
#include
#include "stm32f10x.h"
void RCC_Configuration(void); //聲明RCC初始化函數(shù)
void GPIO_Configuration(void); //聲明GPIO初始化函數(shù)
void NVIC_Configuration(void); //聲明NVIC初始化函數(shù)
void USART_Configuration(void); //聲明串口初始化函數(shù)
void Time_Init(void); //聲明定時(shí)器初始化函數(shù)
unsigned short CRC16(unsigned char* puchMsg, unsigned short usDataLen);
#endif
文件:stm32f103x_it.c
//需要設(shè)置串口接收中斷和定時(shí)器3中斷,中斷時(shí)間為1ms
//------------------------------------------------------------------
//函數(shù)名:void USART1_IRQHandler(void)
//輸入?yún)?shù):null
//返回參數(shù):null
//說明:串口接收中斷服務(wù)
//------------------------------------------------------------------
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //判斷讀寄存器是否非空
{
// GPIO_SetBits(GPIOB,GPIO_Pin_6);
rx_data[RbufCounter++]=USART_ReceiveData(USART1); //接收字節(jié)到接收緩沖區(qū)
if(USART_Rsv_Status==0)
{
if(RbufCounter>1)
{
if(rx_data[0]==0xA5&&rx_data[1]==0x5A) //當(dāng)接收到的數(shù)據(jù)幀頭兩個(gè)字節(jié)同時(shí)為0xA5和0x5A時(shí)
{
USART_Rsv_Status=1;
// USART_SendData(USART1, rx_data[0]);
}
else
{
rx_data[0]=rx_data[1];
RbufCounter=1;
}
}
}
else
{
USART_1ms_Cnt=0;
}
}
}
//------------------------------------------------------------------
//函數(shù)名:void TIM2_IRQHandler(void)
//輸入?yún)?shù):null
//返回參數(shù):null
//說明:定時(shí)器2中斷服務(wù)
//------------------------------------------------------------------
void TIM2_IRQHandler(void)
{
}
//------------------------------------------------------------------
//函數(shù)名:void TIM3_IRQHandler(void)
//輸入?yún)?shù):null
//返回參數(shù):null
//說明:定時(shí)器3中斷服務(wù)
//------------------------------------------------------------------
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) //判斷是否為定時(shí)器3溢出中斷
{
GPIO_SetBits(GPIOB,GPIO_Pin_6);
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);//清中斷標(biāo)記
if(USART_Rsv_Status==1)
USART_1ms_Cnt++;
if(USART_1ms_Cnt>5)
{
// USART_SendData(USART1,0xAA);
USART_Rsv_Status=0;//連續(xù)計(jì)數(shù)超過5次對(duì)USART_Rsv_Status置0,繼續(xù)等待接收
USART_1ms_Cnt=0; //當(dāng)USART_1ms_Cnt>5時(shí)對(duì)USART_1ms_Cnt重新清零
if(RbufCounter==(u16)rx_data[4]+7) //檢驗(yàn)數(shù)據(jù)的完整性
{
int i; //定義循環(huán)變量
int j;
data_length=rx_data[4];
for(i=0;i
{
data[i]=rx_data[i];
}
CRC_data_Hi=rx_data[RbufCounter-1];
CRC_data_Lo=rx_data[RbufCounter-2];
CRC_data=CRC16((unsigned char*)data,data_length+5);
CRC_data_Hi1=CRC_data>>8;
CRC_data_Lo1=CRC_data&0x00ff;
if(CRC_data_Hi==(u8)CRC_data_Hi1 && CRC_data_Lo==CRC_data_Lo1)
{
for(j=0;rx_data[j]!=;j++) //循環(huán)逐字輸出,到結(jié)束字
{
USART_SendData(USART1, rx_data[j]); //發(fā)送字符
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
} //等待字符發(fā)送完畢
}
}
}
RbufCounter=0;
}
}
}
評(píng)論