reme 0 Posted May 10, 2014 Share Posted May 10, 2014 i have msp430g2231 development kit. i wanted to do something with leds and buttons. my leds works fine. but somehow buttons are not. i have limited knowledge in electronics so i could not figure out whether this is because of my code, or i need to do some arrangements in the kit itself, like something related to jumpers.. this is the code that i tried to run. it works when i wrote while(1). but it could not get response from the button. while(P2IN & 0X01) does not seem to work. what is wrong with this? #include "io430.h" void gecikme(void); int main( void ) { // Watchdog timer'? durdur WDTCTL = WDTPW + WDTHOLD; P2SEL &= ~0x01; P2DIR &= ~0x01; P2REN |= 0x01; P1DIR = 0xFF; P1OUT = 0x00; while(P2IN & 0X01) { for(; { P1OUT = 0x01; gecikme(); P1OUT = 0x03; gecikme(); P1OUT = 0x07; gecikme(); P1OUT = 0x0F; gecikme(); P1OUT = 0x1F; gecikme(); P1OUT = 0x3F; gecikme(); P1OUT = 0x7F; gecikme(); P1OUT = 0xFF; gecikme(); P1OUT = 0x7F; gecikme(); P1OUT = 0x3F; gecikme(); P1OUT = 0x1F; gecikme(); P1OUT = 0x0F; gecikme(); P1OUT = 0x07; gecikme(); P1OUT = 0x03; gecikme(); P1OUT = 0x01; gecikme(); } } return 0; } void gecikme(void){ volatile unsigned int i; for(i=0 ; i < 50000 ; i ++); } Quote Link to post Share on other sites
RobG 1,892 Posted May 10, 2014 Share Posted May 10, 2014 How is your button connected? As for your code, here are some notes int maint(void) { //P2SEL &= ~0x01; // those 2 lines are not necessary //P2DIR &= ~0x01; P2REN |= 0x01; P2OUT |= 0x01; //this is necessary if your switch is connected to GND while (P2IN & 0X01) { // this will never execute (unless your switch is connected to Vcc and you are holding in down when starting to run,) pull resistor is set to pull-down, so P2IN & 0x01 is always false } return 0; } reme 1 Quote Link to post Share on other sites
petus 11 Posted May 10, 2014 Share Posted May 10, 2014 If you test the button on the PORT 2, you have to set: P2OUT |= BIT1; //pull-up resistor P2REN |= BIT1; //resistor enable P2SEL &= ~BIT1; //clear if you want to use interrupt: P2IFG &= ~ BIT1; //interrupt flag - flag is zero P2IE |= BIT1; //enable P2IES |= BIT1; //is set with high-to-low transition The datasheet for MSP430 says that after reset is P2SEL set as 0x0C0h edit: http://chiptron.petus.cz/code-programs-msp430f2132/MSP430F2132_interrupt_button_port2.c function code for MSP430F2132 Quote Link to post Share on other sites
reme 0 Posted May 10, 2014 Author Share Posted May 10, 2014 How is your button connected? As for your code, here are some notes int maint(void) { //P2SEL &= ~0x01; // those 2 lines are not necessary //P2DIR &= ~0x01; P2REN |= 0x01; P2OUT |= 0x01; //this is necessary if your switch is connected to GND while (P2IN & 0X01) { // this will never execute (unless your switch is connected to Vcc and you are holding in down when starting to run,) pull resistor is set to pull-down, so P2IN & 0x01 is always false } return 0; } they are connected to GND. P2REN |= 0x01; // These two lines sets a resistor and picks the pull down resistor option right? P2OUT |= 0x01; then how can i control whether the button is pressed or not? what is the right while statement? i thought these are standard lines that works for all. but obviously i was wrong. Quote Link to post Share on other sites
reme 0 Posted May 10, 2014 Author Share Posted May 10, 2014 If you test the button on the PORT 2, you have to set: P2OUT |= BIT1; //pull-up resistor P2REN |= BIT1; //resistor enable P2SEL &= ~BIT1; //clear if you want to use interrupt: P2IFG &= ~ BIT1; //interrupt flag - flag is zero P2IE |= BIT1; //enable P2IES |= BIT1; //is set with high-to-low transition The datasheet for MSP430 says that after reset is P2SEL set as 0x0C0h edit: http://chiptron.petus.cz/code-programs-msp430f2132/MSP430F2132_interrupt_button_port2.c function code for MSP430F2132 i did not use an interrupt in this particular code. i just wanted to make sure that my buttons are working. i wrote exactly the same statements, in a different way, but does he same job. problem should not be related to those 3 lines. Quote Link to post Share on other sites
RobG 1,892 Posted May 10, 2014 Share Posted May 10, 2014 P2SEL is only set for BIT6 and BIT7, so there's no need to clear it for BIT1. Do while(1) {.....} to have infinite loop, the if() statement to detect button. Since your button is grounded, use pull up resistor, P2OUT |= 0x01; reme 1 Quote Link to post Share on other sites
reme 0 Posted May 10, 2014 Author Share Posted May 10, 2014 P2SEL is only set for BIT6 and BIT7, so there's no need to clear it for BIT1. Do while(1) {.....} to have infinite loop, the if() statement to detect button. Since your button is grounded, use pull up resistor, P2OUT |= 0x01; now my code looks like this.but still button is not working. half of the leds are on whether i press or not.. why is that? did i got you wrong? P2REN |= 0x01; P2OUT |= 0x01; P1DIR = 0xFF; P1OUT = 0x00; while(1){ if(P2IN & 0x01){ P1OUT = 0xFF; gecikme(); } else{ P1OUT = 0xF0; gecikme(); } } return 0; } Quote Link to post Share on other sites
RobG 1,892 Posted May 11, 2014 Share Posted May 11, 2014 Try this: int main( void ) { WDTCTL = WDTPW + WDTHOLD; P2REN |= 0x01; P2OUT |= 0x01; P1DIR = 0xFF; P1OUT = 0x00; while(1){ if(P2IN & 0x01){ P1OUT = 0xFF; gecikme(); } else { P1OUT = 0x00; gecikme(); } ?} } BTW, is your switch connected to P2.0 and GND? reme 1 Quote Link to post Share on other sites
reme 0 Posted May 11, 2014 Author Share Posted May 11, 2014 Try this: int main( void ) { WDTCTL = WDTPW + WDTHOLD; P2REN |= 0x01; P2OUT |= 0x01; P1DIR = 0xFF; P1OUT = 0x00; while(1){ if(P2IN & 0x01){ P1OUT = 0xFF; gecikme(); } else { P1OUT = 0x00; gecikme(); } ?} } BTW, is your switch connected to P2.0 and GND? tried, but did not work. i will post an image because i also dont know why p2.4 led is almost on..what is happenning in there, maybe it gives you an idea? switch is connected o p2? well i dont know actually. my lack of knowledge in electronics..i really appriciate the help. Quote Link to post Share on other sites
RobG 1,892 Posted May 12, 2014 Share Posted May 12, 2014 What is the name of this board? P2.4's brightness suggests weak pull resistor (make sure JP3 is in off position) or possible mistake in code (0x10 instead of 0x01.) Looking at the picture, P2.0 switch is most likely SW23. Also, can you changing JP5 to off and see if the switch works? EDIT: OK, I found the schematic. There are some other things connected to P2.0 but above steps should work. reme 1 Quote Link to post Share on other sites
reme 0 Posted May 12, 2014 Author Share Posted May 12, 2014 this also didnt work. but thanks for the help! i guess i need to learn to read schematics myself. otherwise i will not be able to do, even the basic button thing. thank you! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.