blob: 623f5d177153318569aea694ae13cc56d5b2f4e6 (
plain)
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
|
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "spi.h"
void spi_init()
{
DDR_SPI &= ~((1<<DD_MOSI)|(1<<DD_MISO)|(1<<DD_SS)|(1<<DD_SCK));
DDR_SPI |= ((1<<DD_MOSI)|(1<<DD_SS)|(1<<DD_SCK));
SPCR = ((1<<SPE)| // SPI Enable
(0<<SPIE)| // SPI Interupt Enable
(0<<DORD)| // Data Order (0:MSB first / 1:LSB first)
(1<<MSTR)| // Master/Slave select
(1<<SPR1)|(1<<SPR0)| // SPI Clock Rate
(0<<CPOL)| // Clock Polarity (0:SCK low / 1:SCK hi when idle)
(0<<CPHA)); // Clock Phase (0:leading / 1:trailing edge sampling)
SPSR = (0<<SPIF)|(0<<WCOL)|(0<<SPI2X); // Double Clock Rate
}
uint8_t spi_fast_shift(uint8_t data)
{
SPDR = data;
while((SPSR & (1<<SPIF))==0);
return SPDR;
}
uint8_t spi_two_byte(uint8_t cmd, uint8_t arg)
{
uint8_t data_buf;
spi_cs_low();
spi_fast_shift(cmd);
data_buf = spi_fast_shift(arg);
spi_cs_high();
return data_buf;
}
|