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

📄 spi.c

📁 用于ATmega128的一些通讯程序
💻 C
字号:
/*
**	Purpose:    SPI init, read & write routines without interrupt.
**              These routines works only as an SPI Master.
**
**  Version:    1.0.0, 29:th of July 1999
**
**  Author:     Lars Wictorsson
**              LAWICEL / SWEDEN
**              http://www.lawicel.com   lars@lawicel.com
**
**  History:    1999-07-29  Created
*/

#include <avr.h>
#include "spi.h"

/*
**  SpiInit() initializes the SPI as a master, enable it
**  and set it up as requested by parameter Controlregister.
**  See "spi.h" for availible parameter values, they could
**  be ORed together. See AVR SPI documentation for more 
**  detailed information.
*/
void SpiInit(unsigned char ControlRegister)
{
    DDRB  |= 0xB0;  // Set SCK, MOSI & SS as outputs
    PORTB &= 0x5F;  // clear bits MOSI, & SCK

    SPCR = ControlRegister + SPE + MSTR;    // Write to control register.
}

/*
**  SpiWriteByte() writes a byte to the SPI and waits until
**  it has been transmitted. This function doesn't
**  return any value back.
*/
void SpiWriteByte(unsigned char byte)
{
    SPDR = byte;
    while (!(SPSR & 0x80));
    byte = SPDR;
}

/*
**  SpiReadByte() first writes a byte (a dummy, since
**  that byte is to generate clock signals to "poll" home
**  the byte from the slave. The function returns the
**  received byte.
*/
unsigned char SpiReadByte(void)
{
    SPDR = 0x00;
    while (!(SPSR & 0x80));
    return SPDR;
}

/*
**  SpiReadDataReg() reads the last byte in the SPI register
**  without any clock signals generated. Could be used
**  as reading the last byte received.
*/
unsigned char SpiReadDataReg(void)
{
    return SPDR;
}

⌨️ 快捷键说明

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