msp430 buttons to control LED lights in real time
[Copy link]
I believe that everyone who has used 430 knows that its P1 and P2 ports have interrupt functions in addition to the functions of ordinary IO ports.
So let's use its interrupt function to realize a small project!
That is to light up the LED light by pressing the button
Project Framework
Initialize the corresponding IO ports: for example, we want to use P1_0--P1_3 to connect the buttons, and then output through P6 port. Therefore, we need to set the lower four bits of P1 port as the second function pin, the direction is input, and P6 as a normal pin, the direction is output.
Set the interrupt trigger mode of the lower four bits of P1 port, such as falling edge; turn on the corresponding interrupt control bit, and turn on the total interrupt.
Then write the function, download it to the circuit, debug, and implement it.
Development board introduction
8 LEDs are connected to P6 port at one end and VCC at the other end, common anode.
Four independent buttons are connected to the lower four bits of P1 port at one end, and one end is grounded.
Code
/********Main functionmain************/
/************************************************************
*Author: Feng'er and Sha
*Version: 1.0
*Function: Generate external interrupts through four independent buttons to control the lighting of 4 groups of LEDs
*MCU: msp430F149
*****************************************************************/
#include <msp430x14x.h>
#include "Config.h" //Introduce the development board hardware configuration file
#include "irq_port.c" //Introduce the port initialization source file
//Related macros
#define LED8 P6OUT
#define LED8DIR P6DIR
/****************************************************************
* Main function
***************************************************************/
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD; //Turn off watchdog timer
Port_init(); //Port initializationwhile
(1);
}
//**************************************************************************
// P1 port interrupt service routine, needs to be judged
//**************************************************************************
#pragma vector = PORT1_VECTOR
__interrupt void P1_IRQ(void)
{
switch(P1IFG&0x0F)
{
case 0x01: LED8 = 0xFC;P1IFG=0x00;break; // Pin 0 corresponds to S1 interrupt, the flag must be cleared manually to light up D1D2
case 0x02: LED8 = 0xF3;P1IFG=0x00;break; // Pin 1 corresponds to S2 interrupt, the flag must be cleared manually to light up D3D4
case 0x04: LED8 = 0xCF;P1IFG=0x00;break; // Pin 2 corresponds to S3 interrupt, the flag must be cleared manually to light up D5D6
case 0x08: LED8 = 0x3F;P1IFG=0x00;break; // Pin 3 corresponds to S4 interrupt, the flag must be cleared manually to light up D7D8
}
}
/
IQR_PORT source file
//****************************************************************************
// Initialize IO port subroutine
//****************************************************************************
void Port_init()
{
P1SEL = 0x00; //P1 normal IO function
P1DIR = 0xF0; //P10~P13 input mode, external circuit has been connected to pull-up resistor
P1IE = 0x0F; //Open P1 low four-bit interrupt
P1IES = 0x0F; //Falling edge trigger interrupt
P1IFG = 0x00; //Software clears interrupt flag register
_EINT(); //Open general interrupt
LED8DIR |= 0xFF; //P6 port output mode
LED8 |= 0xFF; //Turn off all LEDs first
}
Attached is the partial circuit diagram
LED
button
Done!
|