#include "stm32f10x_lib.h"
#include "usart.h"
#include "delay.h"
#include "sys_config.h"
#include <math.h> //Keil library
#define uchar unsigned char
#define uint unsigned int
/*
********************************************************************************
** 函数名称 : main(void)
** 函数功能 : 主函数
** 输 入 : 无
** 输 出 : 无
** 返 回 : 无
********************************************************************************
*/
int main(void)
{
u8 data[10]={0,50,100,150,200,250,200,150,100,50};
u8 i=0;
RCC_Configuration(); //配置RCC
GPIO_Configuration(); //配置GPIO
USART1_Configuration(); //配置串口1
Delayms(10); //延时
while(1)
{
for(i=0;i<10;i++)
{
USART1_SendData(data[i]);
// USART1_SendData(0X0D); //换行
// USART1_SendData(0X0A); //回车
Delayms(500); //延时
}
}
}
#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;
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 =9600; // 波特率为:9600
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总开关:开启
}
//************************************************
void USART1_SendData(unsigned char SendData)
{
USART_SendData(USART1, SendData);
Delayms(1);
}
void Show_data_short(short k) //显示两个有符号字节
{
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(' ');
}