久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2198|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

我的光傳感器數(shù)碼管顯示程序哪里出錯了,請教各路大神幫忙看一下

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:276357 發(fā)表于 2018-1-15 20:30 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
我在做光傳感器的一個實驗,然后需要把計算的節(jié)拍顯示在數(shù)碼管上,但是現(xiàn)在數(shù)據(jù)能上去,但是數(shù)碼管顯示的總是斷斷續(xù)續(xù)的,不能正常顯示,數(shù)字除了7都不能完整的顯示,我才入門,找了很久也沒有找到問題所在,請各路大神幫我審審看。
  1. #include <STC12C5A60S2.h>
  2. #include "stdio.h"
  3. #include "intrins.h"

  4. #define false 0
  5. #define true 1
  6. #define FOSC 11059200L                //系統(tǒng)時鐘
  7. #define BAUD 115200                                //波特率
  8. #define T0MS (65536-FOSC/12/500)                //500HZ in 12T MODE

  9. #define ADC_POWER 0x80                        //ADC POWER CONTROL BIT
  10. #define ADC_FLAG 0x10                        //ADC COMPLETE FLAG
  11. #define ADC_START 0x08;                        //ADC START CONTROL BIT
  12. #define ADC_SPEEDLL 0x00                //540 CLOCKS
  13. #define ADC_SPEEDL 0x20                        //360 CLOCKS
  14. #define ADC_SPEEDH 0x40                        //180 CLOCKS
  15. #define ADC_SPEEDHH 0x60                //90 CLOCKS
  16. #define ADC_MASK 0x01

  17. //數(shù)碼管段碼顯示:0~f,不亮
  18. unsigned char code LED_Disp[] = {0x3f,0x06,0x5b,0x4f,
  19. 0x66,0x6d,0x7d,0x07,
  20. 0x7f,0x6f,0x77,0x7c,
  21. 0x39,0x5e,0x79,0x71
  22.                         };
  23. sbit dula=P2^6;
  24. sbit wela=P2^7;


  25. unsigned char DisBuff[3];

  26. void UART_init(void);
  27. void LED_Disp_Seg7(void);
  28. void ADC_init(unsigned char channel);
  29. void T0_init(void);
  30. void sendDataToProcessing(char symbol, int dat);
  31. void delay(unsigned int n);
  32. void UART_send(char dat);

  33. unsigned char PulsePin = 0;       // Pulse Sensor purple wire connected to analog pin 0(P1.0為AD口)
  34. sbit blinkPin = P2^0;                // pin to blink led at each beat
  35. sbit fadePin = P2^3;                  // pin to do fancy classy fading blink at each beat
  36. sbit led1 = P2^1;
  37. sbit led2 = P2^2;
  38. int fadeRate = 0;                 // used to fade LED on with PWM on fadePin


  39. // these variables are volatile because they are used during the interrupt service routine!
  40. volatile unsigned int BPM;                   // used to hold the pulse rate
  41. volatile unsigned int Signal;                // holds the incoming raw data
  42. volatile unsigned int IBI = 600;             // holds the time between beats, must be seeded!
  43. volatile bit Pulse = false;     // true when pulse wave is high, false when it's low
  44. volatile bit QS = false;        // becomes true when Arduoino finds a beat.
  45. volatile int rate[10];                    // array to hold last ten IBI values
  46. volatile unsigned long sampleCounter = 0;          // used to determine pulse timing
  47. volatile unsigned long lastBeatTime = 0;           // used to find IBI
  48. volatile int Peak =512;                      // used to find peak in pulse wave, seeded
  49. volatile int Trough = 512;                     // used to find trough in pulse wave, seeded
  50. volatile int thresh = 512;                // used to find instant moment of heart beat, seeded
  51. volatile int amp = 100;                   // used to hold amplitude of pulse waveform, seeded
  52. volatile bit firstBeat = true;        // used to seed rate array so we startup with reasonable BPM
  53. volatile bit secondBeat = false;      // used to seed rate array so we startup with reasonable BPM
  54. static unsigned char order=0;
  55. void sys_init()
  56. {UART_init();             // we agree to talk fast!
  57.         ADC_init(PulsePin);
  58.   T0_init();                 // sets up to read Pulse Sensor signal every 2mS  
  59. }
  60. void main(void)
  61. {
  62.   sys_init();
  63.         while(1)
  64.         {
  65.                 sendDataToProcessing('S', Signal);     // send Processing the raw Pulse Sensor data
  66.                 if (QS == true){                       // Quantified Self flag is true when arduino finds a heartbeat
  67.                                         fadeRate = 255;                  // Set 'fadeRate' Variable to 255 to fade LED with pulse
  68.                                         sendDataToProcessing('B',BPM);   // send heart rate with a 'B' prefix
  69.                                         sendDataToProcessing('Q',IBI);   // send time between beats with a 'Q' prefix
  70.                                         QS = false;                      // reset the Quantified Self flag for next time   
  71.                          }
  72. delay(138);                             //  take a break 19.6ms

  73. }
  74. }
  75. void sendDataToProcessing(char symbol, int dat ){
  76.     putchar(symbol);                // symbol prefix tells Processing what type of data is coming
  77.                 printf("%d\r\n",dat);                                                // the data to send culminating in a carriage return
  78.   }


  79. void UART_init(void)
  80. {
  81.          PCON &= 0x7f;  //波特率不倍速
  82.    SCON = 0x50;  //8位數(shù)據(jù),可變波特率
  83.    BRT = 0xFD;    //獨立波特率產(chǎn)生器初值
  84.    AUXR |= 0x04;  //時鐘設(shè)置為1T模式
  85.    AUXR |= 0x01;  //選擇獨立波特率產(chǎn)生器
  86.    AUXR |= 0x10;  //啟動波特率產(chǎn)生
  87. }

  88. char putchar(unsigned char dat)
  89. {
  90.         TI=0;
  91.         SBUF=dat;
  92.         while(!TI);
  93.         TI=0;
  94.         return SBUF;
  95. }

  96. void delay(unsigned int n)
  97. {
  98.         unsigned int i,j;
  99.         for(i=0;i<n;i++)
  100.                 for(j=0;j<100;j++);
  101. }

  102. void T0_init(void){     
  103.   // Initializes Timer0 to throw an interrupt every 2mS.
  104.     wela=0;
  105.         dula=0;

  106.         TMOD |= 0x01;        //16bit TIMER
  107.         TL0=T0MS;
  108.         TH0=T0MS>>8;
  109.         TR0=1;                //start Timer 0
  110.         ET0=1;                //enable Timer Interrupt
  111.     EA=1;             // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED      
  112. }
  113. void LED_Disp_Seg7(void)
  114. {
  115. switch(order)
  116.         {
  117.         case 0:
  118.             dula=1;
  119.                 P0=DisBuff[2];
  120.                 dula=0;
  121.                 P0=0xff;
  122.                 wela=1;
  123.                 P0=0xfe;
  124.                 wela=0;
  125.                 delay(1);
  126.                 break;

  127.         case 1:
  128.                 dula=1;
  129.                 P0=DisBuff[1];
  130.                 dula=0;
  131.                 P0=0xff;
  132.                 wela=1;
  133.                 P0=0xfd;
  134.                 wela=0;
  135.                 delay(1);
  136.                 break;
  137.         case 2:
  138.                 dula=1;
  139.                 P0=DisBuff[0];
  140.                 dula=0;
  141.                 P0=0xff;
  142.                 wela=1;
  143.                 P0=0xfb;
  144.                 wela=0;
  145.                 delay(1);
  146.                 break;
  147.                 }
  148.                 if(++order>2)
  149.                         order=0;
  150. }
  151. void ADC_init(unsigned char channel)
  152. {
  153.         P1ASF=ADC_MASK<<channel;        //enable PlusePin as ADC INPUT
  154.         ADC_RES=0;        //clear former ADC result
  155.         ADC_RESL=0;        //clear former ADC result
  156.         AUXR1 |= 0x04;        //adjust the format of ADC result
  157.         ADC_CONTR=channel|ADC_POWER|ADC_SPEEDLL|ADC_START;        //power on ADC and start conversion
  158. }

  159. unsigned int analogRead(unsigned char channel)
  160. {
  161.         unsigned int result;

  162.         ADC_CONTR &=!ADC_FLAG;        //clear ADC FLAG
  163.         result=ADC_RES;
  164.         result=result<<8;
  165.         result+=ADC_RESL;
  166.         ADC_CONTR|=channel|ADC_POWER|ADC_SPEEDLL|ADC_START;
  167.         return result;
  168. }
  169. // Timer 0中斷子程序,每2MS中斷一次,讀取AD值,計算心率值
  170. void Timer0_rountine(void) interrupt 1
  171. {                       
  172.   int N;
  173.         unsigned char i;        // keep a running total of the last 10 IBI values
  174.   unsigned int runningTotal = 0;                  // clear the runningTotal variable   

  175.         EA=0;                                      // disable interrupts while we do this
  176.         TL0=T0MS;
  177.         TH0=T0MS>>8;                                //reload 16 bit TIMER0
  178.   Signal = analogRead(PulsePin);              // read the Pulse Sensor
  179.   sampleCounter += 2;                         // keep track of the time in mS with this variable
  180.   N = sampleCounter - lastBeatTime;       // monitor the time since the last beat to avoid noise
  181.         LED_Disp_Seg7();


  182.     //  find the peak and trough of the pulse wave
  183.   if(Signal < thresh && N > (IBI/5)*3){       // avoid dichrotic noise by waiting 3/5 of last IBI
  184.     if (Signal < Trough){                        // T is the trough
  185.       Trough = Signal;                         // keep track of lowest point in pulse wave
  186.     }
  187.   }

  188.   if(Signal > thresh && Signal > Peak){          // thresh condition helps avoid noise
  189.     Peak = Signal;                             // P is the peak
  190.   }                                        // keep track of highest point in pulse wave

  191.   //  NOW IT'S TIME TO LOOK FOR THE HEART BEAT
  192.   // signal surges up in value every time there is a pulse
  193.   if (N > 250){                                   // avoid high frequency noise
  194.     if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) ){        
  195.       Pulse = true;                               // set the Pulse flag when we think there is a pulse
  196.       blinkPin=0;               // turn on pin 13 LED
  197.       IBI = sampleCounter - lastBeatTime;         // measure time between beats in mS
  198.       lastBeatTime = sampleCounter;               // keep track of time for next pulse

  199.       if(secondBeat){                        // if this is the second beat, if secondBeat == TRUE
  200.         secondBeat = false;                  // clear secondBeat flag
  201.         for(i=0; i<=9; i++){             // seed the running total to get a realisitic BPM at startup
  202.           rate[i] = IBI;                     
  203.         }
  204.       }

  205.       if(firstBeat){                         // if it's the first time we found a beat, if firstBeat == TRUE
  206.         firstBeat = false;                   // clear firstBeat flag
  207.         secondBeat = true;                   // set the second beat flag
  208.         EA=1;                               // enable interrupts again
  209.         return;                              // IBI value is unreliable so discard it
  210.       }   



  211.       for(i=0; i<=8; i++){                // shift data in the rate array
  212.         rate[i] = rate[i+1];                  // and drop the oldest IBI value
  213.         runningTotal += rate[i];              // add up the 9 oldest IBI values
  214.       }

  215.       rate[9] = IBI;                          // add the latest IBI to the rate array
  216.       runningTotal += rate[9];                // add the latest IBI to runningTotal
  217.       runningTotal /= 10;                     // average the last 10 IBI values
  218.       BPM = 60000/runningTotal;               // how many beats can fit into a minute? that's BPM!
  219.                         if(BPM>200)BPM=200;                   //限制BPM最高顯示值
  220.                         if(BPM<30)BPM=30;                   //限制BPM最低顯示值
  221.                         DisBuff[0]   = BPM%10;     //取個位數(shù)
  222.                         DisBuff[1]   = BPM%100/10; //取十位數(shù)
  223.                         DisBuff[2]   = BPM/100;           //百位數(shù)
  224.       QS = true;                              // set Quantified Self flag
  225.       // QS FLAG IS NOT CLEARED INSIDE THIS ISR
  226.     }                       
  227.   }

  228.   if (Signal < thresh && Pulse == true){   // when the values are going down, the beat is over
  229.     blinkPin=1;            // turn off pin 13 LED
  230.     Pulse = false;                         // reset the Pulse flag so we can do it again
  231.     amp = Peak - Trough;                           // get amplitude of the pulse wave
  232.     thresh = amp/2 + Trough;                    // set thresh at 50% of the amplitude
  233.     Peak = thresh;                            // reset these for next time
  234.     Trough = thresh;
  235.   }

  236.   if (N > 2500){                           // if 2.5 seconds go by without a beat
  237.     thresh = 512;                          // set thresh default
  238.     Peak = 512;                               // set P default
  239.     Trough = 512;                               // set T default
  240.     lastBeatTime = sampleCounter;          // bring the lastBeatTime up to date        
  241.     firstBeat = true;                      // set these to avoid noise
  242.     secondBeat = false;                    // when we get the heartbeat back
  243.   }

  244.   EA=1;                                   // enable interrupts when youre done!
  245. }// end isr
復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:96682 發(fā)表于 2018-1-16 03:07 | 只看該作者
除了源碼還要看電路圖是否對應(yīng)
回復(fù)

使用道具 舉報

板凳
ID:213173 發(fā)表于 2018-1-16 17:34 | 只看該作者
本帖最后由 wulin 于 2018-1-16 21:01 編輯

你的顯示程序有問題,例如段碼P0=DisBuff[2];這里是直接顯示的緩存數(shù)據(jù),實際應(yīng)用是需要把緩存數(shù)據(jù)轉(zhuǎn)換成數(shù)碼管段碼顯示:P0=LED_Disp[DisBuff[2]];,數(shù)碼管數(shù)組里恰好7的代碼是0x07,所以除了7以外其它數(shù)都成亂碼。
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 综合九九| 欧美日韩国产综合在线 | www.三级| 国产2区| 成人欧美一区二区三区黑人孕妇 | 中文字幕视频三区 | 国产精品久久久久久中文字 | 九色 在线 | 久国产精品 | 福利一区视频 | 天天看片天天干 | 国产高清视频在线播放 | 免费在线观看一区二区 | 91av在线影院 | 亚洲三级在线观看 | 欧美国产亚洲一区二区 | 久久久久久国产精品 | 色综合久久天天综合网 | 亚洲精品视频在线播放 | 在线看亚洲 | 四虎影院在线观看av | 美女视频一区 | 一区二区三区国产精品 | 青春草91 | 亚洲最大av | 国产一区视频在线 | 成人av电影免费在线观看 | 日韩av在线播 | 午夜国产精品视频 | 国产精品一区二区三区在线 | 久久久久网站 | 久久久久久女 | 91中文 | 日韩欧美中文字幕在线观看 | www.99热.com | 国产男女猛烈无遮掩视频免费网站 | 在线中文字幕亚洲 | 亚洲精品久久久久久久久久久 | 国产一区二区三区视频 | 99久久精品国产毛片 | 999久久久久久久久6666 |