STM32F103—有关陶晶驰串口屏的串口使用代码

串口HMI的基本指令集(官网链接) [USART HMI 资料中心] 

注:设备接收指令结束符为”0XFF 0XFF 0XFF”三个字节(HEX数据,不是字符串数据)。

适用于STM32F103系列的串口1,编写串口协议。

usart1.c

#include "usart1.h"

float everything =0;

//接收缓存
u8 reccmd_temp1[command_length1] ;
//接收标志
u8 rec_flag1 = 0;

u8 flag_temp=0;

u8 flag_first=0;


int fputc(int ch, FILE *f)
{
	USART_SendData(USART1, (unsigned char) ch);
	while (!(USART1->SR & USART_FLAG_TXE));
	return (ch);
}

void USART1_Int(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;        

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
    GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;

    USART_InitStructure.USART_BaudRate = 115200; //波特率;
    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; //收发模式;
    USART_Init(USART1, &USART_InitStructure);//配置串口参数;

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中断号;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //响应优先级;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    USART_Cmd(USART1, ENABLE); //使能串口
}

void USART1_IRQHandler(void)
{
		u8 rec_tem   = 0;//--接收缓存
		static u8 rec_count = 0;//--接收计数
		static u8 Start_rec_flag1 = 0;//接受标志位
		if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
		{	
				rec_tem = USART_ReceiveData(USART1);
				if(rec_tem == open_first)//判断接受的数据的指令头是否正确
				{
						rec_count = 0;
						Start_rec_flag1  = 1;
				}	
				if(Start_rec_flag1)//开始接收
				{
						reccmd_temp1[rec_count ++] = rec_tem;//将接收到的一个字节数据存入 接收缓存
						if(rec_count == command_length1)//接收完成
						{	
								Start_rec_flag1 = 0;
								rec_count = 0;
								rec_flag1  = 1;	
						}	
				}
				USART_ClearITPendingBit(USART1, USART_IT_RXNE);	//清除标志位;
		}
}





//--协议解析  AA 01 00 00 00 00 00 00
void protocol_analysis(void)
{
		if(reccmd_temp1[0] == 0xAA )	//判断是起始位和名称
		{
				reccmd_temp1[0] = 0;
				switch(reccmd_temp1[1])//判断类型
				{
					case 0x01:
						led_on;		//开灯
                        everything+=1;
						flag_first = 0;
					break;
					
					case 0x02:		
						led_off;    //关灯
                        everything-=1;
						flag_first = 1;					
					break;
				}			
		}
}

usart1.h

#ifndef _usart1_H
#define _usart1_H

#include "stm32f10x.h"
#include "stdbool.h"
#include "stdio.h"
#include "led.h"

extern float everything;

//--指令长度
#define command_length1				 8

//--接收缓存
extern u8 reccmd_temp1[command_length1];

//--接收标志
extern u8 rec_flag1;
extern u8 dis_tence;
extern u8 flag_first;

//--指令头
#define open_first   					 0xAA

//串口1初始化
void USART1_Int(void);

// 串口1 接收中断函数 
void USART1_IRQHandler(void);

//串口协议
void protocol_analysis(void);

#endif

main.c

#include "stm32f10x.h"
#include "systick.h"
#include "led.h"
#include "usart1.h"

int main()
{
	led_int();
	delay_init();	
	USART1_Int();

	while(1)
	{
		while(flag_first ==0)
		{
			protocol_analysis();     //串口协议解析		
			printf("t1.txt=\" %.3lf\r\n\"\xff\xff\xff", everything);//串口屏实时显示
		}
	}
}

在上述的串口屏实时显示的时候,使用串口屏软件中t1.txt的模块。即:everthing在t1.txt的模块中显示。

在串口屏软件中写的协议,如下所示:

 其中printh用于十六进制

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值