硬件:MM32L373
开发工具:IAR7.8
调试工具:山外多功能调试助手
Introduction
在调试MM32芯片的过程中,有时需要观察数据的实时变化,山外多功能调试助手中的虚拟示波器可以实现这一功能。
山外虚拟示波器的特点:
- 支持8个通道,且通道可隐藏
- 可选择通道数据类型
- 界面可缩放移动
- 可追踪最新数据
山外虚拟示波器通信协议:
具体可阅读山外调试助手使用说明
Solution: Output Triangle Wave
以下代码参考了「顶点元」STM32使用虚拟示波器
-
配置UART
void initUART1_IT(u32 baudrate) {UART_InitTypeDef pUARTInit;COMMON_EnableIpClock(emCLOCK_UART1);COMMON_NVIC_Configure(UART1_IRQn, 1, 1);UART_StructInit(&pUARTInit);pUARTInit.BaudRate = baudrate;pUARTInit.WordLength = UART_WordLength_8b;pUARTInit.StopBits = UART_StopBits_1;pUARTInit.Parity = UART_Parity_No;pUARTInit.Mode = UART_Mode_Rx | UART_Mode_Tx;pUARTInit.HWFlowControl = UART_HWFlowControl_None;UART_Init(UART1, &pUARTInit);UART_ITConfig(UART1, UART_IT_RXIEN, ENABLE);UART_ClearITPendingBit(UART1, UART_IT_RXIEN);UART_Cmd(UART1, ENABLE);BSP_UART_GPIO_CONFIGURE(UART1); }
-
发送一个字节的数据
void usart1_send_char(uint8_t ch) {while (UART_GetFlagStatus(UART1, UART_IT_TXIEN) == RESET);UART_ClearITPendingBit(UART1, UART_IT_TXIEN);UART_SendData(UART1, (uint8_t)ch); }
-
发送一个数组
数组长度即为通道个数
void usart1_putbuff (uint8_t *buff, uint32_t len) {while(len--){usart1_send_char(*buff);buff++;} }
-
根据通信协议向上位机发送数据
void vcan_sendware(uint8_t *wareaddr, uint32_t waresize) { uint8_t cmdf[2] = {0x03, 0xfc};uint8_t cmdr[2] = {0xfc, 0x03};usart1_putbuff(cmdf,sizeof(cmdf));usart1_putbuff(wareaddr,waresize);usart1_putbuff(cmdr,sizeof(cmdr)); }
-
输出三角波
void main(void) { float warebuf[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};MCUID = SetSystemClock(emSYSTICK_On, AppTaskTick);initUART1_IT(115200);while(1){warebuf[0] += 5;vcan_sendware((uint8_t *)warebuf,sizeof(warebuf));for(int i = 0; i < 100000; i++){}if(warebuf[0] >= 0x50){warebuf[0] = 0x00;}} }
-
虚拟示波器显示
Solution: Output ADC Wave
-
UART配置、发送部分同上
-
配置ADC
void Set_ADC_Clock(ADC_TypeDef* ADCx) {if (ADCx == ADC1) COMMON_EnableIpClock(emCLOCK_ADC1); }void ADC_Configure_Init(ADC_TypeDef* ADCx) {ADC_InitTypeDef ADC_InitStructure;ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;ADC_InitStructure.ADC_PRESCARE = ADC_PCLK2_PRESCARE_8;ADC_InitStructure.ADC_Mode = ADC_CR_CONTINUE;ADC_InitStructure.ADC_ExternalTrigConv = 0;ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;Set_ADC_Clock(ADCx);ADC_Init(ADCx, &ADC_InitStructure); } void ADC_Function(ADC_TypeDef* ADCx) {ADC_ExternalTrigConvCmd(ADCx, DISABLE); }void ADC_RegularChannelConfigure(void) {u32 chs = 0 ; chs |= LEFT_SHIFT_BIT(0) | LEFT_SHIFT_BIT(1) | LEFT_SHIFT_BIT(2) | LEFT_SHIFT_BIT(3);ADC1->CFGR &= ~ADC_CFGR_SAMCTL;ADC1->CFGR |= ADC_Samctl_13_5;ADC1->CHSR &= ~chs;ADC1->CHSR |= chs;exADC_TempSensorVrefintCmd(chs, ENABLE); }
-
输出ADC电压波形
int main(void) {MCUID = SetSystemClock(emSYSTICK_On, AppTaskTick);ADC_Configure_Init(ADC1);ADC_Function(ADC1);ADC_RegularChannelConfigure();ADC_Cmd(ADC1, ENABLE);BSP_ADC_GPIO_Configure(ADC1, 15);initUART1_IT(115200);float ADC_ConvertedValue; float warebuf[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};while (1) {ADC_SoftwareStartConvCmd(ADC1, ENABLE);while(!ADC_GetFlagStatus(ADC1, ADC_IT_EOC));ADC_ConvertedValue = ADC_GetConversionValue(ADC1); warebuf[0] = (float)ADC_ConvertedValue;warebuf[1] = (float)ADC_ConvertedValue*(3.3/4096);vcan_sendware((uint8_t *)warebuf,sizeof(warebuf));for(int i = 0; i < 100000; i++){}} }
-
虚拟示波器显示
Code on Github
https://github.com/Miao-T/VirtualOscilloscope
山外多功能调试助手获取
https://download.csdn.net/download/weixin_42150654/86246725