新聞中心

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

STM32串口通信測試程序

作者: 時間:2016-11-28 來源:網(wǎng)絡 收藏
//================================================
void Com5TxChar( vu8 i )
{
USART_SendData(UART5,i); //回發(fā)給PC
while(USART_GetFlagStatus(UART5, USART_FLAG_TXE)==RESET);
}
//---------------------
void Com5TxStr( vu8 *pt, vu8 length )
{
vu8 i;
for( i=0;i
{
Com5TxChar(*pt++);
}
// delay(10);
}
//----------------------------
void Com1TxChar( vu8 i )
{
USART_SendData(USART1,(u8) i); //回發(fā)給PC
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET)
{
}
// return i;
}
void Com1TxStr( vu8 *pt, vu8 length )
{
vu8 i;
for(i=0;i
Com1TxChar(*pt++);
}
// delay(10);
}
void Com2TxChar( vu8 i )
{
USART_SendData(USART2,(u8) i); //回發(fā)給PC
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==RESET)
{
}
// return i;
}
void Com2TxStr( vu8 *pt, vu8 length )
{
vu8 i;
for(i=0;i
Com2TxChar(*pt++);
}
// delay(10);
}
void Com3TxChar( vu8 i )
{
USART_SendData(USART3,(u8) i); //回發(fā)給PC
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE)==RESET)
{
}
// return i;
}
void Com3TxStr( vu8 *pt, vu8 length )
{
vu8 i;
for(i=0;i
Com3TxChar(*pt++);
}
// delay(10);
}
void Com4TxChar( vu8 i )
{
USART_SendData(UART4,(u8) i); //回發(fā)給PC
while(USART_GetFlagStatus(UART4, USART_FLAG_TXE)==RESET)
{
}
// return i;
}
void Com4TxStr( vu8 *pt, vu8 length )
{
vu8 i;
for(i=0;i
Com4TxChar(*pt++);
}
// delay(10);
}
void TIM2_IRQHandler(void)
{
vu8 i;
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
cnt1++;
cnt2++;
if(cnt1>=500)
{
// polling=1;
cnt1=0;
}
if(cnt2>=nt2) //RUNLED,WDI
{
GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0)));
if(Alert_flag==1)
{
GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_6)));
}
cnt2=0;
}
Time20msCnt++; // 秒級硬件延時
if( Time20msCnt>=50 && T1sCnt ){
Time20msCnt=0;
T1sCnt--;
}
if(T20msCnt){ //20ms單位延時
T20msCnt--;
}
capture1 = TIM_GetCapture1(TIM2);
TIM_SetCompare1(TIM2, capture1 + CCR1_Val );
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
capture2 = TIM_GetCapture2(TIM2);
TIM_SetCompare2(TIM2, capture2 + CCR2_Val );
}
}
//********串口COM5配置*****************************
void UART5_Configuration(void)
{
USART_InitTypeDef USART_InitStructure; //串口設置恢復默認參數(shù)
USART_InitStructure.USART_BaudRate = 115200; //波特率115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字長8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止字節(jié)
USART_InitStructure.USART_Parity = USART_Parity_No; //無奇偶校驗
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //無流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //打開Rx接收和Tx發(fā)送功能
//USART_Init(USART1, &USART_InitStructure);
USART_Init(UART5, &USART_InitStructure); //初始化
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); // 若接收數(shù)據(jù)寄存器滿,則產生中斷
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
// USART_ITConfig(UART5, USART_IT_TXE, ENABLE);
USART_Cmd(UART5, ENABLE); //啟動串口
//USART_Cmd(USART1, ENABLE);
//-----如下語句解決第1個字節(jié)無法正確發(fā)送出去的問題-----//
USART_ClearFlag(UART5, USART_FLAG_TC); // 清標志
//USART_ClearFlag(USART1, USART_FLAG_TC);
}
//************串口COM1~COM44配置*********************************
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure; //串口設置恢復默認參數(shù)
USART_InitStructure.USART_BaudRate = 1200; //波特率1200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字長8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止字節(jié)
USART_InitStructure.USART_Parity = USART_Parity_No; //無奇偶校驗
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //無流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //打開Rx接收和Tx發(fā)送功能
USART_Init(USART1, &USART_InitStructure);
USART_Init(USART2, &USART_InitStructure);
USART_Init(USART3, &USART_InitStructure);
USART_Init(UART4 , &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// 若接收數(shù)據(jù)寄存器滿,則產生中斷
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_ITConfig(UART4 , USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); //啟動串口
USART_Cmd(USART2, ENABLE);
USART_Cmd(USART3, ENABLE);
USART_Cmd(UART4, ENABLE);
//-----如下語句解決第1個字節(jié)無法正確發(fā)送出去的問題-----//
USART_ClearFlag(USART1, USART_FLAG_TC);// 清標志
USART_ClearFlag(USART2, USART_FLAG_TC);
USART_ClearFlag(USART3, USART_FLAG_TC);
USART_ClearFlag(UART4 , USART_FLAG_TC);
}
//**********串口中斷函數(shù)********************
void UART5_IRQHandler(void)
{
uchar x,y,z;
uint checksum1,checksum2;
if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) //若接收數(shù)據(jù)寄存器滿
{
x=USART_ReceiveData(UART5);
}
Com5TxChar(x);
}
//------------------------------------------------
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //若接收數(shù)據(jù)寄存器滿
{
Com1RecDat[Com1RecCnt]=USART_ReceiveData(USART1);
}
Com1RecCnt++;
Com1RecCnt%=C1_LEN;
Com1TxChar(USART_ReceiveData(USART1));
}
//----------------------------------------------
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //若接收數(shù)據(jù)寄存器滿
{
Com2RecDat[Com2RecCnt]=USART_ReceiveData(USART2);
}
Com2RecCnt++;
Com2RecCnt%=C1_LEN;
Com2TxChar(USART_ReceiveData(USART2));
}
//-------------------------------------------------
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //若接收數(shù)據(jù)寄存器滿
{
Com3RecDat[Com3RecCnt]=USART_ReceiveData(USART3);
}
Com3RecCnt++;
Com3RecCnt%=C1_LEN;
Com3TxChar(USART_ReceiveData(USART3));
}
//----------------------------------------------------
void UART4_IRQHandler(void)
{
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //若接收數(shù)據(jù)寄存器滿
{
Com4RecDat[Com4RecCnt]=USART_ReceiveData(UART4);
}
Com4RecCnt++;
Com4RecCnt%=C1_LEN;
Com4TxChar(USART_ReceiveData(UART4));
}
//*****************主函數(shù)************************
int main(void)
{
u8 ld[69]=0;
u8 i;
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
UART5_Configuration();
USART_Configuration();
TIM2_Configuration();
SysTick_Configuration();
for(i=0;i<69;i++)
{
ld[i]=i;
}
Com1TxStr(ld,69);
Com2TxStr(ld,69);
Com3TxStr(ld,69);
Com4TxStr(ld,69);
Com5TxStr(ld,69);
while(1)
{
}
}

//-------------------------------------------------------------

本文引用地址:http://m.butianyuan.cn/article/201611/322880.htm

http://long0329998.blog.163.com/blog/static/58823187201252611116247/


上一頁 1 2 下一頁

評論


技術專區(qū)

關閉