Jump to content
43oh

Where is a good place to start for C tutorials? And challenges with button push led program


Recommended Posts

Well I am definately a newbie at this, I have tried to use CCS6 to get rolling with my launchpads but am getting my butt handed to me.  I have been able to get some odds and ends done in Energia, but I want to use and learn CCS.  I can't even get a stupid button push to get a led to turn on to work. 

 

I know learning C will help with this as many of the operators just have me at a loss right now. 

 

I have tried soome pre written codes such as

/* pushy_G2211: a simple demo of configuring push-buttons on
 * input pins.
 */

#include <msp430g2553.h>

#define red_LED   BIT0
#define grn_LED   BIT6
#define BTN       BIT3

void delay(void);

void main(void) {
    unsigned int flash;
    WDTCTL = WDTPW + WDTHOLD;

    P1OUT = 0;
    P1DIR |= red_LED + grn_LED;  // LED pins to outputs, BTN is
                                 // still an input by default.

    for (; {

        for (flash=0; flash<7; flash++) {
            P1OUT |= red_LED;    // red LED on
            delay();             // call delay function
            P1OUT &= ~red_LED;   // red LED off
            delay();             // delay again
        }

        while ((P1IN & BTN) == BTN);  // wait for button press

        for (flash=0; flash<7; flash++) {
            P1OUT |= grn_LED;    // green LED on
            delay();
            P1OUT &= ~grn_LED;   // green LED off
            delay();
        }

        while ((P1IN & BTN) == BTN);  // wait for button press

    }
} // main

void delay(void) {
    unsigned int count;
    for (count=0; count<60000; count++);
} // delay


I can not get anything to work correctly,    both LED's are on at the same time, do I need to add a line in there to set the LED's at 0 for the initial ?   I thought the     P1OUT = 0 would set that at 0 to start??  I am so lost. 

 

Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Link to post
Share on other sites

Your delay function contains no functionality, therefore the compiler may decide to remove it. As a result, your LEDs will both be on 25% of the time whenever you press the button.

add the keyword volatile in front of your count variable declaration, this should prevent the compiler from optimising it away.

Also, you better start using timers to create these kinds of delays as soon as possible.

Link to post
Share on other sites

I can't seem to get anything to work to get a button press to turn a LED on. 

#include <msp430g2553.h>

void main(void) {

    WDTCTL = WDTPW + WDTHOLD;  // disable watchdog


    P1OUT = 0;                 // initialize LED off
    P1DIR = BIT0;              // P1.0 output

    if(P1IN = 0b00001000) {
    	P1IN = 0b00001000;
        P1OUT = 0b00000001;         // toggle LED on P1.0

    }
} // main

I have no idea, I think I have gone in so many circles its unreal.

 

With Energia its way easier

 

if

digitalRead(high)

 

then

digitalWrite(high)          //LED is on 

                         

I want to learn the CCS way, but it is proving to be much smarter than me....

Link to post
Share on other sites

Well I can get it to work in Energia,  can someone translate this to CCS?  THANK YOU!!!!!!!!!!!!!!!!!!

#define LED  2
#define BTN  5

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = BTN;     // the number of the pushbutton pin
const int ledPin =  LED;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(LED, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(BTN, INPUT_PULLUP);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(BTN);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {     
    // turn LED on:    
    digitalWrite(LED, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(LED, LOW); 
  }
}
Link to post
Share on other sites

This line in your working energia code does three things:

 

pinMode(BTN, INPUT_PULLUP);

 

It sets the pin to input mode, but also enables an internal resistor that pulls the pin up to VCC.
 
In the plain C version you only have the pin set to input (which is the default), but the pullup resistor is not enabled.
 
If you're using the P1.3 button on the MSP430G2 launchpad, for example, you'll need to set P1REN bit 3 to enable the internal resistor, and also set P1OUT bit 3 to make the internal resistor a pullup (rather than pulldown). With that done, P1IN bit 3 will be 1 when the button is not pressed, and 0 when the button is pressed.
 
Your last C example also had a bug in the "if" condition. if(P1IN = 0b00001000) assigns the value 0b00001000 to P1IN, then checks if its value is nonzero. I think you probably wanted to use the equality operator instead, like this: if(P1IN == 0b00001000). I'd expect CCS to produce a warning about that, since using the assignment operator instead of the equality operator is a common C programming bug.
 
Finally, you need to be careful how you test P1IN; if(P1IN == 0b00001000) doesn't just require bit 3 to be 1, it also requires all the other bits to be zero (which they might not be). It's best to just check the bit(s) you're interested in: if((P1IN & 0b00001000) == 0b00001000) is only true if bit 3 of P1IN is set, no matter what the other bits are set to.
Link to post
Share on other sites

I am still struggling with this. 

 

Here is what I have come up with now. 

#include <msp430g2553.h>


#define red_LED   BIT0
#define grn_LED   BIT6
#define BTN       BIT3



void main(void) {

    WDTCTL = WDTPW + WDTHOLD;  // disable watchdog

    P1DIR &= ~BTN;  //pin to input
    P1REN |= BTN;   //pin to enable resistor
    P1OUT |= BTN;   //set internal resistor to pullup


    {
    if  (BTN=0);    //check button status
    {
    P1OUT=1;        //if button status meets make P1OUT HIGH
    }

    else;
    {
    P1OUT=0;         //make P1OUT LOW
    }

    }
} // main

I am getting errors on the if and else statements which are lines 20 and 25. 

 

"../main.c", line 20: error #138: expression must be a modifiable lvalue
"../main.c", line 25: error #128: expected a statement

 

I am still working though the C book I have to get my head wrapped around this better, but the time I have had to work on this has been very little lately. 

 

I am checking the button status, if it is within the criteria it makes the P1OUT HIGH,

 

else it is low. 

 

What I am missing here ??

 

Thanks everybody!

 

Jake

Link to post
Share on other sites

That's one step in the right direction, but there are a few more things to fix before it will work. First of all you have an "if" statement with a semicolon at the end of the line. That detaches the following block {in braces} from the if, so it will always be run. It also leaves the "else" hanging out there on its own, which is why the compiler is unhappy about line 25. You need to remove those semicolons:

if (BTN==0) // <- no semicolon here!
{
    // Code here will now only be run if BTN==0
}
else        // <- no semicolon here!
{
    // Code here will now only be run if BTN!=0
}

Also, once the preprocessor has replaced the defines in your code, it will have changed the "if" statement from:

if (BTN==0)

to:

if (BIT3==0)

and finally to:

if (0x08==0)

That expression is always false (eight doesn't equal zero), so the code inside the "if" block will never run. The compiler might even just remove it completely since it's unreachable.

 

You need to test whether bit 3 of P1IN is clear, so this should do the job:

if ((P1IN & BTN)==0)

Finally, you're setting the whole of P1OUT in both branches of the if. That's bad because you need P1OUT bit 3 to be high for the switch to work properly. Search for a tutorial on "bitwise operators in C", that'll help you test or change specific bits of a value without affecting the others.

 

EDIT: Also your program will exit after testing the button just once. You'll probably have to be holding the button down while you start the program to see any effect.

Link to post
Share on other sites

THANKS AGAIN!

 

ok things are starting to make some more sense now.  I will hunt for the bitwise operators tutorials also. I have been trying to learn it at the same time, and have not applied it enough to make things stick.

 

 

Well my code will compile now but I am getting this error now to have to deal with

 

MSP430: GEL: Encountered a problem loading file: C:\Users\Jake\Google Drive\CCS\Turn lights on\Debug\Turn lights on.out Could not open file

 

I need to get that one figured out and we will see,  seems as it is not creating the output file to go to the MSP. 

 

 

Here is the code I have now 

#include <msp430g2553.h>


#define red_LED   BIT0
#define grn_LED   BIT6
#define BTN       BIT3


void delay(void);
void main(void) {

    WDTCTL = WDTPW + WDTHOLD;  // disable watchdog

    P1DIR &= ~BTN;  //pin to input
    P1REN |= BTN;   //pin to enable resistor
    P1OUT |= BTN;   //set internal resistor to pullup

    for(;
    {
    {
    if  (P1IN & BTN==0)    //check button status
    {
    P1OUT |= grn_LED;        //if button status meets make P1OUT HIGH
    delay();
    }
    else
    {
    P1OUT &=grn_LED;
    P1OUT |=red_LED;
    delay();
    }
    }
} // main

I do need to put a return line in to make it go back to the top so it will run continuously.  I think it has been harder to go from Arduino to this than it would have been just to come here first. 

 

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...