This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

EZ430-RF2560 Bluetooth, PWM regulator, ADC conversion in accelerometer application

Hi
I'm modifying EZ430-RF2569 accelerometer application. I have modified sdk_spp_data_read:

void sdk_spp_data_read(UCHAR * buffer, UINT16 buf_len)
{
  float rez;
  char temp[6];
  int buf;
  rez = -1;
      

#ifdef EZ430_PLATFORM
 
  UART_DISABLE_BT_UART_RTS();
 
    
  rez = izmjeri_koncentraciju(76, 85, 312000, 51);
 
  buf = (int) rez;
  if (buf<0) buf = 0;
 
  temp[2] = buf / 1000 + 48;
  temp[3] = (buf / 100) % 10 + 48;
  temp[4] = (buf / 10) % 10 + 48;
  temp[5] = buf % 10 + 48;
 
  temp[0] = 'c';
  temp[1] = '=';
    
    

  UART_ENABLE_BT_UART_RTS();
    


#else /* MSP-EXP430F5438 Platform */
    /* start the ADC to read the accelerometer output */
    //halAdcStartRead();
    /* read the digital accelerometer x- direction and y - direction output */
    //halAccelerometerRead((int *)&accl_x, (int *)&accl_y);
#endif

    /* Pack the x and y co-ordinate values in to buffer */
    buffer[0] = temp[0];
    buffer[1] = temp[1];
    buffer[2] = temp[2];
    buffer[3] = temp[3];
    buffer[4] = temp[4];
    buffer[5] = temp[5];

}

and code for izmjeri_koncentraciju is :

float izmjeri_koncentraciju (unsigned int PHtip, unsigned int PHmax, float RL, short select)
{
  unsigned int Uref, US, i, d;
  float RS, PH, UH;
 
  WDTCTL = WDTPW + WDTHOLD;
 
  //set outputs low
  P8OUT &= ~BIT6;
  P4OUT &= ~BIT2;
  P4OUT &= ~BIT0;
  P8OUT &= ~BIT4;
 
  // set output depending on select
  if (select == 26) //log1
  {
    P8DIR |= BIT6;
    P8OUT |= BIT6;
  }
  else if (select == 27)  //log2
  {
    P4DIR |= BIT2;
    P4OUT |= BIT2;
  }
  else if (select == 51)  //log3
  {
    P4DIR |= BIT0;
    P4OUT |= BIT0;
  }
  else if (select == 55)  //log4
  {
    P8DIR |= BIT4;
    P8OUT |= BIT4;
  }
  else
    return -1;
 
  //   PWM on pin P8.2
  P8DIR |= BIT2;                            // P8.2 output
  P8SEL |= BIT2;                            // P8.2 options select
  TA0CCR0 = 200  ;                          // PWM Period, f = 5 kHz
  TA0CCTL2 = OUTMOD_7;                      // CCR2 reset/set
  TA0CCR2 = 100;                            // CCR2 PWM duty cycle, pocinje sa 70% max outputa
  TA0CTL = TASSEL_2 + MC_1 + TACLR;         // SMCLK, up mode, clear TAR
 
 
  //ADC  
  REFCTL0 &= ~REFMSTR; // no ADC ref
  REFCTL0 &= ~REFON;   // shutdown ref
  P7SEL |= BIT4;                            // P7.4 ADC option select
  ADC12CTL0 = ADC12ON + 0xB000;                  // ADC ON, 768 cycles
  ADC12CTL1 = ADC12SHP + 0xC000;                 // SHP, A12
  ADC12CTL2 = ADC12RES_2;                        // 12 bit resolution
  ADC12MCTL12 = ADC12INCH_12 + ADC12SREF_0;      // A12 to memory register 12, VCC ref
 
 
  // for cca. 5 seconds regulate PH
  for(i=0; i<455; i++)
  {
   ADC12IFG = 0;  /* Clear any pending flags */
   ADC12CTL0 |= ADC12ENC; // enable
   ADC12IE = 0x1000;
   ADC12CTL0 |= ADC12SC;  // start
   
   __delay_cycles(1000); //delay 0.001 s
   
   d = TA0CCR2;
   UH = ADC12MEM12 / 4095 * 3;
   PH = (float)d /1000 /4.99 * (UH - d);  
   
   // PH is dependent on PWM duty cycle
   // P regulator, if PH is to small, increase duty cycle of PWM, if to big, decrease duty cycle
   if (PH < 0.95 * PHmax && TA0CCR2 < 200)
     TA0CCR2++;
   if (PH > 0.95 * PHmax && TA0CCR2 > 0)
     TA0CCR2--;
      
   __delay_cycles(9000); //delay 0.009 s
  }
 
  // set output
  P4DIR |= BIT6;
  P4OUT |= BIT6;
 
  TA0CCR2 = 100;  // set duty on 50%
 
  // for cca. 9 seconds regulate PH
  for(i=0; i<865; i++)
  {  
     
   ADC12IFG = 0;  /* Clear any pending flags */
   ADC12CTL0 |= ADC12ENC; // enable
   ADC12IE = 0x1000;
   ADC12CTL0 |= ADC12SC;  // start
   
   __delay_cycles(1000); //delay 0.001 s
   
   d = TA0CCR2;
   UH = ADC12MEM12 / 4095 * 3;
   PH = (float)d /1000 /4.99 * (UH - d);   //PH u mW
   
   // P regulator
   if (PH <  PHtip && TA0CCR2 < 200)
     TA0CCR2++;
   if (PH >  PHtip && TA0CCR2 > 0)
     TA0CCR2--;
      
   __delay_cycles(9000); //delay 0.009 s
  }
 
   // ADC sequence, channel A13 and A14
  ADC12CTL0 = 0;
  REFCTL0 |= REFMSTR + REFTCOFF + REFON;  // turn on ref for ADC
  REFCTL0 &= ~0x0030;                     // ref = 1.5 V
  P7SEL = BIT5 + BIT6;                      // P7.5 i P7.6 ADC option select
  ADC12CTL0 = ADC12ON + 0xB000 + ADC12MSC; // ON, cycles, sequence
  ADC12CTL1 = ADC12SHP + ADC12CONSEQ_1 + 0xD000; // s&h, sequence once, A13 first signal
  ADC12CTL2 = ADC12RES_2;
  ADC12MCTL13 = ADC12INCH_13 + ADC12SREF_1; //A13 to MEM13, ref 1,5 V
  ADC12MCTL14 = ADC12INCH_14 + ADC12EOS + ADC12SREF_1; //A14 to MEM14, end of sequence, ref 1,5 V
   
  ADC12IFG = 0;  /* Clear any pending flags */
  ADC12CTL0 |= ADC12ENC; // enable conversion
  ADC12IE = 0x4000;     // enable interrupt
  ADC12CTL0 |= ADC12SC;  // start
 
  __delay_cycles(50000); //delay 0.05 s
     
  Uref = ADC12MEM13;  //
  US = ADC12MEM14;    //
  REFCTL0 &= ~REFMSTR; // disable ref
  REFCTL0 &= ~REFON;   // shutdown ref
       
  RS = ( (float) US/Uref - 1) * RL;  //calculate RS
 
  // set output low
  P4OUT &= ~BIT6;
    
  ADC12CTL0=0;  // turn off ADC
  TA0CTL = MC_0 + TACLR;         // Stop PWM counter, clear TAR  
  //set outputs low
  P8OUT &= ~BIT6;
  P4OUT &= ~BIT2;
  P4OUT &= ~BIT0;
  P8OUT &= ~BIT4;
 
  return RS;
}

I also modified the code so the data is send via Bluetooth only by pressing on switch 2. I've done that by commenting all calls of appl_send_spp_data and adding the same line in switch 2 case.
What I am trying to do is that switch 2 triggers appl_send_spp_data, which ultimately starts izmjeri_koncentraciju. However, it seems that Izmjeri_koncentraciju is never started or is started but is shortly terminated after.
Function should make AD conversions for first 5 seconds and regulate output with regard to result of AD conversion. Then it does the same thing for next cca. 9 seconds. After that it should make a sequence AD conversion and calculate the result, which is to be send via Bluetooth.
I would appreciate any suggestions on how to effectively incorporate this function in accelerometer application.

I've tried replacing izmjeri_koncentraciju code with simple AD conversion and sending result over Bluetooth. This works.
At the moment, I'm using switch 2 as trigger, but my final version should start appl_send_spp_data when a special command is received via Bluetooth.

Regards,
bj