如何才能根据PC发送的数据来执行相应的程序?(串口通信)
时间:10-02
整理:3721RD
点击:
#ifndef _usart_H
#define _usart_H
#include "stm32f10x.h"
#include <stdio.h>
void usart_init(void);
#endif
/***********************************************************************************************/
#include "usart.h"
void usart_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO???ú?á11ì??¨ò?
USART_InitTypeDef USART_InitStructure; //USART′??ú?á11ì??¨ò?
NVIC_InitTypeDef NVIC_InitStructure; //?D??ó??è???á11ì??¨ò?
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //A???ú?óê±?ó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //?′ó?1|?ü?óê±?ó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //50MHz?μ?ê
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //PA.91ü??
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //?′ó?í??aê?3??£ê?
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO3?ê??ˉ
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //PA.101ü??
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //?????£ê?ê?è?£?
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO3?ê??ˉ
USART_InitStructure.USART_BaudRate=9600; //2¨ì??êéè??
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //ò??????D·¢?íμ?êy?Yμ???êy
USART_InitStructure.USART_StopBits=USART_StopBits_1; //·¢?íμ?í£?1??êy??
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=
USART_HardwareFlowControl_None; //2???ó?ó2?tá÷
USART_InitStructure.USART_Mode=USART_Mode_Tx | USART_Mode_Rx; //·¢?íoí?óê?í?ê±ê1?ü
USART_Init(USART1,&USART_InitStructure); //USART13?ê??ˉ
USART_Cmd(USART1,ENABLE); //USART1ê1?ü
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //??ó??óê??D??ê1?ü
USART_ClearFlag(USART1,USART_FLAG_TC); //??3t·¢?ííê3é±ê??
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //ó??è??·?×é
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; //?D??í¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //?à??ó??è??
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //?ìó|ó??è??
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //ê1?ü
NVIC_Init(&NVIC_InitStructure); //NVIC3?ê??ˉ
}
/***********************************************************************************************/
void USART1_IRQHandler(void)
{
u8 res;
USART_ClearFlag(USART1,USART_FLAG_TC);
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=Bit_RESET)
{
res=USART_ReceiveData(USART1);
USART_SendData(USART1,res);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==Bit_RESET);
}
}
#define _usart_H
#include "stm32f10x.h"
#include <stdio.h>
void usart_init(void);
#endif
/***********************************************************************************************/
#include "usart.h"
void usart_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO???ú?á11ì??¨ò?
USART_InitTypeDef USART_InitStructure; //USART′??ú?á11ì??¨ò?
NVIC_InitTypeDef NVIC_InitStructure; //?D??ó??è???á11ì??¨ò?
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //A???ú?óê±?ó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //?′ó?1|?ü?óê±?ó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //50MHz?μ?ê
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //PA.91ü??
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //?′ó?í??aê?3??£ê?
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO3?ê??ˉ
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //PA.101ü??
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //?????£ê?ê?è?£?
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO3?ê??ˉ
USART_InitStructure.USART_BaudRate=9600; //2¨ì??êéè??
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //ò??????D·¢?íμ?êy?Yμ???êy
USART_InitStructure.USART_StopBits=USART_StopBits_1; //·¢?íμ?í£?1??êy??
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=
USART_HardwareFlowControl_None; //2???ó?ó2?tá÷
USART_InitStructure.USART_Mode=USART_Mode_Tx | USART_Mode_Rx; //·¢?íoí?óê?í?ê±ê1?ü
USART_Init(USART1,&USART_InitStructure); //USART13?ê??ˉ
USART_Cmd(USART1,ENABLE); //USART1ê1?ü
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //??ó??óê??D??ê1?ü
USART_ClearFlag(USART1,USART_FLAG_TC); //??3t·¢?ííê3é±ê??
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //ó??è??·?×é
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; //?D??í¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //?à??ó??è??
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //?ìó|ó??è??
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //ê1?ü
NVIC_Init(&NVIC_InitStructure); //NVIC3?ê??ˉ
}
/***********************************************************************************************/
void USART1_IRQHandler(void)
{
u8 res;
USART_ClearFlag(USART1,USART_FLAG_TC);
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=Bit_RESET)
{
res=USART_ReceiveData(USART1);
USART_SendData(USART1,res);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==Bit_RESET);
}
}
串口收到数据后,对数据进行解析,根据解析的关键字来设置执行的相应程序就好了
这么儿科还用问别人啊,
需要一套通讯协议的,比如modebus协议,或是自定义协议,约定PC发数据的格式,下位机解析然后执行动作