Implementation of the 3-line SPI bus (ds1302 clock chip) of the MSP430 variant Edition)

Source: Internet
Author: User

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

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.