Guest User

Untitled

a guest
Apr 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. /*
  2. Accelerometer, ADXL345, test code
  3.  
  4. 8/20/2009
  5. Viliam Klein
  6.  
  7. This code sets up the power and mode registers and then prints out the data registers forever.
  8. UART is set to 9600 8N1
  9.  
  10. using ATMega328 on Arduino Pro Board 3.3V, 8MHz board
  11.  
  12. */
  13.  
  14. #include <avr/io.h>
  15. #include <stdio.h>
  16. #include <avr/io.h>
  17. #include <avr/interrupt.h>
  18.  
  19. #define FOSC 8000000
  20. #define BAUD 9600
  21. #define MYUBRR 103
  22.  
  23. #define sbi(var, mask) ((var) |= (uint8_t)(1 << mask))
  24. #define cbi(var, mask) ((var) &= (uint8_t)~(1 << mask))
  25.  
  26. //ADXL345
  27. #define adxl 2 //PORTB
  28.  
  29.  
  30.  
  31.  
  32. //Define functions
  33. //======================
  34. void ioinit(uint8_t baud); //Initializes IO
  35. void delay_ms(uint16_t x); //General purpose delay
  36.  
  37. static int uart_putchar(char c, FILE *stream);
  38. uint8_t uart_getchar(void);
  39. static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
  40.  
  41.  
  42. void iniSPI(void);
  43. void send_byte(uint8_t byte);
  44. void send_dummy (void);
  45. void resetCS(uint16_t delay);
  46.  
  47.  
  48. //======================
  49.  
  50. int main (void)
  51. {
  52. uint8_t recv1=0;
  53. uint8_t recv2=0;
  54. uint8_t byte;
  55. uint8_t data[6];
  56.  
  57. ioinit(103);
  58. iniSPI();
  59.  
  60. printf("\nStart\n");
  61.  
  62. delay_ms(40000);//long delay
  63.  
  64. //waits for power register to respond correctly
  65. while(byte != 0x28)
  66. {
  67. cbi(PORTB, adxl);
  68. send_byte(0x2D);
  69. send_byte(0x28);
  70. sbi(PORTB, adxl);
  71.  
  72. delay_ms(5);
  73.  
  74. cbi(PORTB, adxl);
  75. send_byte(0xAD);
  76. send_byte(0x00);
  77. byte = SPDR;
  78. sbi(PORTB, adxl);
  79.  
  80. printf("Power is: %x\n", byte);
  81.  
  82. delay_ms(1000);
  83. }
  84.  
  85. cbi(PORTB, adxl);
  86. send_byte(0x31);
  87. send_byte(0x08);
  88. sbi(PORTB, adxl);
  89.  
  90. delay_ms(5);
  91.  
  92. cbi(PORTB, adxl);
  93. send_byte(0xB1);
  94. send_byte(0x00);
  95. byte = SPDR;
  96. sbi(PORTB, adxl);
  97.  
  98. printf("Format is: %x\n", byte);
  99.  
  100. cbi(PORTB, adxl);
  101. send_byte(0x38);
  102. send_byte(0x00);
  103. sbi(PORTB, adxl);
  104.  
  105. delay_ms(5);
  106.  
  107. cbi(PORTB, adxl);
  108. send_byte(0xB8);
  109. send_byte(0x00);
  110. byte = SPDR;
  111. sbi(PORTB, adxl);
  112.  
  113. printf("FIFO is: %x\n", byte);
  114.  
  115.  
  116. printf("data is: ");
  117.  
  118. while(1)
  119. {
  120. printf("\n");
  121.  
  122. cbi(PORTB, adxl);
  123. send_byte(0xF2);
  124.  
  125. for(uint8_t i=0; i<6; i++)
  126. {
  127. send_byte(0x00);
  128. data[i] = SPDR;
  129.  
  130. }
  131.  
  132. sbi(PORTB, adxl);
  133.  
  134. for(int i=0; i<6; i++)
  135. {
  136. printf("%x ",data[i]);
  137. }
  138.  
  139. delay_ms(4000);
  140.  
  141. }
  142.  
  143. return(0);
  144. }
  145.  
  146. void send_dummy(void)
  147. {
  148.  
  149. SPDR = 0xAA;
  150. while(!(SPSR & (1<<SPIF)));
  151.  
  152. }
  153.  
  154. void send_byte(uint8_t byte)
  155. {
  156.  
  157. SPDR = byte;
  158. while(!(SPSR & (1<<SPIF)));
  159.  
  160. }
  161.  
  162. void iniSPI(void)
  163. {
  164. // Set MOSI, SCK, and SS output, all others input
  165. sbi(DDRB, adxl); //acc1 cs output
  166.  
  167.  
  168. sbi(PORTB, adxl);
  169.  
  170. cbi(SPCR, CPHA);
  171. SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0)|(1<<CPHA)|(1<<CPOL);
  172. cbi(SPCR, SPR1);
  173. cbi(SPCR, SPR0);
  174. //sbi(SPSR, 0);
  175. delay_ms(1);
  176. }
  177.  
  178.  
  179. void ioinit (uint8_t baud)
  180. {
  181. //1 = output, 0 = input
  182. DDRB = 0b11101111; //All inputs
  183. DDRC = 0b11111111; //All outputs
  184. DDRD = 0b11001110; //PORTD (RX on PD0)
  185. //DDRA = 0b11111111;
  186. //CLKPR = (1 << CLKPCE);
  187. //CLKPR = (3<<CLKPS);
  188.  
  189. UBRR0H = MYUBRR >> 8;
  190. UBRR0L = MYUBRR;
  191. UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  192. UCSR0A = (1<<U2X0);
  193.  
  194. stdout = &mystdout; //Required for printf init
  195.  
  196. }
  197.  
  198. static int uart_putchar(char c, FILE *stream)
  199. {
  200. if (c == '\n') uart_putchar('\r', stream);
  201.  
  202. loop_until_bit_is_set(UCSR0A, UDRE0);
  203. UDR0 = c;
  204.  
  205. return 0;
  206. }
  207.  
  208. uint8_t uart_getchar(void)
  209. {
  210. while( !(UCSR0A & (1<<RXC0)) );
  211. return(UDR0);
  212. }
  213.  
  214.  
  215. //General short delays
  216. void delay_ms(uint16_t x)
  217. {
  218. uint8_t y, z;
  219. for ( ; x > 0 ; x--){
  220. for ( y = 0 ; y < 90 ; y++){
  221. for ( z = 0 ; z < 6 ; z++){
  222. asm volatile ("nop");
  223. }
  224. }
  225. }
  226. }
Add Comment
Please, Sign In to add comment