1462 views|0 replies

3836

Posts

19

Resources
The OP
 

MSP430 variant 3-wire SPI bus implementation (for DS1302 clock chip) [Copy link]

As the title says, this is the so-called abnormal version of the 3-wire SPI bus on the Internet: one clock line, one enable line, and one bidirectional IO line.

  One module, two files:

//spi3.c #include "typedef.h" #include "spi3.h" /******************************************************************** Name: init_spi3 Description: SPI3 initialization function Parameter: (None) Return: (None) Explanation: ***********************************************************************/ void init_spi3(void) { SPI3_DIR |= SPI3_SCLK; SPI3_OUT |= SPI3_SCLK; } /****************************************************************************** * Function: Write a byte to the 3-wire SPI bus, with the low bit first * Parameter: dat - the data byte to be written * Return: * Explanation: **************************************************************************/ 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: * Description: *****************************************************************************/ 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_SCLK

void init_spi3(void);
void spi3_send_byte(unsigned char dat);
unsigned char spi3_recv_byte(void);
#endif//!__SPI3_H__

typedef.h contains the header file of msp430, as well as some basic and commonly used type definitions. You can define them yourself.

This post is from Microcontroller MCU
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list