实现很简单的串口通信,就是出不了结果!!!
void USART_Configuration(void){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD
|RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/*USART1端口配置
   A9 TX 复用推挽输出 PA10 RX 浮空输入模式*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
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_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*USART2端口配置
   D5 TX 复用推挽输出 PD6 RX 浮空输入模式*/
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/*--------------USART1 USART2配置-------------------*/
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_Init(USART2, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
}
中断部分:
void USART1_IRQHandler(void)
{
u16 RX=0xff;
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
//接收到数据 //确认是否接收到数据
//if((USART_GetITStatus(USART1, USART_IT_RXNE))!= RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_TXE);
// USART_ClearITPendingBit(USART1, USART_IT_RXNE);
//RX=USART_ReceiveData(USART1);
USART_SendData(USART1,RX) ;
//将数据回送至超级终端
// USART_SendData(USART1, USART_ReceiveData(USART1));
//等待数据发送完毕
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
main函数:
int main(void)
{
//extern uint16_t value;
//USART_TypeDef*p= USART1;
/* Setup STM32 system (clocks, Ethernet, GPIO, NVIC) and STM3210C-EVAL resources
设置STM32系统(时钟,以太网,GPIO,NVIC)和STM3210C-EVAL资源 */
System_Setup(); //这个函数就是完成上面的各种初始化,
/* Initilaize the LwIP satck */
//LwIP_Init();
// value = ETH_ReadPHYRegister(0x00, PHY_BCR);
/* Initilaize the HelloWorld module */
/*服务器端初始化*/
// HelloWorld_init();
/* Initilaize the webserver module */
/*网页服务初始化*/
// httpd_init();
/* Initialize the TFTP server */
//tftpd_init();
/*串口初始化*/
//USART_DeInit(p);
/* Infinite loop */
while (1)
{
/* Periodic tasks */
// System_Periodic_Handle();
USART_SendData(USART1,0xff) ;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
这就是 System_Setup
void System_Setup(void)
{
RCC_ClocksTypeDef RCC_Clocks;
/* Setup STM32 clock, PLL and Flash configuration)设置STM32时钟,PLL和Flash配置 */
SystemInit();
/* Enable USART2 clock 使USART2时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* Enable ETHERNET clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ETH_MAC | RCC_AHBPeriph_ETH_MAC_Tx |
RCC_AHBPeriph_ETH_MAC_Rx, ENABLE);
/* Enable GPIOs and ADC1 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO |
RCC_APB2Periph_ADC1, ENABLE);
/* NVIC configuration */
NVIC_Configuration();
USART_Configuration();
/* ADC configuration */
ADC_Configuration();
/* Configure the GPIO ports 配置*/
GPIO_Configuration();
/* Initialize the STM3210C-EVAL's LCD */
//STM3210C_LCD_Init();
GLCD_init();
/* Initialize STM3210C-EVAL's LEDs */
STM_EVAL_LEDInit(LED1);
STM_EVAL_LEDInit(LED2);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
/* Turn on leds available on STM3210X-EVAL */
STM_EVAL_LEDOn(LED1);
STM_EVAL_LEDOn(LED2);
STM_EVAL_LEDOn(LED3);
STM_EVAL_LEDOn(LED4);
/* Clear the LCD */
GLCD_clear(White);
/* Set the LCD Back Color */
GLCD_setBackColor(White);
/* Set the LCD Text Color */
GLCD_setTextColor(Blue);
/* Display message on the LCD*/
GLCD_displayStringLn(Line0, MESSAGE1);
GLCD_displayStringLn(Line1, MESSAGE2);
GLCD_displayStringLn(Line2, MESSAGE3);
GLCD_displayStringLn(Line3, MESSAGE4);
/* Configure the Ethernet peripheral */
Ethernet_Configuration();
/* SystTick configuration: an interrupt every 10ms */
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.SYSCLK_Frequency / 100);
/* Update the SysTick IRQ priority should be higher than the Ethernet IRQ */
/* The Localtime should be updated during the Ethernet packets processing */
NVIC_SetPriority (SysTick_IRQn, 1);
/* Configure the Key button */
STM_EVAL_PBInit(Button_KEY, Mode_GPIO);
}
|