[mw_shl_code=c,true]//程序如下:需要先发送'$'才行,但是在实验中,需要点击两次发送按钮,STM32才会回一次。不知道是哪里定义错了还是需要先清除什么。
#include "stm32f10x.h"
u8 uflag1=0;
u8 uflag2=0;
u16 rx_tp[10];
void usart_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE);
USART_DeInit(USART1);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_SetBits(GPIOA, GPIO_Pin_8);
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_SetBits(GPIOD, GPIO_Pin_2);
GPIO_Init(GPIOA,&GPIO_InitStruct);
USART_InitStruct.USART_BaudRate=9600L;
USART_InitStruct.USART_WordLength=USART_WordLength_8b;
USART_InitStruct.USART_StopBits=USART_StopBits_1;
USART_InitStruct.USART_Parity=USART_Parity_No;
USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=3;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TC, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void delay(u32 z)
{
u32 i;
for(i=0;i<z;i++);
}
int main(void)
{
u8 i;
usart_init();
while(1)
{
if(uflag2==1)
{
USART_SendData(USART1,'1');
uflag2=0;
}
}
}
void USART1_IRQHandler(void)
{
u16 rd;
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
rd=USART_ReceiveData(USART1);
if(rd=='$')
uflag2=2;
if(uflag2==2)
rx_tp[uflag1++]=rd;
if(uflag1>5)
{
uflag1=0;
uflag2=1;
}
}
if(USART_GetITStatus(USART1,USART_IT_TC)==SET)
{
USART_ClearFlag(USART1, USART_FLAG_TC);
}
}
[/mw_shl_code]
|