初级会员

- 积分
- 56
- 金钱
- 56
- 注册时间
- 2011-9-21
- 在线时间
- 0 小时
|

楼主 |
发表于 2011-9-21 18:28:13
|
显示全部楼层
回复【2楼】正点原子:
---------------------------------
#include "stm32f10x.h"
#include "SysTickDelay.h"
//#include "UART_INTERFACE.h"
//#include "eval.h"
#include <stdio.h>
//接收状态
//bit7,接收完成标志
//bit6,接收到0x0d
//bit5~0,接收到的有效字节数目
//0x0A=\n换行;0x0D=\r回车
u8 USART_RX_STA=0; //接收状态标记
u8 USART_RX_BUF[64]; //接收缓冲,最大64个字节.
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void UART_Configuration(void);
int main(void)
{
u8 t;
u8 len;
u16 times=0;
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
UART_Configuration();
SysTick_Initaize();
while(1)
{
if(USART_RX_STA&0x80)//如果接收完成
{
len=USART_RX_STA&0x3f;//得到此次接收到的数据长度,不大于64
printf("\n您发送的消息为:\n");
for(t=0;t<len;t++)
{
//USART1->DR=USART_RX_BUF[t];
//while((USART1->SR&0X40)==0);//等待发送结束
//STM_EVAL_SendChar(USED_COM_NUMBER,(uint8_t)USART_RX_BUF[t]);
USART_SendData(USART1, USART_RX_BUF[t]);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
}
printf("\n\n");//插入换行
USART_RX_STA=0;
}
else
{
times++;
if(times%50==0)
{
printf("\nMiniSTM32开发板 串口实验\n");
//STM_EVAL_SendChar(USED_COM_NUMBER,(uint8_t) 0x0a);
printf("正点原子@ALIENTEK\n\n\n");
}
if(times%200==0)printf("请输入数据,以回车键结束\n");
if(times%30==0) GPIOA->ODR ^= GPIO_Pin_8; //取反输出寄存器数据.
delay_ms(10);
}
}
}
void RCC_Configuration(void)
{
SystemInit();
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //使能串口所有GPIO模块时钟,并使能AFIO模块时钟
//RCC_APB1PeriphClockCmd(COM_USART_CLK[COM], ENABLE); //USART2
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //设置TX引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //设置RX引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
//============LED_Configuration========================================
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //配置时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void UART_Configuration(void)
{
//USART_Configration
USART_InitTypeDef USART_InitStructure;
/* Enable UART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能串口模块时钟
USART_InitStructure.USART_BaudRate = 9600;//115200;
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_InitStructure.USART_Clock = USART_Clock_Disable;
//USART_InitStructure.USART_CPOL = USART_CPOL_Low;
//USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
//USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
/* USART configuration */
USART_Init(USART1, &USART_InitStructure); //初始化USART
/* Enable USART */
USART_Cmd(USART1, ENABLE); //使能串口模块
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //中断使能
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC  reemption  riority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置优先级分组:先占优先级0位,从优先级4位
//设置向量表的位置和偏移
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //向量表位于RAM
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //向量表位于FLASH
#endif
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //USART1中断
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1
}
/*******************************************************************************
* Function Name: int fputc(int ch, FILE *f)
* Description : Retargets the C library printf function to the USART.printf重定向
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int fputc(int ch, FILE *f)
{
//STM_EVAL_SendChar(USED_COM_NUMBER,(uint8_t) ch);
USART_SendData(USART1, (uint8_t) ch);
//while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
while (!(USART1->SR & USART_FLAG_TXE));
return ch;
}
/*******************************************************************************
* Function Name: int fgetc(FILE *f)
* Description : Retargets the C library printf function to the USART.fgetc重定向
* Input : None
* Output : None
* Return : 读取到的字符
*******************************************************************************/
int fgetc(FILE *f)
{
while(!(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET))
{
}
assert_param(IS_USART_ALL_PERIPH(USART1));
return (uint16_t)(USART1->DR & (uint16_t)0x01FF);
}
|
|