37 #include <avr/interrupt.h> 41 #pragma message ( "avr_spi.h included" ) 47 #define SPI_MODE_MASTER (1<<MSTR) 48 #define SPI_MODE_SLAVE (0<<MSTR) 49 #define SPI_MODE_MASK (1<<MSTR) 56 #define SPI_ENABLE (1<<SPE) 63 #define SPI_PRESCALER_4 (0<<SPR1) | (0<<SPR0) 64 #define SPI_PRESCALER_16 (1<<SPR0) 65 #define SPI_PRESCALER_64 (1<<SPR1) 66 #define SPI_PRESCALER_128 (1<<SPR1) | (1<<SPR0) 67 #define SPI_PRESCALER_MASK (1<<SPR1) | (1<<SPR0) 68 #define SPI_PRESCALER_SPEED_DOUBLE (1<<SPI2X) 69 #define SPI_PRESCALER_SPEED_NORMAL (0<<SPI2X) 70 #define SPI_PRESCALER_SPEED_MASK (1<<SPI2X) 77 #define SPI_DATA_MODE_0 (0<<CPOL) | (0<<CPHA) 78 #define SPI_DATA_MODE_1 (1<<CPHA) 79 #define SPI_DATA_MODE_2 (1<<CPOL) 80 #define SPI_DATA_MODE_3 (1<<CPOL) | (1<<CPHA) 81 #define SPI_DATA_MODE_MASK (1<<CPOL) | (1<<CPHA) 88 #define SPI_DATA_ORDER_LSB_FIRST (1<<DORD) 89 #define SPI_DATA_ORDER_MSB_FIRST (0<<DORD) 90 #define SPI_DATA_ORDER_MASK (1<<DORD) 97 #define SPI_MISO_PIN (1<<4) 98 #define SPI_MOSI_PIN (1<<3) 99 #define SPI_SCK_PIN (1<<5) 100 #define SPI_SS_PIN (1<<2) 101 #define SPI_PORT PORTB 111 uint8_t MasterSlaveSelect;
131 uint8_t SPI_ReceiveByte();
132 uint16_t SPI_ReceiveTwoBytes();
133 void SPI_ReceiveBytes(uint8_t *Buffer, uint8_t Size);
140 void SPI_TransmitByte(uint8_t Buffer);
141 void SPI_TransmitTwoBytes(uint16_t Buffer);
142 void SPI_TransmitBytes(uint8_t *Buffer, uint8_t Size);
149 uint8_t SPI_TransReceiveByte(uint8_t Buffer);
150 uint16_t SPI_TransReceiveTwoBytes(uint16_t Buffer);
151 void SPI_TransReceiveBytes(uint8_t *Buffer, uint8_t Size);
171 if (Data.MasterSlaveSelect == SPI_MODE_MASTER)
173 SPI_PORT |= SPI_SS_PIN;
174 SPI_DDR |= SPI_SS_PIN;
177 SPCR = Data.MasterSlaveSelect | Data.DataOrder | Data.DataMode | SPI_ENABLE | Data.Prescaler;
178 SPSR = Data.DoubleSpeed;
180 SPI_DDR |= SPI_SCK_PIN;
181 SPI_DDR |= SPI_MOSI_PIN;
182 SPI_DDR &= ~SPI_MISO_PIN;
184 else if (Data.MasterSlaveSelect == SPI_MODE_SLAVE)
186 SPI_DDR &= ~SPI_MOSI_PIN;
187 SPI_DDR |= SPI_MISO_PIN;
188 SPI_DDR &= ~SPI_SCK_PIN;
189 SPI_DDR &= ~SPI_SS_PIN;
192 SPCR = Data.MasterSlaveSelect | Data.DataOrder | Data.DataMode | SPI_ENABLE | Data.Prescaler;
193 SPSR = Data.DoubleSpeed;
217 uint8_t SPI_ReceiveByte()
222 while(!(SPSR & (1<<SPIF)));
233 uint16_t SPI_ReceiveTwoBytes()
245 SPI_PORT &= ~(SPI_SS_PIN);
246 if (!(SPCR & (SPI_DATA_ORDER_LSB_FIRST)))
250 while (!(SPSR & (1<<SPIF))) ;
254 while (!(SPSR & (1<<SPIF))) ;
261 while (!(SPSR & (1<<SPIF))) ;
265 while (!(SPSR & (1<<SPIF))) ;
268 SPI_PORT |= (SPI_SS_PIN);
279 void SPI_ReceiveBytes(uint8_t *Buffer, uint8_t Size)
281 if (Size == 0)
return;
283 SPI_PORT &= ~(SPI_SS_PIN);
287 while (!(SPSR & (1<<SPIF)));
291 SPI_PORT |= (SPI_SS_PIN);
299 void SPI_TransmitByte(uint8_t Buffer)
304 while(!(SPSR & (1<<SPIF)));
313 void SPI_TransmitTwoBytes(uint16_t Buffer)
327 SPI_PORT &= ~(SPI_SS_PIN);
328 if (!(SPCR & (SPI_DATA_ORDER_LSB_FIRST)))
332 while (!(SPSR & (1<<SPIF))) ;
335 while (!(SPSR & (1<<SPIF))) ;
341 while (!(SPSR & (1<<SPIF))) ;
344 while (!(SPSR & (1<<SPIF))) ;
346 SPI_PORT |= (SPI_SS_PIN);
356 void SPI_TransmitBytes(uint8_t *Buffer, uint8_t Size)
358 if (Size == 0)
return;
360 SPI_PORT &= ~(SPI_SS_PIN);
364 while (!(SPSR & (1<<SPIF)));
366 SPI_PORT |= (SPI_SS_PIN);
374 uint8_t SPI_TransReceiveByte(uint8_t Buffer)
377 SPI_PORT &= ~(SPI_SS_PIN);
380 while(!(SPSR & (1<<SPIF)));
382 SPI_PORT |= (SPI_SS_PIN);
391 uint16_t SPI_TransReceiveTwoBytes(uint16_t Buffer)
405 SPI_PORT &= ~(SPI_SS_PIN);
406 if (!(SPCR & (SPI_DATA_ORDER_LSB_FIRST)))
410 while (!(SPSR & (1<<SPIF))) ;
414 while (!(SPSR & (1<<SPIF))) ;
421 while (!(SPSR & (1<<SPIF))) ;
425 while (!(SPSR & (1<<SPIF))) ;
428 SPI_PORT |= (SPI_SS_PIN);
439 void SPI_TransReceiveBytes(uint8_t *Buffer, uint8_t Size)
441 if (Size == 0)
return;
443 SPI_PORT &= ~(SPI_SS_PIN);
447 while (!(SPSR & (1<<SPIF)));
451 SPI_PORT |= (SPI_SS_PIN);
إرسال تعليق