Jump to content
SamSMK

Battery voltage reader.

Recommended Posts

I have made this code with my group 

 

now we want to edit it and make the code read voltage of a battery but we're not sure how to edit it because the professor want it in the same form as below and by the simple C

 

Please don't forget to put comments so I would know what you actually changed



float ThresholdExceeded = 0;
char x[] = " out of 3.3v \n";
 volatile unsigned int i=2001;
void setup()
{
 
ADC12CTL0 = ADC12SHT02 + ADC12ON;         // Sampling time, ADC12 on
  ADC12CTL1 = ADC12SHP;                     // Use sampling timer
  ADC12IE = 0x01;                           // Enable interrupt
  ADC12CTL0 |= ADC12ENC;
  P6SEL |= 0x01;                            // P6.0 ADC option select
  P1DIR |= 0x01;                            // P1.0 output
  P4DIR |= 0x80;                            // P4.7 output
  Serial.begin(9600);
}
 
void loop()
{
   ADC12CTL0 |= ADC12SC;                   // Start sampling/conversion
    
    __bis_SR_register(LPM0_bits + GIE);     // LPM0, ADC12_ISR will force exit
    __no_operation();                       // For debugger
 /* Serial.println(ThresholdExceeded); */
 if(i<1000)                                  // continuous loop
  {
    P1OUT |= BIT0;                          // P1.0=1
    /* for(i=50000;i>0;i--);          */         // Delay  
  }
  else
  { P1OUT &= ~BIT0; }                   // P1.0 = 0
   Serial.println(i);
  i=i+1;
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = ADC12_VECTOR
__interrupt void ADC12_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(ADC12_VECTOR))) ADC12_ISR (void)
#else
#error Compiler not supported!
#endif
{
  switch(/*__even_in_range(ADC12IV,34)*/ADC12IV)
  {
  case  0: break;                           // Vector  0:  No interrupt
  case  2: break;                           // Vector  2:  ADC overflow
  case  4: break;                           // Vector  4:  ADC timing overflow
  case  6:                                  // Vector  6:  ADC12IFG0
    if (ADC12MEM0 >= 0xFA0)                 // ADC12MEM = A0 > 0.5AVcc?
     {i=0;}
     /* {P4OUT &= ~BIT7;                       // P4.7 = 0
      P1OUT &= ~BIT0; }                       // P1.0 = 0
   /* else if (ADC12MEM0 <= 0x6d6)
      {P4OUT &= ~BIT7;                     // P4.7 = 0
       P1OUT |= BIT0; }                       // P1.0 = 1
    else if (ADC12MEM0 <= 0x794)
      {P4OUT |= BIT7;                     // P4.7 = 0
       P1OUT |= BIT0; }    
    /*else 
        {P4OUT &= ~BIT7;                        // P4.7 = 1
       P1OUT &= ~BIT0; }                      // P1.0 = 1 */
       
    __bic_SR_register_on_exit(LPM0_bits);   // Exit active CPU
  case  8: break;                           // Vector  8:  ADC12IFG1
  case 10: break;                           // Vector 10:  ADC12IFG2
  case 12: break;                           // Vector 12:  ADC12IFG3
  case 14: break;                           // Vector 14:  ADC12IFG4
  case 16: break;                           // Vector 16:  ADC12IFG5
  case 18: break;                           // Vector 18:  ADC12IFG6
  case 20: break;                           // Vector 20:  ADC12IFG7
  case 22: break;                           // Vector 22:  ADC12IFG8
  case 24: break;                           // Vector 24:  ADC12IFG9
  case 26: break;                           // Vector 26:  ADC12IFG10
  case 28: break;                           // Vector 28:  ADC12IFG11
  case 30: break;                           // Vector 30:  ADC12IFG12
  case 32: break;                           // Vector 32:  ADC12IFG13
  case 34: break;                           // Vector 34:  ADC12IFG14
  default: break; 
  }
  ThresholdExceeded = ADC12MEM0;
 
}
 

Share this post


Link to post
Share on other sites

Hi Sam, when asking for help you should try to provide as much info as possible. 

What part are you using?

Could you elaborate more what the could should do. I understand that the final product is reading the battery voltage - I am asking for more info on for example the ISR.

Simple C? You mean no direct register access? Like using MSPWare or Energia?

Share this post


Link to post
Share on other sites

Hi Sam, when asking for help you should try to provide as much info as possible. 

 

What part are you using?

 

Could you elaborate more what the could should do. I understand that the final product is reading the battery voltage - I am asking for more info on for example the ISR.

 

Simple C? You mean no direct register access? Like using MSPWare or Energia?

MSP 430

We need to edit it and make it read a battery voltage

 

and we can't use normal c or c++

Share this post


Link to post
Share on other sites

Sounds pretty straight forward in Energia.

 

Read an analog pin, scale the reading appropriately, then serial print the value.

 

Have to be careful that the battery voltage doesn't exceed the MSP430's ADC range, based on which analog reference you select.

 

And you still didn't say which one- makes a difference with regards to the bit depth of the ADC and scaling the reading.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...