串口代码分析
#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_Configuration(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);
}
请大神帮我看一下,为什么不能收发数据啊?谢谢了 帮顶 用示波器看了下,没进中断,具体原因未知,走过路过的帮我看下哈,帮我找找 #include "uart.h"
void USART_InitConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_7);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_7);
/*
*USART1_TX -> PA9 , USART1_RX -> PA10
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_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_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
/*Enable the USART1 Interrupt(??USART1??)*/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UART_send_byte(uint8_t byte) //µ¥×Ö½Ú·¢ËÍ
{
while(!((USART1->ISR)&(1<<7)));
USART1->TDR=byte;
}
void UART_Send(uint8_t *Buffer, uint32_t Length)
{
while(Length != 0)
{
while(!((USART1->ISR)&(1<<7)));//µÈ´ý·¢ËÍÍê
USART1->TDR= *Buffer;
Buffer++;
Length--;
}
}
对照看一下,你的程序写的有点乱。。 不懂帮顶 那片清茶 发表于 2015-5-19 21:55
#include "uart.h"
void USART_InitConfig(void)
好的 版主谢谢 那片清茶 发表于 2015-5-19 21:55
#include "uart.h"
void USART_InitConfig(void)
版主 你的中断函数呢?能给我看一下吗 tjhbwjk 发表于 2015-5-20 10:22
版主 你的中断函数呢?能给我看一下吗
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
rx = USART_ReceiveData(USART1);
}
if(i == 8)
{
i = 0;
uart_flag = 1;
}
页:
[1]