Phil anselmo
Phil anselmo

Reputation: 3

how to switch strings with pushbutton

I'm using the MSP-EXP430G2 board with a MSP430G2231 microcontroller and I'm trying to make my code switch strings when the pushbutton is pushed. This is connected to BIT3.

Its connected to an LCD like this:

Connections
P1.0 - D4 Pin11
P1.1 - D5 Pin12
P1.2 - D6 Pin13
P1.3 - D7 Pin14
P1.4 - RS Pin4
P1.5 - R/W Pin5
P1.6 - E Pin6
#define DR P1OUT = P1OUT | BIT4       // define RS high
#define CWR P1OUT = P1OUT & (~BIT4)   // define RS low
#define READ P1OUT = P1OUT | BIT5     // define Read signal R/W = 1 for reading
#define WRITE P1OUT = P1OUT & (~BIT5) // define Write signal R/W = 0 for writing
#define ENABLE_HIGH P1OUT = P1OUT | BIT6   // define Enable high signal
#define ENABLE_LOW P1OUT = P1OUT & (~BIT6) // define Enable Low signal
void set_input(void) {
  const unsigned char z = 0;
  switch (clock_input) {
  default:
    clock_input = 0;
  case 0:

    lcd_init();
    send_string("Frequency is");
    send_command(0xC0);
    break;
  case 1:

    lcd_init();
    send_string("Period is");
    send_command(0xC0);
    break;
  }
}

void main(void) {

  WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer

  DCOCTL = 0;
  BCSCTL1 = CALBC1_1MHZ; // Set range
  DCOCTL = CALDCO_1MHZ;  // Set DCO step + modulation

  if (!(P1IN & BIT3)) { // Check if pushbutton down
    ++clock_input;      // Switch clock input
    set_input();        //                                                                                                                                                                                                                                                     
  }                                                                                                                                                                                                                                                                            
}

This is how I do it and this is how I send the string:

void send_string(char *s) {
  while (*s) {
    send_data(*s);
    s++;
  }
}

void send_command(unsigned char cmd) {
  check_busy();
  WRITE;
  CWR;
  P1OUT = (P1OUT & 0xF0) | ((cmd >> 4) & 0x0F); // send higher nibble
  data_write();                                 // give enable trigger
  P1OUT = (P1OUT & 0xF0) | (cmd & 0x0F);        // send lower nibble
  data_write();                                 // give enable trigger
}

void lcd_init(void) {
  P1DIR |= 0xFF;
  P1OUT &= 0x00;
  send_command(0x33);
  send_command(0x32);
  send_command(0x28); // 4 bit mode
  send_command(0x0E); // clear screen
  send_command(0x01); // cursor position
  send_command(0x06); // increment cursor
  send_command(0x80);
}

The problem is that when I put the code in the main and push the button nothing happens. When I put it in a loop it just switches back and forward. I don't know if Pin 1.3(S2) on the board is interfering with the button because they have the same name(P1.3).

Upvotes: 0

Views: 71

Answers (1)

Tarick Welling
Tarick Welling

Reputation: 3265

If we look at your code something quite important comes up:

void main(void) {
  // just statements
  if (!(P1IN & BIT3)) {
    // just statements
  }                                                                                                                                                                                                                                                                            
}

Your code doesn't loop. What happens if you let your main function end is the microcontroller stops executing code as you've told it to stop. As such you'll find the following in every microcontroller project at the end of main:

while(1){

}

Thus the application will keep running forever. If you want some things to be done till the end of time (or power), you must add your code to the body of the while loop.

if (!(P1IN & BIT3)) {

This bit of code is quite all right and can be added to the body of the while loop but be aware that while loop is looping at the speed of you CPU minus the time it takes to execute the body of the loop. As such it reads the values of P1IN way too much for it to really be perceived. As such you'll need a function called delay. This keeps the CPU busy for a input amount of time so we can read the input pin and react to it without directly overwriting what we've done.

This is a good enough version of delay for now:

void ms_delay(long milliseconds)
{
    int n;
    while(milliseconds--)
    {
        for (n=0 ; n<200 ; ++n);
    }
}

But before we can actually read something we'll need to configure the port. This is done with the DIR register. As you've connected the button to P1 we will configure P1DIR. P1DIR &= ~BIT3 (set P1Dir to 0 on position 3 and leave the rest)

Now we can read P1IN and get at the value at BIT3.

void ms_delay(long milliseconds)
{
    int n;
    while(milliseconds--)
    {
        for (n=0 ; n<16000 ; ++n) {} // please note that the 16000 value is very approximate and might need to be tweaked.
    }
}

void main(){
    // initialization of your hardware
    WDTCTL = WDTPW + WDTHOLD;
    P1DIR &= ~BIT3; // set bit 3 to 0 (INPUT) and leave the rest. (P1DIR = P1DIR & 11111011)

    while(1){
        ms_delay(100); // wait a 100 milliseconds
        if( P1IN & BIT3 == 1){
            // hurray the button is pressed. (if your button is connected to VCC at least.
        }
    }
}

This above code should probably work. But if you wan't those LED1 or LED2 to light up you'll need to add P1DIR |= BIT0 to the initialization code and change the body of the loop to:

ms_delay(100); // wait a 100 milliseconds
if( P1IN & BIT3 == 1){
    P1OUT |= BIT0;
}
else{
    P1OUT &= ~BIT0;
}

As for your question about a pin conflict. It seems you have one. P1DIR |= 0xFF; is configuring the P1 port as output, but you want to use P1.3 as an input so that won't work together.

Please feel free to look for real tutorials about microcontrollers. And if you really want to know things about the microcontroller, visit the website of the manufacturer. This is MSP in this instance and they have several documents to help you. Just search for the microcontroller part number and you'll find it most of the time. The important documents are:

  1. Datasheet: a general document which compactly describes the features and peripherals of the microcontroller. It also contains some port and register information.
  2. Family's User's Guide: sometimes known as a reference manual, this is the almighty document wherein MSP describes every detail one needs to know if one want so control a microcontroller. Most of the contents aren't that interesting but if you want to do something and it isn't described in the datasheet the user guide is your next option. Use the search function and you'll find it in no time.

Your provided code is incomplete as the variable clock_input isn't even declared anywhere.

Upvotes: 1

Related Questions