For example, this is a three-line SPI bus of the Internet-called variant edition: A clock line, an enabling line, and a bidirectional Io line.
One module and two files:
// Spi3.c # include "typedef. H "# include" spi3.h "/******************************** * ************************************ name: init_spi3 Description: SPI3 initialization function parameter: (none) return :( none) Description: **************************************** * *****************************/void init_spi3 (void) {spi3_dir | = spi3_sclk; spi3_out | = spi3_sclk ;} /*************************************** * *********************************** function: write a byte to the 3-line SPI bus, with the low front * Parameter: Dat-data bytes to be written * return: * description: **************************************** * ******************************/void spi3_send_byte (unsigned char dat) {unsigned char Cx = 8; spi3_dir | = spi3_io; while (CX --) {If (DAT & 0x01) spi3_io_1; else spi3_io_0; spi3_sclk_0; spi3_sclk_1; dat >>= 1 ;}} /*************************************** * *********************************** function: read a byte from the 3-wire SPI bus * parameter: * return: * Note :********** **************************************** * ********************/Unsigned char spi3_recv_byte (void) {unsigned char Cx = 8; unsigned char dat = 0; spi3_dir & = ~ Spi3_io; while (CX --) {dat >>= 1; if (spi3_in & spi3_io) DAT | = 0x80; else dat & = ~ 0x80; spi3_sclk_0; spi3_sclk_1;} return dat ;}
// Spi3.h # ifndef _ spi3_h __# DEFINE _ spi3_h _ // considering that the clock may be controlled externally, // so I put the definition here # define spi3_in p4in # define spi3_out p4out # define spi3_dir p4dir # define spi3_io bit1 # define spi3_io_0 spi3_out & = ~ Spi3_io # define spi3_io_1 spi3_out | = spi3_io # define spi3_sclk bit2 # define spi3_sclk_0 spi3_out & = ~ Spi3_sclk # define spi3_sclk_1 spi3_out | = spi3_sclkvoid init_spi3 (void); void spi3_send_byte (unsigned char dat); unsigned char forward (void); # endif //! _ Spi3_h __
Typedef. h contains the header file of MSP430, as well as some basic and common types. You can define them by yourself. I will not post them again.
Girl don't cry @ 16:19:18 @ http://www.cnblogs.com/nbsofer