新手上路
- 积分
- 42
- 金钱
- 42
- 注册时间
- 2013-10-7
- 在线时间
- 0 小时
|
5金钱
源码:
#include "stm32f10x.h"
#include "usart.h"
#include "sys.h"
#if 1
#pragma import(__use_no_semihosting)
//??????????
struct __FILE
{
int handle;
};
FILE __stdout;
//??_sys_exit()??????????
_sys_exit(int x)
{
x = x;
}
//???fputc??
int fputc(int ch, FILE *f)
{
while((USART2->SR&0X40)==0);//????,??????
USART2->DR = (u8) ch;
return ch;
}
#endif
void uart_init(u32 br_num)
{
USART_InitTypeDef USART_InitStructure;
// USART_ClockInitTypeDef USART_ClockInitStruct;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//开启USART1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
//配置PA2作为USART1 Tx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA , &GPIO_InitStructure);
//配置PA3作为USART1 Rx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA , &GPIO_InitStructure);
//Usart2 NVIC
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1 ;//************//
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //0-3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//配置USART2
//中断被屏蔽了
USART_InitStructure.USART_BaudRate = br_num;//br_num; //波特率可以通过地面站配置
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据
USART_InitStructure.USART_StopBits = USART_StopBits_1; //在帧结尾传输1个停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //禁用奇偶校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制失能
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //发送、接收使能
// //配置USART1时钟
// USART_ClockInitStruct.USART_Clock = USART_Clock_Disable; //时钟低电平活动
// USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low; //SLCK引脚上时钟输出的极性->低电平
// USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge; //时钟第二个边沿进行数据捕获
// USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable; //最后一位数据的时钟脉冲不从SCLK输出
USART_Init(USART2, &USART_InitStructure);
// USART_ClockInit(USART2, &USART_ClockInitStruct);
//使能USART1接收中断
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//使能USART1
USART_Cmd(USART2, ENABLE);
}
u8 TxBuffer[256];
u8 TxCounter=0;
u8 count=0;
u8 RxBuffer[16]={0};
static u8 RxState = 0;
void Uart2_IRQ(void)
{
if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) != RESET)//??!????if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)???
{
USART_ReceiveData(USART2);
}
//发送中断
if((USART2->SR & (1<<7))&&(USART2->CR1 & USART_CR1_TXEIE))//if(USART_GetITStatus(USART1,USART_IT_TXE)!=RESET)
{
USART2->DR = TxBuffer[TxCounter++]; //写DR清除中断标志
if(TxCounter == count)
{
USART2->CR1 &= ~USART_CR1_TXEIE; //关闭TXE中断
//USART_ITConfig(USART1,USART_IT_TXE,DISABLE);
}
}
//接收中断 (接收寄存器非空)
if(USART2->SR & (1<<5))//if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
u8 com_data = USART2->DR;
static u8 _data_len = 0,_data_cnt = 0;
if(RxState==0)
{
RxState=0;
RxBuffer[0]=com_data;
}
else if(RxState==1)
{
RxState=2;
RxBuffer[1]=com_data;
}
else if(RxState==2)
{
RxState=3;
RxBuffer[2]=com_data;
}
if(RxState==3)
{
RxState=4;
RxBuffer[4]=com_data;
}
else if(RxState==4)
{
RxState=5;
RxBuffer[5]=com_data;
}
else if(RxState==5)
{
RxState=6;
RxBuffer[6]=com_data;
}
else if(RxState==6)
{
RxState = 7;
RxBuffer[7]=com_data;
}
else if(RxState==7)
{
RxState = 8;
RxBuffer[8]=com_data;
}
else if(RxState==8)
{
RxState = 9;
RxBuffer[9]=com_data;
// Data_Receive_Anl(RxBuffer,_data_cnt+5);
}
else
RxState = 0;
}
}
void Uart2_Put_Char(unsigned char DataToSend)//static
{
TxBuffer[count++] = DataToSend;
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
}
void Uart2_Put_String(unsigned char *Str)
{
//判断Str指向的数据是否有效.
while(*Str)
{
//是否是回车字符 如果是,则发送相应的回车 0x0d 0x0a
if(*Str=='\r')Uart2_Put_Char(0x0d);
else if(*Str=='\n')Uart2_Put_Char(0x0a);
else Uart2_Put_Char(*Str);
//指针++ 指向下一个字节.
Str++;
}
}
void Uart2_Put_Buf(unsigned char *DataToSend , u8 data_num)
{
uint8_t i;
for(i=0;i<data_num;i++)
TxBuffer[count++] = *(DataToSend+i);
if(!(USART2->CR1 & USART_CR1_TXEIE))
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
}
以上源码可以使用prinft发送字符串
不知道怎么接收字符串,在寄存器里面是怎么跑的……
急求诸位帮忙解决,谢谢
|
|