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

📄 spi.c

📁 freescale最新的16位单片机
💻 C
字号:
/******************************************************************************
*
* Freescale Semiconductor Inc.
* (c) Copyright 2004-2006 Freescale Semiconductor, Inc.
* ALL RIGHTS RESERVED.
*
***************************************************************************//*!
*
* @file      spi.c
*
* @author    R89994
* 
* @version   1.0.8.0
* 
* @date      Jul-28-2006
* 
* @brief     set-up SPI interface, comunication routines
*
*******************************************************************************/

#include "9S12XDP512.h"     
#include "stdtypes.h"
#include "main.h"
#include "spi.h"


/******************************************************************************
* Module            : void SetupUART(void)
* Description       : initialization of SPI interface
* Global Data       : none
* Static Global Data: none
* Returns           : none
* Arguments         : none
* Special Issues    : none
******************************************************************************/

void SetupSPI(void)
{
    SPI1CR1 = 0x14;    /* Control registr 1 of SPI1 */
    SPI1CR2 = 0x02;    /* Control registr 2 of SPI1 */
    SPI1BR  = 0x52;    /* Baud Rate register of SPI1: 24 Mhz/24 = 500 kHz*/
    SPI1CR1_SPE = 1;   /* enable SPI1 */
}

/*******************************************************************************
*
* Module            : byte ReadWriteSPI8bit (byte address, byte readWrite, byte value)
*
* Description       : This is the SPI communication function. It transmit one byte via
*                   : SPI (address | readWrite | value) and pass the received one as an
*                   : argument.
* Global Data       : none
* Static Global Data: none
* Returns           : SPI1DR
* Arguments         : none
* Special Issues    : none
******************************************************************************/

Byte ReadWriteSPI8bit (Byte address, Byte readwrite, Byte value)
{
    while (!SPI1SR_SPTEF);						  // while Tx regnot empty
    SPI1DR = (address | readwrite | value);      // write address, and value to be send
    while (!SPI1SR_SPIF);						    // while Rx reg not empty
    
    return(SPI1DR);						          // return Rx byte
}


/*******************************************************************************
*
* Module            : byte ReadWriteSPI16bit (byte address, byte readWrite, byte value)
*
* Description       : This is the SPI communication function. It transmit one byte via
*                   : SPI (address | readWrite | value) and pass the received one as an
*                   : argument.
* Global Data       : none
* Static Global Data: none
* Returns           : value
* Arguments         : none
* Special Issues    : none
******************************************************************************/

UNION16 ReadWriteSPI16bit (UNION16 value)
{
    while (!SPI1SR_SPTEF);
    SPI1DR = value.BYTES.msB;       // first byte of data
    while (!SPI1SR_SPIF);
    value.BYTES.msB = SPI1DR;

    while (!SPI1SR_SPTEF);
    SPI1DR = value.BYTES.lsB;       // second byte of data
    while (!SPI1SR_SPIF);
    value.BYTES.lsB = SPI1DR;

    return(value);                  // return word
}


/*******************************************************************************
* Module          : byte ReadWriteSPI24bit (byte address, byte readWrite, byte value)
* Description     : This is the SPI communication function. It transmit one byte via
*                 : SPI (address | readWrite | value) and pass the received one as an
*                 : argument.
* Returns         : message - recieve as the SPI received byte
* Global Data     : None
* Arguments       : composite of (address | readWrite | value) which is 1B long value
* Range Issues    : None
* Special Issues  : None
*******************************************************************************/

SPI24 ReadWriteSPI24bit (SPI24 message)
{
    while (!SPI1SR_SPTEF);		            // while Tx regnot empty
    SPI1DR = message.comm;                // write address, and value to be send
    while (!SPI1SR_SPIF);		              // while Rx reg not empty
    message.comm = SPI1DR;			          // store Rx byte

    while (!SPI1SR_SPTEF);
    SPI1DR = message.data.BYTES.msB;      // first byte of data
    while (!SPI1SR_SPIF);
    message.data.BYTES.msB = SPI1DR;

    while (!SPI1SR_SPTEF);
    SPI1DR = message.data.BYTES.lsB;      // second byte of data
    while (!SPI1SR_SPIF);
    message.data.BYTES.lsB = SPI1DR;
    
    return(message);                      // return word
}

⌨️ 快捷键说明

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