18184 views|20 replies

2

Posts

0

Resources
The OP
 

MSP430G2553 programming problem: two lights flash alternately [Copy link]

 
I am a self-taught novice (very new!) and I saw a program and I don't know how to understand it. I hope the experts can help me! #include
void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR |= BIT0 | BIT6; // P1.0, P1.1output. P1REN |= BIT0 | BIT6; // P1.0, P1.1pullup P1OUT |= BIT0; P1OUT &= ~BIT0; while (1) { P1OUT ^= BIT0 | BIT6; __delay_cycles(1000000); } } 1.Why is p1.0 turned on before while and then turned off immediately afterwards? What is the purpose? Code: P1OUT |= BIT0; P1OUT &= ~BIT0; 2.In while, p1out^=(BIT0 | BIT6), I think it should make p1.0 and p1.6 turn on together and then turn off together (bit0 and bit6 are first ORed and then XORed with it), how can they be made to flash alternately? I hope an expert can help me solve this problem!
This post is from Embedded System

Latest reply

Stay calm, stay calm, stay calm...   Details Published on 2019-2-25 16:33

3180

Posts

0

Resources
2
 
1. Make it one on and one off. 2. The opposite is still one on and one off.
This post is from Embedded System
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 

6040

Posts

204

Resources
3
 
If this code is used as you described, there is a problem. 1. The first two sentences of while should be P1OUT |= BIT0; P1OUT &= ~BIT6; Make p1.0 and p1.6 high and low, so that they can flash alternately after entering the loop. 2. If it is changed to the above code, then problem 2 does not exist.
This post is from Embedded System

Comments

Because the port pull-up resistor is enabled, BIT6 does not need to be set to output high. Because of the pull-up, this port is usually pulled high.  Details Published on 2019-2-25 11:58
Because the port pull-up resistor is enabled, BIT6 does not need to be set to output high. Because of the pull-up, this port is usually pulled high.  Details Published on 2019-2-25 09:28

赞赏

1

查看全部赞赏

 
 
 

6366

Posts

4936

Resources
4
 
P1OUT ^= BIT0 | BIT6; This sentence means to invert the output status of the two ports. In the beginning of the program, the two ports are at a high level because of the pull-up resistors, and BIT0 is set low before the main loop. In this way, before the main loop, the status of the two ports is one high level and the other low level. In the main loop, the status of the two ports is inverted, and the alternating change of one high and one low is achieved.
This post is from Embedded System
 
 
 

6366

Posts

4936

Resources
5
 
lcofjp posted on 2019-2-25 08:59 This code has problems if it is used as you described. 1. The first two sentences of while should be P1OUT |= BIT0; P1OUT &= ~BIT6; Let p ...
Because the port pull-up resistor is enabled, BIT6 does not need to be set to output high separately. Because of the pull-up, this port is usually pulled high.
This post is from Embedded System

Comments

430 is a push-pull output, there is no such thing as a pull-up output.  Details Published on 2019-2-25 10:12
 
 
 

6366

Posts

4936

Resources
6
 
You can actually do an experiment to see the running results. If the results are correct, you can infer the reason why the program does this. Also, you should read more about the bit operations of C language.
This post is from Embedded System
 
 
 

6040

Posts

204

Resources
7
 
tiankai001 posted on 2019-2-25 09:28 Because the port pull-up resistor is enabled, BIT6 does not need to be set to output high separately. Because of the pull-up, this port is usually pulled high
430 is push-pull output, there is no such thing as pull-up output.
This post is from Embedded System

Comments

If there is a pull-up resistor inside the port, you can choose whether to enable it. In this program, the pull-up resistor is enabled, so it will be pulled high. After setting it as an output port, the port is at a high level.  Details Published on 2019-2-25 10:48
 
 
 

6366

Posts

4936

Resources
8
 
lcofjp posted on 2019-2-25 10:12 430 is a push-pull output, there is no such thing as a pull-up output.
If there is a pull-up resistor inside the port, you can choose whether to enable it. In this program, the pull-up resistor is enabled, so it will be pulled high. After setting it as an output port, the port is at a high level.
This post is from Embedded System

Comments

The pull-up resistor is only used to pull up the input  Details Published on 2019-2-25 10:50
 
 
 

6040

Posts

204

Resources
9
 
tiankai001 posted on 2019-2-25 10:48 If there is a pull-up resistor inside the port, you can choose whether to enable it. In this program, the pull-up resistor is enabled, so it will be pulled high. After setting it as an output port, the...
The pull-up resistor is only used to pull up the input
This post is from Embedded System

Comments

I don't quite understand the circuit. But if he sets the pull-up resistor of this port in the program and sets the port as output, then the level of this port should not be high?  Details Published on 2019-2-25 11:11
I don't quite understand the circuit. But if he sets the pull-up resistor of this port in the program and sets the port as output, then the level of this port should not be high?  Details Published on 2019-2-25 11:05
 
 
 

6366

Posts

4936

Resources
10
 
lcofjp posted on 2019-2-25 10:50 The pull-up resistor is only used to pull up the input
I don't quite understand the circuit. But if he sets the pull-up resistor of this port in the program and sets the port as output, then the level of this port is not high?
This post is from Embedded System
 
 
 

6366

Posts

4936

Resources
11
 
lcofjp posted on 2019-2-25 10:50 Pull-up resistors are only used to pull up inputs
In fact, after enabling the internal pull-up, it has nothing to do with the input and output of the port. The pull-up resistor will pull the initial level of the port to a high level until there is a forced low level. This can be a low level from the outside or a low level output by the port.
This post is from Embedded System

Comments

How a single chip works cannot be imagined out of thin air, you have to follow the manual. The manual specifies that when the IO port is set to output, the pull-up and pull-down resistors are automatically invalid. Besides, the resistor is not just a pull-up, it can be configured to pull up or pull down according to the settings, all of which need to be checked in the manual.  Details Published on 2019-2-25 11:21
How a single chip works cannot be imagined out of thin air, you have to follow the manual. The manual specifies that when the IO port is set to output, the pull-up and pull-down resistors are automatically invalid. Besides, the resistor is not just a pull-up, it can be configured to pull up or pull down according to the settings, all of which need to be checked in the manual.  Details Published on 2019-2-25 11:19
 
 
 

6040

Posts

204

Resources
12
 
tiankai001 posted on 2019-2-25 11:05 I don't quite understand the circuit. But just like he set the pull-up resistor of this port in the program, and set the port to output, this time...[/quote] [quote]Each bit in each PxOUT register is the value to be output on the corresponding I/O pin when the pin is configured as I/O function, output direction, and the pullup/down resistor is disabled.
This post is from Embedded System
 
 
 

6040

Posts

204

Resources
13
 
tiankai001 posted on 2019-2-25 11:11 In fact, after the internal pull-up is enabled, it has nothing to do with the input and output of the port. The pull-up resistor will pull the initial level of the port to high until there is a forced pull-down...
You can't imagine how a single-chip chicken works, you have to follow the manual. The manual specifies that when the IO port is set to output, the pull-up and pull-down resistors automatically become invalid. Besides, the resistor is not just a pull-up, it can be configured to pull up or pull down according to the settings, all of which require reading the manual.
This post is from Embedded System
 
 
 

6040

Posts

204

Resources
14
 
tiankai001 Published on 2019-2-25 11:11 In fact, after the internal pull-up is enabled, it has nothing to do with the input and output of the port. The pull-up resistor will pull the initial level of the port to high until it is forced to be pulled low...
If you understand digital circuits, you should look at the IO port structure diagram in the datasheet. If you understand software, you should look at the relevant register configuration instructions in the user guide.
This post is from Embedded System

Comments

I really didn't read the data sheet. I just checked it out. I took it for granted when answering the question.  Details Published on 2019-2-25 12:08
I really didn't read the data sheet. I just checked it out. I took it for granted when answering the question.  Details Published on 2019-2-25 11:54
 
 
 

6366

Posts

4936

Resources
15
 
lcofjp posted on 2019-2-25 11:21 If you understand digital circuits, you should look at the IO port structure diagram in the datasheet. If you understand software, you should look at the relevant registers in the user guide...
I did not read the datasheet. I just checked it out. I took it for granted when answering the question.
This post is from Embedded System
 
 
 

6366

Posts

4936

Resources
16
 
lcofjp posted on 2019-2-25 08:59 This code has problems if it is used as you described. 1. The first two sentences of while should be P1OUT |= BIT0; P1OUT &= ~BIT6; Let p ...
P1OUT &= ~BIT0; This sentence should be accidentally wrong. Just change it as you said and it will be fine
This post is from Embedded System
 
 
 

6366

Posts

4936

Resources
17
 
lcofjp posted on 2019-2-25 11:21 If you understand digital circuits, you should look at the IO port structure diagram in the datasheet. If you understand software, you should look at the relevant registers in the user guide...
1. I took the problem for granted and did not read the manual. 2. I still know very little about hardware and need to learn from you.
This post is from Embedded System

Comments

You're being too polite. I just happened to come across something I knew, so I replied. I stopped working in hardware a long time ago and switched to web development.  Details Published on 2019-2-25 13:03
 
 
 

6040

Posts

204

Resources
18
 
tiankai001 posted on 2019-2-25 12:08 1. I took things for granted and didn't read the manual. 2. I still know very little about hardware and need to learn from you.
You're too kind. I just happened to come across what I knew and replied. I haven't worked in hardware for a long time and have switched to web development.
This post is from Embedded System
 
 
 

2

Posts

0

Resources
19
 
tiankai001 posted on 2019-2-25 09:26 P1OUT ^= BIT0 | BIT6; This sentence means to invert the output status of the two ports. At the beginning of the program, these two ports were connected because of the pull-up resistors...
Thank you all, I still don't understand the p1ren. I know it is related to the pull-up resistor, but the button is p1.3. What does p1ren |=bit0|bit6 mean? I have been looking for it for a long time but I can't find a clear explanation. And this program can make the two lights flash, but the brightness of the lights is very small, especially the green light!
This post is from Embedded System

Comments

You can first turn on both LEDs and keep them on to see if the brightness of the two LEDs is the same. Then increase the delay time in the main loop to see if there is any change in brightness (you can also shorten the delay time and observe the change in brightness).  Details Published on 2019-2-25 14:17
 
 
 

6366

Posts

4936

Resources
20
 
Timfma7 posted on 2019-2-25 13:06 Thank you all, I still don't understand the p1ren. I know it is related to the pull-up resistor, but the button is p1.3, and it is p1r like this...
You can first turn on both LEDs and keep them on to see if the brightness of the two LEDs is the same. Then increase the delay time in the main loop to see if there is any change in brightness (you can also shorten the delay time and observe the change in brightness).
This post is from Embedded System
 
 
 

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