QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2553|回复: 0

Learn lumit Step 12: 四位拨码开关实验

[复制链接]
发表于 2005-6-11 17:19:41 | 显示全部楼层 |阅读模式
Learn lumit Step 12 :  四位拨码开关实验
++++++++++++++++++++++++++++++++++++++++++++++++++++++

    这一小节我们着重讲解四位拨码开关的驱动原理和方法。从硬件设计上,拨码开关
类似与 led 的实现,它直接使用了 PIO 0, 1, 2, 3 ,通过设置 PIO 的方向为输入,
从而从内部 IO 数据寄存器中得到当前的拨码状态。

    首先我们用图示说明硬件连接情况,同时 dip4_open 初始化代码也不难理解。

/*  
; hardware connection
; PIO0         PIO1         PIO2         PIO3        (gpio)
; 1          2           3           4         (dip4)
; so output value = 0x0000000f = (0b0000 0000  0000 0000  0000 0000  0000 1111)
*/

/* set dip4 related gpio */
int dip4_open( void )
{
        // set gpio's direction: set IOPMOD register mode bit to 0 = input
        IOPMOD = IOPMOD & (~0x0000000F);
       
        return 0;
}

    在驱动程序 dip4_driver 中,作为一个标准的输入设备,我们仅仅实现了它的读操作。

/* get dip4 status */
int dip4_read( char * buf, int count )
{       
        int i = 0;
        int dip4_value;
       
        // the count has exceeds the count of our board
        if( count > DIP4_NUM )
                return -1;
       
        // get low-4-bits of IOPDATA        
        dip4_value = IOPDATA & 0x0F;
       
        for( i = 0; i < count; i++ )
                buf = (dip4_value & ( 1 << i ))? 1 : 0;       
               
        return count;
}

    在编程接口 dip4_api 中,我们模仿 led 的实现方法,给出了 dip4_get_value 接口实现:

/* get 4 dip4s status to low-4-bits of hex value */
int dip4_get_value( void )
{
        char buf[DIP4_NUM];
        int value = 0;
        int i;

        // read the dip status from dip4 driver
        dip4_read( buf, DIP4_NUM );

        for( i = 0; i < DIP4_NUM; i++ )
                value = value | (buf << i);
       
        // return the dip4 status in low-bits of value
        return value;
}

    一个简单的例子在 dip4_test 中,不过它仅仅是一个读接口的测试,没有输出设备来显示读出
的状态值,不过我们可以通过 第九节中 使用 JTAG 调试 的方法来观察读出的 value 值。

/* test dip4 api */
int dip4_test( void )
{       
        int value;
       
        dip4_init();
       
        while(1)
        {       
                value = dip4_get_value();       
        }
       
        return 0;
}

    在下面的综合实验一中,我们将 led / seg7 / dip4 这三个设备联系起来,用 dip4 作为输入,
led 和 seg7 作为输出,这样就可以看出实际的效果了。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-5-2 09:53 , Processed in 0.073889 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表