#include "
STM32f30x.h"
#define uint unsigned int
#define uchar unsigned char
uint i,j,k;
uchar h;
/*void Delay(i)
{
for(j=0;j<=i;j++)
{
for(k=0;k<=1000;k++);
}
}*/
void NVIC_Configura
tion(void);
void USART1_IRQHandler(void);
void RCC_Configuration(void);
void UASRT_Configuration(void);
void GPIO_Configyration(void);
int main()
{
RCC_Configuration();
USART1_IRQHandler();
GPIO_Configyration();
UASRT_Configuration();
NVIC_Configuration();
//USART_SendData(USART1, 0X99);
while(1);
}
void RCC_Configuration(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//enable clk
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_DeInit(GPIOA);
USART_DeInit(USART1);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef USART_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
USART_InitStructure.NVIC_IRQChannel=USART1_IRQn;
USART_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
USART_InitStructure.NVIC_IRQChannelSubPriority=5;
USART_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&USART_InitStructure);
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
USART_SendData(USART1,USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
}
void UASRT_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;//UART
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_FLAG_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);//EN UART
USART_ClearFlag(USART1,USART_FLAG_TC);
}
void GPIO_Configyration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
}
请大神帮我看一下,为什么不能收发数据啊?谢谢了
好的 版主谢谢
版主 你的中断函数呢? 能给我看一下吗
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
rx[i++] = USART_ReceiveData(USART1);
}
if(i == 8)
{
i = 0;
uart_flag = 1;
}
一周热门 更多>