1506 views|0 replies

1668

Posts

0

Resources
The OP
 

MSP430 MCU Example 4 - Button-controlled colorful lights [Copy link]

1. Task Requirements

The P4 port of the MSP430F247 microcontroller is used to control eight light-emitting diodes D1~D8. The light-emitting diodes display different patterns according to the switches connected to the P0 port.

When K1 is closed, LED1 and LED2 light up, and after a delay of 0.5 seconds, LED2 and LED3 light up... and finally LED7 and LED8 light up.

When K2 is closed, LED1~LED8 are equivalent to 8-bit binary numbers. After a delay of 0.8 seconds, 1 is added and the corresponding LED is lit.

When K3 is closed, LED1~LED4 light up first, then LED5~LED8 light up after a delay of 0.5 seconds, then LED1, LED2, LED5, LED6 light up, then LED3, LED4, LED7, LED8 light up one by one after a delay of 0.5 seconds, finally LED1, LED3, LED4, LED6 light up, then LED2, LED4, LED6, LED8 light up after a delay of 0.5 seconds.

When K4 is closed, LED1~LED4 are lit first, and then the LED is moved from right to left. When LED8 is lit, LED1 is re-lit in the next step, and the cycle continues.

When K5 is closed, the four LEDs light up simultaneously in a cycle.

2. Circuit Design

Open the Proteus development environment and create a new project based on the MSP430F247 microcontroller.

Add the following components: microcontroller MSP430F247, resistor array, light-emitting diode, switch, resistor.

MSP430 MCU simulation example 4 based on Proteus - colorful lights controlled by buttons

3. Program Code


#include "msp430f247.h"

#include "stdlib.h"

#include "string.h"

/************************************************Software delay, main frequency 1M*******************/

#define CPU_F1 ((double)1000000)

#define delay_us1M(x) __delay_cycles((long)(CPU_F1*(double)x/1000000.0))

#define delay_ms1M(x) __delay_cycles((long)(CPU_F1*(double)x/1000.0))

/****************************************************** ***************************/

/************************************************

Function name: main function

Function: Color light control

Entry parameters: None

Export parameters: None

************************************************/

main()

{

unsigned char uiLEDValue=0;

unsigned char ucCounter;

unsigned char ucLEDDispaly1=0x80;

unsigned char ucLEDDispaly2=0x01;

unsigned char ucMode = 4;

static unsigned char LedState=0x0f;

_DINT(); //Disable interrupt

WDTCTL = WDTPW + WDTHOLD; // Turn off the watchdog

P4DIR = 0xff; //Set P4 port as output port

P4SEL = 0x00; //Set P4 port to normal I/O port

P4OUT = 0xff; //Set the P4 port to output high level

while(1)

{

if((P2IN&0x1f) == 0x1e)

{

uiLEDValue = 0x03;

for(ucCounter=0;ucCounter<8;ucCounter++)

{

P4OUT = uiLEDValue; //

uiLEDValue <<= 1;

delay_ms1M(500);

}

}

if((P2IN&0x1f) == 0x1d)

{

P4OUT = ~(uiLEDValue++);//

delay_ms1M(500);

}

if((P2IN&0x1f) == 0x1b)

{

switch(ucMode)

{

case 4:

P4OUT = 0xf0;

delay_ms1M(500);

P4OUT = 0x0f;

delay_ms1M(500);

break;

case 2:

P4OUT = 0xcc;

delay_ms1M(500);

P4OUT = 0x33;

delay_ms1M(500);

break;

case 1:

P4OUT = 0x55;

delay_ms1M(500);

P4OUT = 0xaa;

delay_ms1M(500);

break;

}

ucMode /= 2;

if(ucMode == 0) ucMode = 4;

}

if((P2IN&0x1f) == 0x17)

{

P4OUT = ~(ucLEDDispaly1|ucLEDDispaly2);

ucLEDDisplay1 >>= 1;

ucLEDDispaly2 <<= 1;

delay_ms1M(500);

if((ucLEDDispaly1|ucLEDDispaly2) == 0x00)

{

ucLEDDispaly1 = 0x80;

ucLEDDispaly2 = 0x01;

}

}

if((P2IN&0x1f) == 0x0f)

{

P4OUT = LedState;

delay_ms1M(500);

if(((LedState&0x01) == 0x01) && (LedState != 0x0f))

{

LedState = LedState<<1;

LedState += 1;

}

else

LedState = LedState<<1;

if(LedState == 0xe0) LedState += 1;

}

P4OUT = 0xff;

}

}

IV. Program Description

The program uses the closing of five switches to control the LED pattern display. A variety of different algorithms are used in the loop to process the pattern display.

Pattern 1 uses left shift to output the transformation of two LEDs.

Pattern 2 uses binary numbers to directly accumulate and output to the port, although it is relatively simple, but the display effect is better.

Pattern 3 uses the method of directly assigning values to ports.

Pattern 4 uses two 8-bit numbers shifted left and right and then ORed together to achieve the effect of two LEDs moving forward relative to each other.

Pattern 5 is a serpentine transformation method, which uses left shift operation and OR operation to achieve the effect of lighting up four LEDs in a cycle at the same time.

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list