Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.08 KB | None | 0 0
  1. /*
  2. * invio tramite Bluetooth Classic dei segnali acquisiti dall'ADC interno al microcontrollore
  3. * con una frequenza di campionamento di circa 10hz
  4. */
  5. //******************************************************************************
  6.  
  7. #include <msp430.h>
  8. #include <stdint.h>
  9. #include <ads_.h>
  10. #include <ads1292.h>
  11.  
  12. #define ADC_TIMER_PERIOD 16384 // 32768 Hz/Timer_period = F_sample Hz.(Modificare se necessario)
  13. #define Num_of_Results 8
  14. volatile unsigned int A0results[Num_of_Results];
  15. volatile unsigned int A1results[Num_of_Results];
  16. volatile unsigned int A2results[Num_of_Results];
  17. volatile unsigned int A3results[Num_of_Results];
  18. #define ACK "OK\0"
  19. #define SAMPLETOBESENT 22 // + 9START + 4 dati + 9STOP
  20. //#define DIMPACKET 20
  21. //unsigned char rec_chars[1024];
  22.  
  23. //------------Setting Variables---------
  24.  
  25. ////-------------check respiro-----------------
  26. //int RESP_PH [15]={0x06,0x0A,0x0C,0x10,0x14,0x18,0x1C,0x20,0x24,0x28, 0x2C,0x30,0x34,0x38,0x3C};
  27. //int flag_resp_funz=0;
  28. //int resp_ph_ind=0;
  29. //int check=0;
  30. //char b=0;
  31.  
  32. unsigned char data_buffer[SAMPLETOBESENT];
  33. int device_state = IDLE;
  34. int ignore_zero = 0;
  35. Cmode connection_mean;
  36. unsigned char configuration[12];
  37. int status;
  38.  
  39. //------------------Flag-----------------------------
  40. unsigned char controlFlag = 0;
  41. unsigned char dataReady = 0;
  42. unsigned char readySend = 1;
  43. unsigned char btconnectionFlag = 0;
  44.  
  45. //-----------Bluetooth---------------------------------------------------------
  46. unsigned char string[20]; //valore casuale
  47. int istr = 0; //contatore per debug
  48. volatile unsigned char ready = 0;
  49.  
  50. //--------debugging--------------------
  51. unsigned char ping[SAMPLETOBESENT];
  52. unsigned char pong[SAMPLETOBESENT];
  53. unsigned char *ppointer;
  54. unsigned char *popointer;
  55. unsigned char *temp;
  56. int j = 0;
  57. //int j2 = 0;
  58. unsigned char tempchar;
  59. unsigned long conteggiopacchetti=0;
  60.  
  61. //unsigned char debugging[SAMPLETOBESENT];
  62.  
  63. //------------Function Declaration----------------------------------------------
  64. //void readSamplesAndForward(void);
  65. void MSP430init(void);
  66. //void start_ADS(void);
  67. void start_ADC(void);
  68. //void Init_RTC(int hour, int min, int sec, int day, int month, int year);
  69. //void processCtrlMessage(void);
  70. //void readSamplesAndForward(void);
  71. void (*send_Response_to_Host)(unsigned char*, int, int) = NULL;
  72.  
  73.  
  74. int main(void)
  75. {
  76.  
  77. //ppointer = &ping[0];
  78. // popointer = &pong[0];
  79. //unsigned char *data_to_send;
  80. //int dim_data_to_send = SAMPLETOBESENT+DIMPACKET;
  81.  
  82. MSP430init();
  83. //init_MSP430_to_ADS_interface();
  84. init_Bluetooth_interface();
  85. // power_up_ADS1192();
  86. // init_Bluetooth();
  87. connect_Bluetooth();
  88. //int counter = 0;
  89. //unsigned char i;
  90.  
  91. while(1)
  92. {
  93. if (ready == 1)
  94. {
  95. ready = 0;
  96.  
  97. ppointer = &ping[0];
  98. popointer = &pong[0];
  99. j=0;
  100.  
  101. //
  102. // *(ppointer+j)= 0x7E;
  103. // *(ppointer+j+1)= 0x7E;
  104. // *(ppointer+j+2)= 0x7E;
  105. // *(ppointer+j+3)= 0x7E;
  106. // *(ppointer+j+4)= 0x7E;
  107. // *(ppointer+j+5)= 0x7E;
  108. // *(ppointer+j+6)= 0x7E;
  109. // *(ppointer+j+7)= 0x7E;
  110. // *(ppointer+j+8)= 0x7E;
  111.  
  112. *(ppointer+j)= 0x01;
  113. *(ppointer+j+1)= 0x02;
  114. *(ppointer+j+2)= 0x03;
  115. *(ppointer+j+3)= 0x04;
  116. *(ppointer+j+4)= 0x05;
  117. *(ppointer+j+5)= 0x06;
  118. *(ppointer+j+6)= 0x07;
  119. *(ppointer+j+7)= 0x08;
  120. *(ppointer+j+8)= 0x09;
  121. send_string_UART_Bluetooth(ppointer,9,0);
  122.  
  123.  
  124.  
  125. // //conteggio pacchetti
  126. // *(ppointer+j+11)= tempchar;
  127. // *(ppointer+j+12)= tempchar;
  128. // *(ppointer+j+13)= tempchar;
  129. // *(ppointer+j+14)= tempchar;
  130.  
  131. // j=j+9;
  132. // //__disable_interrupt(); //Disable interrupts globally
  133. // start_ADC();
  134. // //send_string_UART_Bluetooth(&debugging[0],3,0);
  135. // //UCA1IE &= ~UCRXIE;
  136. // //readySend = 1;
  137. __enable_interrupt(); //Enable interrupts globally
  138. }
  139. //
  140. // if ((dataReady == 1)&&(readySend == 1))
  141. // {
  142. // dataReady = 0;
  143. // readySend = 0;
  144. // send_string_UART_Bluetooth(popointer,SAMPLETOBESENT,0);
  145. //
  146. // }
  147.  
  148. _bis_SR_register(LPM0_bits); // go to low power mode
  149. }
  150. }
  151.  
  152.  
  153.  
  154.  
  155. void MSP430init(void)
  156. {
  157. WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
  158.  
  159. P8DIR |= BIT1;
  160. P8OUT &= ~BIT1; // LED2=P8.1
  161.  
  162. P6SEL = 0x0F; // Enable A/D channel inputs
  163.  
  164. P7DIR |= BIT7; // MCLK set out to pins
  165. P7SEL |= BIT7;
  166. P1DIR |= BIT0; // ACLK set out to pins
  167. P1SEL |= BIT0;
  168.  
  169. //------------------XT1-----------------------------------------------------------------
  170. P5SEL |= BIT4+BIT5; // Port select XT1
  171. UCSCTL6 &= ~(XT1OFF); // XT1 On
  172. UCSCTL6 |= XCAP_0; // Internal load cap
  173. // Loop until XT1 fault flag is cleared
  174. do
  175. {
  176. UCSCTL7 &= ~XT1LFOFFG; // Clear XT1 fault flags
  177. }while (UCSCTL7&XT1LFOFFG); // Test XT1 fault flag
  178.  
  179. //------------------DCO--------
  180.  
  181. __bis_SR_register(SCG0); //disable FLL
  182.  
  183. UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
  184. UCSCTL2 = FLLD_1 + 374; // Set DCO Multiplier for 12.5MHz
  185. // (N + 1) * FLLRef = Fdco
  186. // (374 + 1) * 32768 = 12MHz
  187. // Set FLL Div = fDCOCLK/2
  188. UCSCTL1 = DCORSEL_5; // Select DCO range 24MHz operation
  189.  
  190. UCSCTL4 = SELA__XT1CLK + SELS__DCOCLKDIV + SELM__DCOCLKDIV;
  191.  
  192. __bic_SR_register(SCG0); // Enable the FLL control loop
  193.  
  194. // Worst-case settling time for the DCO when the DCO range bits have been
  195. // changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
  196. // UG for optimization.
  197. // 32 x 32 x 12 MHz / 32,768 Hz = 375000 = MCLK cycles for DCO to settle
  198. // __delay_cycles(375000);
  199.  
  200. // Loop until XT1,XT2 & DCO fault flag is cleared
  201. do
  202. {
  203. UCSCTL7 &= ~(DCOFFG);
  204. // Clear XT2,XT1,DCO fault flags
  205. SFRIFG1 &= ~OFIFG; // Clear fault flags
  206. }while (SFRIFG1&OFIFG); // Test oscillator fault flag
  207.  
  208.  
  209. }
  210. void start_ADC(void)
  211. {
  212. ADC12CTL1 = ADC12SHP+ADC12CONSEQ_1; // Use sampling timer, one burst per trigger
  213. ADC12CTL2=ADC12RES_0;// 8bit
  214.  
  215. ADC12MCTL0 = ADC12INCH_0; // ref+=AVcc, channel = A0
  216. ADC12MCTL1 = ADC12INCH_1; // ref+=AVcc, channel = A1
  217. ADC12MCTL2 = ADC12INCH_2; // ref+=AVcc, channel = A2
  218. ADC12MCTL3 = ADC12INCH_3+ADC12EOS; // ref+=AVcc, channel = A3, end seq.
  219. ADC12IE = 0x08; // Enable ADC12IFG.3
  220.  
  221. // SLAS590M Table 6-11 shows that ADC12SHS=1 corresponds to
  222. // TA0:CCI1B, i.e. a pulse generated via TA0CCR1 on the F5529.
  223. // For others, check the data sheet.
  224. // This code generates a pulse train with a rising edge halfway
  225. // through the period for no particular reason.
  226. TA0CCTL1 = OUTMOD_3; // Set/Reset for rising edge
  227. TA0CCR1 = ADC_TIMER_PERIOD/2-1; // halfway through
  228. TA0CCR0 = ADC_TIMER_PERIOD-1; // Cycle every 20000 SMCLKs, i.e. 50Hz.
  229. ADC12CTL1 |= ADC12SHS_1; // SHS=1 means TA0:CCI1B on the F5529
  230. ADC12CTL0 |= ADC12ENC; // Note this locks sundry ADC12 fields.
  231. TA0CTL = TASSEL_1 | ID_0 | MC_1 | TACLR; // ACLK, /1, Up (,clear)
  232. }
  233. //
  234. //#pragma vector=PORT2_VECTOR
  235. //interrupt void data_ready_int(void)
  236. //{
  237. // //_bic_SR_register(LPM3_bits);
  238. // //DISABLE_DRDY_INT;
  239. //
  240. // P2IFG&=~DATA_READY_PIN;
  241. // //dataFlag = 1;
  242. //
  243. // while(!(UCA0IFG&UCTXIFG));
  244. // UCA0TXBUF = 0x00; // Perform dummy write in order to receive data
  245. // //__delay_cycles(TX_DEL);
  246. // while(!(UCA0IFG&UCRXIFG)); // Wait until word is received
  247. // data_buffer[0] = UCA0RXBUF;
  248. // UCA0TXBUF = 0x00; // Perform dummy write in order to receive data
  249. // //__delay_cycles(TX_DEL);
  250. // while(!(UCA0IFG&UCRXIFG)); // Wait until word is received
  251. // data_buffer[1] = UCA0RXBUF;
  252. // UCA0TXBUF = 0x00; // Perform dummy write in order to receive data
  253. // //__delay_cycles(TX_DEL);
  254. // while(!(UCA0IFG&UCRXIFG)); // Wait until word is received
  255. // data_buffer[2] = UCA0RXBUF;
  256. // UCA0TXBUF = 0x00; // Perform dummy write in order to receive data
  257. // //__delay_cycles(TX_DEL);
  258. // while(!(UCA0IFG&UCRXIFG)); // Wait until word is received
  259. // *(ppointer+j) = UCA0RXBUF;
  260. // UCA0TXBUF = 0x00; // Perform dummy write in order to receive data
  261. // //__delay_cycles(TX_DEL);
  262. // while(!(UCA0IFG&UCRXIFG)); // Wait until word is received
  263. // *(ppointer+j+1) = UCA0RXBUF;
  264. // UCA0TXBUF = 0x00; // Perform dummy write in order to receive data
  265. // //__delay_cycles(TX_DEL);
  266. // while(!(UCA0IFG&UCRXIFG)); // Wait until word is received
  267. // *(ppointer+j+2) = UCA0RXBUF;
  268. // UCA0TXBUF = 0x00; // Perform dummy write in order to receive data
  269. // //__delay_cycles(TX_DEL);
  270. // while(!(UCA0IFG&UCRXIFG)); // Wait until word is received
  271. // *(ppointer+j+3) = UCA0RXBUF;
  272. // UCA0TXBUF = 0x00; // Perform dummy write in order to receive data
  273. // //__delay_cycles(TX_DEL);
  274. // while(!(UCA0IFG&UCRXIFG)); // Wait until word is received
  275. // *(ppointer+j+4) = UCA0RXBUF;
  276. // UCA0TXBUF = 0x00; // Perform dummy write in order to receive data
  277. // //__delay_cycles(TX_DEL);
  278. // while(!(UCA0IFG&UCRXIFG)); // Wait until word is received
  279. // *(ppointer+j+5) = UCA0RXBUF;
  280. //
  281. // j = j+6;
  282. //
  283. // if (j == SAMPLETOBESENT-9)
  284. // {
  285. //
  286. // conteggiopacchetti=conteggiopacchetti+1;
  287. //
  288. // *(ppointer+j)= 0x7F;
  289. // *(ppointer+j+1)= 0x7F;
  290. // *(ppointer+j+2)= 0x7F;
  291. // *(ppointer+j+3)= 0x7F;
  292. // *(ppointer+j+4)= 0x7F;
  293. // *(ppointer+j+5)= 0x7F;
  294. // *(ppointer+j+6)= 0x7F;
  295. // *(ppointer+j+7)= 0x7F;
  296. // *(ppointer+j+8)= 0x7F;
  297. //
  298. // j=0;
  299. // dataReady = 1;
  300. // temp = ppointer;
  301. // ppointer = popointer;
  302. // popointer = temp;
  303. //
  304. // *(ppointer+j)= 0x7E;
  305. // *(ppointer+j+1)= 0x7E;
  306. // *(ppointer+j+2)= 0x7E;
  307. // *(ppointer+j+3)= 0x7E;
  308. // *(ppointer+j+4)= 0x7E;
  309. // *(ppointer+j+5)= 0x7E;
  310. // *(ppointer+j+6)= 0x7E;
  311. // *(ppointer+j+7)= 0x7E;
  312. // *(ppointer+j+8)= 0x7E;
  313. //
  314. //
  315. // //heart and respiration rate
  316. // *(ppointer+j+9)= 0x50; //heart rate
  317. // *(ppointer+j+10)= 0x10; //breath rate
  318. //
  319. // //conteggio pacchetti
  320. // tempchar=(char)(conteggiopacchetti&&0xFF);
  321. // *(ppointer+j+14)= tempchar;
  322. //
  323. // tempchar=(char)((conteggiopacchetti>>8)&&0xFF);
  324. // *(ppointer+j+13)= tempchar;
  325. //
  326. // tempchar=(char)((conteggiopacchetti>>16)&&0xFF);
  327. // *(ppointer+j+12)= tempchar;
  328. //
  329. // tempchar=(char)((conteggiopacchetti>>24)&&0xFF);
  330. // *(ppointer+j+11)= tempchar;
  331. //
  332. // j=j+15;
  333. //
  334. // }
  335. //
  336. // _bic_SR_register_on_exit(LPM0_bits);
  337. //}
  338.  
  339.  
  340. //#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  341. //#pragma vector=ADC12_VECTOR
  342. //__interrupt void ADC12ISR (void)
  343. //#elif defined(__GNUC__)
  344. //void __attribute__ ((interrupt(ADC12_VECTOR))) ADC12ISR (void)
  345. //#else
  346. //#error Compiler not supported!
  347. //#endif
  348. //{
  349. // static unsigned int index = 0;
  350. //
  351. // switch(__even_in_range(ADC12IV,34))
  352. // {
  353. // case 0: break; // Vector 0: No interrupt
  354. // case 2: break; // Vector 2: ADC overflow
  355. // case 4: break; // Vector 4: ADC timing overflow
  356. // case 6: break; // Vector 6: ADC12IFG0
  357. // case 8: break; // Vector 8: ADC12IFG1
  358. // case 10: break; // Vector 10: ADC12IFG2
  359. // case 12: // Vector 12: ADC12IFG3
  360. //// A0results[index] = ADC12MEM0; // Move A0 results, IFG is cleared
  361. //// A1results[index] = ADC12MEM1; // Move A1 results, IFG is cleared
  362. //// A2results[index] = ADC12MEM2; // Move A2 results, IFG is cleared
  363. //// A3results[index] = ADC12MEM3; // Move A3 results, IFG is cleared
  364. ////
  365. //// *(ppointer+j) = A0results;
  366. //// *(ppointer+j+1) = A1results;
  367. //// *(ppointer+j+2) = A2results;
  368. //// *(ppointer+j+3) = A3results;
  369. //
  370. // *(ppointer+j) = ADC12MEM0;
  371. // *(ppointer+j+1) = ADC12MEM1;
  372. // *(ppointer+j+2) = ADC12MEM2;
  373. // *(ppointer+j+3) = ADC12MEM3;
  374. //
  375. //
  376. // j = j+4;
  377. //
  378. // if (j == SAMPLETOBESENT-9)
  379. // {
  380. //
  381. // //conteggiopacchetti=conteggiopacchetti+1;
  382. //
  383. // *(ppointer+j)= 0x7F;
  384. // *(ppointer+j+1)= 0x7F;
  385. // *(ppointer+j+2)= 0x7F;
  386. // *(ppointer+j+3)= 0x7F;
  387. // *(ppointer+j+4)= 0x7F;
  388. // *(ppointer+j+5)= 0x7F;
  389. // *(ppointer+j+6)= 0x7F;
  390. // *(ppointer+j+7)= 0x7F;
  391. // *(ppointer+j+8)= 0x7F;
  392. //
  393. // j=0;
  394. // dataReady = 1;
  395. // temp = ppointer;
  396. // ppointer = popointer;
  397. // popointer = temp;
  398. //
  399. // *(ppointer+j)= 0x7E;
  400. // *(ppointer+j+1)= 0x7E;
  401. // *(ppointer+j+2)= 0x7E;
  402. // *(ppointer+j+3)= 0x7E;
  403. // *(ppointer+j+4)= 0x7E;
  404. // *(ppointer+j+5)= 0x7E;
  405. // *(ppointer+j+6)= 0x7E;
  406. // *(ppointer+j+7)= 0x7E;
  407. // *(ppointer+j+8)= 0x7E;
  408. //
  409. //
  410. // j=j+9;
  411. //
  412. // }
  413. ////
  414. //
  415. // ADC12CTL0 &= ~ADC12ENC; // Re-arm burst trigger
  416. // ADC12CTL0 |= ADC12ENC; // See also SLAU208P Fig 28-8
  417. //
  418. // index++; // Increment results index, modulo; Set Breakpoint1 here
  419. //
  420. // if (index == Num_of_Results)
  421. // {
  422. // index = 0;
  423. //
  424. // P1OUT ^= BIT0; // Toggle LED1 Fcamp/num_results/toggle (1Hz/1/2=~0.5Hz)
  425. //
  426. // }
  427. // case 14: break; // Vector 14: ADC12IFG4
  428. // case 16: break; // Vector 16: ADC12IFG5
  429. // case 18: break; // Vector 18: ADC12IFG6
  430. // case 20: break; // Vector 20: ADC12IFG7
  431. // case 22: break; // Vector 22: ADC12IFG8
  432. // case 24: break; // Vector 24: ADC12IFG9
  433. // case 26: break; // Vector 26: ADC12IFG10
  434. // case 28: break; // Vector 28: ADC12IFG11
  435. // case 30: break; // Vector 30: ADC12IFG12
  436. // case 32: break; // Vector 32: ADC12IFG13
  437. // case 34: break; // Vector 34: ADC12IFG14
  438. // default: break;
  439. // }
  440. //}
  441. //
  442. //
  443.  
  444.  
  445. #pragma vector=USCI_A1_VECTOR
  446. interrupt void bluetooth_RX_int(void)
  447. {
  448. string[istr] = UCA1RXBUF;
  449. if(string[istr]==0x7D)
  450. {
  451. //device_state=READY;
  452. ready = 1;
  453. }
  454.  
  455. if(string[istr]==0xFF)
  456. {
  457. readySend = 1;
  458. }
  459. if(string[istr]==0xFE)
  460. { ready=0;
  461. }
  462. // if(string[istr]==0xFE)
  463. // {START_PORT&=~
  464. // START_PIN;
  465. // CS_PORT|=CS_PIN;
  466.  
  467. // controlFlag = 1;
  468. // istr ++;
  469. // if (istr == 20)
  470. // istr = 0;
  471.  
  472. _bic_SR_register_on_exit(LPM0_bits); // exit from low power mode on exit
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement