CC2530 port resources: three ports, represented as P0, P1 and P2. P0 and P1 are full 8-bit ports, while P2 only has 5 bits available
Some functions of CC2530’s IO port:
General IO port, peripheral IO port (timer, USART, ADC)
Input pin, output pin
When input, pull-up, pull-down, high-impedance state (three-state)
A brief introduction to registers:
PxSEL is set to 0 for a general-purpose I/O port and set to 1 for a dedicated function.
PxDIR is set to 0 for input mode and set to 1 for output mode.
PxINP is set to 0 for pull-up/pull-down mode and set to 1 for tri-state mode (high impedance)
1. For P1INP and P0INP, bits 0~7 all meet the above rules.
2. For P2INP, bits 0 ~ 4 meet the above rules, and bits 5 ~ 7 are dedicated functions, that is, 5, 6, and 7 are used to control the pull-up and pull-down modes of the P0, P1, and P2 group ports respectively (0 pull-up, 1 pull-down )
port configuration register
PxSEL register
Register PxSEL, where x is the port label 0~2, is used to set each pin of the port to be a general-purpose I/O (0) or an external device I/O (1).
By default, it is a common IO port pull-up input after power-on.
Group P2 only has 5 IO ports, but P2_1 and P2_2 are used for downloading programs, so these two IOs do not need to be configured.
Therefore, the lower 3 bits of P2SEL correspond to P2_0, P2_3, and P2_4 respectively.
PxDIR Register
To change the direction of a port pin at any time, use register PxDIR to set each port pin as input or output.
If using the output function, there is no need to use the PxINP register
PxINP register
Set the input mode of the general IO port: pull-up, pull-down or three-state (high-impedance state)
-
Select whether the input port is in high impedance state
-
Use bits 0~4 of P0INP, P1INP and P2INP
-
It should be noted that P1_0 and P1_1 do not have pull-up/pull-down functions
-
-
If pull-up and pull-down mode is selected, use bits 5~7 of P2INP to determine whether it is pull-up or pull-down.
-
P2_5: Determine P0 port
-
P2_6: Determine the P1 port
-
P2_7: Determine P2 port
-
Register usage
For a certain position 1, use the OR operation [|]
For a certain position 0, use the AND operation [&]
Example 1: Set P1_0 port as a general output IO port
P1SEL &= ~0x01; //P1.0设置为通用I/O口
P1DIR |= 0x01; //P1.0设置为输出
Example 2: Set P0_6 port as a general pull-up input IO port
P0SEL &= ~0x40;//P0.6设置为通用I/O口
P0DIR &= ~0x40;//P0.6设置为输入
P0INP &= ~0x40;//P0.6设置为上下拉模式
P2INP &= ~0x20;//P0.6设置为上拉输入
Example 3: Combine Example 1 and Example 2 to monitor the high and low levels of the external P0_6 port, and the high level lights up the LED light (P1_0)
Experimental phenomenon: the light turns on when the jumper cap is pulled out, and the light goes off when the jumper cap is plugged in.
#include<iocc2530.h>
/*
PxSEL 置0为通用I/O口,置1为专用功能
PxDIR 置0为输入模式,置1为输出模式
PxINP 置0为上/下拉模式,置1位三态模式(高阻态)
1.对应P1INP和P0INP来说,0~7位均满足上述规则
2.对于P2INP来说,0~4位满足上述规则,5~7为专用功能,
即5、6、7分别用来控制P0、P1、P2组端口的上下拉模式(0上拉、1下拉)
*/
void main(){
//寄存器上电默认置0,即普通IO口上拉输入
P1SEL &= ~0x01; //P1.0设置为通用I/O口
P1DIR |= 0x01; //P1.0设置为输出
P0SEL &= ~0x40;//P0.6设置为通用I/O口
P0DIR &= ~0x40;//P0.6设置为输入
P0INP &= ~0x40;//P0.6设置为上下拉模式
P2INP &= ~0x20;//P0.6设置为上拉输入
while(1){
if(P0_6 == 1){
//监测到高电平,点亮LED
P1_0 = 0;
}else{
//监测到低电平,熄灭LED
P1_0 = 1;
}
}
}
#include<iocc2530.h>
/*
PxSEL Set 0 for general I/O port, set 1 for special function
PxDIR Set 0 for input mode, set 1 for output mode
PxINP Set 0 for pull-up/down mode, set 1 for tri-state Mode (high impedance state)
1. For P1INP and P0INP, bits 0~7 all meet the above rules.
2. For P2INP, bits 0 and 4 meet the above rules, and 5 and 7 are dedicated functions,
that is, 5, 6, and 7 respectively. Used to control the pull-up and pull-down modes of the P0, P1, and P2 group ports (0 pull-up, 1 pull-down)
*/
void main(){
//The register is set to 0 by default after power-on, that is, the common IO port pull-up input
P1SEL &= ~0x01; //P1.0 is set as a general-purpose I/O port
P1DIR |= 0x01; //P1.0 is set as an output
P0SEL &= ~0x40;//P0.6 is set to the general I/O port
P0DIR &= ~0x40;//P0.6 is set to the input
P0INP &= ~0x40;//P0.6 is set to the pull-up and pull-down mode
P2INP &= ~0x20;//P0.6 is set as pull-up input
while(1){ if(P0_6 == 1){ //Detect high level, turn on LED P1_0 = 0; }else{ //Detect low level, turn off LED P1_0 = 1; } } }