IanosStefanCristian

Comunicare I2C cu modul accelerometru ADXL345 Proiect Sisteme cu Microprocesor II

May 19th, 2021 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. #include <msp430g2553.h>
  2.  
  3. #define NUM_BYTES_TX 2
  4. #define NUM_BYTES_RX 6
  5. #define ADXL_345 0x53
  6.  
  7. int RXByteCtr, x1,y1,z1; //activare repetare start la valoarea 1
  8. volatile unsigned char RxBuffer[6]; //alocare 6 biti de RAM
  9. unsigned char *PRxData; //pointer pentru retinerea adresei RX
  10. unsigned char TXByteCtr, RX = 0;
  11. unsigned char MSData[2];
  12.  
  13. void Setup_TX(unsigned char);
  14. void Setup_RX(unsigned char);
  15. void Transmit(unsigned char,unsigned char);
  16. void TransmitOne(unsigned char);
  17. void Receive(void);
  18. void Setup_UART();
  19. void UARTSendArray(unsigned char *TxArray, unsigned char ArrayLength);
  20. void UARTSendInt(unsigned int x);
  21.  
  22. int main(void)
  23. {
  24. WDTCTL = WDTPW + WDTHOLD;
  25.  
  26. // LED
  27. P1DIR |= BIT0;
  28. P1OUT |= BIT0;
  29. // UART
  30. BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
  31. DCOCTL = CALDCO_1MHZ; // Set DCO to 1MHz
  32.  
  33. // Configurare hardware UART
  34. P1SEL |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
  35. P1SEL2 |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
  36.  
  37.  
  38. // ADXL345
  39. P1SEL |= BIT6 + BIT7; //Atribuire pini I2C la USCI_B0 // Assign I2C pins to USCI_B0
  40. P1SEL2 |= BIT6 + BIT7; //Atribuire pini I2C la USCI_B0 // Assign I2C pins to USCI_B0
  41.  
  42. // Initializare secventa ADXL345
  43. //Transmitere
  44. Setup_TX(ADXL_345);
  45. Transmit(0x2D,0x00); // blocat
  46. while (UCB0CTL1 & UCTXSTP); // Asigurare ca conditia de stop a fost trimisa
  47.  
  48. //Transmitere
  49. Setup_TX(ADXL_345);
  50. Transmit(0x2D,0x10);
  51. while (UCB0CTL1 & UCTXSTP); //Asigurare ca conditia de stop a fost trimisa
  52.  
  53. //Transmit process
  54. Setup_TX(ADXL_345);
  55. Transmit(0x2D,0x08);
  56. while (UCB0CTL1 & UCTXSTP); //Asigurare ca conditia de stop a fost trimisa
  57.  
  58. long long i;
  59.  
  60. while(1){
  61. Setup_UART();
  62. UARTSendArray("0\n", 2);
  63.  
  64. //bucla pt masurarea timpului de completare
  65. for(i=0;i<1000;i++) {
  66.  
  67. //Transmitere
  68. Setup_TX(ADXL_345);
  69. TransmitOne(0x32); // Request Data from ADXL345 in 2g Range 10Bit resolution
  70. while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
  71.  
  72. //Primire
  73. Setup_RX(ADXL_345);
  74. Receive();
  75. while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
  76.  
  77. x1 = (((int)RxBuffer[1]) << 8) | RxBuffer[0];
  78. y1 = (((int)RxBuffer[3]) << 8) | RxBuffer[2];
  79. z1 = (((int)RxBuffer[5]) << 8) | RxBuffer[4];
  80.  
  81. }
  82. Setup_UART();
  83. UARTSendArray("1\n", 2);
  84. }
  85.  
  86. __bis_SR_register( LPM3_bits + GIE );
  87.  
  88. for( ; ; ) { }
  89. }
  90.  
  91. #pragma vector = USCIAB0TX_VECTOR
  92. __interrupt void USCIAB0TX_ISR(void)
  93. {
  94.  
  95. if(RX == 1){
  96. RXByteCtr--;
  97. if (RXByteCtr)
  98. {
  99. *PRxData++ = UCB0RXBUF;
  100. }
  101. else
  102. {
  103. UCB0CTL1 |= UCTXSTP;
  104. *PRxData++ = UCB0RXBUF;
  105. __bic_SR_register_on_exit(CPUOFF);
  106. }}
  107. else{
  108. if (TXByteCtr)
  109. {
  110. TXByteCtr--;
  111. UCB0TXBUF = MSData[TXByteCtr];
  112. }
  113. else
  114. {
  115. UCB0CTL1 |= UCTXSTP;
  116. IFG2 &= ~UCB0TXIFG;
  117. __bic_SR_register_on_exit(CPUOFF);
  118. }
  119. }
  120. }
  121.  
  122. void Setup_UART() {
  123. _DINT();
  124. IE2 &= ~UCB0RXIE;
  125. IE2 &= ~UCB0TXIE;
  126. UCA0CTL1 |= UCSSEL_2; // Utilizare SMCLK
  127. UCA0BR0 = 104; //setare rata de transfer la 9600 cu 1MHz ceas
  128. UCA0BR1 = 0; //setare rata de transfer la 9600 cu 1MHz ceas
  129. UCA0MCTL = UCBRS0; // UCBRSx = 1
  130. UCA0CTL1 &= ~UCSWRST; // initializare USCI
  131. IE2 |= UCA0RXIE; //activare USCI_A0 RX intrerupere
  132. }
  133.  
  134. void Setup_TX(unsigned char Dev_ID){
  135. _DINT();
  136. RX = 0;
  137. IE2 &= ~UCA0RXIE; // Disable USCI_A0 RX interrupt
  138. IE2 &= ~UCB0RXIE;
  139. while (UCB0CTL1 & UCTXSTP); //asigurare conditie de stop //dezactivare intrerupere RX
  140. UCB0CTL1 |= UCSWRST; //activare reset SW
  141. UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; //I2C Master, mod de sincronizare
  142. UCB0CTL1 = UCSSEL_2 + UCSWRST; //utilizare SMCLK, pastrare reset SW
  143. UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
  144. UCB0BR1 = 0;
  145. UCB0I2CSA = Dev_ID; // adresa slave = 048h
  146. UCB0CTL1 &= ~UCSWRST; //curatare SW reset, continuare operatie
  147. IE2 |= UCB0TXIE; // activare intrerupere TX
  148. }
  149.  
  150. void Setup_RX(unsigned char Dev_ID){
  151. _DINT();
  152. RX = 1;
  153. IE2 &= ~UCA0RXIE; //dezactivare intrerupere USCI_AO RX
  154. IE2 &= ~UCB0TXIE;
  155. UCB0CTL1 |= UCSWRST; // activare SW Reset
  156. UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; //I2C Master, mod sincronizare
  157. UCB0CTL1 = UCSSEL_2 + UCSWRST; //utilizare SMCLK, pastrare SW Reset
  158. UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
  159. UCB0BR1 = 0;
  160. UCB0I2CSA = Dev_ID; // adresa slave este 048h
  161. UCB0CTL1 &= ~UCSWRST; // curatare SW Reset, continuare operatie
  162. IE2 |= UCB0RXIE; // activare intrerupere RX
  163. }
  164.  
  165. void Transmit(unsigned char Reg_ADD,unsigned char Reg_DAT){
  166. MSData[1]= Reg_ADD;
  167. MSData[0]= Reg_DAT;
  168. TXByteCtr = NUM_BYTES_TX; // incaracare TX numarator de biti
  169. while (UCB0CTL1 & UCTXSTP); // asigurare ca conditia de stop a fost trimisa
  170. UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start conditie
  171. __bis_SR_register(CPUOFF + GIE); //intrare LPM0 cu intreruperi
  172. }
  173.  
  174. void TransmitOne(unsigned char Reg_ADD){
  175. MSData[0]= Reg_ADD;
  176. TXByteCtr = 1; // Load TX byte counter // incaracare TX numarator de biti
  177. while (UCB0CTL1 & UCTXSTP); // asigurare ca conditia de stop a fost trimisa
  178. UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start conditie
  179. __bis_SR_register(CPUOFF + GIE); //intrare LPM0 cu intreruperi
  180. }
  181.  
  182. void Receive(void){
  183. PRxData = (unsigned char *)RxBuffer; //start buffer RX
  184. RXByteCtr = NUM_BYTES_RX; //incarcare RX numarator biti
  185. while (UCB0CTL1 & UCTXSTP); //asigurare ca conditia de stop a fost trimisa
  186. UCB0CTL1 |= UCTXSTT; //I2C, start conditie
  187. __bis_SR_register(CPUOFF + GIE); //intrare LPM0 cu intreruperi
  188. }
  189.  
  190. void UARTSendArray(unsigned char *TxArray, unsigned char ArrayLength){
  191.  
  192. while(ArrayLength--){ //bucla while pana cand StringLength==0 si post decrementare
  193. while(!(IFG2 & UCA0TXIFG)); //asteptare pana cand bufferul TX e gata pt date noi
  194. UCA0TXBUF = *TxArray; //Scrie caracterul la locatia specificata de pointer
  195. TxArray++; //Incrementarea pointerului TxString la punctul urmatorului caracter
  196. }
  197. IFG2 &= ~UCA0TXIFG; // Curatare USCI_A0 int flag
  198. }
  199.  
  200. void UARTSendInt(unsigned int x){
  201. unsigned char buff[10];
  202. unsigned char data[10];
  203. unsigned char index = 0, i = 0;
  204.  
  205. while(x > 0) {
  206. unsigned char val = x % 16;
  207. if(val < 10)
  208. buff[index] = 48+val;
  209. else
  210. buff[index] = 97+val-10;
  211. index++;
  212. x /= 16;
  213. }
  214. buff[index] = '\n';
  215.  
  216. while(index > 0) {
  217. index--;
  218. data[i] = buff[index];
  219. i++;
  220. }
  221.  
  222. if(i==0) {
  223. data[0] = '0';
  224. i++;
  225. }
  226. data[i] = '\n';
  227. UARTSendArray(data, i+1);
  228. }
  229.  
Add Comment
Please, Sign In to add comment