MSP430 library SPI Synchronous Serial Communication

Source: Internet
Author: User

The SPI bus system is a synchronous serial peripheral interface. It is a high-speed, full-duplex, synchronous communication bus, and occupies only four lines on the chip pins, chip pins are saved, space is saved for PCB layout, and convenience is provided. Thanks to this simple and easy-to-use feature, more and more chips are now integrated with this communication protocol. Many chips use this protocol for communication: EEPROM, Flash, real-time clock, AD converter, digital signal processor, etc. The usart module of MSP430 can not only implement asynchronous mode (see: MSP430 library <2> UART asynchronous serial port), and supports Synchronous Serial Communication (that is, SPI mode). Its SPI supports 3-line and 4-line operations, and supports host mode and slave mode, the length can be 7 or 8 characters. Because the ad7708 chip is used for AD sampling, ad7708 communicates with other devices through SPI.ProgramThe initialization of the host mode is simplified.

  1. Hardware introduction:

    SPI: SPI is first defined by Motorola on its mc68hcxx series processor. It is a synchronous high-speed serial communication protocol. For details about the SPI protocol, see SPI _ interactive encyclopedia.

    SPI support for MSP430: When the uxusart module controller uxctl bit sync is set, the usart module works in synchronization mode. For 149, it works in SPI mode. If it is 169, usart0 can support I2C, you can use another control bit I2C control, while I2C bit 0 works on SPI. In spi mode, the microcontroller is allowed to send and receive 7-bit or 8-bit data at a specified rate.

    Synchronous communication is similar to asynchronous communication. synchronous communication and asynchronous communication register resources are the same, and the functions of different bit of a specific register are different. For details about the specific register content, see the User Guide provided by TI.

    The SPI operation of the usart module can be 3-wire and 4-wire. The signal is as follows:
    Simo: data output in master-to-Master mode and master-to-Master mode. Data Input in master-to-Master mode.
    Somi: Data Input in Master/Slave Mode and data output in Master/Slave Mode.
    Uclk: usart SPI-mode clock. The signal is output from the host and input from the slave.
    Ste: allows the control pin for sending and receiving in the slave mode. It is used in the 4-wire mode to control multiple slave hosts in multiple Master/Slave systems to avoid conflicts. The specific method is as follows (the figure is taken from the user guide ):

    Four-line host mode: ste is high, Simo and uclk operations are normal, Ste is low, Simo and uclk are set to the input direction, control of the host is given out.
    Four-line slave mode: ste is a high level, the transmitting and receiving of the slave is invalid, and somi is set to the input direction; ste is a low level, the sending and receiving are normal, and somi is also a normal output.

    Usart module serial clock polarity and phase settings:

    The polarity and phase of the usart clock uclk are controlled by the ckph and ckpl bit in the uxtctl register. For example, in the program, I call it, clock Mode 0, clock Mode 1, clock Mode 2, clock Mode 3.

    The baud rate of usart is generated. SPI is different from asynchronous communication. asynchronous communication is controlled by three registers, uxbr1 \ uxbr0 \ uxmctl, to generate a standard frequency. In synchronous mode, the master and slave devices use the same clock, the standard clock is no longer required, so the uxmctl register is no longer used and its value is set to 0.

    Others are basically the same as asynchronous communication. For more information, see user guide.

  2. Program Implementation:

    The program and asynchronous communication method are similar: Initialize the function first, then read and write data functions. This program adopts a structure similar to my previous UART library. After data is written, the program enters the low power consumption and waits for interruption. It judges the flag bit for data writing and reading.

    Here, the function only implements the Host Mode of 430. If you need to switch from the host mode, you can follow my program to simplify the implementation.

    Because the SPI device I'm going to use (ad7708) is not a stand-alone device, I will not write the string function here, nor transplant the printf and scanf functions here. You can add them as needed, for porting printf and scanf, see <4> transplantation of printf and scanf functions in the MSP430 library.

    Initialize the function: spimasterinit to initialize the host mode. The function content is as follows:

     Char Spimasterinit ( Long Baud, Char Databits, Char Mode, Char Clkmode ){ Long int Brclk; // Clock frequency of the baud rate generator Uxctl | = swrst; // Initial // feedback selection bit, which is 1. The number of sent messages is received by the user and used for testing. Comment out/uxctl | = listen; Uxctl | = Sync + mm;// SPI Host Mode // clock source settings Uxtctl & = ~ (Ssel0 + ssel1 ); // Clear the previous clock settings  If (Baud <= 16364) // {Uxtctl | = ssel0; // Aclk to reduce power consumption Brclk = 32768; // Baud rate generator clock frequency = aclk (32768) } Else {Uxtctl | = ssel1; // Smclk to ensure speed Brclk = 1000000; // Baud rate generator clock frequency = smclk (1 MHz) }// ------------------------ Set the baud rate -------------------------  If (Baud <300 | baud> 115200) // The baud rate exceeds the range. { Return 0 ;} // Set the baud rate register  Int Fen = brclk/baud; // Frequency division Coefficient  If (FEN <2) Return (0 ); // The frequency division coefficient must be greater than 2  Else {Uxbr0 = fen/256; uxbr1 = Fen % 256 ;}// ------------------------ Set the data bit -------------------------  Switch (Databits ){ Case 7: Case  '7' : Uxctl & = ~ Char; Break ; // 7-Bit Data  Case 8: Case  '8' : Uxctl | = char; Break ; // 8-Bit Data  Default :Return (0 ); // Parameter error } // ------------------------ Set the mode ---------------------------  Switch (Mode ){ Case 3: Case  '3' : Uxtctl | = STC; uspi3on; Break ; // Three-line mode  Case 4: Case  '4' : Uxtctl & = ~ STC; uspi4on; Break ; // Four-line mode  Default : Return (0 ); // Parameter error } // ------------------------ Set uclk mode -----------------------  Switch (Clkmode ){ Case 0: Case  '0' : Uxtctl & = ~ Ckph; uxtctl & = ~ Ckpl; Break ; // Mode 0  Case 1:Case  '1' : Uxtctl & = ~ Ckph; uxtctl | = ckpl; Break ; // Mode 1  Case 2: Case  '2' : Uxtctl | = ckph; uxtctl & = ~ Ckpl; Break ; // Mode 2  Case 3: Case  '3' : Uxtctl | = ckph; uxtctl | = ckpl; Break ;// Mode 3  Default : Return (0 ); // Parameter error } Uxme | = uspiex; // Module enabling Uctl0 & = ~ Swrst; // Initialize usart state machine Uxie | = urxiex + utxiex; // Enable usart0 RX interrupt  Return (1 ); // Set successfully }

    The program annotations are already detailed. I will not elaborate on them here. If you want to change to the slave mode, you should just remove the clock setting and the baud rate setting.

    Sending and receiving functions:

    VoidSpiwritedat (CharC ){While(Txflag = 0) spilpm ();// Wait until the last byte is sent and sleepTxflag = 0;//Uxtxbuf = C ;}

     
    CharSpireaddat (){While(Rxflag = 0) spilpm ();// Receive one byte?Rxflag = 0;Return(Uxrxbuf );}

    The sending and receiving functions are almost the same as those in asynchronous communication. If the flag is 0, the flag is changed to 1 and then written or read. The flag is changed in the interrupt function; the interrupt function is as follows:

     
    # PragmaVector = usartxrx_vector _ interruptVoidUartrx () {rxflag = 1; _ low_power_mode_off_on_exit ();}

    # PragmaVector = usartxtx_vector _ interruptVoidUarttx () {txflag = 1; _ low_power_mode_off_on_exit ();}

    After only the flag is set in the interrupt, the power consumption is terminated. After the interruption, data is written or read.

    The spilpm function called to read or write functions:

     
    VoidSpilpm (){If(Uxtctl & ssel0) lpm3;// If aclk is used as the clock, lpm3 sleep (only aclk is enabled)ElseLpm0;// If smclk is used as the clock, it enters lpm0 sleep (do not disable smclk)}

    Low Power Consumption is enabled according to different situations. If the single-chip microcomputer is not allowed to access low power consumption elsewhere, you can change this function.

    There are so many programs. The required functions are declared in the header file for ease of use.

  3. Example:

    The program is used in the same way as the previous library. Add the c file, including the H file, and call the initialization function to drop the function in the library.

    # Include"Rjx16x. H"// Register header file 430# Include"SPI. H"// Header file of the serial communication libraryVoidMain (){// Stop watchdog timer to prevent time out ResetWdtctl = wdtpw + wdthold; clkinit ();// Host mode, with a baud rate of, 8-Bit Data bit, three-line mode, and 0 clock mode (for details, see SPI. c)Spimasterinit (25000,8, 3,0); _ Eint ();While(1)// Serial port Test{Spiwritedat (0x20 );CharA = spireaddat ();}}

    This is just a simple example. The detailed usage will be provided in the next article, the next article: MSP430 library <6> using SPI to operate ad7708; today's library will be used, complete the SPI communication section.

  4. Note:

    SPI is full-duplex communication. Each time 8-bit/7-bit data is written (sent), 430 of the SPI main module reads 0/1 of the sampled signals in the second half of the clock cycle, it is stored in the receiving buffer register. Therefore, each write operation reads data, but it is not necessarily sent back from the device. Note this when using the 430 host mode, very error-prone (I also noticed this place when debugging ad7708); the SPI function has added the spiwritedata function, this function will return the data received after half a clock cycle is sent at the same time for ease of use. It is not recommended to use the previous sending and reading functions, which are prone to errors; we recommend that you use the newly added function. The library has been updated and can be downloaded again. Function spiwritedata:

     
    CharSpiwritedata (CharC) {spiwritedat (C );ReturnSpireaddat ();}

    It can be read after sending, and the program is relatively simple.

    New sample program:

    VoidMain (){// Stop watchdog timer to prevent time out ResetWdtctl = wdtpw + wdthold; clkinit ();// Host mode, with a baud rate of, 8-Bit Data bit, three-line mode, and 0 clock mode (for details, see SPI. c)Spimasterinit (25000,8, 3,0); _ Eint ();While(1)// Serial port Test{Spiwritedata (0x20 );// Write onlyCharA = spiwritedata (0xff );// Read only}}

    For detailed examples, refer to the next article: MSP430 library <6> using SPI to operate ad7708

The SPI communication is partially completed. If you have any questions, please discuss them. Thank you!

Attachment: Library

Author:Give me a drink

Source: http://Engin.cnblogs.com/

The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. repost the text and indicate the source. Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.