Advertisement
Guest User

SPI_Routines.c

a guest
Nov 5th, 2010
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. //**************************************************************
  2. // ****** FUNCTIONS FOR SPI COMMUNICATION *******
  3. //**************************************************************
  4. //Controller: ATmega168 (Clock: 8 Mhz-internal)
  5. //Compiler: AVR-GCC
  6. //Version : 2.0
  7. //Author: CC Dharmani, Chennai (India)
  8. // www.dharmanitech.com
  9. //With some changes by Mujda
  10. //Date: 26 Feb 2009
  11. //**************************************************************
  12.  
  13. //**************************************************
  14. // ***** SOURCE FILE : SPI_routines.c ******
  15. //**************************************************
  16. #include <avr/io.h>
  17. #include "SPI_routines.h"
  18.  
  19. //SPI initialize for SD card
  20. //clock rate: 125Khz
  21. void spi_init(void)
  22. {
  23. SPCR = 0x52; //setup SPI: Master mode, MSB first, SCK phase low, SCK idle low
  24. SPSR = 0x00;
  25. }
  26.  
  27.  
  28. unsigned char SPI_transmit(unsigned char data)
  29. {
  30. // Start transmission
  31. SPDR = data;
  32.  
  33. // Wait for transmission complete
  34. while(!(SPSR & (1<<SPIF)));
  35.   data = SPDR;
  36.  
  37. return(data);
  38. }
  39.  
  40. unsigned char SPI_receive(void)
  41. {
  42. unsigned char data;
  43. // Wait for reception complete
  44.  
  45. SPDR = 0xff;
  46. while(!(SPSR & (1<<SPIF)));
  47.   data = SPDR;
  48.  
  49. // Return data register
  50. return data;
  51. }
  52.  
  53. //******** END ****** www.dharmanitech.com *****
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement