2957 views|1 replies

6366

Posts

4929

Resources
The OP
 

MSP430 MCU simulation example 2 based on Proteus - color light control [Copy link]




This content is originally created by EEWORLD forum user tiankai001. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source. 1. Task requirements
Use the P4 port of the MSP430F247 microcontroller to control 8 light-emitting diodes D1~D8, and the access dip switch of the P2 port.
When SW1-1 is connected to ON, D1 and D5 flash, and the flashing interval is 0.2S. When SW1-2 is turned on, D2 and D6 flash, and the flashing interval is 0.4S. When SW1-3 is turned ON, D1~D8 flash in a cycle, with a flashing interval of 0.2S.
2. Analysis and explanation
LED is a semiconductor device. When the voltage drop across the two ends is greater than 1V, it can emit light through a current of about 5mA (the conduction voltage and conduction current of light-emitting diodes of different specifications and colors are different). The greater the conduction current, the higher the brightness of the LED. However, if the current is too large, the LED will be burned. In general, the conduction current is 3~20mA. In this circuit, the purpose of the resistor is to connect a current-limiting resistor in series with the LED. Its purpose is to limit the current passing through the light-emitting diode from being too large. This is why the resistor is called a "current-limiting resistor". The resistance value of this resistor is usually between 300 and 1000 ohms. When the I/O port of the MSP430F249 microcontroller is used as an output port, the maximum current of the high-level output is 6mA, and the sum of the output currents of all its ports cannot exceed 48mA. When the port outputs a low level, the maximum current that can be absorbed is 40mA. Therefore, in this example, the design of controlling the cathode of the light-emitting diode using the microcontroller I/O port is adopted. 3. Circuit Design Open the Proteus development environment and create a new project based on the MSP430F247 microcontroller. Add the following components: single-chip microcomputer MSP430F247, resistor, light-emitting diode, resistor, dip switch.
Fourth, 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 function:Colored light control Input parameters: None Output parameters: None Description: Author: Lao Ma Shi Tu MCU Date: January 2, 2018 ******************************************************/main(){ unsigned int uiLEDValue = 0x01; _DINT(); // Turn off interrupt WDTCTL = WDTPW + WDTHOLD; // Turn off watchdog P4DIR = 0xff; // Set P4 port to output port P4OUT = 0xff;//Set P4 port to output high level
while(1)
{
if((P2IN & 0x07) == 0x06)//Button K1 is closed
{
P4OUT ^= (BIT0+BIT4);//LED1, LED5 flash
delay_ms1M(200);//Delay 0.2s
}
else if((P2IN & 0x07) == 0x05)//Button K2 is closed
{
P4OUT ^= (BIT1+BIT5);//LED2, LED6 flash
delay_ms1M(400);//Delay 0.4s
}
else if((P2IN & 0x07) == 0x03)//Button K3 is closed
{
P4OUT = ~uiLEDValue;//LEDs light up one by one
delay_ms1M(200);//Delay 0.2s
//uiLEDValue's value ranges from 0x01, 0x02, 0x04......0x80. Corresponding to 8 LEDs
uiLEDValue += uiLEDValue;
if(uiLEDValue == 0x100)//Restore to 0x01
{
uiLEDValue = 0x01;
}
}
if((P2IN & 0x07) == 0x07)//No key is pressed
{
P4OUT = 0xff;//Turn off all LEDs
//delay_ms1M(200);
}
}
}
5. Program Description
The program first includes the "msp430f247.h" header file, which gives the C language definition of the internal register names of this series of microcontrollers, such as P4DIR, P4OUT, etc. By defining these names and using C language, you can directly assign or read the values of the registers to complete the call of the microcontroller function.
In the main program, first use the WDTCTL = WDTPW + WDTHOLD; statement to turn off the watchdog, because the MSP430 microcontroller starts the watchdog by default after reset. When the program is executed normally, this function should be turned off, or the watchdog should be reset regularly.
In the main loop, read the input value of the P2 port, and then determine which switch is closed, and then control the corresponding LED to flash. The delay_ms1M(x) function is a software delay function, and the delay duration can be achieved by modifying the parameter value. 34, 34, 34)]http://p3.pstatp.com/large/53f500025110d818300c[/img]
VI. Precautions When applying I/O port output, the following issues should be paid attention to in the system's software and hardware design
1. Output level matching and conversion
34)]Generally, the working power supply of MSP430 single-chip system is 3.3V, so the output level of I/O port is 3.3V. When the connected peripheral devices and circuits use 5V, 9V and other power supplies different from 3.3V, the output level conversion circuit should be considered.
2. Output current driving capability
The I/O port output of MSP430 single-chip can provide a driving current of about 4mA. The maximum total output current is 48mA. When the connected peripheral devices and circuits require high current drive or high current injection, the power drive circuit should be considered.
3. Delay of output level conversion
MSP430 is a high-speed single-chip microcomputer. When the system clock is 8M, the time to execute an instruction is 0.125 microseconds. In some applications, high and low level pulse driving is often required for a long time, such as the driving of a stepper motor. Therefore, the conversion delay time should be considered in software design. For applications that do not require precise delay, the software delay method can be used. If precise delay is required, it is best to use the internal timer of the single-chip microcomputer.

This post is from Microcontroller MCU

Latest reply

I learn from you every day! I benefit a lot!  Details Published on 2018-1-24 17:09
 

18

Posts

0

Resources
2
 
I learn from you every day! I benefit a lot!
This post is from Microcontroller MCU
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

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