ATtiny85 Template Code
Code snippets for the ATtiny85
 All Files Functions Variables Typedefs Enumerations Enumerator Macros
softspi.c
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------*
2 * Software SPI
3 *---------------------------------------------------------------------------*
4 * 19-Apr-2014 ShaneG
5 *
6 * An implementation of SPI in software. Useful for LCD displays and other
7 * devices where a full hardware SPI implementation would waste IO pins.
8 *--------------------------------------------------------------------------*/
9 #include <avr/io.h>
10 #include "iohelp.h"
11 
22 void sspiInit(uint8_t sck, uint8_t mosi, uint8_t miso) {
23  // Set SCK and MOSI to output and bring them both low
24  uint8_t val = (1 << sck) | (1 << mosi);
25  PORTB &= ~val;
26  DDRB |= val;
27  // Set MISO to input, no pullup
28  val = ~(1 << mosi);
29  PORTB &= val;
30  DDRB &= val;
31  }
32 
40 void sspiOutMSB(uint8_t sck, uint8_t mosi, uint16_t data, uint8_t bits) {
41  uint16_t mask = (1 << (bits - 1));
42  uint8_t output = (1 << mosi);
43  uint8_t clock = (1 << sck);
44  while(bits) {
45  // Set data
46  if(data&mask)
47  PORTB |= output;
48  else
49  PORTB &= ~output;
50  // Bring the clock high
51  PORTB |= clock;
52  // Move to the next bit
53  mask = mask >> 1;
54  bits--;
55  // Bring the clock low again
56  PORTB &= ~clock;
57  }
58  }
59 
68 uint16_t sspiInMSB(uint8_t sck, uint8_t miso, uint8_t bits) {
69  uint16_t result = 0;
70  uint8_t input = (1 << miso);
71  uint8_t clock = (1 << sck);
72  while(bits) {
73  // Bring the clock high
74  PORTB |= clock;
75  // Read the next bit
76  result = result << 1;
77  if(PINB&input)
78  result |= 0x01;
79  // Bring the clock low again
80  PORTB &= ~clock;
81  }
82  return result;
83  }
84 
95 uint16_t sspiInOutMSB(uint8_t sck, uint8_t mosi, uint8_t miso, uint16_t data, uint8_t bits) {
96  uint16_t result = 0;
97  uint16_t mask = (1 << bits);
98  uint8_t input = (1 << miso);
99  uint8_t output = (1 << mosi);
100  uint8_t clock = (1 << sck);
101  while(bits) {
102  // Set data
103  if(data&mask)
104  PORTB |= output;
105  else
106  PORTB &= ~output;
107  // Bring the clock high
108  PORTB |= clock;
109  // Move to the next bit
110  mask = mask >> 1;
111  bits--;
112  // Read the next bit
113  result = result << 1;
114  if(PINB&input)
115  result |= 0x01;
116  // Bring the clock low again
117  PORTB &= ~clock;
118  }
119  return result;
120  }
121 
129 void sspiOutLSB(uint8_t sck, uint8_t mosi, uint16_t data, uint8_t bits) {
130  uint8_t output = (1 << mosi);
131  uint8_t clock = (1 << sck);
132  while(bits) {
133  // Set data
134  if(data&0x01)
135  PORTB |= output;
136  else
137  PORTB &= ~output;
138  // Bring the clock high
139  PORTB |= clock;
140  // Move to the next bit
141  data = data >> 1;
142  bits--;
143  // Bring the clock low again
144  PORTB &= ~clock;
145  }
146  }
147 
156 uint16_t sspiInLSB(uint8_t sck, uint8_t miso, uint8_t bits) {
157  uint16_t result = 0;
158  uint8_t input = (1 << miso);
159  uint8_t clock = (1 << sck);
160  for(uint8_t count=0; count<bits; count++) {
161  // Bring the clock high
162  PORTB |= clock;
163  // Read the next bit
164  result = result >> 1;
165  if(PINB&input)
166  result |= 0x8000;
167  // Bring the clock low again
168  PORTB &= ~clock;
169  }
170  result = result >> (16 - bits);
171  return result;
172  }
173 
184 uint16_t sspiInOutLSB(uint8_t sck, uint8_t mosi, uint8_t miso, uint16_t data, uint8_t bits) {
185  uint16_t result = 0;
186  uint8_t input = (1 << miso);
187  uint8_t output = (1 << mosi);
188  uint8_t clock = (1 << sck);
189  for(uint8_t count=0; count<bits; count++) {
190  // Set data
191  if(data&0x01)
192  PORTB |= output;
193  else
194  PORTB &= ~output;
195  // Bring the clock high
196  PORTB |= clock;
197  // Read the next bit
198  data = data >> 1;
199  result = result >> 1;
200  if(PINB&input)
201  result |= 0x8000;
202  // Bring the clock low again
203  PORTB &= ~clock;
204  }
205  result = result >> (16 - bits);
206  return result;
207  }
208 
void sspiOutLSB(uint8_t sck, uint8_t mosi, uint16_t data, uint8_t bits)
Definition: softspi.c:129
uint16_t sspiInLSB(uint8_t sck, uint8_t miso, uint8_t bits)
Definition: softspi.c:156
uint16_t sspiInOutMSB(uint8_t sck, uint8_t mosi, uint8_t miso, uint16_t data, uint8_t bits)
Definition: softspi.c:95
uint16_t sspiInMSB(uint8_t sck, uint8_t miso, uint8_t bits)
Definition: softspi.c:68
void sspiInit(uint8_t sck, uint8_t mosi, uint8_t miso)
Definition: softspi.c:22
uint16_t sspiInOutLSB(uint8_t sck, uint8_t mosi, uint8_t miso, uint16_t data, uint8_t bits)
Definition: softspi.c:184
void sspiOutMSB(uint8_t sck, uint8_t mosi, uint16_t data, uint8_t bits)
Definition: softspi.c:40