惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
罗磊的独立博客
C
Check Point Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
I
InfoQ
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
B
Blog RSS Feed

博客园 - zelu

质量工程读书笔记 - 零缺陷管理的基本原则 质量工程读书笔记 - 品质部的职责 Arduino SoftwareSerial + CO2传感器模组MH-Z14 质量工程读书笔记 - 精益生产 质量工程读书笔记 - 预防为主的质量管理系统 质量工程读书笔记 - 优秀经理人的潜质 质量工程读书笔记 - 经理人的修炼 质量工程读书笔记 - 产品检验 质量工程读书笔记 - 质量工程师的工作 距离 Restart MSP430G2553 Launchpad 硬件I2C驱动 MSP430G2553 模数转换器 ADC10 MSP430 G2553 Launchpad实现电容测量 MSP430 G2553 比较器Comparator_A+、数据流程图DFD、状态转换图STD MSP430 G2553 低功耗模式LPMx MSP430 G2553 计时/计数器 Timer_A MSP430 G2553 基本时钟模块+ (Basic Clock Module+) MSP430 G2553 LaunchPad GPIO中断
MSP430 G2553 LaunchPad设置GPIO
zelu · 2015-06-06 · via 博客园 - zelu

一. 背景知识:逻辑运算符的使用

当程序初始化时,对于复位状态有不确定性的寄存器(如PxOUT),建议采用直接赋值;其他情况下最好使用逻辑运算符修改寄存器。

直接赋值

REGISTER = 0b11110000;
REGISTER = 0xF0;

“开启”某位(置1),保持其他位不变

REGISTER |= BITx; //turn bit x on
REGISTER |= BITx + BITy; //both on

“关闭”某位(置0),保持其他位不变

REGISTER &= ~BITx; //turn bit x off
REGISTER &= ~(BITx +BITy); //both off

“翻转”某位(取反),保持其他位不变

REGISTER ^= BITx; //toggle bit x
REGISTER ^= BITx + BITy; //toggle both

二. GPIO对应的寄存器

如下表所示。

  Register                       Short Form         Register Type         Initial State            
  Input   PxIN   Read only   -
  Output   PxOUT   Read/write   Unchanged
  Direction   PxDIR   Read/write   Reset with PUC

  Pullup/Pulldown

  Resistor Enable

  PxREN   Read/write   Reset with PUC

PxDIR:设置IO管脚的方向

- Bit = 0:输入(默认)

- Bit = 1:输出

PxREN:使能管脚内部上拉/下拉,典型上拉/下拉电阻阻值35kOhm

- Bit=0:禁用上/下拉电阻功能(默认)

- Bit=1:使能上/下拉电阻功能

PxIN:反映管脚上的电平高低

- Bit=0:输入为低电平

- Bit=1:输入为高电平

PxOUT

- 当禁用上/下拉电阻时,功能为设置输出电平高低

- - Bit=0:输出高电平

- - Bit=1:输出低电平

- 当使能上/下拉电阻时,功能为选择上拉还是下拉

- - Bit=0:下拉

- - Bit=1:上拉

特别留意,P2.6、P2.7两个IO口上电默认功能选择为晶体XIN和XOUT,如下图。若要使用P2.6、P2.7的IO功能,需要将P2SEL.6和P2SEL.7置零。

三. 初始化程序示例

例1:设置P1.0为输出

 1 #include "io430.h"
 2 
 3 void main(void)
 4 {
 5   // Stop watchdog timer to prevent time out reset
 6     WDTCTL = WDTPW + WDTHOLD;
 7 
 8     P1OUT = 0; //initialize the output state before changing the pin to an output
 9     P1DIR |= BIT0; //P1.0 output 0
10 
11     while(1)
12     {
13         //code...
14     }
15 
16 }

例2:设置P1.3为上拉输入

 1 #include "io430.h"
 2 
 3 #define PUSH2 BIT3
 4 
 5 void main( void )
 6 {
 7     // Stop watchdog timer to prevent time out reset
 8     WDTCTL = WDTPW + WDTHOLD;
 9 
10     P1OUT = 0;
11     P1OUT |= PUSH2; //initialize the pullup state
12     P1REN |= PUSH2; //enable internal pullup
13     P1DIR &= ~PUSH2; //set P1.3 to input mode (default)
14     //state on P1.3: (P1IN & PUSH2) == PUSH2
15 
16     while(1)
17     {
18         //code...
19     }
20   
21 }

参考资料

MSP430x2xx Family User's Guide
MSP430G2553 datasheet