一、百度网盘链接
链接:https://pan.baidu.com/s/1XOzXy5ypABkSnOlDPTd8vg
提取码:slg0
二、串口示波器协议
点击菜单栏的help->communication…
有两种通讯协议 一种是CRC16另一种是ChkSum,我们使用CRC16
上面是通信协议,简单的来说就是把我单片机的数据转换为示波器可以识别的形式
The data from computer to MPU is like this:
Ch1_Addr_LL,Ch1_Addr_LH,
Ch1_Addr_HL,Ch1_Addr_HH,
Ch2_Addr_LL,Ch2_Addr_LH,
Ch2_Addr_HL,Ch2_Addr_HH,
Ch3_Addr_LL,Ch3_Addr_LH,
Ch3_Addr_HL,Ch3_Addr_HH,
Ch4_Addr_LL,Ch4_Addr_LH,
Ch4_Addr_HL,Ch4_Addr_HH,
CRC16_L,CRC16_H
这是我们需要发给示波器的数据格式
三、代码
1、串口代码
(1).c
我用的是STM32F4系列的,所以从任意F4系列的库函数版本文件中复制过来usart.h 和"usart.c或者自己新建一个也可以,下面存放串口初始化和通讯协议,这边使用的是串口2。
//串口初始化
void mx_usart2_init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_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(USART2,&USART_InitStructure);
USART_Cmd(USART2,ENABLE);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
NVIC_Init(&NVIC_InitStructure);
}
//串口协议,就是上面提到的CRC16协议
unsigned short CRC_CHECK(unsigned char *Buf, unsigned char CRC_CNT)
{
unsigned short CRC_Temp;
int i,j;
CRC_Temp = 0xffff;
for (i=0;i<CRC_CNT; i++){
CRC_Temp ^= Buf[i];
for (j=0;j<8;j++) {
if (CRC_Temp & 0x01)
CRC_Temp = (CRC_Temp >>1 ) ^ 0xA001;
else
CRC_Temp = CRC_Temp >> 1;
}
}
return(CRC_Temp);
}
//这个是发送数据到示波器的程序,
void VisualScope_Output(float data1 ,float data2 ,float data3 ,float data4)
{
int temp[4] = {0};
unsigned int temp1[4] = {0};
unsigned char databuf[10] = {0};
int i;
unsigned short CRC16 = 0;
temp[0] = (int)data1;
temp[1] = (int)data2;
temp[2] = (int)data3;
temp[3] = (int)data4;
temp1[0] = (unsigned int)temp[0] ;
temp1[1] = (unsigned int)temp[1];
temp1[2] = (unsigned int)temp[2];
temp1[3] = (unsigned int)temp[3];
for(i=0;i<4;i++)
{
databuf[i*2] = (unsigned char)(temp1[i]%256);
databuf[i*2+1] = (unsigned char)(temp1[i]/256);
}
CRC16 = CRC_CHECK(databuf,8);
databuf[8] = CRC16%256;
databuf[9] = CRC16/256;
for(i=0; i<10; i++)
{
while((USART2->SR&0X40)==0);
USART_SendData(USART2, databuf[i]);
}
}
需要注意的是,在USART_SendData(USART2, databuf[i]);上面有一个while,意思是要等待串口发送完毕,否则还没发完就再发,就会乱码。其中入口参数的data1-4是你要观察的内容数值大小,对应串口示波器的四个通道。
//可以直接使用这个程序,通过对OutDate赋值,然后将其输出到示波器上,以后直接调用这个函数就好了,可以加入延时,不然波形走的太快了
void display(void)
{
OutData[0] =1000;
OutData[1] =2000;
OutData[2] =3000;
OutData[3]= 4000;
VisualScope_Output(OutData[0],OutData[1] ,OutData[2] ,OutData[3]);
//delay_ms(1000);
}
//USART2接受中断
void USART2_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //½ÓÊÕÖжÏ(½ÓÊÕµ½µÄÊý¾Ý±ØÐëÊÇ0x0d 0x0a½áβ)
{
Res =USART_ReceiveData(USART2);//(USART1->DR); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
if((USART_RX_STA&0x8000)==0)//½ÓÊÕδÍê³É
{
if(USART_RX_STA&0x4000)//½ÓÊÕµ½ÁË0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//½ÓÊÕ´íÎó,ÖØпªÊ¼
else USART_RX_STA|=0x8000; //½ÓÊÕÍê³ÉÁË
}
else //»¹Ã»ÊÕµ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½ÓÊÕÊý¾Ý´íÎó,ÖØпªÊ¼½ÓÊÕ
}
}
}
}
}
(2).h
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "stm32f4xx_conf.h"
#include "sys.h"
#define USART_REC_LEN 200
#define EN_USART1_RX 1
extern u8 USART_RX_BUF[USART_REC_LEN];
extern u16 USART_RX_STA;
extern u8 get_d;
void mx_usart2_init(void);
unsigned short CRC_CHECK(unsigned char *Buf, unsigned char CRC_CNT);
void VisualScope_Output(float data1 ,float data2 ,float data3 ,float data4);
void display(void);
#endif
2、验证
在main.c中的while里加入display();
编译无误后下载到开发板,使用TTL转串口模块或者线连接电脑和开发板
打开串口示波器观察现象
左下方的这个screen width不宜设置过大,如果按照原始值50000,波形只会在x轴50000标度之后开始出现
点击右上角的setup的第一个选项,配波特率和串口,确认无误后点击右下角的RUN开始运行。