#include "stm32f10x_lib.h"
#include "usart.h"
#include "delay.h"
/*
********************************************************************************
** 函数名称 : USART1_Configuration(void)
** 函数功能 : 串口1初始化
** 输 入 : 无
** 输 出 : 无
** 返 回 : 无
********************************************************************************
*/
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
NVIC_usart1();
GPIO_usart1();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |RCC_APB2Periph_USART1, ENABLE );
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; // 时钟低电平活动
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; // 时钟低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; // 时钟第二个边沿进行数据捕获
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; // 最后一位数据的时钟脉冲不从SCLK输出
/* Configure the USART1 synchronous paramters */
USART_ClockInit(USART1, &USART_ClockInitStructure); // 时钟参数初始化设置
USART_InitStructure.USART_BaudRate =19200; // 波特率为:19200
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_Rx | USART_Mode_Tx; // 发送使能+接收使能
/* Configure USART1 basic and asynchronous paramters */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 */
USART_ClearFlag(USART1, USART_IT_RXNE); //清中断,以免一启用中断后立即产生中断
USART_ITConfig(USART1,USART_IT_RXNE, ENABLE); //使能USART1中断源
USART_Cmd(USART1, ENABLE); //USART1总开关:开启
/* 如下语句解决第1个字节无法正确发送出去的问题 */
USART_ClearFlag(USART1, USART_FLAG_TC); // 清标志
}
void NVIC_usart1(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //中断响应优先级1
NVIC_Init(&NVIC_InitStructure);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; //通道设置为串口1中断
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //中断响应优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure); //初始化
}
void GPIO_usart1(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE );
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // 选中管脚9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 最高输出速率50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); // 选择A端口
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //选中管脚10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //选择A端口
}
//************************************************
void USART1_SendData(unsigned char SendData)
{
USART_SendData(USART1, SendData);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //Tansmit Data Register empty interrupt
}
void Show_short(short k) //显示两个有符号字节 shourt2字节数据
{
unsigned char a,b,c,d,e;
if(k<0)
{
k=-k;
USART1_SendData('-');
}
else
USART1_SendData('+');
e=k%10+0x30;
d=(unsigned char)(( k/10 )%10)+0x30;
c=(unsigned char)(( k/100 )%10)+0x30;
b=(unsigned char)(( k/1000 )%10)+0x30;
a=k/10000+0x30;
USART1_SendData(a);
USART1_SendData(b);
USART1_SendData(c);
USART1_SendData(d);
USART1_SendData(e);
USART1_SendData(' ');
}
void Show_double(double kk) //double为4字节与float一样 在这里只显示5位整数部分和3位小数部分
{
unsigned char a,b,c,d,e,f,g,h;
u32 k;
if(kk<0)
{
kk=-kk;
USART1_SendData('-');
}
else
USART1_SendData('+');
k=kk;
e=k%10+0x30;
d=(unsigned char)(( k/10 )%10)+0x30;
c=(unsigned char)(( k/100 )%10)+0x30;
b=(unsigned char)(( k/1000 )%10)+0x30;
a=(unsigned char)(( k/10000 )%10)+0x30;
k=kk*10;
f=(unsigned char)(k%10)+0x30;
k=kk*100;
h=(unsigned char)(k%10)+0x30;
k=kk*1000;
g=(unsigned char)(k%10)+0x30;
USART1_SendData(a);
USART1_SendData(b);
USART1_SendData(c);
USART1_SendData(d);
USART1_SendData(e);
USART1_SendData('.');
USART1_SendData(f);
USART1_SendData(h);
USART1_SendData(g);
USART1_SendData(' ');
}
void Show_u16(u16 k) //显示两个无符号字节
{
unsigned char a,b,c,d,e;
e=k%10+0x30;
d=(unsigned char)(( k/10 )%10)+0x30;
c=(unsigned char)(( k/100 )%10)+0x30;
b=(unsigned char)(( k/1000 )%10)+0x30;
a=k/10000+0x30;
USART1_SendData(a);
USART1_SendData(b);
USART1_SendData(c);
USART1_SendData(d);
USART1_SendData(e);
USART1_SendData(' ');
}
void Show_u32(u32 k) //显示无符号long的数据,4字节 0~4294967295
{
unsigned char a,b,c,d,e,f,g,h,i,j;
j=k%10+0x30;
k=k/10;
i=k%10+0x30;
k=k/10;
h=k%10+0x30;
k=k/10;
g=k%10+0x30;
k=k/10;
f=k%10+0x30;
k=k/10;
e=k%10+0x30;
k=k/10;
d=k%10+0x30;
k=k/10;
c=k%10+0x30;
k=k/10;
b=k%10+0x30;
k=k/10;
a=k%10+0x30;
USART1_SendData(' ');
USART1_SendData(a);
USART1_SendData(b);
USART1_SendData(c);
USART1_SendData(d);
USART1_SendData(e);
USART1_SendData(f);
USART1_SendData(g);
USART1_SendData(h);
USART1_SendData(i);
USART1_SendData(j);
USART1_SendData(' ');
}
void Showstring(unsigned char *p)
{
unsigned char *k;
k=p;
while(*p!='\0') //'\0'的ascii值为0
{
USART1_SendData(*p);
p++;
}
p=k;
//USART1_SendData(' ');
//USART1_SendData(0X0D); //换行
//USART1_SendData(0X0A); //回车
}
/*******************************************************************/
/* */
/* STM32在串口1接收1字节 */
/* 说明:串口1接收中断 */
/* */
/*******************************************************************/
//在中断服务程序中,由于主机响应中断时并不知道是哪个中断源发出中断请求,
// 因此必须在中断服务程序中对中断源进行判别,然后分别进行处理。
//当然,如果只涉及到一个中断请求,是不用做上述判别的。
//但是无论什么情况,做上述判别是个好习惯
void USART1_IRQHandler(void)
{
u8 dat;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //若接收数据寄存器满
{
dat = (u8)(USART_ReceiveData(USART1)&0xff); //第2个字节才是接受到的数据
dat=dat;
}
}