【问题标题】:Reading the status of an input pin and displaying onto an LED - LPC1115读取输入引脚的状态并显示在 LED 上 - LPC1115
【发布时间】:2018-12-01 01:51:04
【问题描述】:

此代码应该通过按钮读取数字输入引脚的状态并将状态输出到 LED。 即当输入为高时,LED 亮,反之亦然 由于按钮连接到上拉电阻,当按下按钮时,输入应该读为低电平,反之亦然。

我的代码:

    #include "board.h"
    #include <stdio.h>

    //setting pointers
    #define Port0 ((LPC_GPIO_T *) 0x50000000) //Port 0
    #define IOCON ((LPC_IOCON_T *) 0x40044000) //IO configuration

    int main(void)
    {

        /* Initialize pins */       
        Port0->DIR &= ~((1 << 1)); //PIO0_1 input - onboard switch (unpressed state is pulled-up)
        Port0->DIR |= (1<<7);      //PIO0_7 output - onboard LED

        //Pin configuration
        IOCON->REG[IOCON_PIO0_7] &= 0x0 << 3; //No addition pin function
        IOCON->REG[IOCON_PIO0_1] &= 0x0 << 3; // "

        Port0->DATA[1<<7] &= ~(1<<7); // output initially low 

        while (1) {

            if((Port0->DATA[1<<1]) & (1<<1)) //When input is high
            {
                Port0->DATA[1<<7] |= (1<<7); //drive PIO0_7 High

            }
            else
            {
                 Port0->DATA[1<<7] &= ~(1<<7); //Drive PIO0_7 Low
            }
        }

        return 0;
    }

执行这部分代码时,PIO0_7 保持低电平,除非按下按钮。但是,由于上拉开关,它不意味着相反的工作方式吗?我还用电压表仔细检查了这一点。

我尝试改变

     if((Port0->DATA[1<<1]) & (1<<1)) //When input is high

     if(!(Port0->DATA[1<<1]) & (1<<1)) //When input is Low

即使按下按钮,LED 输出仍保持高电平。

【问题讨论】:

    标签: c arm gpio lpc nxp-microcontroller


    【解决方案1】:

    假设您的 Port0-&gt;DATA[0] 指向基地址 0x5000 0000 并定义为对齐的 8 位数组,那么您的 Pin-Port 寻址/屏蔽是错误的。

    LPC111x user manual UM10398 Rev. 12.4 p196 Chapter 12.4.1 write/read data operation:

    为了让软件能够设置 GPIO 位而不影响单个引脚中的任何其他引脚 写操作,14 位宽地址总线的位 [13:2] 用于创建 12 位宽 每个端口的 12 个 GPIO 引脚上的写入和读取操作的掩码。

    所以地址中有 2 位的偏移量来获取/设置所需引脚的值。 因此,您必须将地址移动 2 位,以下应该可以解决问题:

    Port0->DATA[1<<(7+2)] &= ~(1<<7); // output initially low 
    
    while (1) {
        if((Port0->DATA[1<<(1+2)]) & (1<<1)) //When input is high
        {
            Port0->DATA[1<<(7+2)] |= (1<<7); //drive PIO0_7 High
        }
        else
        {
             Port0->DATA[1<<(7+2)] &= ~(1<<7); //Drive PIO0_7 Low
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 2021-11-24
      相关资源
      最近更新 更多