//这是我的程序。
#include "stm32f0xx.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
static __IO uint32_t TimingDelay; //_IOÊÇviolateµÄºê¶¨Òå ÊÇÔÚCORE_CM0.H STM32F0XX.MISC.HÖж¨ÒåµÄ
//UINT32_T ÊÇUNSIGNED INT µÄºê¶¨ÒåÔÚSTDINT.H STM32F0XX.MISC.HÖж¨ÒåµÄ
void Delay(__IO uint32_t nTime); //ÉùÃ÷
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure; //Óù¹Ô캯Êý¶¨ÒåÒ»¸ö¹¹Ô캯ÊýµÄ±äÁ¿
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
uint8_t Res = 0xAA;
int main(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //¿ªÆôGPIOʱÖÓ
USART_DeInit(USART2); //¸´Î»´®¿Ú2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //UART_TXD
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍì
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1); // ÕâÊÇ´íµÄ===GPIO_PinAFConfig(GPIOA,GPIO_PinSource2|GPIO_PinSource3,GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //UART_RXD
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //ÊäÈë
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //¸¡¿Õ
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No; //ÉèÖÃУÑéλ
USART_InitStructure.USART_Mode |= USART_Mode_Tx; //·¢ËÍ¿ÕÏÐλ×÷ΪµÚÒ»´ÎÊý¾Ý·¢ËÍ
USART_InitStructure.USART_Mode |= USART_Mode_Rx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //Á÷¿Ø
USART_Init(USART2,&USART_InitStructure);
// USART_ITConfig(USART2, USART_IT_TXE, ENABLE); //ʹÄÜ·¢ËÍÖжÏ
// USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //½ÓÊÕµ½USART_IT_RXNEÊý¾Ý£¬ÖжϿªÆô
USART_Cmd(USART2,ENABLE); //ʹÄÜ´®¿Ú
while (1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
if(USART_GetFlagStatus(USART2,USART_FLAG_RXNE) != RESET) //if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3) == 0x00)
{
USART_ClearFlag(USART2,USART_FLAG_RXNE);
USART_SendData(USART2,Res);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC) == RESET);
USART_ClearFlag(USART2,USART_FLAG_TC);
}
}
}
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
|