新手入门
- 积分
- 9
- 金钱
- 9
- 注册时间
- 2016-5-7
- 在线时间
- 0 小时
|
1金钱
串口4程序哪有问题,怎么改?
#include "stm32f10x.h"
#include "sys.h"
#include "delay.h"
#include "uart4.h"
void uart_init(u32 bound);
void LED(void);
int main(void)
{
char x1='a';
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éèÖÃNVICÖD¶Ï·Ö×é2:2λÇàÕ¼óÅÏè¼¶£¬2λÏìó|óÅÏè¼¶
uart_init(115200); //′®¿ú3õê¼»ˉÎa115200
LED();
while(1)
{
USART_SendData(UART4,x1);
while(USART_GetFlagStatus(UART4,USART_FLAG_TC)!=SET);
USART_SendData(UART4,0x11);
while(USART_GetFlagStatus(UART4,USART_FLAG_TC)!=SET);
GPIO_SetBits(GPIOG,GPIO_Pin_15);
delay_ms(500);
GPIO_ResetBits(GPIOG,GPIO_Pin_15);
delay_ms(500);
}
}
void uart_init(u32 bound)
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure1;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE); //ê1ÄüUSART1£¬GPIOAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
//USART1_TX GPIOC10
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_10; //PC10
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOC, &GPIO_InitStructure1);//3õê¼»ˉGPIOC10
//USART1_RX GPIOC113õê¼»ˉ
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_11;//PC11
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
GPIO_Init(GPIOC, &GPIO_InitStructure1);//3õê¼»ˉGPIOC11
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//ÇàÕ¼óÅÏè¼¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×óóÅÏè¼¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
//USART 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//′®¿ú2¨ìØÂê
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæÅ¼D£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(UART4, &USART_InitStructure); //3õê¼»ˉ′®¿ú4
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//¿aÆô′®¿ú½óêüÖD¶Ï
USART_Cmd(UART4, ENABLE); //ê1Äü′®¿ú4
}
void UART4_IRQHandler(void) //′®¿ú4ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(UART4); //¶á衽óêÕμ½μÄêy¾Y
delay_ms(2);
USART_SendData(UART4,Res);
while(USART_GetFlagStatus(UART4,USART_FLAG_TC)!=SET);
}
}
void LED()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOG,&GPIO_InitStructure);
GPIO_ResetBits(GPIOG,GPIO_Pin_15);
}
//
//void Send_String(char *buff,u16 Len)
//{
// u16 i;
// for(i=0;i<8;i++)
// {
// USART_SendData(UART4,*buff++);
// while(USART_GetFlagStatus(UART4, USART_FLAG_TXE) ==RESET);
// }
//}
|
|