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.

Magnetic Filed sensor with msp430G2553

Other Parts Discussed in Thread: MSP430G2553

Hello I am a bit new but still trying to do something meaningful, I've already gone trough the tutorials, for lighting up leds and using buttons and ect. now I'd like to ask you about that, I've got an US1881 Hall Latch, and I am trying to detect magnetic field with 430g2553, the idea is I've got a diode with 1k resistor connected to I've got a red diode connected to P1.0 (bit0) and the signal pin of the Hall Latch to P1.6(bit6) but I am having difficulties with the code here is what I've got for now.

#include<msp430G2553.h>

void main(void){
WDTCTL = WDTPW + WDTHOLD;
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALDCO_1MHZ;
    P1DIR=0x0F;   /// P1.0 through P1.3 will be outputs, while P1.4 through P1.7 will be inputs.
    P1OUT |= 0x01; // turn bit zero of port 1 on. P1.0 is now high

while (1)
    if (0x0F == 1)
    {
        P1OUT |= 0x01; // turn bit zero of port 1 on. P1.0 is now high
    }
    else {
        P1OUT &= 0xFE;  //turn bit zero of port 1 off. P1.0 is now low
    }
}

I've got a red diode connected to P1.0 (bit0) and the signal pin of the Hall Latch to P1.6(bit6)

my idea is: when Hall Latch feels magnetic filed, the diode goes RED, when no magnetic field is sensed the the diode is off.

the diode does not light up :(


please assist :/

  • Georgi Daskalov said:
    while (1)
        if (0x0F == 1)


     The if clause has an expression in parenthesis that forever evaluate false!

     Your writing is not so linear and appear you wish to do some very simple operation just turning on or off led following Hall sensor:

     I think your intention where to use bit6 of P1 to sense hall status then set led so:

    if(P1IN &BIT6)

     do what you are tryingg to do

  • Well I tried that but did not work also...:/

  • Georgi Daskalov said:
    while (1)
        if (0x0F == 1)
        {
            P1OUT |= 0x01; // turn bit zero of port 1 on. P1.0 is now high

     Just to remove doubt , try this and see if led power on...

    if (1)

    {

     On the other way I can think you don't provided one pullup resistor, try this way:

    Georgi Daskalov said:
        P1DIR=0x0F;   /// P1.0 through P1.3 will be outputs, while P1.4 through P1.7 will be inputs.
        P1OUT |= 0x01; // turn bit zero of port 1 on. P1.0 is now high

        P1OUT |= BIT0 | BIT6; // turn bit zero & 6 of port 1 on. P1.0, P1.6  are now high

       P1REN = BIT6;  // turn on pullup resistor

  • yep it turned it on... thanks!

    but still can get input from BIT6 and continue to struggle :/

    #include<msp430G2553.h>
    void main(void){
    WDTCTL = WDTPW + WDTHOLD;
        BCSCTL1 = CALBC1_1MHZ;
        DCOCTL = CALDCO_1MHZ;
        P1DIR=0x0F;   /// P1.0 through P1.3 will be outputs, while P1.4 through P1.7 will be inputs.
        P1OUT |= BIT0 | BIT6; // turn bit zero & 6 of port 1 on. P1.0, P1.6  are now high
        P1REN = BIT6;  // turn on pullup resistor
        P1DIR = 0x00; // switiching the LED off,
    while (1)
        if ((P1IN & BIT6) == 1) // this is supposed to trigger the LED on when I put the magnet next to the Hall sensor, but nothing happens :X

        {
            P1OUT |= 0x01; // turn bit zero of port 1 on. P1.0 is now high
        }
        if((P1IN & BIT6) != 1) // this gets warning of STATEMENT UNREACHABLE (wtf) ?!
        {
            P1OUT &= 0xFE;  //turn bit zero of port 1 off. P1.0 is now low
        }
    }


  •  Oh my.. A basic C course?? ;)

     See error one at time and learn many error programmer still do:

     for( ); // this just lose time but never do next line, semicolon terminate for

     while(); // next instruction never get in loop

    while()

     statement <--- this is while part

    {statement... statement... } <-- this is a composed statement, no error from compiler but this is equivalent of one line

    ------------------   now see your code:

    Georgi Daskalov said:
        P1DIR = 0x00; // switiching the LED off,

     this switch the led off forever, P1 is input remove this line

    Georgi Daskalov said:
    while (1)
        if ((P1IN & BIT6) == 1) // this is supposed to trigger the LED on when I put the magnet next to the Hall sensor, but nothing happens :X

        {
            P1OUT |= 0x01; // turn bit zero of port 1 on. P1.0 is now high
        }

     this statement get executed forever

     what follow never get executed so code is unreachable

    Georgi Daskalov said:
    #include<msp430G2553.h>
    void main(void){
    WDTCTL = WDTPW + WDTHOLD;
        BCSCTL1 = CALBC1_1MHZ;
        DCOCTL = CALDCO_1MHZ;
        P1DIR=0x0F;   /// P1.0 through P1.3 will be outputs, while P1.4 through P1.7 will be inputs.
        P1OUT |= BIT0 | BIT6; // turn bit zero & 6 of port 1 on. P1.0, P1.6  are now high
        P1REN = BIT6;  // turn on pullup resistor
        P1DIR = 0x00; // switiching the LED off,
    while (1) 

    // <-missing
        if ((P1IN & BIT6) == 1) // this is supposed to trigger the LED on when I put the magnet next to the Hall sensor, but nothing happens :X

        {
            P1OUT |= 0x01; // turn bit zero of port 1 on. P1.0 is now high
        }
        if((P1IN & BIT6) != 1) // this gets warning of STATEMENT UNREACHABLE (wtf) ?!
        {
            P1OUT &= 0xFE;  //turn bit zero of port 1 off. P1.0 is now low
        }
    }

    P1&BIT6

       when BIT6 is high return BIT6 value and is not zero or true but not 1

       when BIT6 is LOW return ZERO value and is NOT true so it is false

     false IS NOT !=1 is true.. but code forever unreachable....

    & | ^ ~ are arithmetic logic operator and or xor not and act to bit operation bit for bit

    && || ! are logic operator, act to value, if zero false if not zero TRUE.. arithmetic has precedence

    this execute when BIT HIGH

    if (P1IN & BIT6)

    this execute when BIT LOW

          if (!(P1IN & BIT6))

    
    
    #include<msp430G2553.h>
     void main(void)
    { WDTCTL = WDTPW + WDTHOLD;
     BCSCTL1 = CALBC1_1MHZ;
     DCOCTL = CALDCO_1MHZ;
     P1DIR=0x0F; /// P1.0 through P1.3 will be outputs, while P1.4 through P1.7 will be inputs.
     P1OUT |= BIT0 | BIT6; // turn bit zero & 6 of port 1 on. P1.0, P1.6 are now high
     P1REN = BIT6; // turn on pullup resistor
     // P1DIR = 0x00; // switiching the LED off,
    
     while (1)
     {
          if (P1IN & BIT6)  // this the LED on when magnet is  next to the Hall sensor,
              P1OUT |= BIT0; // turn bit zero of port 1 on. P1.0 is now high
         else
            P1OUT &= ~BIT0;  //turn bit zero of port 1 off. P1.0 is now low
        }
    }
    

  • basically I messed up :/ well I can't find words to thank you and I am going back to reading reading reading and trying to build something rather different from starting on and off leds thanks Roberto!

  • Georgi Daskalov said:

    basically I messed up :/ well I can't find words to thank you and I am going back to reading reading reading and trying to build something rather different from starting on and off leds thanks Roberto!

     Don't worry about, but firmly grasp how bit logic and decision structure work then everything got natural to you.

     Error are more simple to find than an issue sometimes we never know or understand from where it can.

  • True, at the beginning hardware design and hardware building a scheme seems like a horror, now I can' stop searching for new stuff.

  • Georgi Daskalov said:
    at the beginning hardware design and hardware building a scheme seems like a horro

     Some experienced sometimes forgot to do project before coding and beginner error are nothing like that HELL!

     Happy coding and all kind are welcome too.

**Attention** This is a public forum