-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspi.c
More file actions
59 lines (46 loc) · 1.08 KB
/
spi.c
File metadata and controls
59 lines (46 loc) · 1.08 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* spi.c
*SPI is not affected by delay functions, I DONT KNOW WHY
* Created: 2/4/2022 7:28:34 PM
* Author: Ali
*/
#include <avr/io.h>
#include "spi.h"
/*Initialize master mode*/
void spi_master_init()
{
DDRB |= (1<<DDB2) | (1<<DDB1) | (1<<DDB0); //MOSI ,SS and SCK output,
DDRB &= ~(1<<DDB3); //MISO input
PORTB |= (1<<PB0); //Sets SS high, use it when u need it otherwise ground slave SS
SPCR = (1<<SPE) | (1<<MSTR) |(1<SPR0); //Enable SPI, master, set clock rate to f/16
SPSR &= ~(1<<SPI2X); //Disable speed doubler
}
void spi_ss_set()
{
PORTB|=(1<<PB0);
}
void spi_ss_reset()
{
PORTB &= 0xfe;
}
/*master transceive function, no need for separate functions as it does the job*/
char spi_master_trx(char data)
{
SPDR =data;
while(!(SPSR & (1<<SPIF)))
;
return SPDR;
}
void spi_slave_init()
{
DDRB &= ~((1<<DDB2) | (1<<DDB1) | (1<<DDB0));
DDRB |= (1<<DDB3); //MISO output, others input
SPCR = (1<<SPE); //Enable SPI
}
char spi_slave_trx(char data)
{
SPDR=data;
while(!(SPSR & (1<<SPIF)))
;
return SPDR;
}