Пример #1
0
void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;                // Stop the watchdog
    __enable_interrupt();                    // Enable general interrupts
    Board_init();                            // Configure's the F5529 EXP board's I/Os
    
    // Initialize power/clocks for use with USB
    SetVCore(3);                             // The USB module requires that VCore be set to highest setting, independent of MCLK freq
    ClockUSB();
    
    disk_initialize(0);                      // SD-cards must go through a setup sequence after powerup.  This FatFs call does this.  
    USB_init();                              // Initializes the USB API, and prepares the USB module to detect USB insertion/removal events
    USB_setEnabledEvents(kUSB_allUsbEvents); // Enable all USB events
      
    // The data interchange buffer (used when handling SCSI READ/WRITE) is declared by the application, and 
    // registered with the API using this function.  This allows it to be assigned dynamically, giving 
    // the application more control over memory management.  
    USBMSC_registerBufInfo(&RW_dataBuf[0], NULL, sizeof(RW_dataBuf));
 
    
    // The API maintains an instance of the USBMSC_RWbuf_Info structure.  If double-buffering were used, it would
    // maintain one for both the X and Y side.  (This version of the API only supports single-buffering,
    // so only one structure is maintained.)  This is a shared resource between the API and application; the 
    // application must request the pointers.    
    RWbuf_info = USBMSC_fetchInfoStruct();
  
    
    // USBMSC_updateMediaInfo() must be called prior to USB connection.  We check if the card is present, and if so, pull its size
    // and bytes per block.  
    // LUN0
    mediaInfo.mediaPresent = 0x01;        // The medium is present, because internal flash is non-removable.  
    mediaInfo.mediaChanged = 0x00;        // It can't change, because it's in internal memory, which is always present.  
    mediaInfo.writeProtected = 0x00;      // It's not write-protected
    mediaInfo.lastBlockLba = 774;         // 774 blocks in the volume. (This number is also found twice in the volume itself; see mscFseData.c. They should match.)
    mediaInfo.bytesPerBlock = BYTES_PER_BLOCK; // 512 bytes per block. (This number is also found in the volume itself; see mscFseData.c. They should match.)
    USBMSC_updateMediaInfo(0, &mediaInfo); 
    
    // LUN1
    if(detectCard())
      mediaInfo.mediaPresent = kUSBMSC_MEDIA_PRESENT;
    else mediaInfo.mediaPresent = kUSBMSC_MEDIA_NOT_PRESENT;
    mediaInfo.mediaChanged = 0x00;
    mediaInfo.writeProtected = 0x00;
    disk_ioctl(0,GET_SECTOR_COUNT,&mediaInfo.lastBlockLba);   // Returns the number of blocks (sectors) in the media. 
    mediaInfo.bytesPerBlock = BYTES_PER_BLOCK;                // Block size will always be 512
    USBMSC_updateMediaInfo(1, &mediaInfo); 
    
    
    // At compile-time for this demo, there will be one file on the volume.  The root directory and data cluster
    // for this file need to be initialized.  
    //flashWrite_LBA(Root_Dir, (BYTE*)Root_Dir_init); 
    //flashWrite_LBA(Data559, (BYTE*)Data559_init); 
    
    
    // Configure Timer_A0 to prompt detection of the SD card every second
    TA0CCTL0 = CCIE;                          // Enable interrupt
    TA0CCR0 = 32768;                          // Clock will be 32kHz, so we set the int to occur when it counts to 32768
    TA0CTL = TASSEL_1 + MC_1 + TACLR;         // ACLK = 32kHz, up mode, clear TAR... go!

  
    // If USB is already connected when the program starts up, then there won't be a USB_handleVbusOnEvent(). 
    // So we need to check for it, and manually connect if the host is already present.   
    if (USB_connectionInfo() & kUSB_vbusPresent)
    {
      if (USB_enable() == kUSB_succeed)
      {
        USB_reset();
        USB_connect();      
      }
    }
  
    while(1)
    {
      BYTE ReceiveError=0, SendError=0;
      WORD count;
      switch(USB_connectionState())
      {
        case ST_USB_DISCONNECTED:
                 __bis_SR_register(LPM3_bits + GIE); 	       // Enter LPM3 until VBUS-on event
                
                 // Check if the reason we woke was a button press; and if so, log a new piece of data.  
                 if(fS1ButtonEvent)
                 {
                   // Build string
                   char str[14] = "Data entry #0\n";
                   str[12] = logCnt++;                             // Number the entries 0 through....?
                   
                   memcpy(RW_dataBuf, Data559, BYTES_PER_BLOCK);  // Copy data block 559 from flash to RAM buffer
                   memcpy(&RW_dataBuf[DataCnt], str, sizeof(str)); // Write the new entry to the RAM buffer
                   flashWrite_LBA((PBYTE)Data559, RW_dataBuf);    // Copy it back to flash
                   
                   DataCnt += sizeof(str);                         // Increment the index past the new entry
                   if((DataCnt + sizeof(str)>= BYTES_PER_BLOCK))   // Roll index back to 0, if no more room in the block
                     DataCnt = 0;
        
                   fS1ButtonEvent = 0;
                 }
                break;
          
        case ST_USB_CONNECTED_NO_ENUM:
                break;
          
        case ST_ENUM_ACTIVE:             

                // Call USBMSC_poll() to initiate handling of any received SCSI commands.  Disable interrupts 
                // during this function, to avoid conflicts arising from SCSI commands being received from the host
                // AFTER decision to enter LPM is made, but BEFORE it's actually entered (in other words, avoid
                // sleeping accidentally).  
                __disable_interrupt();
                if((USBMSC_poll() == kUSBMSC_okToSleep) && (!bDetectCard))
                {
                    __bis_SR_register(LPM0_bits + GIE);  // Enable interrupts atomically with LPM0 entry
                }
                __enable_interrupt();
                
                                               
                // If the API needs the application to process a buffer, it will keep the CPU awake by returning kUSBMSC_processBuffer
                // from USBMSC_poll().  The application should then check the 'operation' field of all defined USBMSC_RWbuf_Info
                // structure instances.  If any of them is non-null, then an operation needs to be processed.  A value of 
                // kUSBMSC_READ indicates the API is waiting for the application to fetch data from the storage volume, in response 
                // to a SCSI READ command from the USB host.  After the application does this, it must indicate whether the 
                // operation succeeded, and then close the buffer operation by calling USBMSC_bufferProcessed().  
                
                while(RWbuf_info->operation == kUSBMSC_READ)
                {
                  switch(RWbuf_info->lun)
                  {
                    case 0:
                      RWbuf_info->returnCode = Read_LBA(RWbuf_info->lba, RWbuf_info->bufferAddr, RWbuf_info->lbCount); // Fetch a block from the medium, using file system emulation
                      USBMSC_bufferProcessed();                           // Close the buffer operation
                      break;
                      
                    case 1:
                      read_LUN1();
                      break;
                  }
                }
                
                // Everything in this section is analogous to READs.  Reference the comments above.   
                while(RWbuf_info->operation == kUSBMSC_WRITE)
                {                  
                  switch(RWbuf_info->lun)
                  {
                    case 0:
                      RWbuf_info->returnCode = Write_LBA(RWbuf_info->lba, RWbuf_info->bufferAddr, RWbuf_info->lbCount); // Write the block to the medium, using file system emulation
                      USBMSC_bufferProcessed();                            // Close the buffer operation
                      break;
                    case 1:
                      write_LUN1();
                      break;
                  }
                }
                
                // Every second, the Timer_A ISR sets this flag.  The checking can't be done from within the timer ISR, because the
                // checking enables interrupts, and this is not a recommended practice due to the risk of nested interrupts.  
                if(bDetectCard)
                {
                  checkInsertionRemoval();
                  bDetectCard = 0x00;                          // Clear the flag, until the next timer ISR
                }
                
                if(bHID_DataReceived_event) //Message is received from HID application
                {
                	bHID_DataReceived_event = FALSE;          // Clear flag early -- just in case execution breaks below because of an error
                	count = hidReceiveDataInBuffer((BYTE*)dataBuffer,BUFFER_SIZE,HID0_INTFNUM);
                	strncat(wholeString," \r\nRx->",7);
                    strncat(wholeString,(char*)dataBuffer,count);
                  	strncat(wholeString," \r\n ",4);
                	if(cdcSendDataInBackground((BYTE*)wholeString,strlen(wholeString),CDC0_INTFNUM,1)) // Send message to other CDC App
                	{
                		SendError = 0x01;
                		break;
                	}
                	memset(wholeString,0,MAX_STR_LENGTH);           // Clear wholeString
                }
                if(bCDC_DataReceived_event)  //Message is received from CDC application 
                {
                  bCDC_DataReceived_event = FALSE;                  // Clear flag early -- just in case execution breaks below because of an error
                  cdcReceiveDataInBuffer((BYTE*)wholeString,MAX_STR_LENGTH,CDC0_INTFNUM);
                  ASCII(wholeString);
                  if(hidSendDataInBackground((BYTE*)wholeString,strlen(wholeString),HID0_INTFNUM,1))  // Send message to HID App
                  {
                    SendError = 0x01;                          // Something went wrong -- exit
                    break;
                  }
                  memset(wholeString,0,MAX_STR_LENGTH);  // Clear wholeString
                }
                                               
                break;
                
            case ST_ENUM_SUSPENDED:
                __bis_SR_register(LPM3_bits + GIE);            // Enter LPM3, until a resume or VBUS-off event
                break;
              
          case ST_ENUM_IN_PROGRESS:
                break;
              
          case ST_ERROR:
                break;           
          default:;
      }
      if(ReceiveError || SendError)
      { 
          //TO DO: User can place code here to handle error
      }    
    }
}
Пример #2
0
void BP(int transmit_mode){
	USE_LCD();

	_DINT();
	buttonsPressed=0;

    Init_UART2();
    UCA0IE |= UCRXIE;



	int ad_f ;
	ad_f = 0;
    SPILCD_Clear(WHITE);
    drawbpletter();   //draw 'BLOOD PRESURE'

//    PMM_setVCore(PMM_BASE, PMM_CORE_LEVEL_3);
    initClocks(20000000);   // Config clocks. MCLK=SMCLK=FLL=8MHz; ACLK=REFO=32kHz

    int_adc12();
    //bcUartInit();          // Init the back-channel UART
//    USB_setup(TRUE,TRUE);  // Init USB; if a USB host (PC) is present, connect

    n=0;xaxis=0;flag=0;xx=0;mm=0;t1=0;pkn=0;section=0;x=0;
//   	int_adc12();//采样设置
    ADC12CTL0 |= ADC12SC;//启动采样
    ADC12CTL0 |= ADC12ENC+ADC12ON;
    ad_f = 0;
   	_EINT();//允许中断

  int_aerate();  //启动气阀自动充放气
   	 	//section=1;
  //int_adc12();
  while(n<500);
 // TA2CCR2 = 600;
  while(n<1000);
//  TA2CCR2 = 350;
  while(n<1400){           //等待采1500个样点
       // _EINT();
//__bis_SR_register(LPM0_bits + GIE); //低功耗,使能全局中断
 		_NOP();

 	}
 //	TA2CCR2 = 200;
 	while(n<1500);
 	_DINT();
 	ADC12IE &= ~0x02;
 	ADC12CTL0 &=~ ADC12ENC;      //停止转换
 	TA2CCR2 =0;
 	smooth();       //滤波
 	int_findpk();//找到脉搏波峰峰值
 	findBP();  //计算并显示血压


 	_EINT();


//    long a;
//    char b[5];
//    char c[3];
//    char cc[3];
/*    ltoa(temp_final1,b);
    if(temp_final1>=0&&temp_final1<=9)
    {
    c[0]='0';
    c[1]='0';
    c[2]=b[0];
    }
    if(temp_final1>=10&&temp_final1<=99)
    {
    c[0]='0';
    c[1]=b[0];
    c[2]=b[1];
    }
    if(temp_final1>=100&&temp_final1<=999)
    {
    c[0]=b[0];
    c[1]=b[1];
    c[2]=b[2];
    }

    ltoa(temp_final2,b);
    if(temp_final2>=0&&temp_final2<=9)
    {
    cc[0]='0';
    cc[1]='0';
    cc[2]=b[0];
    }
    if(temp_final2>=10&&temp_final2<=99)
    {
    cc[0]='0';
    cc[1]=b[0];
    cc[2]=b[1];
    }
    if(temp_final2>=100&&temp_final2<=999)
    {
    cc[0]=b[0];
    cc[1]=b[1];
    cc[2]=b[2];
    }
*/
    //char end[3]={'E','N','D'};
    int i;

      /*
       */
//    CRCresult=0;
           for(i=1508;i>=9;i--){
               results1[i]=results1[i-9]>>5;
           }
//           for(i=9;i<1509;i++)
//           {
//               CRCtmp = CRCresult%256;
//               CRCindex = CRCtmp^results1[i];
//               CRCtmp = CRCresult/256;   //除以256
//               CRCresult = CRCtmp;
//               CRCresult = CRCresult^CRC_TA[CRCindex];
//           }
//           a=CRCresult;
//           ltoa(a,b);
           results1[0]='S';
           results1[1]='T';
           results1[2]='A';
           results1[3]='R';
           results1[4]='T';
           results1[5]='B';
           results1[6]='P';
           if(sh>=127){
           results1[7]=0x7F;
           results1[8]=(char)(sh-127);}
           else{
         	  results1[7]=(char)(sh);
         	  results1[8]=0;
           }
           if(re>=127){
           results1[9]=0x7F;
           results1[10]=(char)(re-127);}
           else{
         	  results1[9]=(char)(re);
         	  results1[10]=0;
           }
           results1[1511]='A';
           results1[1512]='A';
           results1[1513]='A';
           results1[1514]='A';
           results1[1515]='A';
           results1[1516]='E';
           results1[1517]='N';
           results1[1518]='D';

    if(transmit_mode==1){
    	bcUartInit();
        bcUartSend_char(results1,1519);//buf_bcuartToUsb
    }


    if(transmit_mode==2){
           hidSendDataInBackground(results1,3038, HID0_INTFNUM,10000);
    }


    if(transmit_mode==3){
    	Init_UART2();

        	 int i;
        	 for(i=0;i<1519;i++)
        	 {
                 UCA0TXBUF = results1[i];

                 while(!(UCTXIFG==(UCTXIFG & UCA0IFG))&&((UCA0STAT & UCBUSY)==UCBUSY));

                 int a,k;
                 for(a=0;a<10;a++)
                 {
                      for(k=0;k<80;k++);
                 }
        	 }
         }

    __no_operation();                       // For debugger

    _EINT();

    Init_UART2();
    UCA0IE |= UCRXIE;

    buttonsPressed=0;
    BUTTON_S4=0;
    while(!(buttonsPressed & BUTTON_S4));
 /***************UART**************************/
 //	 rxByteCount = 1500;//bcUartReceiveBytesInBuffer(buf_bcuartToUsb);
 	// bcUartSend(results1,rxByteCount);//buf_bcuartToUsb
 	 //bcUartSend(results,rxByteCount);
/***************USB**************************/
 /*	 for(i=0;i<1500;i++){
 		 convertTwoDigBinToASCII(results1, str1);
 		 __delay_cycles(110000);
 		 hidSendDataInBackground(str1,5, HID0_INTFNUM,0);
 	 }
 	for(i=0;i<1500;i++){
 	 		 convertTwoDigBinToASCII(results, str1);
 	 		 __delay_cycles(110000);
 	 		 hidSendDataInBackground(str1,5, HID0_INTFNUM,0);
 	 	 }*/
/***************BlueTooth**************************//*
 	long a;
 	char b[20];
 	char c[2];
 	int bb;
 	Init_UART1();
 	for(i=0;i<1500;i++){
 		a=(long)results1[i]/40;
 		if(a>=0&&a<=9){
 			ltoa(a,b);
 			c[0]='0';
 			c[1]=b[0];
 		}
 		 if(a>=10&&a<=99){
 			 ltoa(a,b);
 			 c[0]=b[0];
 			 c[1]=b[1];
 		 }
 		 for(bb=0;bb<2;bb++){
 		 degc=c[bb];
 		UCA0IE |= UCTXIE;
 		for(i=0;i<500;i++){
 			for(j=0;j<100;j++);
 		}
 		 }
 	}
 	*/
}
Пример #3
0
/*----------------------------------------------------------------------------+
| Main Routine                                                                |
+----------------------------------------------------------------------------*/
VOID main(VOID)
{
    WDTCTL = WDTPW + WDTHOLD;	    // Stop watchdog timer
    Init_StartUp();
    USB_init();
    
    USB_setEnabledEvents(kUSB_VbusOnEvent + kUSB_VbusOffEvent + kUSB_UsbSuspendEvent + kUSB_UsbResumeEvent + kUSB_receiveCompletedEvent + kUSB_UsbResetEvent);

    // Check if we're already physically attached to USB, and if so, connect to it
    // This is the same function that gets called automatically when VBUS gets attached.  
    if (USB_connectionInfo() & kUSB_vbusPresent)
      USB_handleVbusOnEvent();
                 
    while(1)
    {
        switch(USB_connectionState())
        {
           case ST_USB_DISCONNECTED:
                 __bis_SR_register(LPM3_bits + GIE); 	  // Enter LPM3 w/interrupt
                break;
                
           case ST_USB_CONNECTED_NO_ENUM:
                break;
                
           case ST_ENUM_ACTIVE:
                if(!bCommandBeingProcessed)                  // If no command is being processed, then make sure there's a rcv operation 
                {                                            // open to receive the start of the "packet"
                  if(!(USBHID_intfStatus(0,&x,&y) & kUSBHID_waitingForReceive))      // Only open it if we haven't already done so
                    if(USBHID_receiveData(buffer,1,0) == kUSBHID_busNotAvailable) // Start a receive operation for a single byte -- the "size" byte of the "packet"
                    {
                      USBHID_abortReceive(&x,0);                                     // Abort receive
                      break;                                 // If bus is no longer available, escape out of the loop
                    }
                }
                __bis_SR_register(LPM0_bits + GIE);          // Wait in LPM0 until a receive operation has completed
                
                if(bDataReceiveCompleted_event)
                {
                  bDataReceiveCompleted_event = FALSE;
                  if(!bCommandBeingProcessed)                // This means that the incoming byte is the start of the "packet" -- the "size" byte
                  {
                     if ((buffer[0]>=0x31) &&  (buffer[0]<= 0x39))
                    {
                        size = buffer[0]-0x30;                   // It's in ASCII, so convert it to a number
                        
                        if(USBHID_receiveData(buffer,size,0) == kUSBHID_busNotAvailable)  // And then open a rcv operation for that size
                        {
                          USBHID_abortReceive(&x,0);                                      // Abort receive
                          break;                                 // If bus is no longer available, escape out of the loop
                        }
                        bCommandBeingProcessed = TRUE;           // Now we're waiting for the "data" part of the "packet"
                    }
                    
                    else 
                    {
                       strcpy(outString,"\r\nEnter a valid number between 1 and 9\r\n\r\n");     // Prepare the outgoing string
                       if(hidSendDataInBackground((BYTE*)outString,strlen(outString),0,0)) // Send the response over USB
                       {
                          USBHID_abortSend(&x,0);                                         // Operation may still be open; cancel it
                          break;                                                          // If the send fails, escape the main loop
                        }
                        bCommandBeingProcessed = FALSE;                                   // Now we're back to waiting for the "size" byte
                    }
                  }

                  else                                       // This means that the incoming data is the "data" part of the "packet"
                  {
                    strcpy(outString,"\r\nI received your packet with size of ");     // Prepare the outgoing string
                    c[0] = (char)(size+0x30);                                    // Convert the size back to ASCII
                    c[1] = 0;                                    // Convert the size back to ASCII
                    outString[64] = 0; 
                    strcat(outString,c);
                    strcat(outString," bytes.\r\n\r\n");                              
                    if(hidSendDataInBackground((BYTE*)outString,strlen(outString),0,0)) // Send the response over USB
                    {
                      USBHID_abortSend(&x,0);                                         // Operation may still be open; cancel it
                      break;                                                          // If the send fails, escape the main loop
                    }
                    bCommandBeingProcessed = FALSE;                                   // Now we're back to waiting for the "size" byte
                  }
                }
                break;
                
           case ST_ENUM_SUSPENDED:
                __bis_SR_register(LPM3_bits + GIE); 	// Enter LPM3 w/interrupt
                break;
           
          case ST_ENUM_IN_PROGRESS:
                break;
                
           case ST_NOENUM_SUSPENDED:
                __bis_SR_register(LPM3_bits + GIE);                
                break;                
           case ST_ERROR:
                _NOP();
                break;
                
           default:;
        }
    }  // while(1) 
} //main()
Пример #4
0
void SPO2H(int transmit_mode)
{
	USE_LCD();
	SpO2_lcd();
    int_adc();

    Init_UART2();
    UCA0IE |= UCRXIE;

    //PMM_setVCore(PMM_BASE, PMM_CORE_LEVEL_2);
    initClocks(20000000);   // Config clocks. MCLK=SMCLK=FLL=8MHz; ACLK=REFO=32kHz
    delay_2();
    delay_2();
//    USB_setup(TRUE,TRUE);
    BUTTON_S4=0;
    while(!(buttonsPressed & BUTTON_S4))
    {
        Init_UART2();
        UCA0IE |= UCRXIE;
        _EINT();
       // delay_2();
      //  delay_2();
        ad();
        if(BUTTON_S4==0){
        on_ired();
        wave(1);    //显示红光
      /*  for(i=0;i<480;i++){
            results[i+480]=results[i];
        }*/
        NONUSE_LCD();
        save();  //      前0-480为红外 1000-1480为
       // delay_2();
  //      delay_2();
        ad();
        if(BUTTON_S4==0){
        on_red();
        wave(0);    //显示红外
       unsigned int count=0;   //ired
       unsigned int count1=0;  //red
       unsigned int count_spa=0;
       unsigned int count1_spa=0;
       //调试时 使用

       int red_hr=0;
       int hr = 0; //心率
       unsigned int j = 0, j1 = 0;


       for(i = 9; i < max-10 ;i++)  //ired
       {
           if(results[i]>=results[i+1]&&results[i]>=results[i+2]&&results[i]>=results[i+3]&&results[i]>=results[i-1]&&results[i]>=results[i-2]&&results[i]>=results[i-3]&&j<4&&
                   results[i]>=results[i+4]&&results[i]>=results[i+5]&&results[i]>=results[i+6]&&results[i]>=results[i-4]&&results[i]>=results[i-5]&&results[i]>=results[i-6]&&
                   results[i]>=results[i+7]&&results[i]>=results[i+8]&&results[i]>=results[i+9]&&results[i]>=results[i-7]&&results[i]>=results[i-8]&&results[i]>=results[i-9])
           {
               r_locate[count] = i;
               count++;
               if(count>=10)break;
               i = i+40;
           }

       }

       for(i = 9+480; i < max-10+480 ;i++)  //red
       {
           if(results[i]>=results[i+1]&&results[i]>=results[i+2]&&results[i]>=results[i+3]&&results[i]>=results[i-1]&&results[i]>=results[i-2]&&results[i]>=results[i-3]&&j1<4&&
                   results[i]>=results[i+4]&&results[i]>=results[i+5]&&results[i]>=results[i+6]&&results[i]>=results[i-4]&&results[i]>=results[i-5]&&results[i]>=results[i-6]&&
                   results[i]>=results[i+7]&&results[i]>=results[i+8]&&results[i]>=results[i+9]&&results[i]>=results[i-7]&&results[i]>=results[i-8]&&results[i]>=results[i-9])
           {
               r1_locate[count1] = i;
               count1++;
               if(count1>=10)break;
               i=i+40;
           }
       }

       /*计算心率 其实最后一个峰值坐标减去第一个 再除以个数就可以了哦*/
       count_spa = count - 1;
       count1_spa = count1 - 1;

      hr = (r_locate[count_spa]-r_locate[0])/count_spa;
      hr=(1.0/(float)(hr*0.01))*60;

      red_hr = (r1_locate[count1_spa]-r1_locate[0])/count1_spa;
      red_hr=(1.0/(float)(red_hr*0.01))*60;  //


      /*直流算平均值*/
      red_dc = 0.0;
      ired_dc = 0.0;
      length_idc = (float)(r_locate[count_spa]-r_locate[count_spa -2]);    //ired
      length_dc = (float)(r1_locate[count1_spa] - r1_locate[count1_spa -2]);
      for(i = r1_locate[count1_spa -2] ; i <r1_locate[count1_spa];i++)    //最后的位置是采样到最后一个峰峰值的坐标
      {
          red_dc += (float)results1[i]/length_dc;
      }
      for(i = r_locate[count_spa -2] ; i <r_locate[count_spa];i++)    //最后的位置是采样到最后一个峰峰值的坐标
      {
          ired_dc += (float)results1[i]/length_idc;
      }

      /*最小值及交流峰峰值 最后2个周期才稳定 可以用最后2个周期算峰峰值*/
      /*同时 红光用前一个峰值减去谷值 红外用后一个峰值减去谷值 去基线漂移*/
       red_min = results[1+480];//(float)red[0];
       ired_min = results[1];//(float)results[0];  避免第一个数采得不准
       ired_ac = 0;
       red_ac = 0;

       for(i =( count_spa -2); i <count_spa ;i++)  //ired
       {
           for(j=r_locate[i]; j < r_locate[i+1] ;j++)
           {
               if(results[j]<ired_min){ired_min=results[j];}
           }
           ired_ac += results[r_locate[i+1]] - ired_min;
           nr_locate[i]=ired_min;
           ired_min = results[1]; //更新初值
       }
       ired_ac = ired_ac/2;

       for(i = (count1_spa -2); i <count1_spa ;i++)  //red
       {
           for(j=r1_locate[i]; j < r1_locate[i+1] ;j++)
           {
               if(results[j]<red_min){red_min=results[j];}
           }
           red_ac += results[r1_locate[i]] - red_min;
           nr1_locate[i] = red_min;
           red_min = results[481];
       }
       red_ac = red_ac/2;


       /*计算血氧含量*/
       Q1 = (float)(red_ac/red_dc);
       Q2 = (float)ired_ac/(float)ired_dc ;
       Q = Q1/Q2 ;
       sao2 = (-4.1768)*Q + 100.9352 + 0.5;

       if(abs(hr-red_hr)>=10)                  //两次采样的心率值相差不大的情况下才更新Q
       {
           Q = pre_Q;
       }

       /*显示*/
       USE_LCD();
       SPILCD_Clear_Lim(212,260,18,52,WHITE); //371 355
       SPILCD_Clear_Lim(348,396,18,52,WHITE);
       unsigned int temp1,temp10,temp100;
       long temp_final1;
       long temp_final2;

       temp1=hr%10;             //计算心率个位
       temp10=(hr%100-temp1)/10;    //计算心率十位
       temp100=(hr-temp10*10-temp1)/100;    //计算心率百位
       temp_final1=(long)(temp1+temp10*10+temp100*100);
       if(temp100>0)    DRAW_NUM(244,20,temp100,BLUE);  //显示心率百位
       DRAW_NUM(228,18,temp10,BLUE);         //显示心率十位
       DRAW_NUM(212,18,temp1,BLUE);       //显示心率个位

       sao2=99;
       temp1=sao2%10;                  //计算个位
       temp10=(sao2%100-temp1)/10;    //计算十位
       temp100=(sao2-temp10*10-temp1)/100;    //计算百位
       temp_final2=(long)(temp1+temp10*10+temp100*100);
       if(temp100>0)    DRAW_NUM(380,18,temp100,BLUE);  //显示心率百位
       DRAW_NUM(364,18,temp10,BLUE);  //显示十位
       DRAW_NUM(348,18,temp1,BLUE);    //显示个位
       NONUSE_LCD();
       i = 0;

       pre_Q = Q;   //记录这次显示的Q

  /*     char start[7]={'S','T','A','R','T','S','O'};
       long a;
       char b[5];
       char c[3];
       char d[3];
       ltoa(temp_final1,b);
       if(temp_final1>=0&&temp_final1<=9)
       {
       c[0]='0';
       c[1]='0';
       c[2]=b[0];
       }
       if(temp_final1>=10&&temp_final1<=99)
       {
       c[0]='0';
       c[1]=b[0];
       c[2]=b[1];
       }
       if(temp_final1>=100&&temp_final1<=999)
       {
       c[0]=b[0];
       c[1]=b[1];
       c[2]=b[2];
       }

       ltoa(temp_final2,b);
       if(temp_final2>=0&&temp_final2<=9)
       {
       d[0]='0';
       d[1]='0';
       d[2]=b[0];
       }
       if(temp_final2>=10&&temp_final2<=99)
       {
       d[0]='0';
       d[1]=b[0];
       d[2]=b[1];
       }
       if(temp_final2>=100&&temp_final2<=999)
       {
       d[0]=b[0];
       d[1]=b[1];
       d[2]=b[2];
       }

       char end[3]={'E','N','D'};
       j=0;
       for(j=0;j<960;j++)        //采样数据除以40转化成两位,便于传送
           {
           results[j]=results[j]/40;
           }*/
       int i;
       for(i=959;i>=0;i--)
       {
           results[i+10]=results[i]>>5;
       }
       results[0]='S';
       results[1]='T';
       results[2]='A';
       results[3]='R';
       results[4]='T';
       results[5]='S';
       results[6]='O';
       if(hr>=127){
       results[7]=0x7F;
       results[8]=(char)(hr-127);}
       else{
     	  results[7]=(char)(hr);
     	  results[8]=0;
       }
       results[9]=sao2;
       results[970]='A';
       results[971]='A';
       results[972]='A';
       results[973]='A';
       results[974]='A';
//       results[974]=0x7F;
       results[975]='E';
       results[976]='N';
       results[977]='D';
//       CRCresult=0;
//       for(i=9;i<969;i++)
//       {
//           results[i]=results[i]>>5;
//           CRCtmp = CRCresult%256;
//           CRCindex = CRCtmp^results[i];
//           CRCtmp = CRCresult/256;   //除以256
//           CRCresult = CRCtmp;
//           CRCresult = CRCresult^CRC_TA[CRCindex];
//       }
//       long a;
//       char b[5];
//       a=CRCresult;
//       ltoa(a,b);
//       results[969]=b[0];
//       results[970]=b[1];
//       results[971]=b[2];
//       results[972]=b[3];
//       results[973]=b[4];
//       results[974]='E';
//       results[975]='N';
//       results[976]='D';



       if(transmit_mode==1){
           bcUartInit();
           bcUartSend_char(results,978);//buf_bcuartToUsb
       }


       if(transmit_mode==2){
           hidSendDataInBackground(results,1956, HID0_INTFNUM,10000);
       }


        if(transmit_mode==3){
      	 int i;
      	 for(i=0;i<978;i++)
      	 {
               UCA0TXBUF = results[i];

               while(!(UCTXIFG==(UCTXIFG & UCA0IFG))&&((UCA0STAT & UCBUSY)==UCBUSY));

               int a,k;
               for(a=0;a<10;a++)
               {
                    for(k=0;k<80;k++);
               }
      	 }
       }

	   BUTTON_S4=0;
	   buttonsPressed=0;
	   NONUSE_LCD();
        }
        }