USART往电脑发送数据(STM 32)
2019-02-24 本文已影响0人
tomato_20fc
要有上位机 配合使用
<usart.h>********************************
#ifndef __USART_H
#define __USART_H
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "stdio.h"
#include "sys.h"
#define USART_n USART1 //
#define USART1_REC_LEN 200 //定义usart1 最大接收字节数
#define USART2_REC_LEN 200 //定义usart2 最大接收字节数
#define USART3_REC_LEN 200 //定义usart3 最大接收字节数
#define EN_USART1 1 //使能(1)禁止(0)
#define EN_USART2 0 //
#define EN_USART3 0 //
extern u8 USART1_RX_BUF[USART1_REC_LEN]; //接收缓存,最大为USART1_REC_LEN,末字节为换行符
extern u8 USART2_RX_BUF[USART2_REC_LEN];
extern u8 USART3_RX_BUF[USART3_REC_LEN];
extern u16 USART1_RX_STA; //接收状态标记
extern u16 USART2_RX_STA;
extern u16 USART3_RX_STA;
void USART1_Init(u32 bound);//串口1 初始化并启动
void USART2_Init(u32 bound);
void USART3_Init(u32 bound);
void USART1_printf(char* fmt,...); //串口1的专用printf函数
void USART2_printf(char* fmt,...);
void USART3_printf(char* fmt,...);
#endif
<usart.c>********************************
#include "sys.h"
#include "usart.h"
#if 1
#pragma import(__use_no_semihosting)
struct __FILE {
int handle;
};
FILE __stdout;
//退出
_sys_exit(int x){
x = x;
}
int fputc(int ch, FILE *f){
while((USART_n->SR&0X40)==0);//循环发送 直至成功
USART_n->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1
u8 USART1_RX_BUF[USART1_REC_LEN]; //数据长度
u16 USART1_RX_STA=0; //接收数据标志位
void USART1_printf (char *fmt, ...){
char buffer[USART1_REC_LEN+1]; // 数据长度
u8 i = 0;
va_list arg_ptr;
va_start(arg_ptr, fmt);
vsnprintf(buffer, USART1_REC_LEN+1, fmt, arg_ptr);
while ((i < USART1_REC_LEN) && (i < strlen(buffer))){
USART_SendData(USART1, (u8) buffer[i++]);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
va_end(arg_ptr);
}
void USART1_Init(u32 bound){ //串口1初始化
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//悬空输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart1 中断设置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure);
//根据指定的参数 初始化VIC寄存器
USART_InitStructure.USART_BaudRate = bound;//一般为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 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_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); //使能串口
}
void USART1_IRQHandler(void){ //串口1中断函数(函数名不能改)
u8 Res;
//
USART1_RX_STA??0
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
Res =USART_ReceiveData(USART1);//(USART1->DR); printf("%c",Res);
if((USART1_RX_STA&0x8000)==0){
if(USART1_RX_STA&0x4000){
if(Res!=0x0a)USART1_RX_STA=0;
else USART1_RX_STA|=0x8000; //?????????
}else{
if(Res==0x0d)USART1_RX_STA|=0x4000;
else{
USART1_RX_BUF[USART1_RX_STA&0X3FFF]=Res ;
USART1_RX_STA++;
if(USART1_RX_STA>(USART1_REC_LEN-1))
USART1_RX_STA=0;
}
}
}
}
}
#endif
/*
USART2相关函数
*/
#if EN_USART2
u8 USART2_RX_BUF[USART2_REC_LEN];
u16 USART2_RX_STA=0;
void USART2_printf (char *fmt, ...){
char buffer[USART2_REC_LEN+1];
u8 i = 0;
va_list arg_ptr;
va_start(arg_ptr, fmt);
vsnprintf(buffer, USART2_REC_LEN+1, fmt, arg_ptr);
while ((i < USART2_REC_LEN) && (i < strlen(buffer))){
USART_SendData(USART2, (u8) buffer[i++]);
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
}
va_end(arg_ptr);
}
void USART2_Init(u32 bound){ //????1??????????
//GPIO???????
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //???UART2????GPIOA?????
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //???????RCC???
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //????USART2??RX?????PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //????USART2??TX?????PA2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???????????
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART2 ?????????
USART_InitStructure.USART_BaudRate = bound;//????????9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//????8λ??????
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(USART2, &USART_InitStructure); //?????????
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//????ENABLE/???DISABLE?ж?
USART_Cmd(USART2, ENABLE); //??????
//Usart2 NVIC ????
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //?????3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ??????
NVIC_Init(&NVIC_InitStructure); //?????????????????VIC?????
}
void USART2_IRQHandler(void){ //????2?ж?????????????????????????
u8 Res;
//????????????????USART2_RX_BUF[]?????(USART2_RX_STA&0x3FFF)??????????????????????
//??(USART2_RX_STA&0xC000)?????????????????????????????????????
//??????????д?ж?if(USART2_RX_STA&0xC000)??????USART2_RX_BUF[]???飬????0x0d 0x0a?????????
//???????????????????????????USART2_RX_STA??0
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET){ //?????ж?(????????????????0x0d 0x0a??β)
Res =USART_ReceiveData(USART2);//(USART1->DR); //??????????????
printf("%c",Res); //????????????? a??????? ????????
if((USART2_RX_STA&0x8000)==0){//????δ???
if(USART2_RX_STA&0x4000){//???????0x0d
if(Res!=0x0a)USART2_RX_STA=0;//???????,??????
else USART2_RX_STA|=0x8000; //?????????
}else{ //??????0X0D
if(Res==0x0d)USART2_RX_STA|=0x4000;
else{
USART2_RX_BUF[USART2_RX_STA&0X3FFF]=Res ; //??????????????????
USART2_RX_STA++; //????????????1
if(USART2_RX_STA>(USART2_REC_LEN-1))USART2_RX_STA=0;//???????????,??????????
}
}
}
}
}
#endif
#if EN_USART3 //???????????
u8 USART3_RX_BUF[USART3_REC_LEN]; //???????,???USART_REC_LEN?????.
//??????
//bit15?? ?????????
//bit14?? ?????0x0d
//bit13~0?? ?????????Ч??????
u16 USART3_RX_STA=0; //?????????
/*
USART3????printf????
????????2????????????printf?????????????????????????????????????printf????
???÷?????USART3_printf("123"); //??USART3???????123
*/
void USART3_printf (char *fmt, ...){
char buffer[USART3_REC_LEN+1]; // ???????
u8 i = 0;
va_list arg_ptr;
va_start(arg_ptr, fmt);
vsnprintf(buffer, USART3_REC_LEN+1, fmt, arg_ptr);
while ((i < USART3_REC_LEN) && (i < strlen(buffer))){
USART_SendData(USART3, (u8) buffer[i++]);
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
}
va_end(arg_ptr);
}
void USART3_Init(u32 BaudRate){ //USART3??????????
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //???UART3????GPIOB?????
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //???????RCC???
//????????GPIO??????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//????USART3??RX?????PB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????? ????????
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//????USART3??TX?????PB10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//??????50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//????? ???????????
GPIO_Init(GPIOB, &GPIO_InitStructure);
//???????
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//????8λ??????
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(USART3, &USART_InitStructure);//???????3
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//??????????ж?
//USART_ITConfig(USART3, USART_IT_TXE, ENABLE);//????????ж???????????????
USART_Cmd(USART3, ENABLE);//??????3
//?????ж?????
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//????USART3?ж?
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//?ж???
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//????3?ж?????????????????????????
//???÷?????if(USART3_RX_STA&0xC000){ ?????????????? }//???λ??0xC000?????????????ɡ?
void USART3_IRQHandler(void){
u8 Res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){ //?????ж?
Res =USART_ReceiveData(USART3);//??????????????
if(Res=='S'){//?ж??????????STOP???????S??
USART3_RX_STA=1;//?????STOP????λ?1
}else if(Res=='K'){//?ж??????????OK???????K??
USART3_RX_STA=2;//?????OK????λ?2
}
}
}
#endif
<main.c>******************************
#include "stm32f10x.h" //STM32????
#include "sys.h"
#include "delay.h"
#include "usart.h"
int main (void){
u8 a=7,b=8;
RCC_Configuration(); //
USART1_Init(115200); //
while(1){
USART_SendData(USART1 , 0x55);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET); //??鷢???ж???λ
//方法2
// printf("STM32F103 "); //????????????????????
// printf("STM32 %d %d ",a,b);
/* 方法3 */
// USART1_printf("STM32 %d %d ",a,b);
delay_ms(1000); //???
}
}