新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > STM32串口通信測試程序

STM32串口通信測試程序

作者: 時間:2016-11-28 來源:網(wǎng)絡(luò) 收藏
// 5個串口均可工作,已經(jīng)把定時器中斷、 串口中斷和配置函數(shù)寫在一個文件中 晶振 8Mhz
#include "stm32f10x_lib.h"
#include"stdio.h"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uint TimingDelay;
#define YES 1
#define NO 0
#define COM1_TX_EN GPIO_ResetBits(GPIOC,GPIO_Pin_1) // GPIOC->BSRR=0xfd //&=0xfd //USART1 發(fā)送使能
#define COM1_RX_EN GPIO_SetBits(GPIOC,GPIO_Pin_1) // GPIOC->BSRR=0x02 //|=0x02 //USART1 禁止發(fā)送
#define COM2_TX_EN GPIO_ResetBits(GPIOC,GPIO_Pin_2) // GPIOC->BSRR=0xfd //&=0xfd //USART2 發(fā)送使能
#define COM2_RX_EN GPIO_SetBits(GPIOC,GPIO_Pin_2)
#define COM3_TX_EN GPIO_ResetBits(GPIOC,GPIO_Pin_3) // GPIOC->BSRR=0xfd //&=0xfd //USART3 發(fā)送使能
#define COM3_RX_EN GPIO_SetBits(GPIOC,GPIO_Pin_3) // GPIOC->BSRR=0x02 //|=0x02 //USART3 禁止發(fā)送
#define COM4_TX_EN GPIO_ResetBits(GPIOC,GPIO_Pin_4) // GPIOC->BSRR=0xfd //&=0xfd //UART4 發(fā)送使能
#define COM4_RX_EN GPIO_SetBits(GPIOC,GPIO_Pin_4) // GPIOC->BSRR=0x02 //|=0x02 //UART4 禁止發(fā)送
//UART5 上行串口接收中斷數(shù)據(jù)定義
uchar ComSynFlag; //=YES同步建立,UART0內(nèi)部私有
uchar ComReceiveCounter; //接收字節(jié)計數(shù),UART0內(nèi)部私有
uchar ComReceiveData[24]; //接收緩沖,UART0內(nèi)部私有
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 Status; //定時器累加
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void TIM2_Configuration(void);
void UART5_Configuration(void);
void SysTick_Configuration(void) ;
void Com3TxChar(vu8);
vu16 CCR1_Val = 0x0E10; //360 50Hz 20ms
vu16 CCR2_Val = 0x0048; //7200 10hz 100ms
vu16 CCR3_Val = 0x7530; //30000 10Hz
vu16 CCR4_Val = 0x1000;
u16 capture1 = 0;
u16 capture2 = 0;
u16 capture3 = 0;
u16 capture4 = 0;
int cnt1,cnt2,nt2=50;
//UART1 下行串口接收中斷數(shù)據(jù)定義
#define C1_LEN 300 //緩沖區(qū)長度
uint Com1RecCnt; //COM1接收字節(jié)計數(shù)
uchar Com1RecDat[C1_LEN]; //COM1接收緩沖
uchar Com1AnsDat[24];
uint Com1AnsCnt;
uint Com2RecCnt; //COM2接收字節(jié)計數(shù)
uchar Com2RecDat[C1_LEN]; //COM2接收緩沖
uchar Com2AnsDat[24];
uint Com2AnsCnt;
uint Com3RecCnt; //COM3接收字節(jié)計數(shù)
uchar Com3RecDat[C1_LEN]; //COM3接收緩沖
uchar Com3AnsDat[24];
uint Com3AnsCnt;
uint Com4RecCnt; //COM4接收字節(jié)計數(shù)
uchar Com4RecDat[C1_LEN]; //COM4接收緩沖
uchar Com4AnsDat[24];
uint Com4AnsCnt;
uint Time20msCnt; //20ms定時器
uint T20msCnt; //20ms定時器
uint T1sCnt; //1s定時器
uchar RunCnt; //RUN LED閃光頻率參數(shù)
//=======================================================================
void RCC_Configuration()
{
ErrorStatus HSEStartUpStatus; //定義外部高速晶振啟動狀態(tài)枚舉變量
RCC_DeInit(); //復(fù)位RCC外部寄存器到默認值
RCC_HSEConfig(RCC_HSE_ON); //打開外部高速晶振
HSEStartUpStatus=RCC_WaitForHSEStartUp(); //等待外部高速時鐘準備好
if(HSEStartUpStatus==SUCCESS)
{ //外部高速時鐘已經(jīng)準備好
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //開啟FLASH預(yù)讀緩沖功能,加速FLASH的讀取。所有程序中必須的用法,位置:RCC初始化子函數(shù)里面,時鐘起振之后
FLASH_SetLatency(FLASH_Latency_2); //FLASH時序延遲幾個周期,等待總線同步操作。推薦按照單片機系統(tǒng)運行頻率,0—24MHz時,取Latency=0;24—48MHz時,取Latency=1;48~72MHz時,取Latency=2。
RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)==系統(tǒng)時鐘/1
RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(高速)(PCLK2)==系統(tǒng)時鐘/1
RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(低速)(PCLK1)==系統(tǒng)時鐘/2
//注:AHB主要負責(zé)外部存儲器時鐘。APB2負責(zé)AD,I/O,高級TIM,串口1。APB1負責(zé)DA,USB,SPI,I2C,CAN,串口2345,普通TIM。
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //配置PLL時鐘==(外部高速晶體時鐘/1)* 9 ==72MHz
RCC_PLLCmd(ENABLE); //使能PLL時鐘
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET); //等待PLL時鐘就緒
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系統(tǒng)時鐘==PLL時鐘
while(RCC_GetSYSCLKSource()!=0x08); //等待系統(tǒng)時鐘源的啟動
}
//==============以下為開啟外設(shè)時鐘的操作============================//
// RCC_AHBPeriphClockCmd (ABP2設(shè)備1 | ABP2設(shè)備2 , ENABLE); //啟動AHB設(shè)備
// RCC_APB2PeriphClockCmd(ABP2設(shè)備1 | ABP2設(shè)備2 , ENABLE); //啟動ABP2設(shè)備
// RCC_APB1PeriphClockCmd(ABP2設(shè)備1 | ABP2設(shè)備2 , ENABLE); //啟動ABP1設(shè)備
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1 , ENABLE); //打開APB2外設(shè)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB
|RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 |RCC_APB1Periph_UART4 |RCC_APB1Periph_USART2 |RCC_APB1Periph_USART3 | RCC_APB1Periph_TIM2,ENABLE);
}
//==============================================================
void SysTick_Configuration(void) //延時函數(shù)初始化
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);
SysTick_SetReload(72000);
SysTick_ITConfig(ENABLE);
}
//=================================
void delay(u32 nTime)
{
SysTick_CounterCmd(SysTick_Counter_Enable);
TimingDelay = nTime;
while(TimingDelay != 0);
SysTick_CounterCmd(SysTick_Counter_Disable);
SysTick_CounterCmd(SysTick_Counter_Clear);
}
//==================================
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
//============================================
void NVIC_Configuration(void )
{
NVIC_InitTypeDef NVIC_InitStructure; //定義一個中斷結(jié)構(gòu)體
// NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //設(shè)置中斷向量表的起始地址為0x08000000
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //設(shè)置NVIC優(yōu)先級分組,方式。
//注:一共16個優(yōu)先級,分為搶占式和響應(yīng)式。兩種優(yōu)先級所占的數(shù)量由此代碼確定,NVIC_PriorityGroup_x可以是0、1、2、3、4,
//分別代表搶占優(yōu)先級有1、2、4、8、16個和響應(yīng)優(yōu)先級有16、8、4、2、1個。規(guī)定兩種優(yōu)先級的數(shù)量后,所有的中斷級別必須在其中選擇,
//搶占級別高的會打斷其他中斷優(yōu)先執(zhí)行,而響應(yīng)級別高的會在其他中斷執(zhí)行完優(yōu)先執(zhí)行。
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQChannel; //通道設(shè)置為串口5中斷
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中斷響應(yīng)優(yōu)先級0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打開中斷
NVIC_Init(&NVIC_InitStructure); //初始化
//定時器2中斷
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定義GPIO初始化結(jié)構(gòu)體
//--------將UART5 的TX 配置為復(fù)用推挽輸出 AF_PP---------------------//
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12; //管腳位置定義,標號可以是NONE、ALL、0至15。
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //輸出速度50MHz
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //推挽輸出模式 Out_PP
GPIO_Init(GPIOC,&GPIO_InitStructure); //E組GPIO初始化
//--------將USART1,USART2 的TX 配置為復(fù)用推挽輸出 AF_PP---------------------//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 |GPIO_Pin_2 | GPIO_Pin_0 | GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//--------將USART3 的TX 配置為復(fù)用推挽輸出 AF_PP---------------------//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//--------將UART4 的TX 配置為復(fù)用推挽輸出 AF_PP---------------------//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//--------將UART5 的RX 配置為復(fù)用浮空輸入 IN_FLOATING---------------------//
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; //管腳位置定義
//輸入模式下配置輸出速度無意義
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz; //輸出速度2MHz
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空輸入 IN_FLOATING
GPIO_Init(GPIOD,&GPIO_InitStructure); //C組GPIO初始化
//--------將USART1 USART2 的RX 配置為復(fù)用浮空輸入 IN_FLOATING---------------------//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 |GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空輸入 IN_FLOATING
GPIO_Init(GPIOA, &GPIO_InitStructure);
//--------將USART3 的RX 配置為復(fù)用浮空輸入 IN_FLOATING---------------------//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空輸入 IN_FLOATING
GPIO_Init(GPIOB, &GPIO_InitStructure);
//--------將UART4 的RX 配置為復(fù)用浮空輸入 IN_FLOATING---------------------//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空輸入 IN_FLOATING
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
//==============================================================
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定時器初始化結(jié)構(gòu)
//TIM_ICInitTypeDef TIM_ICInitStructure; //通道輸入初始化結(jié)構(gòu)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 0xffff;
TIM_TimeBaseStructure.TIM_Prescaler = 499; //時鐘分頻
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //時鐘分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //基本初始化
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//TIM_OCInitStructure.TIM_Pulse = 255;
//TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_Cmd(TIM2, ENABLE);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); //清除TIM2等待中斷更新中斷標志位
TIM_ITConfig(TIM2, TIM_IT_CC1 |TIM_IT_CC2 , ENABLE);
}

上一頁 1 2 下一頁

關(guān)鍵詞: STM32串口通信測試程

評論


技術(shù)專區(qū)

關(guān)閉