-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspi.c
More file actions
25 lines (21 loc) · 684 Bytes
/
spi.c
File metadata and controls
25 lines (21 loc) · 684 Bytes
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
/* Author: Steve Gunn
* Licence: This work is licensed under the Creative Commons Attribution License.
* View this license at http://creativecommons.org/about/licenses/
*/
#include "spi.h"
void init_spi_master(void)
{
DDRB = _BV(PB4) | _BV(PB5) | _BV(PB7);
SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR0); /* F_SCK = F_CPU/16 = 750 kHz */
}
//transmit
void spi_tx(uint8_t b)
{
SPDR = b; //load data into register
while(!(SPSR & _BV(SPIF))); //wait for transmission to complete
}
//receive
uint8_t spi_rx(void) {
while(!(SPSR & _BV(SPIF))); //wait for reception to complete(When a serial transfer is complete, the SPIF Flag is set)
return SPDR; //return data register
}