打印
[ZLG-MCU]

祝贺ZLG-MCU版面成为Luminary专版,特发一个简单的GPIO例程

[复制链接]
2271|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
LPC900|  楼主 | 2007-11-28 15:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// 定时反转PB0管脚,如果接有LED则会不断闪烁发光

#include  "hw_types.h"
#include  "hw_memmap.h"
#include  "hw_sysctl.h"
#include  "hw_gpio.h"
#include  "src/sysctl.h"
#include  "src/gpio.h"

// 将较长的标识符定义为较短的形式
#define  SysCtlPeriphEn     SysCtlPeripheralEnable
#define  GPIOPinTypeIn      GPIOPinTypeGPIOInput
#define  GPIOPinTypeOut     GPIOPinTypeGPIOOutput

#define  PB0    GPIO_PORTB_BASE, GPIO_PIN_0

void  timeDelay (unsigned long  ulVal)
{
    do {
    } while ( --ulVal != 0 );
}

int  main (void)
{
    unsigned char  ucPins = 0x00;               // 定义临时变量

    timeDelay(1500000L);                        // 开机延迟

    SysCtlPeriphEn(SYSCTL_PERIPH_GPIOB);        // 使能GPIOB端口
    GPIOPinTypeOut(PB0);                        // 设置PB0为输出

    for (;;) {
        GPIOPinWrite(PB0, ucPins);              // ucPins最低位写入PB0
        ucPins ^= 0x01;                         // 反转ucPins最低位
        timeDelay(200000L);
    }
}

相关链接:http://www.zlgmcu.com/LUMINARY/Stellaris.asp

相关帖子

沙发
LPC900|  楼主 | 2007-11-28 15:32 | 只看该作者

GPIO读写例程

// 如果PB0接按键、PB1接LED,则可以通过按键控制LED亮灭

#include  "hw_types.h"
#include  "hw_memmap.h"
#include  "hw_sysctl.h"
#include  "hw_gpio.h"
#include  "src/sysctl.h"
#include  "src/gpio.h"

// 将较长的标识符定义成较短的形式
#define  SysCtlPeriphEn     SysCtlPeripheralEnable
#define  GPIOPinTypeIn      GPIOPinTypeGPIOInput
#define  GPIOPinTypeOut     GPIOPinTypeGPIOOutput

#define  PB0    GPIO_PORTB_BASE, GPIO_PIN_0
#define  PB1    GPIO_PORTB_BASE, GPIO_PIN_1

void  timeDelay (unsigned long  ulVal)
{
    do {
    } while ( --ulVal != 0 );
}

int  main (void)
{
    unsigned char  ucVal;

    timeDelay(1500000L);                    // 开机延迟

    SysCtlPeriphEn(SYSCTL_PERIPH_GPIOB);    // 使能GPIOB模块
    GPIOPinTypeIn(PB0);                     // 设置PB0为输入模式
    GPIOPinTypeOut(PB1);                    // 设置PB1为输出模式

    for (;;) {
        ucVal = GPIOPinRead(PB0);           // 读取PB0的状态
        GPIOPinWrite(PB1, (ucVal << 1));    // 将PB0的状态写入PB1
        timeDelay(300);
    }
}

使用特权

评论回复
板凳
LPC900|  楼主 | 2007-11-28 15:34 | 只看该作者

分三步走实现对GPIO的操作

操作GPIO只需要分三步走,很简单:

  1. 使能GPIO模块
     通过调用函数SysCtlPerphEn( )实现。

  2. 配置GPIO管脚
     函数GPIOPinTypeOut( ):配置指定管脚为输出模式。
     函数GPIOPinTypeIn( ):配置指定管脚为输入模式。

  3. 访问GPIO管脚
     函数GPIOPinWrite( ):改写管脚状态。
     函数GPIOPinRead( ):读取管脚状态。

使用特权

评论回复
地板
ATmega16| | 2007-11-28 16:27 | 只看该作者

GPIO实例要弄就弄个BITBAND读写IO口实例。

楼上能否给个BITBAND读写IO口实例。

使用特权

评论回复
5
LPC900|  楼主 | 2007-11-28 16:53 | 只看该作者

GPIO结构比较特殊,如果用Bit-Banding访问反而不方便

这两个例程是利用《Stellaris驱动库》访问GPIO,已经比较方便了,只需分三步走。

如何利用Bit-Banding访问GPIO,我已经研究过了,发现较难理解,操作也麻烦,还不如直接用《Stellaris驱动库》方便。

使用特权

评论回复
6
ATmega16| | 2007-11-29 08:53 | 只看该作者

BITBAND读写IO口效率更高

BITBAND读写IO口效率更高,还可以替代位域读写IO口,很好啊

使用特权

评论回复
7
LPC900|  楼主 | 2007-11-29 09:03 | 只看该作者

GPIO模块遵循的是FiRM规范

  Luminary单片机GPIO模块的设计遵循FiRM(Foundation IP for Real-Time Microcontrollers)规范。
  虽然ARM Cortex-M3内核支持片内SRAM和外设的Bit-band位操作,但是GPIO设计成了FiRM规范,本来就不打算用Bit-band,因此建议还是要用《手册》里规定的方法来访问。这种结构的GPIO也有很大好处,能够通过一条语句来对多个管脚同时进行操作,而与操作不相关的管脚保持不变。

在贴个例程,同时访问多个GPIO管脚:

#include  "hw_types.h"
#include  "hw_memmap.h"
#include  "hw_sysctl.h"
#include  "hw_gpio.h"
#include  "src/sysctl.h"
#include  "src/gpio.h"

#define  SysCtlPeriphEn     SysCtlPeripheralEnable
#define  GPIOPinTypeOut     GPIOPinTypeGPIOOutput

#define  PIN0   GPIO_PIN_0
#define  PIN1   GPIO_PIN_1

void  timeDelay (unsigned long  ulVal)
{
    do {
    } while ( --ulVal != 0 );
}

int  main (void)
{
    unsigned char  ucVal = 0x01;

    timeDelay(1500000L);                                      // 开机延迟

    SysCtlPeriphEn(SYSCTL_PERIPH_GPIOB);                      // 使能GPIOB模块
    GPIOPinTypeOut(GPIO_PORTB_BASE, (PIN0 | PIN1));           // 设置PB0和PB1为输出模式

    for (;;) {
        GPIOPinWrite(GPIO_PORTB_BASE, (PIN0 | PIN1), ucVal);  // ucVal写入PB0和PB1
        ucVal ^= 0x03;                                        // ucVal最低2位同时取反
        timeDelay(200000L);
    }
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

130

帖子

1

粉丝