Thread: Variable change query

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    6

    Variable change query

    Hi,
    Below is an example code that PWM's an LED continually up/down. Variable pwmDirection becomes +/-. You will see at the bottom of the code that if pwmDirection is <20 (as we are subtracting), the variable pwmDirection is changed to -pwmDirection. My question is if pwmDirection is already (-) and we make it (-) again how does it become (+)? The code is working as it should, im trying to learn why. Thanks in advance!
    Code:
    
    #include<msp430g2553.h>
    int pwmDirection = 1;
    void main(void){
    Code:
    WDTCTL = WDT_MDLY_32; // Watchdog timer ≈32ms
    IE1 |= WDTIE; // enable Watchdog timer interrupts
    P1DIR |= BIT6; // Green LED for output P1SEL |= BIT6; // Green LED Pulse width modulation
    TA0CCR0 = 1000; // PWM period
    TA0CCR1 = 1; // PWM duty cycle, time cycle on vs. off, on 1/1000 initially
    TA0CCTL1 = OUTMOD_7; // TA0CCR1 reset/set -- high voltage below count and // low voltage when past
    TA0CTL = TASSEL_2 + MC_1; // Timer A control set to SMCLK clock TASSEL_2, 1MHz // and count up mode MC_1 _BIS_SR(LPM0_bits + GIE); // Enter Low power mode 0 } #pragma vector=WDT_VECTOR // Watchdog Timer interrupt service routine __interrupt void watchdog_timer(void) { TA0CCR1 += pwmDirection*20; // Increase/decrease duty cycle, on vs. off time if( TA0CCR1 > 980 || TA0CCR1 < 20 ) // Pulse brighter (increasing TA0CCR1) or dimmer pwmDirection = -pwmDirection; }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    pwmDirection = -pwmDirection;
    Yes is will
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    Yes, I know it will because I've ran the code! Please could you explain why as I understand '=' is making an assignment so it should become (-)?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The unary - operator changes sign in C, for any signed numeric type (int, float, long, double, signed char, etc). Mathematically it is equivalent to multiplying by -1. Which turns a positive value to a negative value, and vice versa. The only limitation is that the result has to be representable for that type.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    To elaborate on grumpy, this is the same
    Code:
    pwmDirection = (-1) * pwmDirection;
    and -1 * -1 is 1
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    Thanks for this grumpy and click_here, I only found this multiplication by -1 was occurring while running this code. It's not something I would expect to happen. I am confused why the left side just does not equal the right side with pwmDirection = -pwmDirection as an assignment should...I hope you understand the confusion!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to know when a value of a variable change?
    By Hannibal2010 in forum C Programming
    Replies: 12
    Last Post: 11-25-2011, 02:16 PM
  2. Can't change variable from another function...
    By Ryan. in forum C Programming
    Replies: 9
    Last Post: 03-11-2011, 12:10 AM
  3. Replies: 4
    Last Post: 02-28-2011, 07:57 AM
  4. change variable and repeat
    By altf4thc in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2010, 05:27 PM
  5. Replies: 2
    Last Post: 12-11-2009, 03:43 PM