⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ads8343_crc7_pwm_proto_2_h.c

📁 MSP430F1610 generates a sine wave from a look up table using timer interrupt.
💻 C
📖 第 1 页 / 共 2 页
字号:
  byte3 = byte3 >> 7;              
  adc_data[0] = 0;
  adc_data[0] = (byte1 | byte2 | byte3);
  byte0 = 0;
  byte0 = adc_data[0];
//------ Normal code  sends EEG
  if(!Rampa)
  {
    byte0 += 32767; 
    result = (unsigned int) byte0;
  }
//------ Code generating a ramp
  else
  { 
 /*
    result = result + 10;
    if (result >= (32767+1500) )
        result = 32767-1500; 
*/

    result = result + 100;
    if (result >= 65400)
    result = 0; 
  }
}
//==================== make_PACKET ( ) ==================================
void make_PACKET(void)
{ 
  unsigned int tempo, tempo1, tempo2, index_For, i, PatientCableIn;
  tempo = tempo1 = tempo2 = 0;

  tempo1 = SampleNumber;
  tempo1 = tempo1 & 0x0001;

  if(!tempo1) // First of 2 samples in this packet.
  {   //---- ADC_sample_1_HIGH
      tempo      = result;
      tempo      = tempo >> 8; 
      paquete[2] = (unsigned char)tempo;
      //---- ADC_sample_1_LOW
      tempo      = result;
      tempo     &= 0x00FF;  
      paquete[3] = (unsigned char)tempo;
  }
  else  // 2nd sample of packet arrived => then, packet is stored in BigPaquete
  {
      //---- INDEX BYTE
      paquete[0] = packet_INDEX;
      //---- CONTROL BYTE
      PatientCableIn = P5IN;
      PatientCableIn &= 0x08;	// 0000 0000 0000 1000 Leave only P5.3

      paquete[1] = orden;  // to put in the high nibble multiply by 16.
      /*
      if(PatientCableIn == 0 ) 
	{
          paquete[1] = orden;  // to put in the high nibble multiply by 16.
	}
      else
	{
	  paquete[1] = 7;
	}
      */
      //---- ADC_sample_2_HIGH
      tempo      = result;
      tempo      = tempo >> 8; 
      paquete[4] = (unsigned char)tempo;
      //---- ADC_sample_2_LOW
      tempo      = result;
      tempo     &= 0x00FF;  
      paquete[5] = (unsigned char)tempo;
      
      //---- CRC_16 WORD calculation and storage
      //paquete[0]='1';paquete[1]='2';paquete[2]='3';paquete[3]='4';paquete[4]='5';paquete[5]='6';
      tempo  = CRC16 = 0; 
      for(i=0; i < (PACKETSIZE - 2)  ; i++)
      {  CRC16 = addCRC( CRC16 , paquete[i] );
      }
      tempo2 = CRC16;
              //---- CRC16_HIGH
      tempo      = tempo2;
      tempo      = tempo >> 8; 
      paquete[6] = (unsigned char)tempo;
              //---- CRC16_LOW
      tempo      = tempo2;
      tempo     &= 0x00FF;  
      paquete[7] = (unsigned char)tempo;

      packet_INDEX ++;  //Index ranges from 0 to 255
      if(packet_INDEX > STOP_PACKET_INDEX)
        packet_INDEX = START_PACKET_INDEX;
      index_For=0;
      for(index_For=FIRS_UART_BYTE ; index_For <= LAST_UART_BYTE ; index_For++)
      {    BigPaquete[index_For+((SampleNumber-1) * 4)] = paquete[index_For];
      }
  }//End of "else"
}
//==================== CRC16 ( ) ===================================
unsigned int addCRC( unsigned int CRC, unsigned char b )
{
    return ( CRC >> 8 ) ^ CRCtbl[ ( CRC & 0xFF ) ^ b ];
}
//=======================================================================
//=======================================================================
//=======================  COMMUNICATIONS  ==============================
//=======================================================================
//=======================================================================

//==================== RX_complete ( ) ==================================
void RX_complete(void)
{   do { IFG1 &=~ URXIFG0;
       } while (URXIFG0 & IFG1);      
}
//==================== send_UART0( ) ================================
void send_UART0(void)
{ int k;
  for ( k=FIRS_UART_BYTE ; k <= LAST_UART_BYTE  ; k++)                             
     {   while ((IFG1 & UTXIFG0) != UTXIFG0); //USART0 TX buffer ready?. 
         TXBUF0 = paquete[k]; //TXBUF0 = string[k];
     }
}
//==================== send_UART1( ) ================================
void send_UART1(void)
{ int k; unsigned int temp[4];
  for (k=2;k<4;k++)                             
     {   while ((IFG2 & UTXIFG1) != UTXIFG1);      //USART0 TX buffer ready?. 
         TXBUF1 = temp[k];
     }
}
//=======================================================================
//=======================================================================
//====================== COMFIGURATONS ==================================
//=======================================================================
//=======================================================================

//====================== MUXs_Port4(VOID) ===============================
void MUXs_Port4(void)
{
  unsigned int TEMPO = 0;
 //SENDING THE MUXs CONTROL BYTE.
  //BIT P4.0 IS NOT MODIFIED (IS FOR Z_CURRENT)
  TEMPO =  MuxByte[Signal_State]; //CONTROL BYTE SEARCH
  TEMPO |= 0x01;  // SET TEMPO BIT 0 TO 1 BEFORE "&"
  P4OUT &= TEMPO; // SET THE ZEROS
  TEMPO &= 0xFE;  // SET TEMPO BIT 0 TO 0 BEFORE "OR"
  P4OUT |= TEMPO; // SET THE ONES.
}
//======================= init_vectors() ================================
void init_vectors()
{ //CONTROL BYTES FOR THE ADC  (ALREADY CHECKED)
  ADC_control[0]  = 0x9F;  // CH0: OXIMETER
  ADC_control[1]  = 0xDF;  // CH1: EEG1 (On the processor board)
  ADC_control[2]  = 0xAF;  // CH2: EEG2
  ADC_control[3]  = 0xEF;  // CH3: ECG  1110 1111
  //MULTIPLEXERS CONTROL BYTES   
  MuxByte[0]  = 0xD8;  // ECG1_DATA (CHECKED) 11 011 00 0
  MuxByte[1]  = 0x58;  //original 0x58;  // ECG1_Z+   (CHECKED) 01 01 1 00 0
  MuxByte[2]  = 0x48;  //original 0x48;  // ECG1_Z-   (CHECKED) 01 00 1 00 0
  MuxByte[3]  = 0x60;  //original 0x60;  // ECG1_DRL  (CHECKED) 01 10 0 00 0
}
//==================== Clock Configuration ==============================
void config_MainCLK()
{ int i; 
  BCSCTL1 &= ~XT2OFF;           // XT2on => runing from 8MHz crystal
  do { IFG1 &= ~OFIFG;           // Clear OSCFault flag
       for (i = 0xFF; i > 0; i--);// Time for flag to set
     }
  while ((IFG1 & OFIFG));       // OSCFault flag still set?                
  BCSCTL2 |= SELM_2 + SELS;     // MCLK= SMCLK= XT2 (safe)
}
//==================== Timer A Configuration ============================
void config_Timer_A()
{ CCTL0 = CCIE;                 // CCR0 interrupt enabled
//  CONFIGURATION FOR 32 kHZ
//  CCR0 = TIMER_A_CCR0 - 1;                  //Timer interrrupt every XXX ms
//  TACTL = TASSEL_1 + MC_1;      // ACLK, upmode
//  CONFIGURATION FOR 8MHZ
  CCR0 = TIMER_A_CCR0;                  //Timer interrrupt every XXX ms
  TACTL = TASSEL_2 + MC_1;      // SMCLK, upmode
}
//==================== IO_control_lines( ) ==============================
void IO_control_lines()
{ 
  //******** PORT 1 *******//
  //--- Line Port P1.5,P1.6,P1.7 Configuration
  P1DIR  = 0x7F;     // 0111 1111 : Port 1 as output except P1.7
  P1OUT  = 0xFF;

  //******** PORT 2 *******//
     // (GPIOs) : All. In next design(with Z_Pulses tied to P2.4, this line will be
     //           controlled by TA module to generate PWM. 
  P2DIR  = 0x3F;  // 0011 1111 : P2.6 & P2.7 inputs for button detection.

  //******** PORT 3 *******//
     // GPIOs:  P3.0(ADC_/CS), P3.4 & P3.5(GPIOs to AT91)
     // MODULE: P3.2 & P3.3(SPI), P3.6 & P3.7(UART1 to AT91)
  P3SEL |= 0xCE;     // 0xFE= 1100 1110 => if 1 controlled by module.


  //******** PORT 4 *******//
  P4DIR  = 0xFF;     // 1111 1111 : Port 4 as output.
  //--- SET P4.0 FOR TIMER_B, used as PWM output for Z_current_generation.
  P4SEL |= 0x01;              // 1111 1110 : P4.0 TBx options



  //******* START CONDITIONS ********//

  //--- Set ADC /CS in 1
  P3DIR  = 0xFF;                // Port 3 as output.
  P3OUT |= 0X01;//ADC_CS_1;     // Put P3.0 = 1 (0000 0001)=0x01

  //--- Set the MUXs to START condition
  P4OUT  = MuxByte[ExG_DATA]; // Put P4=0xD8 (1101 1000),


  
/*                              // => EEG1 DATA measurement.
  //--- P5.1 as input for BT LED state detection
  //--- Set P5.2 for BT pairing reset & P5.3 for cable detection: P5.2 output, P5.3 Input
  P5DIR  = 0xF5;    // 1111 0101 : Port 5 as output except P5.3 & P5.1
  P5OUT  = 0xFB;    // 1111 1011  	
*/
}
//==================== Timer B Configuration ============================
void config_Timer_B()
{ 
  TBCCR0 = TIMER_B_COUNTER - 1;   // PWM Period 178
  TBCCTL0 = OUTMOD_4;             // CCR0 toggle mode
  TBCTL = TBSSEL_1 + MC_1;        // ACLK, up mode, START TIMER
}
//================== timer_B_OFF (void) ============================
void timer_B_OFF(void)
{
  TBCTL &= TIMER_B_OFF;
  TBCCTL0 = OUTMOD_0;       // CCR0 OUT mode
}
//==================== set_USART0_SPI( ) ================================
void set_USART0_SPI(void)
{ //Disable P3.4 & P3.5 lines to avoid false pulses USART0 TX line while acquiring ADC.
  P3SEL &= 0xCE;     // 0xCE = 1100 1110 => P3.0, P3.4 & P3.5 as ports
  P3DIR |= 0x31;     // 0x30 = 0011 0001 => P3.0, P3.4 to P3.5 as Outputs
  P3OUT |= 0x31;     // 0x31 = 0011 0001 => P3.0, P3.4 & P3.5 set to 1
  //---------SPI configuration----------------------------------------
  UCTL0  |= 0x01;               // Set SWRST=1 to reset USART0.
  ME1   |= USPIE0;              // Module Enable - SPI
//  U0CTL &= ~SWRST;              // Make sure the RESET bit is off(0)                                   
  //U0CTL = 0x16;               //00010110=0x16:CHAR,SYNC,MM
  U0CTL |= CHAR + SYNC + MM;    // USART0 module operation
     // CHAR = 1 => 8-bit data, SYNC = 1 => SPI selected, MM = 1 => master mode,
     // MSP is the master
  //U0TCTL = 0x33;               //0011 0011=0x33:SSEL0/1,STC,TXEPT(flag)
  U0TCTL |= CKPH + SSEL0 + SSEL1 + STC;// USART0 Tranmit control register
     // CKPH = 1 => clock from 0 to 1 and get sample when rising
     // SSEL0 = 1 & SSEL1 = 1 => SMCLK is used for baud-rate generation
     // STC = 1 => 3-pin SPI mode selected
  U0BR0  = 0x03;  // Divide SMCLK by 4 => transfer clock
  U0BR1  = 0x00;                
  U0MCTL = 0x00;  // Modulation control - not used, ensure all these bits are reset.
  U0CTL &= ~SWRST;              // Make sure the RESET bit is off(0)(Put bit in 0)
  //--- Set ADC /CS in 1
  P3OUT |= ADC_CS_1;           // Put P3.0 = 1 (0000 0001)=0x01
}
//==================== set_USART0_UART () ===============================
void set_USART0_UART()
{
  P3SEL |= 0x30;     // 0x30 = 0011 0000 => P3.4 to P3.5 as Module
  UCTL0  |= 0x01;               // Set SWRST=1 to reset USART0.
  ME1 |= UTXE0 + URXE0;         // Enable USART0 TXD/RXD.
  UCTL0 = 0x10;                // 8-bit character and UART mode 0001 0000=0x10.
  UTCTL0 = 0x31;              // UCLK = SMCLK.  0011 0001
  //Configuration for 115200 bps
  UBR00 = 0x45;               // 8Mhz/115200 = 69.44
  UBR10 = 0x00;               //
  UMCTL0 = 0x2C;                // modulation
  UCTL0 &= ~SWRST;              // Initialize USART state machine  (SWRST=0)
}
//==================== set_USART1_UART( ) ================================
void set_USART1_UART(void)
{
  P3SEL |= 0xC0;     // 0xC0 = 1100 0000 => P3.6,7 = USART1 TXD/RXD
  ME2 |= UTXE1 + URXE1;         // Enable USART1 TXD/RXD
  UCTL1 |= CHAR;                // 8-bit character
  UTCTL1 |= SSEL1;              // UCLK = SMCLK
  //Configuration for 115200 bps
  UBR01 = 0x45;               // 8Mhz/115200 = 69.44
  UBR11 = 0x00;               //
  UMCTL1 = 0x2C;                // modulation
  UCTL1 &= ~SWRST;              // Initialize USART state machine
}
//==================== config_DMA0 () ===================================
void config_DMA()
{
//--------DMA0 CONFIGURATION
  //DMACTL0 = DMA0TSEL_10;                     // 0-UART 1 transmit
  //DMA0SA = (unsigned int)string1;           // Source block address
  DMA0SA = (unsigned int)BigPaquete;           // Source block address
  DMA0DA = (int)&U1TXBUF;                   // Dest single address
  //DMA0SZ = sizeof string1-1;                // Block size
  DMA0SZ = 4 * Nr_Samples;                               // Block size
  DMA0CTL = DMASRCINCR_3 + DMASBDB+ DMALEVEL;// Rpt, inc src, enable

//--------DMA1 CONFIGURATION
  DMA1SA = U1RXBUF_;                        // Src address = UART RX Buffer
  DMA1DA = (unsigned int)commands;          // Dst address = RAM memory
  DMA1SZ = COMMAND_PACKET_LENGHT;           // Size in bytes
  DMA1CTL = DMADT_0 + DMADSTINCR_3 + DMASBDB + DMAEN; // Sng, config

//-------- FOR BOTH DMA CHANNELS:
  DMACTL0 = DMA1TSEL_9 + DMA0TSEL_10;       // DMA1=URXIFG1, DMA0=UTXIFG1
}
//================== PLOT COMMAND RECEIVED
void Plot_Command(void)
{
   if(commands[COMMAND_PACKET_LENGHT-1]!=0)
   {
      DMA1CTL |= DMAEN;
      int k = 0;
      for(k=0 ; k<COMMAND_PACKET_LENGHT ; k++)                             
       {   while ((IFG2 & UTXIFG1) != UTXIFG1);      //USART0 TX buffer ready?. 
           TXBUF1 = commands[k];
           commands[k] = 0;         
       }//End of for
   }//End of " if(commands[COMMAND_PACKET_LENGHT-1]!=0) " 
}

//===================================================
//============= BLUETOOTH ===========================
//===================================================
void BT_StartResetBT(void)
{      if(BT_PairingResetCounter <= 1000)
      {	    BT_PairingResetCounter ++;  
      }
      else if(BT_PairingResetCounter < 1100)
      {	    P5OUT = 0xFF ; // 1111 1111
            BT_PairingResetCounter = 1110;
      }
}
//==================== BT_BTLEDStateDetection() ===================================
void BT_BTLEDStateDetection(void)
{

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -