標(biāo)題: Arduino r3驅(qū)動(dòng)廢棄電子產(chǎn)品的LCD128*64顯示屏制作示波器 [打印本頁(yè)]

作者: 544919169    時(shí)間: 2019-2-27 22:03
標(biāo)題: Arduino r3驅(qū)動(dòng)廢棄電子產(chǎn)品的LCD128*64顯示屏制作示波器
這個(gè)diy的示波器彩樣頻率大概1M左右,可以顯示峰值電壓,幅值電壓,頻率!占空比。后續(xù)功能再添加。

IMG_20190227_145258.jpg (708.09 KB, 下載次數(shù): 142)

IMG_20190227_145258.jpg

IMG_20190222_094415.jpg (678 KB, 下載次數(shù): 131)

IMG_20190222_094415.jpg

作者: 544919169    時(shí)間: 2019-2-27 22:04
想上傳代碼附件不知道怎么上傳
作者: admin    時(shí)間: 2019-2-28 02:17
先把附件壓縮成rar格式,這里有上傳教程http://www.torrancerestoration.com/bbs/dpj-49501-1.html
作者: 544919169    時(shí)間: 2019-3-2 11:30
現(xiàn)在可以提供下載了
  1. /*
  2. 廢棄的顯示屏示波器
  3. */

  4. #include <Arduino.h>
  5. #include <U8g2lib.h>
  6. #include <EEPROM.h>

  7. #ifdef U8X8_HAVE_HW_SPI
  8. #include <SPI.h>
  9. #endif

  10. U8G2_ST7565_EA_DOGM128_F_4W_SW_SPI u8g2(U8G2_R0,/*clock=*/5,/*data=*/4,/*cs=*/8,/*dc=*/6,/*reset=*/7);

  11. int input     =A0;      //采集信號(hào)輸入口
  12. int Key_hold  =12;      //hold顯示
  13. int Key_mode  =13;      //延時(shí)選擇
  14. int dis_width =128;     //顯示屏寬度
  15. int dis_hight =64;      //顯示屏高度
  16. int x;                  //繪點(diǎn)座標(biāo)
  17. int Buffer[128];        //緩存值儲(chǔ)存數(shù)組
  18. long Freq;              //頻率
  19. float Vp_p,Vpp;         //實(shí)際電壓,峰值電壓
  20. int Dr;                 //占空比
  21. int t,i1,i2,i3,V_min,V_max,V_mid,hold=0;
  22. float a,b;
  23. int mode=0;
  24. int adc_n=4;
  25. int dTime=1;

  26. //產(chǎn)生62.5K方波
  27. void pwm_62_5k()
  28. {
  29.   cli(); //禁止中斷
  30.   pinMode(9, OUTPUT);
  31.   pinMode(10, OUTPUT);
  32.   //設(shè)置timer1快速pwm,頻率62.5kHz,D9 = OCR1A; D10 = OCR1B
  33.   TCCR1A = 0;
  34.   TCCR1B = 0;
  35.   //比較匹配時(shí)清零 OC1A/OC1B
  36.   TCCR1A |= _BV(COM1A1) | _BV(COM1B1);
  37.   //配置8位快速PWM, TOP = 0xFF
  38.   TCCR1A |= _BV(WGM10);
  39.   TCCR1B |= _BV(WGM12);
  40.   //時(shí)鐘無(wú)分頻,輸出頻率: 16 MHz/1/256= 62500Hz
  41.   TCCR1B |= _BV(CS10);
  42.   OCR1A = 127;    // 占空比127/255=50%
  43.   OCR1B = 25.5;   // 占空比25.5/255=10%
  44.   sei(); //允許中斷
  45. }

  46. void setup(void) {
  47.   Serial.begin(115200);
  48.   u8g2.begin();
  49.   pinMode(input,INPUT);          //A0要接20K下拉電阻到地
  50.   pinMode(Key_hold,INPUT_PULLUP);//接1K下拉電阻低電平觸發(fā)
  51.   pinMode(Key_mode,INPUT_PULLUP); //采樣模式和速率選擇
  52.   u8g2.setFont(u8g2_font_blipfest_07_tr);
  53.   u8g2.setFontDirection(0);
  54.   u8g2.setContrast(180);         //設(shè)置屏幕對(duì)比度0-255
  55.   pwm_62_5k();
  56.   set_adc(16);                    //ADC 16分頻模式采樣
  57.   pinMode(11,OUTPUT);
  58.   analogWrite(11,127);
  59. }

  60. void loop(void) {
  61.   sample();          //ADC采樣
  62.   Measure();         //測(cè)量參數(shù)
  63.   Transform();       //計(jì)算坐標(biāo)
  64.   u8g2.firstPage();  //清屏
  65.   if (hold==0){      //凍結(jié)顯示
  66.     do{draw();}
  67.     while (u8g2.nextPage());
  68.   }
  69.   san_key();
  70. }

  71. //寄時(shí)器中斷處理A0-A5
  72. ISR(PCINT1_vect){
  73.   /*ADC中斷
  74.   放在setup內(nèi)
  75.   PCMSK1 |= bit (PCINT8); //PCINT8關(guān)聯(lián)A0中斷
  76.   PCIFR |= bit (PCIF1);
  77.   PCICR |= bit (PCIE1);
  78.   */
  79. }

  80. //使用寄時(shí)器ADC采樣,analogRead(pin)函數(shù)的采樣速率太慢
  81. void sample(){
  82.     ADCSRA |= _BV(ADSC);          //第一次讀數(shù)不準(zhǔn)
  83.   for(x = 0;x < dis_width;x++){
  84.     ADCSRA |= _BV(ADSC);
  85.     loop_until_bit_is_set(ADCSRA, ADIF);
  86.     Buffer[x] = ADC;
  87.     bitClear(ADCSRA, ADIF);
  88.     //暫停x微秒
  89.     //delayMicroseconds(dTime);
  90.   }
  91. }

  92. //測(cè)量參數(shù)
  93. void Measure(){
  94.   V_max=Buffer[0];//取最大值
  95.   V_min=Buffer[0];//取最小值
  96.   for(x=0;x<dis_width;x++){
  97.     if(Buffer[x]>V_max)
  98.     V_max=Buffer[x];
  99.     if(Buffer[x]<V_min)
  100.     V_min=Buffer[x];
  101.   }
  102.   //平均值=(最大值+最小值)/2
  103.   V_mid=(V_max+V_min)/2;
  104.   //振幅電壓=(最大值+最小值)/2*基準(zhǔn)電壓/1023
  105.   Vp_p=(V_max+V_min)/2*4.99/1023;
  106.   //峰值電壓=(最大值-最小值)*基準(zhǔn)電壓/1023
  107.   Vpp=(V_max-V_min)*4.99/1023;
  108.   for(x=0;x<dis_width-2;x++){
  109.     //周期開始點(diǎn)
  110.     if(Buffer[x]<V_mid&&Buffer[x+1]>=V_mid)
  111.     {i1=x;break;}
  112.   }
  113.   for(x=i1+1;x<dis_width-2+i1;x++){
  114.     //周期內(nèi)高電平結(jié)束點(diǎn)
  115.     if(Buffer[x]>V_mid&&Buffer[x+1]<=V_mid)
  116.     {i3=x;}
  117.     //周期結(jié)束點(diǎn)
  118.     if(Buffer[x]<V_mid&&Buffer[x+1]>=V_mid)
  119.     {i2=x;break;}
  120.   }
  121.   //周期時(shí)間=周期結(jié)速點(diǎn)-周期開始點(diǎn)
  122.   t=i2-i1;
  123.   //高電平時(shí)間=高電平結(jié)束時(shí)間-周期開始點(diǎn)
  124.   a=(i3-i1);
  125.   //占空比=高電平時(shí)間/周期時(shí)間(高電平時(shí)間+低電平時(shí)間)
  126.   Dr=a/t*100;
  127.   //頻率=1000000/周期時(shí)間(高電平時(shí)間+低電平時(shí)間)
  128.   if(t>1){
  129.     if(adc_n<=16){
  130.        Freq=16000000/adc_n/t;  //16分頻模式
  131.     }else if(adc_n>16){
  132.        Freq=16000000/255/adc_n;//非16分頻模式
  133.     }
  134.   }else{
  135.     Freq=0;
  136.   }
  137.   if(mode==0){
  138.     //自動(dòng)選擇采樣速率以適應(yīng)高頻低頻信號(hào)
  139.     if(t>30||t<10){
  140.       adc_n=adc_n*2;
  141.       if(adc_n>128){adc_n=4;}
  142.       set_adc(adc_n);
  143.     }
  144.   }
  145. }

  146. //計(jì)算座標(biāo)
  147. void Transform(){
  148.   for(x = 0;x < dis_width;x++){
  149.     //使用map函數(shù)將0-1023的模擬值映射為7~62之間的坐標(biāo)值
  150.     Buffer[x] = map(Buffer[x],0,1023,55,7);  
  151.   }
  152. }
  153.    
  154. //顯示
  155. void draw(){
  156.   int dis_h=dis_hight/2;
  157.   //顯示電壓峰值
  158.   u8g2.drawStr(6,7, "V:");
  159.   u8g2.setCursor(13, 7);
  160.   u8g2.print(Vpp);
  161.   //顯示實(shí)際電壓
  162.   u8g2.drawStr(35,7, "Vp:");
  163.   u8g2.setCursor(45, 7);
  164.   u8g2.print(Vp_p);
  165.   //顯示頻率
  166.   u8g2.drawStr(68,7, "Hz:");
  167.   u8g2.setCursor(78, 7);
  168.   u8g2.print(Freq);
  169.   //顯示占空比
  170.   u8g2.drawStr(105,7, "DR:");
  171.   u8g2.setCursor(115, 7);
  172.   u8g2.print(Dr);
  173.   //畫x坐標(biāo)軸
  174.   u8g2.drawLine(0,dis_h,dis_width,dis_h);
  175.   //畫y坐標(biāo)軸
  176.   u8g2.drawLine(dis_hight,7,dis_hight,dis_width);
  177.   //畫邊框
  178.   u8g2.drawFrame(4,0,124,dis_hight);
  179.   for(int x=1;x<dis_width;x++){
  180.     //顯示波形
  181.     u8g2.drawLine(x,Buffer[x],x,Buffer[x+1]);
  182.   }
  183.   for(x=0;x<dis_width;x+=4){
  184.     //畫x坐標(biāo)刻度
  185.     u8g2.drawLine(x,dis_h-1,x,dis_h+1);
  186.     if (x<=dis_hight-14){
  187.       //畫Y坐標(biāo)刻度
  188.       u8g2.drawLine(dis_hight-1,x+7,dis_hight+1,x+7);
  189.     }
  190.   }
  191.   //ADC采樣模式,采樣速率
  192.   if(mode==0){
  193.     u8g2.drawStr(6,62, "A:");//自動(dòng)選頻
  194.   }else{
  195.     u8g2.drawStr(6,62, "M:");//手動(dòng)設(shè)置
  196.   }
  197.   u8g2.setCursor(12,62);
  198.   u8g2.print(adc_n);
  199.   //發(fā)送數(shù)據(jù)給顯示屏
  200.   u8g2.sendBuffer();
  201. }

  202. void san_key(){
  203.   if(digitalRead(Key_hold)==LOW){
  204.     while(digitalRead(Key_hold)==LOW);
  205.     hold=~hold;
  206.    }
  207.   if(digitalRead(Key_mode)==LOW){
  208.     while(digitalRead(Key_hold)==LOW);
  209.       mode=mode+1;
  210.       if(mode>6){mode=0;Serial.print(0,DEC);}
  211.       if(mode!=0){
  212.         adc_n=adc_n*2;
  213.         if(adc_n>128){adc_n=4;}
  214.         set_adc(adc_n);
  215.       }
  216.       
  217.    }
  218. }

  219. //配置 ADC,提高ADC采樣速率
  220. void set_adc(int key) {
  221.   ADMUX = _BV(REFS0); // 使用A0腳采樣,精度為(0-1023)
  222.   //ADMUX=_BV(REFS0)|_BV(ADLAR)|0; //精度降為(0-255)
  223.   switch (key){
  224.     case 4://ADC Prescaler = 4
  225.       //理論 Sample Rate可達(dá)16MHz/4/13=307.6KHz
  226.       ADCSRA &=  ~(1 << ADPS2);  // 0
  227.       ADCSRA |=  (1 << ADPS1);   // 1
  228.       ADCSRA &=  ~(1 << ADPS0);  // 0
  229.       break;
  230.     case 8://ADC Prescaler = 8
  231.       //理論 Sample Rate可達(dá)16MHz/8/13=153.8KHz,實(shí)測(cè)93.5KH
  232.       ADCSRA &=  ~(1 << ADPS2); // 0
  233.       ADCSRA |=  (1 << ADPS1);  // 1
  234.       ADCSRA |=  (1 << ADPS0);  // 1
  235.       break;
  236.     case 16://ADC Prescaler = 16
  237.       //理論 Sample Rate 可達(dá)16MHz/32/13=76.8KHz
  238.       ADCSRA |=  (1 << ADPS2);   // 1
  239.       ADCSRA &=  ~(1 << ADPS1);  // 0
  240.       ADCSRA &=  ~(1 << ADPS0);  // 0
  241.       break;
  242.     case 32://ADC Prescaler = 32
  243.       //理論 Sample Rate 可達(dá)16MHz/32/13= 38.4KHz
  244.       ADCSRA |=  (1 << ADPS2);  // 1
  245.       ADCSRA &=  ~(1 << ADPS1); // 0
  246.       ADCSRA |=  (1 << ADPS0);  // 1
  247.       break;
  248.     case 64://ADC Prescaler = 64
  249.       //理論 Sample Rate 可達(dá)16MHz/64/13= 19.2KHz
  250.       ADCSRA |=  (1 << ADPS2);  // 1
  251.       ADCSRA |=  (1 << ADPS1);  // 1
  252.       ADCSRA &=  ~(1 << ADPS0); // 0
  253.       //ADCSRA  = ADCSRA&(~7)|0x06;
  254.       break;
  255.     case 128://ADC Prescaler = 128
  256.       //理論 Sample Rate 可達(dá)16MHz/128/13=9600Hz
  257.       ADCSRA |=  (1 << ADPS2);  // 1
  258.       ADCSRA |=  (1 << ADPS1);  // 1
  259.       ADCSRA |=  (1 << ADPS0);  // 1
  260.       break;
  261.     default:
  262.       break;
  263.   }
  264. }
復(fù)制代碼



作者: IdeaMing    時(shí)間: 2019-12-13 09:07
厲害!
作者: 不思量joo    時(shí)間: 2019-12-15 11:25
厲害,真是厲害
類似主題:
http://www.torrancerestoration.com/bbs/dpj-27356-1.html
http://www.torrancerestoration.com/bbs/dpj-190973-1.html
http://www.torrancerestoration.com/bbs/dpj-35520-1.html
http://www.torrancerestoration.com/bbs/dpj-213269-1.html
http://www.torrancerestoration.com/bbs/dpj-186003-1.html
http://www.torrancerestoration.com/bbs/dpj-59981-1.html
http://www.torrancerestoration.com/bbs/dpj-129947-1.html
http://www.torrancerestoration.com/bbs/dpj-202400-1.html
http://www.torrancerestoration.com/bbs/dpj-88462-1.html
http://www.torrancerestoration.com/bbs/dpj-36379-1.html
http://www.torrancerestoration.com/bbs/dpj-80238-1.html
http://www.torrancerestoration.com/bbs/dpj-178374-1.html
http://www.torrancerestoration.com/bbs/dpj-151707-1.html
http://www.torrancerestoration.com/bbs/dpj-155801-1.html

作者: 1156479136    時(shí)間: 2020-4-11 17:21
引腳圖呢???
作者: 中恒電子asus    時(shí)間: 2020-6-9 23:43

謝謝分享
作者: diversj    時(shí)間: 2020-11-10 08:41
感謝分享,樓主大人有沒有接線圖啊
作者: zyh12398    時(shí)間: 2020-11-10 10:37
這個(gè)厲害了 我記得有個(gè)U  哪天試試

作者: Highnose    時(shí)間: 2020-11-10 12:03
強(qiáng)人呀,給力




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1