找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 18385|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

基于STM32F103C8T6的USART串行通信(PC與STM32)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
基于STM32F103C8T6USART串行通信(PCSTM32
一、功能要求及通信協(xié)議設(shè)計(jì)
    通過PC控制STM32最小系統(tǒng)上的LED。采用中斷接收法,當(dāng)串口接收到數(shù)據(jù)即進(jìn)入中斷函數(shù),接收該數(shù)據(jù)。接收和發(fā)送的數(shù)據(jù)幀長度可變,但不超過255字節(jié)。
    通信協(xié)議設(shè)計(jì)如下:
1.  PC
(1)  數(shù)據(jù)幀頭為0xEA,數(shù)據(jù)幀尾為0x55。
(2)  如果數(shù)據(jù)幀中間數(shù)據(jù)為0x01,表示要點(diǎn)亮相對應(yīng)的LED。
(3)  如果數(shù)據(jù)幀中間數(shù)據(jù)為0x02,表示要關(guān)閉相對應(yīng)的LED。
2.  STM32實(shí)驗(yàn)系統(tǒng)端
(1)  正確點(diǎn)亮LED,則STM32回發(fā):“LED in STM32 has been turnon!”,否則回發(fā):“LED in STM32 has been on!”。
(2)  正確關(guān)閉LED,則STM32回發(fā):“LED in STM32 has been turnoff!”,否則回發(fā):“LED in STM32 has been off!”。
二、程序算法
1.  算法設(shè)計(jì)
上位機(jī)(這里采用PC)按通信協(xié)議要求發(fā)送一幀數(shù)據(jù)給STM32實(shí)驗(yàn)系統(tǒng)(下位機(jī)),下位機(jī)以中斷方式接收數(shù)據(jù)幀,如果是有效數(shù)據(jù)幀則設(shè)置標(biāo)志,否則清除標(biāo)志;如果接收到有效數(shù)據(jù)幀,STM32實(shí)驗(yàn)系統(tǒng)(下位機(jī))對數(shù)據(jù)幀進(jìn)行分析并進(jìn)行相應(yīng)處理。如果命令是0x01,則點(diǎn)亮LED;如果命令是0x02,則關(guān)閉LED。
2.  程序框架
由于采用中斷方式,因此工程模板中的兩個中斷相關(guān)文件均必須被使用:stm32f10x_it.h和stm32f10x_it.c,前者主要是進(jìn)行全局變量說明、中斷函數(shù)說明,后者主要是中斷函數(shù)的定義。另外設(shè)計(jì)的文件是:main.c,stm32f10x_conf.h,前者是主函數(shù)所在文件,后者是功能模塊頭文件設(shè)置文件。其它自定義文件有:UserDefine.h和User.c,前者是用戶類型定義及函數(shù)聲明所在文件,后者是用戶函數(shù)定義所在文件。因此,涉及工程模板中的文件有4個,分別是:
(1)      stm32f10x_it.h;
(2)      stm32f10x_it.c;
(3)      stm32f10x_conf.h;
(4)      main.c;
(5)      UserDefine.h;
(6)      User.c。
三、工程配置
1.  選擇設(shè)備為STM32F103C8


2.       修改晶振頻率為8.0

       STM32F103C8的內(nèi)部晶振頻率為8MHz
3.       設(shè)置預(yù)處理宏定義符號:USE_STDPERIPH_DRIVER,STM32F10X_MD

       USE_STDPERIPH_DRIVER表示要使用固件庫,STM32F10X_MD表示選用的是中等容量的STM32芯片。
4.   設(shè)置IncludePath路徑

該項(xiàng)配置是為了將STM32的啟動文件及官方庫包含進(jìn)項(xiàng)目中,這樣能加快開發(fā)速度,而且使用官方庫開發(fā)相較于其他方式而言更淺顯易懂。
5.   設(shè)置調(diào)試方式

       此項(xiàng)為PCSTM32芯片的連接方式,具體請自行調(diào)整
6.   設(shè)置啟動方式

       此項(xiàng)設(shè)置為程序下載后及啟動,免去了手動啟動的環(huán)節(jié)。
四、源程序
1.    文件1——stm32f10x_it.h
#ifndef __STM32F10x_IT_H
#define __STM32F10x_IT_H
#include "stm32f10x.h"
#include "UserDefine.h"
void NMI_Handler(void);
void HardFault_Handler(void);
void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void USART1_IRQHandler(void);
#endif /* __STM32F10x_IT_H */

2.    文件2——stm32f10x_it.c
#include "stm32f10x_it.h"
extern RxTxBuffer_t Data;
void NMI_Handler(void)
{
}
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Faultexception occurs */
while (1)
{
}
}
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manageexception occurs */
while (1)
{
}
}

void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
while (1)
{
}
}

void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Faultexception occurs */
while (1)
{
  }
}

void SVC_Handler(void)
{
}
void DebugMon_Handler(void)
{
}
void PendSV_Handler(void)
{
}
void SysTick_Handler(void)
{
}
void USART1_IRQHandler(void)
{
if(USART_GetFlagStatus(USART1,USART_IT_RXNE)!=RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
Data.RxBuffer[Data.RxCount++]=USART_ReceiveData(USART1);
//Data.RxCount &= 0xFF;
}
if (Data.RxBuffer[Data.RxCount - 1] == 0xEA)
Data.RxHeader = Data.RxCount - 1;
if(Data.RxBuffer[Data.RxHeader]==0xEA&&Data.RxBuffer[Data.RxCount -1] == 0x55)
{
Data.RxLen = Data.RxCount-1-Data.RxHeader;
Data.RxOK = 1;
}
if(USART_GetFlagStatus(USART1, USART_IT_ORE) == SET)
{
USART_ClearFlag(USART1, USART_IT_ORE);
USART_ReceiveData(USART1);
}
}

3.    文件3——stm32f10x_conf.h
#ifndef __STM32F10x_CONF_H
#define __STM32F10x_CONF_H
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include"stm32f10x_usart.h"
#include "misc.h"
#endif /* __STM32F10x_CONF_H */

4.   文件4——main.c
#include "UserDefine.h"
RxTxBuffer_t Data;
int main(void)
{
u8 kcnt = 0;
u8 cmd1, cmd2;
SystemInit();
GPIO_Configure();
USART_Configure();
NVIC_Configure();
EmptyRxBuffer(BufferMAX);
SendString((u8 *)("Welcome to my STM32!!!\n"));
while (true)
  {
if (Data.RxOK)
  {
cmd1 = Data.RxBuffer[Data.RxHeader + 1];
cmd2 = Data.RxBuffer[Data.RxHeader + 2];
switch (cmd1)
   {
case 0x01:   LED_ON;
Data.RxOK = 0;
Data.RxCount = 0;
EmptyRxBuffer(BufferMAX);
break;
case 0x02:   LED_OFF;
Data.RxOK = 0;
Data.RxCount = 0;
EmptyRxBuffer(BufferMAX);
break;
}
}
}
}

5.    文件5——UserDefine.h
#ifndef __USERDEFINE_H__
#define __USERDEFINE_H__
#include <stm32f10x.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#define BufferMAX 255
#define LED_ON    GPIO_ResetBits(GPIOB, GPIO_Pin_12)
#define LED_OFF   GPIO_SetBits(GPIOB, GPIO_Pin_12)
typedef struct
{
u8 RxBuffer[BufferMAX];
u8 TxBuffer[BufferMAX];
vu8 RxCount;
vu8 TxCount;
vu8 RxHeader;
vu8 RxOK;
vu8 RxLen;
}
RxTxBuffer_t;
typedef enum { ERR = 0, OK = !ERR } RxTxState_t;
void GPIO_Configure();
void USART_Configure();
void NVIC_Configure();
void DelayNms(u16 time);
RxTxState_t SendOneByte(u8 dat);
void SendString(u8 *Message);
void EmptyRxBuffer(u8 len);
#endif /* __USERDEFINE_H__ */

6.  文件6——User.c
#include "UserDefine.h"
extern RxTxBuffer_t Data;
void GPIO_Configure()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2P
eriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configure()
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_HardwareFlowControl=USART_
HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode
_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength
_8b;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void NVIC_Configure()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
void DelayNms(u16 time)
{
u16 i = 0;
while (time--)
{
i = 12000;
while (i--) ;
}
}
RxTxState_t SendOneByte(u8 dat)
{
vu32 cnt = 0;
USART_SendData(USART1, dat);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==
RESET)
{
cnt++;
if (cnt == 100000)
return ERR;
}
return OK;
}
void SendString(u8 *Message)
{
while (*Message != '\0')
SendOneByte(*Message++);
}
void EmptyRxBuffer(u8 len)
{
u8 i;
for(i = 0; i < len; i++)
Data.RxBuffer = 0;
}

如有錯誤,歡迎指出討論
如果感興趣的的話,也歡迎關(guān)注我們的公眾號,我們會定期更新一些技術(shù)類文章


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:501249 發(fā)表于 2019-10-22 18:05 | 只看該作者
我下載了這個程序是挺有用的,謝謝大神分享這么好的筆記
回復(fù)

使用道具 舉報

板凳
ID:515985 發(fā)表于 2019-10-26 10:41 | 只看該作者
怎么下載
回復(fù)

使用道具 舉報

地板
ID:93341 發(fā)表于 2021-6-22 09:46 | 只看該作者
我下載了這個程序是挺有用的,謝謝大神分享這么好的筆記
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表