//USART
void UART2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//USART_WordLength_8b; //8 位數(shù)據(jù)
//USART_WordLength_9b; //9 位數(shù)據(jù)
USART_InitStructure.USART_StopBits = USART_StopBits_1;
//USART_StopBits_1 ;//在幀結(jié)尾傳輸 1 個停止位
//USART_StopBits_0.5;//在幀結(jié)尾傳輸 0.5 個停止位
//USART_StopBits_2 ;//在幀結(jié)尾傳輸 2 個停止位
//USART_StopBits_1.5;//在幀結(jié)尾傳輸 1.5 個停止位
USART_InitStructure.USART_Parity = USART_Parity_No;
//USART_Parity_No ;//奇偶失能
//USART_Parity_Even;//偶模式
//USART_Parity_Odd ;//奇模式
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//USART_HardwareFlowControl_None; //硬件流控制失能
//USART_HardwareFlowControl_RTS; //發(fā)送請求 RTS使能
//USART_HardwareFlowControl_CTS; //清除發(fā)送 CTS使能
//USART_HardwareFlowControl_RTS_CTS;//RTS和 CTS使能
//TIM1_OCIdleState是檢測定時器是否空閑,USART_HardwareFlowControl是硬件數(shù)據(jù)流控制,這個一般是設(shè)為無
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//USART_Mode_Tx;//發(fā)送使能
//USART_Mode_Rx;//接收使能
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
//USART_Clock_Enable ;//時鐘高電平活動
//USART_Clock_Disable;//時鐘低電平活動
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
//USART_CPOL_High;//時鐘高電平
//USART_CPOL_Low ;//時鐘低電平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
//USART_CPHA_1Edge;//時鐘第一個邊沿進(jìn)行數(shù)據(jù)捕獲
//USART_CPHA_2Edge;//時鐘第二個邊沿進(jìn)行數(shù)據(jù)捕獲
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
//USART_LastBit_Disable;//最后一位數(shù)據(jù)的時鐘脈沖不從 SCLK輸出
//USART_LastBit_Enable ;//最后一位數(shù)據(jù)的時鐘脈沖從 SCLK輸出
USART_Init(USART2, &USART_InitStructure);//初始化外設(shè) USARTx 寄存器
USART_Cmd(USART2, ENABLE);
//ENABLE-DISABLE//使能或者失能 USART 外設(shè)
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//ENABLE-DISABLE//使能或者失能指定的 USART 中斷
//USART_IT_PE //奇偶錯誤中斷
//USART_IT_TXE //發(fā)送中斷
//USART_IT_TC //傳輸完成中斷
//USART_IT_RXNE//接收中斷
//USART_IT_IDLE//空閑總線中斷
//USART_IT_LBD //LIN中斷檢測中斷
//USART_IT_CTS //CTS中斷
//USART_IT_ERR //錯誤中斷
USART_ClockInit(USART2, &USART_ClockInitStructure);
}
void USART3_Putc(unsigned char c)//串口3發(fā)送一個字符
{
USART_SendData(USART3, c);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET );
}
void USART3_Puts(unsigned char * str)//串口3發(fā)送一個字
{
while(*str)
{
USART_SendData(USART3, *str++);
/* 循環(huán),直到傳輸結(jié)束*/
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
}
//用于查詢接收
void USART1_IRQHandler(void)
{
u8 c;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
c=USART1->DR;
}
}
/**********************調(diào)試成功例程**************************/
//==================串口1 NVIC初始化==============
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
//NVIC_ClearIRQChannelPendingBit(USART1_IRQChannel);
//設(shè)置NVIC優(yōu)先級分組為Group2:0-3搶占式優(yōu)先級,0-3的響應(yīng)式優(yōu)先級
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* enabling interrupt */
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//==================串口1 RCC初始化==============
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
//==================串口1初始化GPIO_USART==============
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1,ENABLE);
}
//==================串口1中斷處理函數(shù)==============
void USART1_IRQHandler(void)
{
u16 usarnum;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Disable USART3 RXNE Interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
usarnum=USART_ReceiveData(USART1);
USART_SendData(USART2, usarnum);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_ITConfig( USART1,USART_IT_RXNE, ENABLE);
}
}
//==================串口2中斷處理函數(shù)==============
void USART2_IRQHandler(void)
{
}
//==================串口3中斷處理函數(shù)==============
void USART3_IRQHandler(void)
{
}
//==================串口發(fā)送處理函數(shù)==============
void USART1_Putc(unsigned char c)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1, c);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
}
//==================串口發(fā)送底層函數(shù)==============
void USART_SendData(USART_TypeDef* USARTx, u16 Data)//
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_DATA(Data));
/* Transmit Data */
USARTx->DR = (Data & (u16)0x01FF);
}
//==================串口main==============
int main(void)
{
RCC_Configuration(); //初始化時鐘
NVIC_Configuration();//初始化中斷
USART_Configuration();//初始化串口
while (1) ;