led显示按下的key的值

开发板:EDUKIT-III实验箱,S3C2410+LINUX2.4,实验箱随箱光盘提供的Zimage,nor flash启动。

主机:ubnutn10.4LTS,arm-linux-gcc 2.95.3

 

/******************************************************************************
*Name: keydisled.c
*Desc: 按下一个按键,led0和串口终端显示该按键。运行该程序前,应使用insmod指令加载i2c.o驱动模块。
*Parameter:
*Return:
*Author: yoyoba(stuyou@126.com)
*Date: 2010-9-19
*Modify: 2010-9-19
********************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <linux/i2c.h>
#include <linux/fcntl.h>
#include <sys/stat.h>
#include "test-8led.h"
#include <sys/types.h>
/* control code */
#define I2C_SET_DATA_ADDR 0x0601
#define I2C_SET_BUS_CLOCK 0x0602
unsigned char digit[] ={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6,0xEE,0x3E,0x9C,0x7A,0x9E,0x8E};

char key_set(char ucChar)
{
 switch(ucChar)
 {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
    ucChar-=1; break;
  case 9:
  case 10:
  case 11:
  case 12:
  case 13:
    ucChar-=4; break;
  case 17:
  case 18:
  case 19:
  case 20:
  case 21:
    ucChar-=7; break;
  case 25: ucChar = 0xF; break;
  case 26: ucChar = '+'; break;
  case 27: ucChar = '-'; break;
  case 28: ucChar = '*'; break;
  case 29: ucChar = 0xFF; break;
  default: ucChar = 0xFE; // error

 }
 return ucChar;
}

int main()
{
 int fd,index=0;
 char chon=0xff,choff=0x0,key=0;
 
 printf("this is a example for 8-led,writed by youhaidong!\n");
 
 if((fd=open("/dev/i2c/0",O_RDWR))==-1) /*打开i2c设备*/
  {
   printf("open led device FAILD!\n");
   exit(1);
  }
 else
 printf("open led device SUCCESS!\n");
 
 ioctl(fd, I2C_SLAVE_FORCE, ZLG_SLAVE_ADDR); /*设置zlg7290从设备地址*/
 ioctl(fd, I2C_SET_BUS_CLOCK, 250*1000); /*设置i2c总线时钟频率为250khz*/
 
 while(1)
 {
  ioctl(fd,I2C_SET_DATA_ADDR,REG_Sys); //设置SystemReg寄存器的地址

  read(fd,&key,1); //读取SystemReg寄存器的内容,放入key中

  if(key&0x01) //SystemReg寄存器的值为1,这时才相应按键。

  {
   ioctl(fd, I2C_SET_DATA_ADDR, REG_Key); //设置key寄存器的地址

   read(fd,&key,1); //从key寄存器读取按键的值

   write(fd,&choff,1); //让led0熄灭

   key=key_set(key); //把key寄存器的值转换为0-15


   printf("the press key is %d\n",key);
   
   ioctl(fd, I2C_SET_DATA_ADDR, REG_Dis0); //设置led0地址

   index=(int)key; //该值为digit[]数组中的索引号

   write(fd,&digit[index],1); //显示按键的值

  }
 }
 printf("led example is over!\n");

 return 0;
}

 

/************************
*test-8led.h
***********************/


#ifndef __ZLG9290_H__
#define __ZLG9290_H__


#define ZLG_SLAVE_ADDR (0x70 >> 1)


#define REG_Sys 0x00
#define REG_Key 0x01
#define REG_Cnt 0x02
#define REG_Func 0x03
#define REG_Cmd0 0x07
#define REG_Cmd1 0x08
#define REG_Flas 0x0C
#define REG_Num 0x0D
#define REG_Dis0 0x10
#define REG_Dis1 0x11
#define REG_Dis2 0x12
#define REG_Dis3 0x13
#define REG_Dis4 0x14
#define REG_Dis5 0x15
#define REG_Dis6 0x16
#define REG_Dis7 0x17

#define SEG_A (1<<7)
#define SEG_B (1<<6)
#define SEG_C (1<<5)
#define SEG_D (1<<4)
#define SEG_E (1<<3)
#define SEG_F (1<<2)
#define SEG_G (1<<1)
#define SEG_DIP (1<<0)


#endif /*__ZLG9290_H__*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值