-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspi_2.c
60 lines (51 loc) · 1.34 KB
/
spi_2.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "spi_2.h"
#include <avr/io.h>
/*èíèöèàëèçàöèÿ SPI*/
void SPI_Init(void)
{
/*íàñòðîéêà ïîðòîâ ââîäà-âûâîäà
âñå âûâîäû, êðîìå MISO âûõîäû*/
SPI_DDRX |= (1<<SPI_MOSI)|(1<<SPI_SCK)|(1<<SPI_SS);
SPI_DDRX &= ~(1<<SPI_MISO);
SPI_PORTX |= (1<<SPI_MOSI)|(1<<SPI_SCK)|(1<<SPI_SS)|(1<<SPI_MISO);
/*ðàçðåøåíèå spi,ñòàðøèé áèò âïåðåä,ìàñòåð, ðåæèì 0*/
SPCR = (0<<SPIE)|(1<<SPE)|(0<<DORD)|(1<<MSTR)|(0<<CPOL)|(0<<CPHA)|(0<<SPR1)|(0<<SPR0);
SPSR = (1<<SPI2X);
}
/*îòîñëàòü áàéò äàííûõ ïî SPI*/
void SPI_WriteByte(unsigned char data)
{
SPDR = data;
while(!(SPSR & (1<<SPIF)));
}
/*ïîëó÷èòü áàéò äàííûõ ïî SPI*/
unsigned char SPI_ReadByte(void)
{
SPDR = 0xff;
while(!(SPSR & (1<<SPIF)));
return SPDR;
}
/*îòîñëàòü è ïîëó÷èòü áàéò äàííûõ ïî SPI*/
unsigned char SPI_WriteReadByte(unsigned char data)
{
SPDR = data;
while(!(SPSR & (1<<SPIF)));
return SPDR;
}
/*îòïðàâèòü íåñêîëüêî áàéò äàííûõ ïî SPI*/
void SPI_WriteArray(unsigned char num, unsigned char *data)
{
while(num--){
SPDR = *data++;
while(!(SPSR & (1<<SPIF)));
}
}
/*îòïðàâèòü è ïîëó÷èòü íåñêîëüêî áàéò äàííûõ ïî SPI*/
void SPI_WriteReadArray(unsigned char num, unsigned char *data)
{
while(num--){
SPDR = *data;
while(!(SPSR & (1<<SPIF)));
*data++ = SPDR;
}
}