目录
I/O端口是单片机最常用的外设模块,可直接用于输入/输出,也可为单片机应用系统扩展提供必要逻辑控制信号。
MSP432单片机最多可以提供11个 通用I/O端口(P1-P10与PJ):
- 大部分端口有8个管脚,少数端口管脚少于8。
- 每个I/O管脚都可以独立设置为输入or输出方向。
- 每个I/O管脚都可独立地读取or写入。
- 所有端口寄存器都可被独立地置位or清零。
- 每个独立端口都可以进行字节访问。
PS:MSP432P401R的P1-P9有9个引脚,P10和PJ有6个引脚。
一、端口P1、P2、P3、P4、P5、P6
端口P1-P6具有输入/输出、中断、外部模块功能,可通过各自的9个控制寄存器的设置来实现:
1. 输入寄存器PxIN
CPU扫描I/O引脚信号的只读寄存器。用户不能对其写入,只能通过读取该寄存器的内容获取I/O端口的输入信号,此时引脚的方向必须选定为输入。
0---该引脚输入为低;1---该引脚输入为高
2. 输出寄存器PxOUT
(1)该寄存器为I/O端口的输出缓冲寄存器。其内容可以像操作内存数据一样写入,以达到改变I/O口输出状态的目的。在读取时输出缓存的内容与引脚方向定义无关。改变方向寄存器的内容,输出缓存的内容不受影响。
0---该引脚输出为低;1---该引脚输出为高
(2)如果引脚为I/O功能、输入模式并且上拉/下拉电阻使能,该寄存器内容决定上拉or下拉电阻。
0---下拉电阻;1---上拉电阻
3. 方向寄存器PxDIR Q
相互独立的8位分别定义了8个引脚的输入/输出方向。8位在PUC之后都被复位。使用输入或者输出功能时,应该先定义端口的方向,输入/输出才能满足设计者的要求。作为输入时,只能读;作为输出时,可读可写。
0---输入模式;1---输出模式
4. 上拉/下拉电阻使能寄存器PxREN
该寄存器的每一位可以使能相应I/O引脚的上拉/下拉电阻。该寄存器需和输出寄存器配合使用,才能完成上拉/下拉电阻的配置。
0---上拉/下拉电阻禁止;1---上拉/下拉电阻使能
5. 输出驱动能力调节寄存器PxDS
PxDS寄存器的每一位可使相关引脚选择全驱动模式和次驱动模式(减弱驱动能力)。默认的是次驱动模式。
0---次驱动模式;1---全驱动模式。
6. 功能选择寄存器PxSEL
P1~P6端口还有其他片内外设功能,为了减少引脚,将这些功能与芯片外的联系通过复用P1~P6 引脚的方式来实现。PxSEL用来选择引脚的I/O端口功能与外围模块功能.
0---选择引脚为普通I/O功能;1---选择引脚为外围模块功能。
7. 中断使能寄存器PxIE
该寄存器的每一位对应I/O引脚的中断使能。
0---关闭中断;1---使能中断。
8. 中断触发边沿选择寄存器PxIES
如果允许Px口的某个引脚中断,还需定义该引脚的中断触发沿。该寄存器的8位分别定义了Px端口的8个引脚的中断触发沿。
0---上升沿中断;1---下降沿中断。
9. 中断标志寄存器PxIFG
该寄存器有8个标志位,分别对应Px的8个引脚,它们含有相应引脚是否有待处理中断的信息,即相应引脚是否有中断请求。如果Px的某个引脚允许中断,同时选择上升沿,则当该引脚发生由低电平向高电平跳变时,PxIFG的相应位就会置位,表明该引脚上有中断事件发生。
0---有中断请求;1---无中断请求。
10. 中断向量寄存器PxIV(Read only)
该寄存器的值表明哪个引脚产生中断。所有的P1端口的I/O管脚的中断都来源于同一个中断向量P1IV,同理,P2端口的中断源都来源于另一个中断向量P2IV。
前6组端口可单独配置中断,每个中断都可以单独使能并配置为在输入信号的上升沿或下降沿提供中断。所有中断都被送入编码的中断向量寄存器,允许应用程序确定端口的具体是哪个引脚存在触发事件。
二、端口P7、P8、P9、P10、PJ
这些端口没有中断能力,其余功能与P1~P6端口一样,能实现输入/输出功能和外围模块能。每个端口有6个寄存器供用户使用,用户可通过这6个寄存器对他们进行访问和控制。每个端口的6个寄存器分别为:输入寄存器(PxIN)、输出寄存器(PxOUT)、方向选择寄存器(PxDIR)、输出驱动能力调节寄存器(PxDS)、上拉/下拉电阻使能寄存器(PxREN)和功能选择寄存器(PxSEL)。具体用法同P1~P6端口。
三、GPIO配置一般步骤:
step1: 配置功能选择寄存器PxSEL
step2: 若为I/O端口功能,配置方向寄存器PxDIR
step3: 配置上拉/下拉电阻使能寄存器PxREN
step4: 配置输出寄存器PxOUT。
(若为输入,配置中断使能寄存器PxIE;若允许中断,继续配置中断触发沿选择寄存器PxIES。)
四、经典点亮LED
#include "msp.h"
#include "../inc/Clock.h"
#include <stdio.h>
// built-in red LED connected to P2.0
// built-in green LED connected to P2.1
// built-in blue LED connected to P2.2
// Color LED(s) Port2
// dark --- 0
// red R-- 0x01
// blue --B 0x04
// green -G- 0x02
// yellow RG- 0x03
// sky blue -GB 0x06
// white RGB 0x07
// pink R-B 0x05
#define RED 0x01
#define GREEN 0x02
#define BLUE 0x04
#define yellow 0x03
#define skyblue 0x06
#define white 0x07
#define pink 0x05
void Port2_Init(void){
P2->SEL0 = 0x00;
P2->SEL1 = 0x00; // configure P2.2-P2.0 as GPIO
P2->DS = 0x07; // make P2.2-P2.0 high drive strength
P2->DIR = 0x07; // make P2.2-P2.0 out
P2->OUT = 0x00; // all LEDs off
}
void Port2_Output(uint8_t data){ // write all of P2 outputs
P2->OUT = data;
}
/**
* main.c
*/
void main(void)
{
//WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
Clock_Init48MHz();
Port2_Init();
while(1)
{
Port2_Output(RED);
Clock_Delay1ms(1000);
Port2_Output(GREEN);
Clock_Delay1ms(1000);
Port2_Output(BLUE);
Clock_Delay1ms(1000);
Port2_Output(yellow);
Clock_Delay1ms(1000);
Port2_Output( skyblue );
Clock_Delay1ms(1000);
Port2_Output(pink );
Clock_Delay1ms(1000);
Port2_Output(white );
Clock_Delay1ms(2000);
}
}