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

📄 interfacing spi adcs to msp430f449.c

📁 TLV2541, TLC2551,TLC3541,TLC4541与MSP430接口的SPI程序。
💻 C
📖 第 1 页 / 共 2 页
字号:
P3DIR |= CS;                        // Set the CS bit as an output
P3OUT = CS;                         // De-assert CS for the adc - HIGH 

}

/************************************************************/
/* Prototype - setupSPI                                     */
/*                                                          */
/* !NA setupSPI                                             */
/* !LA ANSI C                                               */
/* !PI *                                                    */
/* !PO *                                                    */
/* !LV *                                                    */
/*  Description                                             */
/*  This prototype sets-up the P3 for communication via SPI */
/************************************************************/
    
void setupSPI (void)

/************************************************************/
/* System definitions                                       */
/************************************************************/

#define     SPIen       0x40
{
    
ME1 |= SPIen;                       // Module Enable - SPI
                                    // 
U0CTL &= ~SWRST;                    // Make sure the RESET bit is off                                   
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 |= SSEL0 + SSEL1 + STC;      // USART0 Tranmit control register
                                    // SSEL0 = 1 & SSEL1 = 1
                                    // => SMCLK is used for baud-rate generation
                                    // STC = 1 => 3-pin SPI mode selected
U0BR0  = 0x02;                      // Divide SMCLK by 4 => transfer clock
U0BR1  = 0x00;                      //
U0MCTL = 0x00;                      // Modulation control - not used. Ensure 
                                    // all these bits are reset                                                           
}

/************************************************************/
/* Prototype - init_adc                                     */
/*                                                          */
/* !NA init_adc                                             */
/* !LA ANSI C                                               */
/* !PI *                                                    */
/* !PO *                                                    */
/* !LV *                                                    */
/*  Description                                             */
/*  This prototype sets-up the TLV2553 ADC                  */
/************************************************************/ 
     
void init_adc (void)
{     
int i;
unsigned int trash;

U0TXBUF = 0x00;                     // Start SCLK to ADC
complete();                          
trash = U0RXBUF;
				    // Reset the adc
			    
P3OUT &= ~CS;                       //  Assert CS* LOW
      U0TXBUF = 0x00;               //  Start SCLK
      				    //	4SCLK =< CS* < 8SCLK
      for (i=0; i< 1; i++); 	    //  This depends on the Baud rate
				    //  the user sets. 
				    //  May be need to be altered.
P3OUT |= CS; 			    //  De-assert CS* HIGH

trash = U0RXBUF;                    // De-assert CS HIGH

}

/************************************************************/
/* Prototype - complete                                     */
/*                                                          */
/* !NA complete                                             */
/* !LA ANSI C                                               */
/* !PI *                                                    */
/* !PO *                                                    */
/* !LV i                                                    */
/*  Description                                             */
/*  This prototype checks the URXIFG0-bit. If the receiver  */
/*  is still receiving data the program waits until all bits*/
/*  are received and the URXIFG0 bit is asserted            */
/************************************************************/

void complete(void)
{
      do
      {
      IFG1 &=~ URXIFG0;
      }
      while (URXIFG0 & IFG1);      
}                                          

/************************************************************/
/* Prototype - delay                                        */
/*                                                          */
/* !NA delay                                                */
/* !LA ANSI C                                               */
/* !PI *                                                    */
/* !PO *                                                    */
/* !LV i                                                    */
/*  Description                                             */
/*  This prototype gives a delay of around 1 second         */
/************************************************************/

void delay(void)
      {
      unsigned int i;
      for (i = 65000; i > 0; i--);
      }  


/************************************************************/
/* Prototype - flash LED1                                       */
/*                                                          */
/* !NA flash                                                */
/* !LA ANSI C                                               */
/* !PI *                                                    */
/* !PO *                                                    */
/* !LV i                                                    */
/*  Description                                             */
/*  This prototype indicates the program is complete        */
/************************************************************/

void display(void)
      {
      while(1)
            {
            P3OUT |= BIT5;          // Clear P3.5
            delay();
            P3OUT &= ~BIT5;         // Set P3.5
            delay();
            }       
      }
/************************************************************/
/* Prototype - convert                                      */
/*                                                          */
/* !NA convert                                              */
/* !LA ANSI C                                               */
/* !PI *                                                    */
/* !PO *                                                    */
/* !LV *                                                    */
/*  Description                                             */
/*  This prototype does the adc conversion                  */
/************************************************************/ 
    
void convert (void)
{
unsigned int hi_byte, lo_byte, temp, trash, k;


P3OUT &= ~CS;                       // For ALL converters:  Assert CS LOW,
      U0TXBUF = 0x00;               // Send clocks to the ADC, this shifts 
			            // in the first 8 bits of data.
      complete();                   // Wait until all 8 bits have been received.
      hi_byte = U0RXBUF;            // Store this data in the hi-byte variable.
   
      hi_byte = hi_byte << 8;       // Left-shift the hi-byte 8-bits, to prepare 
				    // for the second byte.
      U0TXBUF = 0x00;               // Send clocks to the ADC, this shifts
				    // in the second 8 bits of data.
      complete();                   // Wait until all 8 bits have been received.           
      lo_byte = U0RXBUF;	    // Store this data in the lo-byte variable.
    

       
      #if tlc3541
      {      
      U0TXBUF = 0x00;               // Send clocks to the ADC. third byte...
                                    // Needed for tlc3541
      complete();                   // Wait until all 8 bits have been received.           
      trash = U0RXBUF;		    // Throw this data away
      }      
      #endif

      
      #if tlc4541
      {      
      U0TXBUF = 0x00;               // Send clocks to the ADC. third byte...
                                    // Needed for tlc4541
      complete();                   // Wait until all 8 bits have been received.           
      trash = U0RXBUF;		    // Throw this data away 
      }     
      #endif
      
      #if tlc2551 
      {     
      U0TXBUF = 0x00;               // Send clocks to the ADC. third byte...
                                    // Needed for tlc2551
      complete();                   // Wait until all 8 bits have been received.           
      trash = U0RXBUF;		    // Throw this data away
       
      U0TXBUF = 0x00;               // fourth byte...
      complete();                   // Needed for tlc2551
      trash = U0RXBUF;		    // Throw this data away
      
      U0TXBUF = 0x00;               // fifth byte...
      complete();                   // Needed for tlc2551
      trash = U0RXBUF;		    // Throw this data away
      
      U0TXBUF = 0x00;               // sixth byte...
      complete();                   // Needed for tlc2551
      trash = U0RXBUF;		    // Throw this data away
      }
      #endif


    

P3OUT |= CS;                        // De-assert CS HIGH

      U0TXBUF = 0x00;               // for TLV2541, TLC2551 only: These parts 
      complete();                   // require at least 1 falling clock edge 
      trash = U0RXBUF;	            // while /CS is de-asserted high...

      
      temp = hi_byte + lo_byte;		  // Join the hi-byte and the lo-byte together.
                                          // no need to arrange data for 16-bit converter	
                                          
      
      #if tlv2541                      // Arrange the data format - for 12-bit converters 
        adc_data[element] = temp >> 4;	 
      #endif
      
      
      
      #if tlc2551                      // Arrange the data format - for 12-bit converters 
        adc_data[element] = temp >> 4;	 
      #endif
      
      
      
      #if tlc3541                      // Arrange the data format - for 14-bit converter 
        adc_data[element] = temp >> 2;	 
      #endif
      
      #if tlc4541                      // Arrange the data format - for 16-bit converter 
        adc_data[element] = temp;	 
      #endif
       
					  
      for (k = 0; k < 1000; k++);      // Wait while the ADC completes its conversion
      
      
      element = element + 1;		  // Increment the buffer      
}
      
// End of program

⌨️ 快捷键说明

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