STM32学习(十)串口收发HEX数据包 新手入门及常见问题解决

main:

#include "led.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "delay.h"
#include "chuankou.h"
uint8_t data;
uint8_t keynum;	
 int main(void)
{	 
 
	chuankou_Init();
	KEY_Init();
	chuankou_txpacket[0] =0x01;
    chuankou_txpacket[1] =0x02;
	chuankou_txpacket[2] =0x03;
	chuankou_txpacket[3] =0x04;
	
	
	
  	while(1) 
	{	
		keynum = Key_GetNum();
		if(keynum == 1)
		{
		  chuankou_txpacket[0] ++;
          chuankou_txpacket[1] ++;
	      chuankou_txpacket[2] ++;
	      chuankou_txpacket[3] ++;
		  sendpacket();
		
		}
//		if(getflag()==1)
//		{
//		  	chuankou_printf("Num=%d/r/n",666);
//		}
//			
	} 
}

 

串口:

#include "stm32f10x.h"                  // Device header
#include <stdio.h> 
#include <stdarg.h>

uint8_t flagdata;
uint8_t chuankou_txpacket[4];//储存发送数据
uint8_t chuankou_rxpacket[4];
void chuankou_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
     
	GPIO_InitTypeDef  GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;				 //LED0-->PB.5 端口配置
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 		 //复用 推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
    GPIO_Init(GPIOA, &GPIO_InitStructure);	
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;				 //LED0-->PB.5 端口配置
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		 //复用 推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
    GPIO_Init(GPIOA, &GPIO_InitStructure);	
	//配置USART
	USART_InitTypeDef USART_InitStruct;
	USART_InitStruct.USART_BaudRate = 9600;//波特率
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;// 字长
	USART_InitStruct.USART_StopBits = USART_StopBits_1;//停止位长度
	USART_InitStruct.USART_Parity = USART_Parity_No;//校验位
	USART_InitStruct.USART_Mode =USART_Mode_Rx|USART_Mode_Tx ;//发送模式+接受
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流控
	USART_Init(USART1,&USART_InitStruct);
	
	//配置中断
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
 //使能
	USART_Cmd(USART1,ENABLE);
}

void chuankou_fasong(uint8_t zijie)
{

  USART_SendData(USART1,zijie);//发送数据
  while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);//

}
void chuankou_shuzu(uint8_t *shuzu,uint16_t length)//发送数组
{
   uint16_t i;
   for(i=0;i<length;i++)
{
   chuankou_fasong(shuzu[i]);

}	
   

}
void chuankou_zifuchuan(char *string)//发送字符串
{
  uint8_t i;
  for(i=0;string[i]!=0;i++)
{
   chuankou_fasong(string[i]);

}	

}
//发送数字需要把每一位拆分开 数字除以10的多少位次方再对10取余 即可获得
uint32_t cifang(uint32_t x,uint32_t y)
{
   uint32_t res=1;
   uint32_t i;
   for(i=y;i>0;i--)
	{
	   res = res*x;   
	}
  return res;

}
void chuankou_sendnumber(uint32_t number,uint8_t length)
{
    uint8_t i;
    for(i=0;i<length;i++)
{
  chuankou_fasong(number/cifang(10,length-i-1)%10 + '0');//需要加偏移
}	

}
 
int fputc(int ch,FILE *f)//把fputc重定向到串口 是printf的底层,可以将printf输出到串口
{
 chuankou_fasong(ch);
 return ch;

}
//对sprintf进行封装
void chuankou_printf(char *format,...)
{
   char string[100];
   va_list arg;
   va_start(arg,format);
   vsprintf(string,format,arg);
   va_end(arg);
   chuankou_zifuchuan(string);
}
uint8_t getflag(void)
{
   if(flagdata == 1)
   {
     flagdata =0;
	 return 1;
   }
     return 0;

}
void sendpacket(void)
{
   chuankou_fasong(0xFF);
   chuankou_shuzu(chuankou_txpacket,4);
   chuankou_fasong(0xFE);

}	

void USART1_IRQHandler(void)
{
	 static uint8_t s= 0;//静态变量运用于本函数
	 static uint8_t i= 0;
	
     if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)== SET)
	 {  //状态机
		 uint8_t rxdata = USART_ReceiveData(USART1);
		 if(s==0)
		 {
		   if(rxdata == 0xFF)
		   {
		     s=1; 
			 i = 0;
		   }
		 }
		 else if(s==1)
		 {
		   chuankou_rxpacket[i] = rxdata;
			i++;
			 if(i>=4)
			 {
			   s=2;
			 }
		 }
		 else if(s==2)
		 {
		   if(rxdata == 0xFE)
		   {
		     s=0;
			 flagdata = 1;
	  
		   }
		 
		 }
	  
		USART_ClearITPendingBit(USART1,USART_FLAG_RXNE);
	 
	 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值