1581 views|0 replies

1668

Posts

0

Resources
The OP
 

MSP430 MCU Example 5-16 Pattern Light Control [Copy link]

1. Task Requirements

The P1 and P4 ports of the MSP430F247 microcontroller are used to control 16 LEDs D1~D16. The LEDs have 8 patterns of display, and the display speed is adjustable. They are controlled by three buttons on the P2 port, namely the mode button, the acceleration button, and the deceleration button. Press the mode button once, and the pattern display mode changes once. After pressing it 8 times, it cycles to the first mode. The acceleration and deceleration buttons can control the flashing speed of the LED.

2. Analysis and explanation

According to the task requirements, 16 light-emitting diodes can form richer pattern changes. Since each port of the MSP430 microcontroller is 8 bits, two complete ports are required to control the light-emitting diodes. This also requires converting the 16-bit output into two 8-bit outputs in the program. In addition, this example will also introduce the modular design of the program, function pointer array and other contents.

3. Circuit Design

The hardware circuit for controlling 16 pattern lights is shown in the figure below. It is worth noting that when the P1 and P4 ports are connected to the LED, the network labels Q1~Q16 are used to indicate the network connection relationship, which simplifies the connection of the drawing. Otherwise, it will lead to too many and too dense connections on the drawing, affecting the simplicity and beauty of the drawing.


4. Program Code

Since the functions of the 16 pattern lights are relatively complex, it is not a good programming practice to write all the program codes in one file. At this time, modular programming is generally used.

Modular programming means dividing a larger program into several modules with independent functions, developing each module independently, and then merging these modules into a complete program. This method is a process-oriented programming method in C language, which can shorten the development cycle and improve the readability and maintainability of the program.

In the MCU program, when the program is small or the function is simple, we do not need to use modular programming. However, when the program function is complex and involves more resources, modular programming can reflect its superiority. For example, the flashing light program, the running light program and the pattern light program written earlier, each program can be completed with only one source file, but the program is relatively complex and involves more functions. Putting all the programs in one source file will make the main program bloated and messy. Doing so reduces the readability, maintainability and code reuse rate of the program. If these three programs are treated as three independent modules and put into the main program for modular programming, the effect will be different.

In fact, modular programming is the process of merging modules, and also the process of creating header files and source files for each module and adding them to the main program. The main program calls the module's function by including the module's header file. The module's header file and source file are two inseparable parts of the module, and neither can be missing. Therefore, modular programming must provide the header file and source file of each module.

The following uses the fancy light as an example to introduce modular programming. First, the program of the 16-bit fancy light is decomposed into three parts, namely the main function, the key program and the LED display program.

1. Create the source file of the module

In modular programming, the source file of a module is the variable definition and function definition that implements the module's functions. The main function cannot be defined. There are many ways to create a module source file. You can directly create a new file in the main program, add the code to it, save it as a .c file, and then add the file to the main program; or you can create a notepad file outside the program, add the code to it, save it as a .c file, and then add the file to the main program. In this example, put the keyboard processing code in the key.c file.

The procedure is as follows.


#include "msp430f247.h"

#include "stdlib.h"

#include "string.h"

#include "LedDisplay.h"

#include "key.h"

extern unsigned char RunMode;

extern unsigned char LEDDirection;

extern unsigned int SystemSpeed,SystemSpeedIndex;

extern unsigned char LEDFlag;

extern unsigned int LEDIndex;

unsigned int SpeedCode[]={1,2,3,5,8,10,14,17,20,30,

40,50,60,70,80,90,100,120,140,160,

180,200,300,400,500,600,700,800,900,1000};

unsigned char GetKey(void)

{

unsigned char KeyTemp;

unsigned char Key=0;

if((P2IN&0x07) != 0x07)

{

delayms(20);

if((P2IN&0x07) == 0x07)

return 0x00;

KeyTemp=P2IN&0x07;

if(KeyTemp == 0x06)

Key=0x01;

if(KeyTemp == 0x05)

Key=0x02;

if(KeyTemp == 0x03)

Key=0x03;

}

return Key;

}

void SetSpeed(unsigned char Speed)

{

SystemSpeed = SpeedCode[Speed];

}

void process_key(unsigned char Key)

{

if(Key&0x01)

{

LEDDirection=1;

LEDIndex=0;

LEDFlag=1;

RunMode=(RunMode+1)%8;

}

if(Key&0x02)

{

if(SystemSpeedIndex>0)

{

--SystemSpeedIndex;

SetSpeed(SystemSpeedIndex);

}

}

if(Key&0x03)

{

if(SystemSpeedIndex<28)

{

++SystemSpeedIndex;

SetSpeed(SystemSpeedIndex);

}

}

}

2. Create the module header file

The module header file is the interface between the module and the main program, and contains the function declarations of the module source file. The method of creating a header file is similar to the method of creating a source file, except that when saving, the file is saved in .h format. In this example, the function declarations in the keyboard processing source file are put into the file and saved as key.h. Each module header file must eventually be included in the main program, and cannot be included repeatedly, otherwise the compiler will report an error.

The procedure is as follows.


/************************************************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))

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

unsigned char GetKey(void);

void SetSpeed(unsigned char Speed);

void process_key(unsigned char key);

3. Similarly, the header file for establishing LED display control is as follows


void Mode_0(void);

void Mode_1(void);

void Mode_2(void);

void Mode_3(void);

void Mode_4(void);

void Mode_5(void);

void Mode_6(void);

void Mode_7(void);

void Mode_8(void);

void LedShow(unsigned int LedState);

void delayms(unsigned int t);

4. Create the C file for LED display control as follows


#include "msp430f247.h"

#include "stdlib.h"

#include "string.h"

#include "LedDisplay.h"

#include "key.h"

extern unsigned char RunMode;

extern unsigned char LEDDirection;

extern unsigned char LEDFlag;

extern unsigned int LEDIndex;

void (*run[8])(void)={Mode_0,Mode_1,Mode_2,Mode_3,

Mode_4,Mode_5,Mode_6,Mode_7};

void Mode_0(void)

{

LedShow(0x0001<<LEDIndex);

LEDIndex=(LEDIndex+1)%16;

}

void Mode_1(void)

{

LedShow(0x8000>>LEDIndex);

LEDIndex=(LEDIndex+1)%16;

}

void Mode_2(void)

{

if(LEDDirection) LedShow(0x0001<<LEDIndex);

else LedShow(0x8000>>LEDIndex);

if(LEDIndex==15) LEDDirection =! LEDDirection;

LEDIndex=(LEDIndex+1)%16;

}

void Mode_3(void)

{

if(LEDDirection)

LedShow(~(0x0001<<LEDIndex));

else

LedShow(~(0x8000>>LEDIndex));

if(LEDIndex==15)

LEDDirection =! LEDDirection;

LEDIndex=(LEDIndex+1)%16;

}

void Mode_4(void)

{

if(LEDDirection)

{

if(LEDFlag)

LedShow(0xfffe<<LEDIndex);

else

LedShow(~(0x7fff>>LEDIndex));

}

else

{

if(LEDFlag)

LedShow(0x7fff>>LEDIndex);

else

LedShow(~(0xfffe<<LEDIndex));

}

if(LEDIndex==15)

{

LEDDirection =! LEDDirection;

if(LEDDirection) LEDFlag =! LEDFlag;

}

LEDIndex=(LEDIndex+1)%16;

}

void Mode_5(void)

{

if(LEDDirection)

LedShow(0x000f<<LEDIndex);

else

LedShow(0xf000>>LEDIndex);

if(LEDIndex==15)

LEDDirection =! LEDDirection;

LEDIndex=(LEDIndex+1)%16;

}

void Mode_6(void)

{

if(LEDDirection)

LedShow(~(0x000f<<LEDIndex));

else

LedShow(~(0xf000>>LEDIndex));

if(LEDIndex==15)

LEDDirection =! LEDDirection;

LEDIndex=(LEDIndex+1)%16;

}

void Mode_7(void)

{

if(LEDDirection)

LedShow(0x003f<<LEDIndex);

else

LedShow(0xfc00>>LEDIndex);

if(LEDIndex==15)

LEDDirection =! LEDDirection;

LEDIndex=(LEDIndex+1)%16;

}

void Mode_8(void)

{

LedShow(++LEDIndex);

}

void LedShow(unsigned int LedState)

{

P1OUT=~(LedState&0x00ff);

P4OUT=~((LedState>>8)&0x00ff);

}

void delayms(unsigned int t)

{

unsigned int i;

while(t--)

{

for(i=1000;i>0;i--);

if((P2IN&0x07) != 0x07) break;

}

}

This post is from Microcontroller MCU
 

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