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.

msp430f2012 + analogRead()

Other Parts Discussed in Thread: MSP430F2012

Hello,

I am having a couple issues with the final version of my  code, and am asking for help. I have decided to use analogRead( ) to determine when the code starts by referencing voltage across a switch. My question is when using the command analogRead(P1IN), which pin is it referencing?  Which pins can I use as inputs, as I am using pin 3&4 as PWM outputs. I am having trouble understanding this. because alot of pins are digital I/Os, but is there a specific pin used for  the analogRead() function.

The other issue is that every two pulses a pulse overlaps the next. I am assuming this is to do with the counter, that it reaches max and then resets causing the overlap. Is this correct? If so, what are some corrective actions to prevent pulse overlapping. I have tried varing the OUTMOD, which then prevents the desired signal from being achieved.


//Troubleshooting: Program A is good, Program B overlaps Peak and Hold

#include <msp430f2012.h>
#include <msp430.h>


void main(void)                   

 
  if(analogRead(P1IN)<0.5)      //Initiate cycle start

  {

  WDTCTL = WDTPW + WDTHOLD;      //Stop Watch Dog Timer
  BCSCTL1 = CALBC1_1MHZ;         //Set range
  DCOCTL = CALDCO_1MHZ;          //SMCLK = DCO = 1MHz
  P1OUT = 0;                     //Initializes P1.0 to off state
  P1DIR = BIT1 + BIT2;           //Sets P1.1 + P1.2 to output direction
 
  while(1)                       //Runs Continously
  {  
                     
  if(analogRead(P1IN)<0.5)      //Configure Switch (Choose Continous 5ms x 10ms)

  {
    
  P1SEL |= (BIT2);               //Declares  TA1
  TACTL= MC_1 + TASSEL_2;        //Tells CCR0 to count up with SMCLK
  TACCTL1=OUTMOD_7;              //Sets Set/Reset Mode with TACCR1
  TACCR0=10000-1;                //Set CCR0 to 100 Hz (10ms Period)
  TACCR1=5000;                   //Sets 50% duty cycle

  }
 
  // Peak and Hold Signal
 
 //Step 1 (High Side & Low Side Static for 2.5 ms or by I=1A)
   
  P1OUT |= BIT1;                 //Turns P1.1 on statically
  P1OUT |= BIT2;                 //Turns P1.2 on statically
  __delay_cycles(2500);          //2.5 ms hold time
 
 //Step 2 (High Side=Static Low Side=15 kHz)
  
  P1SEL |= (BIT2);               //Declares  TA1
  TACTL= MC_1 + TASSEL_2;        //Tells CCR0 to count up with SMCLK
  TACCTL1=OUTMOD_7;              //Sets Set/Reset Mode with TACCR1
  TACCR0=66-1;                   //Set CCR0 to 15 kHz (Hold Frequency)
  TACCR1=33;                     //Sets 50% duty cycle
  __delay_cycles(1500);          //1.5ms hold time
               
 //Step 3 (High side clamp on P1.1)
 
  TACCR1=0;                      // 0% duty cycle
  P1OUT &= BIT1;                 //Turns P1.1 off
  P1OUT &= BIT2;                 //Turns P1.2 off
  __delay_cycles(500);           //500 microsecond clamp
  P1OUT |= BIT1;                 //Turns P1.1 on
  __delay_cycles(500);           //550 microsecond on time
 
  //Step 4 (5ms off time)

  P1OUT &= BIT1;                 //Turns P1.1 off
  P1OUT &= BIT2;                 //Turns P1.2 off
  __delay_cycles(5000);          //5ms delay
 
  }}

  • Joshua Hatfield said:
    I have decided to use analogRead( )

    To avoid any confusion here - what's analogRead()?  - Never heard of it. Maybe you have source code and/or reference to original library(?) it comes from?

    [edit] Also it is not quite clear what you are trying to achieve with your code. Please describe.

  • Sorry for the confusion. My hardware will supply power to the MCU, and the program should not initate until a switch is flipped (As why I am trying to monitor a voltage input with AnalogRead() ). The code outputs two different PWM parameters which are chosen by a different switch. The MCU is to monitor and switch programs when the switch is flipped. The first PWM (Program A), outputs a simple 5ms x 10ms PWxP on pin 4. The second PWM (Program B) outputs a static signal and the switches to PWM @ 15kHz. This provides a peak and hold signal with a PWxP of 5ms x 10ms. This signal is shown below. The issue I am having is how to monitor the switches for switching from a high to low state. I used AnalogRead() by habbit from the use of the Arduino on other projects, and realized when you replied that it is not a funciton of the MSP430. If i need to monitor switches on Pin 2 and Pin 6 of a MSP430F2012, what is the easiest way about doing this? The other issue that has arised is in the peak and hold signal, every third pulse the pulse overlaps. I am assuming the clock resets and that point, and I need to figure a way to avoid this. Any help would be greatly appreciated.

  • Joshua Hatfield said:
    If i need to monitor switches on Pin 2 and Pin 6 of a MSP430F2012, what is the easiest way about doing this?

    Definitely not using ADC. Configure those pins as inputs and read their logic level. Download source code examples for your chip - from product page. There you will find sample how to configure and poll pin of port.

  • I went through some tuitorals online, and most are using the ADC for monitoring inputs. Please let me know if it is as simple as I attached below, or point me in the right direction. I set P1.0 + P1.4 to the input direction, and use an if statement to monitor the pin.

     

    void main(void)                   
    {
     
      WDTCTL = WDTPW + WDTHOLD;      //Stop Watch Dog Timer
      BCSCTL1 = CALBC1_1MHZ;         //Set range
      DCOCTL = CALDCO_1MHZ;          //SMCLK = DCO = 1MHz
      P1OUT = 0;                     //Initializes P1 to off state
      P1DIR = BIT1 + BIT2;           //Sets P1.1 + P1.2 to output direction
      P1DIR &= ~BIT0 + ~BIT4         //Sets P1.0 + P1.4 to input direction
        
          
      while(1)                       //Runs Continously
      {  
       
      if(BIT0>0.5)      //Initiate cycle start
      {  
       
      if(BIT4<0.5)      //Configure Switch (Choose Continous 5ms x 10ms)
      {

  • Joshua Hatfield said:
      if(BIT0>0.5)      //Initiate cycle start

    Don't you see yourself that this is not OK?


    if(P1IN & BIT0)      //Initiate cycle start
    { }

    //     

    if(P1IN & BIT4)      //Configure Switch (Choose Continous 5ms x 10ms)
    {  }
    // 

  • Joshua Hatfield said:
      P1DIR &= ~BIT0 + ~BIT4         //Sets P1.0 + P1.4 to input direction

    This line definitely doesn't do what you think it does.

    ~BIT0 os 0xFFFE and ~BIT4 is 0xFFEF. 0xFFFE+0xFFEF is 0x1FFED. Which then is trucated to 0xED. So the line resolves to  P1DIR = P1DIR & 0xED.
    Don't add bits with an arithmetic operator (+). use a bit operator (|) instead. And first join the bits, then invert the value:

    P1DIR &= ~(BIT0|BIT4);

    Joshua Hatfield said:
      if(BIT0>0.5)      //Initiate cycle start

    BIT0 equals 0x01. So this condition is always true.

    I guess you meant

    if ((P1IN & BIT0)>0.5). Bit since P1IN&NIT0 can only be 0 or BIT0 (==1), the comparison is superfluous. SImply do

    if (P1IN & BIT0)
    It is true if BIT0 is set (input is high) and false if BIT0 is clear (input is low).

    Same for the other IF with BIT4.

  • Thank you for the explanation of my mistake. I am trying to learn the MSP430, as I have only worked with the Arduino in the past. It is a completly different animal, and like the explanation of why I am changing something. I will experiment with this and run a few trials. Thank you very much

**Attention** This is a public forum