有时需要测一下引脚的输出情况,最直观的就是测波形了,买个示波器不太可能了,太贵了没必要。只好自己做一个了。
硬件:
Arduino UNO一块
Arduino nano一块(用来产生方波)
Nokia5110屏幕一块
电位器一个(我用的是500欧的)
面包板一块
面包线若干
连接:
Nokia5110:
RST — D3
CE — D4
DC — D5
Din — D6
CLK — D7
Vcc — Vcc
BL — Vcc
Gnd — Gnd
电位器:
两脚的两端分别接Vcc和Gnd,另一脚接A1.
Arduino nano的D9接UNO的A0.
连线图(点击放大):
代码:
UNO:
#include <Adafruit_GFX.h> #include <Adafruit_PCD8544.h> #include <SPI.h> #define DISPLAY_WIDTH 84 #define DISPLAY_HEIGHT 48 #define ARDUINO_PRECISION 1023.0 Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3); //Analog Pins int channelAI = A0; // probe #define DELAY_POTENTIMETER //disabled it I don't have it connected #ifdef DELAY_POTENTIMETER int delayAI = A1; // delay potentiometer #endif float delayVariable = 0; float scale = 0; int xCounter = 0; int yPosition = 0; int readings[DISPLAY_WIDTH+1]; int counter = 0; unsigned long drawtime = 0; unsigned long lastdraw = 0; int frames = 0; void setup(void) { display.begin(); display.setContrast(60);// you might have a slightly different display so it might not be the optimal value for you display.clearDisplay(); } void loop() { #ifdef DELAY_POTENTIMETER delayVariable = analogRead(delayAI); delayVariable = (delayVariable/100); #endif scale = (float)(DISPLAY_HEIGHT-1)/ARDUINO_PRECISION; //record readings for(xCounter = 0; xCounter <= DISPLAY_WIDTH; xCounter++) { yPosition = analogRead(channelAI); readings[xCounter] = (yPosition*scale); #ifdef DELAY_POTENTIMETER delay (delayVariable); #endif } display.clearDisplay(); //Draw Voltage Ref Lines display.drawLine( 10, 0, 10, DISPLAY_HEIGHT-1, BLACK); display.drawLine( 5, (DISPLAY_HEIGHT-1)-(.2 *ARDUINO_PRECISION * scale), 10, (DISPLAY_HEIGHT-1)-(.2 *ARDUINO_PRECISION * scale), BLACK); display.drawLine( 0, (DISPLAY_HEIGHT-1)-(.4 *ARDUINO_PRECISION * scale), 10, (DISPLAY_HEIGHT-1)-(.4 *ARDUINO_PRECISION * scale), BLACK); display.drawLine( 5, (DISPLAY_HEIGHT-1)-(.6 *ARDUINO_PRECISION * scale), 10, (DISPLAY_HEIGHT-1)-(.6 *ARDUINO_PRECISION * scale), BLACK); display.drawLine( 0, (DISPLAY_HEIGHT-1)-(.8 *ARDUINO_PRECISION * scale), 10, (DISPLAY_HEIGHT-1)-(.8 *ARDUINO_PRECISION * scale), BLACK); //display.drawLine( 5, (DISPLAY_HEIGHT-1)-(.84 *ARDUINO_PRECISION * scale), 10, (DISPLAY_HEIGHT-1)-(.84 *ARDUINO_PRECISION * scale), BLACK); //Draw Voltage Ref Numbers display.setCursor(0,((DISPLAY_HEIGHT-1)-(.2 *ARDUINO_PRECISION * scale))-3); display.print((int)(5.0*0.2)); display.setCursor(0,((DISPLAY_HEIGHT-1)-(.4 *ARDUINO_PRECISION * scale))-3); display.print((int)(5.0*0.4)); display.setCursor(0,((DISPLAY_HEIGHT-1)-(.6 *ARDUINO_PRECISION * scale))-3); display.print((int)(5.0*0.6)); display.setCursor(0,((DISPLAY_HEIGHT-1)-(.8 *ARDUINO_PRECISION * scale))-3); display.print((int)(5.0*0.8)); for(xCounter = 0; xCounter <= DISPLAY_WIDTH; xCounter++) { display.drawPixel(xCounter, (DISPLAY_HEIGHT-1)-readings[xCounter], BLACK); if(xCounter>1){ display.drawLine(xCounter-1, (DISPLAY_HEIGHT-1)-readings[xCounter-1], xCounter, (DISPLAY_HEIGHT-1)-readings[xCounter], BLACK); } } //Draw FPS display.setCursor((DISPLAY_WIDTH-1)-11,0); display.print(frames); //Draw Voltage display.setCursor(((DISPLAY_WIDTH-1)/2),0); display.print(analogRead(channelAI)/ARDUINO_PRECISION*5.0); display.display(); //Calculate FPS drawtime = micros(); frames=1000000/*a second*//(drawtime-lastdraw); lastdraw = drawtime; }
void setup() { // 下面的等值于 OC1A = 输出用于定时器1的CTC模式 pinMode(9, OUTPUT); // 设置默认关联了D9(查表得知)为输出 TCCR1B=0; // 只是个复位的习惯可以不要 TCCR1A=_BV(COM1A0); // 要是困扰的话你也可以写成 TCCR1A = 0b01000000; //这里和上一个表示了 = CTC 模式, 开启比较器(默认输出), prescaler = 1 TCCR1B = _BV(WGM12)|_BV(CS10) ; // 这里等值于 TCCR1B = 0b00001001; // 最有意思的了,16位的玩意来确定要一个怎样的频率 OCR1A = 1000; // 计算为:F_CPU/2/Need_HZ-1 来得到你想要的,遗憾的是四舍五入来截取频率 } void loop(){ //这次啥也没做 }
涉及到的库文件下载:
https://github.com/adafruit/Adafruit-PCD8544-Nokia-5110-LCD-library
https://github.com/adafruit/Adafruit-GFX-Library
参考文章:
http://www.instructables.com/id/Nokia-5110-Arduino-Oscilloscope/
http://www.geek-workshop.com/thread-13315-1-1.html