找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3053|回复: 0
打印 上一主题 下一主题
收起左侧

请大家帮忙看看我的数码管为什么显示不正常?

[复制链接]
跳转到指定楼层
楼主
ID:276357 发表于 2018-1-15 20:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我在做光传感器的实验,数据采集后需要显示在数码管上,但目前除了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                //系统时钟
  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. //数码管段码显示: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位数据,可变波特率
  83.    BRT = 0xFD;    //独立波特率产生器初值
  84.    AUXR |= 0x04;  //时钟设置为1T模式
  85.    AUXR |= 0x01;  //选择独立波特率产生器
  86.    AUXR |= 0x10;  //启动波特率产生
  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;     //取个位数
  222.                         DisBuff[1]   = BPM%100/10; //取十位数
  223.                         DisBuff[2]   = BPM/100;           //百位数
  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
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表