【CH559L单片机】基于Arduino框架下实现USB CDC虚拟串口打印ADC数据

基于Arduino框架下实现USB CDC虚拟串口打印ADC数据


示例代码

/*
  AnalogReadSerial

  Reads an analog input on pin P1.1, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin P1.1, and the outside pins to 5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

#include <Serial.h>

// the setup routine runs once when you press reset:
void setup() {
  // No need to init USBSerial
//getLineCodingHandler();
  // By default 8051 enable every pin's pull up resistor. Disable pull-up to get full input range.
  pinMode(11, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin P1.1. You may use P1.1, P1.4, P1.5 and P3.2
  int sensorValue = analogRead(11);
     float voltage = sensorValue * (3.3/ 2048.0);
  // print out the value you read:
  USBSerial_print("sensorValue=");
  USBSerial_print(sensorValue);
  USBSerial_print(" ;电压值:");
   USBSerial_print(voltage);
   USBSerial_println("V");
//  USBSerial_flush();
  delay(800);
  }
  • 🌿选择USB CDC虚拟串口监视器打印
    在这里插入图片描述
    在这里插入图片描述

⛳注意事项

当使用Arduino IDE编译后,将程序烧录到了目标开发板后,推荐使用Arduino IDE自带的串口监视器查看调试信息:打开对应的USB CDC虚拟串口,在Arduino IDE环境下,代码的编码格式和其他平台例如Keil环境下有所不同,如果用其他串口调试工具软件查看串口调试信息,打印出来的信息中如果有中文会是显示乱码,这是编码问题,这一点需要注意。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值