/* * lab3_base.c * * Created on: Nov 27, 2017 * Author: johnsontimoj */ ////////////////////////////////////////////////// // // Lab3 base - button - LED // // Turn on an LED based on button push // no decision code // ////////////////////////////////////////////////// // Includes #include "msp432.h" #include #define clkcnt 3000000 void main(void){ setbuf(stdout, NULL); // fix code composer issue // Greeting code printf(" !! Lab 3 base program !!\n"); // Local Variables uint8_t in_val1; uint8_t in_bit1; uint8_t P4_2_on; uint8_t P4_2_off; // Setup LED // Note: Port 4, bit 2 (P4.2) is pin 25 // | bit 2 // 7654 3210 // 0000 0100 -> 0x04 P4->DIR |= 0x04; // Configure P4.2 as output by setting to 1 in DIR P4->OUT &= 0xFB; // initialize the LED to off by setting to 0 in OUT // P4->OUT &= ~0x04; // alternate way to turn off // Setup button // Button pushed - pin -> 0 // Note: Port 1, bit 5 (P1.5) is pin 7 // | bit 5 // 7654 3210 // 1101 1111 -> 0xDF P1->DIR &= 0xDF; // Configure P1.5 as input // P1->DIR &= ~0x20; // alternate way to set to 0 // Infinite Loop while(1){ // Read the button in_val1 = P1->IN & 0x20; // read input port and isolate bit 5(0 or 32) in_bit1 = in_val1 && 1; // get logical value for bit 5 (0 or 1) // Prepare to output to the LED // Here we don't want to change any other bits in the output port // Note - this approach is in-efficient but instructive P4_2_on = P4->OUT | 0x04; // define required 'on' output - only sets P4.2 to 1, others unchanged P4_2_off = P4->OUT & ~0x04; // define required 'off' output - only sets P4.2 to 0, others unchanged // output required value (one side or the other of the addition = 0) P4->OUT = (P4_2_on * (!in_bit1)) + (P4_2_off * in_bit1); printf("%i\n", in_bit1); __delay_cycles(clkcnt/4); // 250ms } // end while return; } // end main