找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1644|回复: 1
收起左侧

STC12C5A60S2单片机多通道采集程序怎么写

[复制链接]
ID:781376 发表于 2020-12-21 22:57 | 显示全部楼层 |阅读模式
如题,STC12C5A60S2不是很懂写,我用的是Pulsensor传感器,因为师傅要求我用多通道采集数据,网上找到的关于pulsensor传感器的资料比较少,论坛关于多通道采集的程序也比较少,特地来问问

单片机代码如下
  1. #include <STC12C5A60S2.h>
  2. #include "stdio.h"

  3. #define false 0
  4. #define true 1
  5. #define FOSC 11059200L                //系统时钟
  6. #define BAUD 115200                                //波特率
  7. #define T0MS (65536-FOSC/12/500)                //500HZ in 12T MODE

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


  16. void UART_init(void);
  17. void LED_Disp_Seg7(void);
  18. void ADC_init(unsigned char channel);
  19. void T0_init(void);
  20. void sendDataToProcessing(char symbol, int dat);
  21. void delay(unsigned int n);
  22. void UART_send(char dat);

  23. unsigned char PulsePin = 0;       // Pulse Sensor purple wire connected to analog pin 0(P1.0为AD口)
  24. unsigned char PulsePin2 = 2;
  25. //sbit blinkPin = P2^0;                // pin to blink led at each beat
  26. //sbit fadePin = P2^3;                  // pin to do fancy classy fading blink at each beat
  27. //sbit led1 = P2^1;
  28. //sbit led2 = P2^2;
  29. int fadeRate = 0;                 // used to fade LED on with PWM on fadePin


  30. // these variables are volatile because they are used during the interrupt service routine!
  31. volatile unsigned int BPM;                   // used to hold the pulse rate
  32. volatile unsigned int Signal;                // holds the incoming raw data
  33. volatile unsigned int IBI = 600;             // holds the time between beats, must be seeded!
  34. volatile bit Pulse = false;     // true when pulse wave is high, false when it's low
  35. volatile bit QS = false;        // becomes true when Arduoino finds a beat.
  36. volatile int rate[10];                    // array to hold last ten IBI values
  37. volatile unsigned long sampleCounter = 0;          // used to determine pulse timing
  38. volatile unsigned long lastBeatTime = 0;           // used to find IBI
  39. volatile int Peak =512;                      // used to find peak in pulse wave, seeded
  40. volatile int Trough = 512;                     // used to find trough in pulse wave, seeded
  41. volatile int thresh = 512;                // used to find instant moment of heart beat, seeded
  42. volatile int amp = 100;                   // used to hold amplitude of pulse waveform, seeded
  43. volatile bit firstBeat = true;        // used to seed rate array so we startup with reasonable BPM
  44. volatile bit secondBeat = false;      // used to seed rate array so we startup with reasonable BPM
  45. static unsigned char order=0;

  46. void sys_init()
  47. {
  48.   //pinMode(blinkPin,OUTPUT);         // pin that will blink to your heartbeat!
  49. // pinMode(fadePin,OUTPUT);          // pin that will fade to your heartbeat!
  50.   UART_init();             // we agree to talk fast!
  51.         ADC_init(PulsePin);
  52.         ADC_init(PulsePin2);
  53.   T0_init();                 // sets up to read Pulse Sensor signal every 2mS  
  54. }

  55. void main(void)
  56. {
  57.   sys_init();
  58.         while(1)
  59.         {
  60.                 sendDataToProcessing('S', Signal);     // send Processing the raw Pulse Sensor data
  61.                 if (QS == true){                       // Quantified Self flag is true when arduino finds a heartbeat
  62.                                         fadeRate = 255;                  // Set 'fadeRate' Variable to 255 to fade LED with pulse
  63.                         //                sendDataToProcessing('B',BPM);   // send heart rate with a 'B' prefix
  64.                                         sendDataToProcessing('Q',IBI);   // send time between beats with a 'Q' prefix
  65.                                         QS = false;                      // reset the Quantified Self flag for next time   
  66.                          }

  67.   //ledFadeToBeat();

  68.   delay(138);                             //  take a break 19.6ms

  69. }
  70. }


  71. //void ledFadeToBeat(){
  72.    // fadeRate -= 15;                         //  set LED fade value
  73.    // fadeRate = constrain(fadeRate,0,255);   //  keep LED fade value from going into negative numbers!
  74.   // analogWrite(fadePin,fadeRate);          //  fade LED
  75.   //}


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

  80. void UART_init(void)
  81. {
  82.          PCON &= 0x7f;  //波特率不倍速
  83.    SCON = 0x50;  //8位数据,可变波特率
  84.    BRT = 0xFD;    //独立波特率产生器初值
  85.    AUXR |= 0x04;  //时钟设置为1T模式
  86.    AUXR |= 0x01;  //选择独立波特率产生器
  87.    AUXR |= 0x10;  //启动波特率产生
  88. }
  89. char putchar(unsigned char dat)
  90. {
  91.         TI=0;
  92.         SBUF=dat;
  93.         while(!TI);
  94.         TI=0;
  95.         
  96.         return SBUF;
  97. }
  98. void delay(unsigned int n)
  99. {
  100.         unsigned int i,j;
  101.         for(i=0;i<n;i++)
  102.                 for(j=0;j<100;j++);
  103. }



  104. void T0_init(void){     
  105.   // Initializes Timer0 to throw an interrupt every 2mS.
  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 ADC_init(unsigned char channel)
  114. {
  115.         P1ASF=ADC_MASK<<channel;        //enable PlusePin as ADC INPUT
  116.         ADC_RES=0;        //clear former ADC result
  117.         ADC_RESL=0;        //clear former ADC result
  118.         AUXR1 |= 0x04;        //adjust the format of ADC result
  119.         ADC_CONTR=channel|ADC_POWER|ADC_SPEEDLL|ADC_START;        //power on ADC and start conversion
  120. }

  121. unsigned int analogRead(unsigned char channel)
  122. {
  123.         unsigned int result;

  124.         ADC_CONTR &=!ADC_FLAG;        //clear ADC FLAG
  125.         result=ADC_RES;
  126.         result=result<<8;
  127.         result+=ADC_RESL;
  128.         ADC_CONTR|=channel|ADC_POWER|ADC_SPEEDLL|ADC_START;
  129.         return result;
  130. }
  131. // Timer 0中断子程序,每2MS中断一次,读取AD值,计算心率值
  132. void Timer0_rountine(void) interrupt 1
  133. {                       
  134.   int N;
  135.         unsigned char i;
  136.         // keep a running total of the last 10 IBI values
  137.   unsigned int runningTotal = 0;                  // clear the runningTotal variable   

  138.         EA=0;                                      // disable interrupts while we do this
  139.         TL0=T0MS;
  140.         TH0=T0MS>>8;                                //reload 16 bit TIMER0
  141. // Signal = analogRead(PulsePin);     
  142.   Signal = analogRead(PulsePin2);          // read the Pulse Sensor
  143.   sampleCounter += 2;                         // keep track of the time in mS with this variable
  144.   N = sampleCounter - lastBeatTime;       // monitor the time since the last beat to avoid noise


  145.     //  find the peak and trough of the pulse wave
  146.   if(Signal < thresh && N > (IBI/5)*3){       // avoid dichrotic noise by waiting 3/5 of last IBI
  147.     if (Signal < Trough){                        // T is the trough
  148.       Trough = Signal;                         // keep track of lowest point in pulse wave
  149.     }
  150.   }

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

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

  162.       if(secondBeat){                        // if this is the second beat, if secondBeat == TRUE
  163.         secondBeat = false;                  // clear secondBeat flag
  164.         for(i=0; i<=9; i++){             // seed the running total to get a realisitic BPM at startup
  165.           rate[i] = IBI;                     
  166.         }
  167.       }

  168.       if(firstBeat){                         // if it's the first time we found a beat, if firstBeat == TRUE
  169.         firstBeat = false;                   // clear firstBeat flag
  170.         secondBeat = true;                   // set the second beat flag
  171.         EA=1;                               // enable interrupts again
  172.         return;                              // IBI value is unreliable so discard it
  173.       }   



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

  178.       rate[9] = IBI;                          // add the latest IBI to the rate array
  179.       runningTotal += rate[9];                // add the latest IBI to runningTotal
  180.       runningTotal /= 10;                     // average the last 10 IBI values
  181.       BPM = 60000/runningTotal;               // how many beats can fit into a minute? that's BPM!
  182.                         if(BPM>200)BPM=200;                        //限制BPM最高显示值
  183.                         if(BPM<30)BPM=30;                                //限制BPM最低显示值
  184.       QS = true;                              // set Quantified Self flag
  185.       // QS FLAG IS NOT CLEARED INSIDE THIS ISR
  186.     }                       
  187.   }

  188.   if (Signal < thresh && Pulse == true){   // when the values are going down, the beat is over
  189. //    blinkPin=1;            // turn off pin 13 LED
  190.     Pulse = false;                         // reset the Pulse flag so we can do it again
  191.     amp = Peak - Trough;                           // get amplitude of the pulse wave
  192.     thresh = amp/2 + Trough;                    // set thresh at 50% of the amplitude
  193.     Peak = thresh;                            // reset these for next time
  194.     Trough = thresh;
  195.   }

  196.   if (N > 2500){                           // if 2.5 seconds go by without a beat
  197.     thresh = 512;                          // set thresh default
  198.     Peak = 512;                               // set P default
  199.     Trough = 512;                               // set T default

  200.     firstBeat = true;                      // set these to avoid noise
  201.     secondBeat = false;                    // when we get the heartbeat back
  202.                 lastBeatTime = sampleCounter;          // bring the lastBeatTime up to date            
  203.   }

  204.   EA=1;                                   // enable interrupts when youre done!
  205. }// end isr
复制代码
回复

使用道具 举报

ID:584814 发表于 2020-12-23 09:28 | 显示全部楼层
个人理解多通道,可以用不同的口来接多个设备,也可以用总线来接。
传感器,单通道和多通道基础编程的差异不会多大的,看器件说明书。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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