-
Notifications
You must be signed in to change notification settings - Fork 49
/
spi.c
41 lines (32 loc) · 798 Bytes
/
spi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <avr/io.h>
#include "integer.h"
#include "spi.h"
void SPI_Init()
{
SPI_DDR |= _BV(SPI_MOSI) | _BV(SPI_SCK) | _BV(SPI_SS); // setup SPI output ports
SPI_DDR &= ~_BV(SPI_MISO); // setup SPI input ports
SPI_PORT |= _BV(SPI_MOSI) | _BV(SPI_SCK) | _BV(SPI_SS); // SPI outputs high
SPI_PORT |= _BV(SPI_MISO); // set pull-up resistor on input
SPCR = _BV(SPE) | _BV(MSTR) |_BV(SPR1);
}
void SPI_Speed_Slow()
{
SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR1) | _BV(SPR0);
SPSR &= ~_BV(SPI2X);
}
void SPI_Speed_Fast()
{
SPCR = _BV(SPE) | _BV(MSTR);
SPSR |= _BV(SPI2X);
}
void SPI_Send( BYTE data )
{
SPDR = ( data );
while( !( SPSR & ( 1<<SPIF ) ) );
}
BYTE SPI_Recv( void )
{
SPDR = 0xFF;
while( !( SPSR & ( 1<<SPIF ) ) );
return SPDR;
}