?? zlg7290.c
字號(hào):
#include <stdio.h>#include <unistd.h>#include <fcntl.h> /* 文件操作 */#include <sys/ioctl.h> /* ioctl()函數(shù) */#include "i2c.h" /* I2C 命令 */#define I2C_ADDR 0x70 /* zlg7290從機(jī)地址 */int fd;int zlg7290_getkey(unsigned short *key);int zlg7290_sndcmd(unsigned char dat1, unsigned char dat2);void zlg7290_disp(unsigned char *buf, unsigned char num);int main(){ int ret,i; unsigned char buf[9]; unsigned short key; fd = open("/dev/i2c/0", O_RDWR); /* 打開(kāi)設(shè)備 */ if(fd == -1) { printf("Can't open I2C device!\n"); exit(-1); } ret = ioctl(fd, I2C_TENBIT, 0); /* 指定從機(jī)地址為7bit */ if (ret != 0) { printf("Can't set I2C address bit number.\n"); close(fd); exit(-1); } ret = ioctl(fd, I2C_SLAVE, I2C_ADDR >> 1); /* 設(shè)置從機(jī)地址,7位地址,須右移1位*/ if (ret != 0) { printf("Can't set I2C slave device address.\n"); close(fd); exit(-1); } /* 設(shè)置I2C總線頻率小于30KHz */ ret = ioctl(fd, I2C_S3C2410_SET_SPEED, 3); /* 3為I2C總線頻率分頻值 */ if (ret != 0) { printf("Can't set I2C speed.\n"); close(fd); exit(-1); } /* 讀取I2C總線頻率 */ ret = ioctl(fd, I2C_S3C2410_GET_SPEED, &i); if (ret != 0) { printf("Can't read I2C speed.\n"); close(fd); exit(-1); } printf("I2C Speed is:%dKHz\n", i); // S3C2410A // buf[7] = 0x05; buf[6] = 0x03; buf[5] = 0x0C; buf[4] = 0x02; buf[3] = 0x04; buf[2] = 0x01; buf[1] = 0x00; buf[0] = 0x0A; /* 顯示 S3C2410A */ zlg7290_disp(buf, 8); for (;;) { if (zlg7290_getkey(&key) != 0) /* 讀取按鍵鍵值 */ { printf("Read key value fail\n"); exit(-1); } if (zlg7290_sndcmd(0x70, 1<<(key - 1)) != 0) /* 閃爍 */ { printf("Display fail\n"); exit(-1); } usleep(1000); } close(fd); return 0;}/*********************************************************************************************************** Function name: zlg7290_getkey** Descriptions: get the key value from zlg7290** 從zlg7290取得按鍵鍵值 ** Input: unsigned short *key: the gotten key value** Output: 0 : OK -1: fail ** Created by: Ming Yuan Zheng 鄭明遠(yuǎn) ** Created Date: 2006-01-09 **-------------------------------------------------------------------------------------------------------** Modified by:** Modified Date: **------------------------------------------------------------------------------------------------------********************************************************************************************************/int zlg7290_getkey(unsigned short *key){ unsigned char temp[2]; temp[0] = 1; temp[1] = 0; if (write(fd, temp, 1) != 1) /* 發(fā)送子地址 */ return -1; if (read(fd, temp, 2) != 2) /* 讀取鍵值 */ return -1; *key = temp[0] + (temp[1] * 256); return 0;}/*********************************************************************************************************** Function name: zlg7290_sndcmd** Descriptions: send command to zlg7290** 向zlg7290發(fā)送命令** Input: unsigned char dat1: command word 1 unsigned char dat2: command word 2** Output: 0 : OK -1: fail ** Created by: Ming Yuan Zheng 鄭明遠(yuǎn) ** Created Date: 2006-01-09 **-------------------------------------------------------------------------------------------------------** Modified by:** Modified Date: **------------------------------------------------------------------------------------------------------********************************************************************************************************/int zlg7290_sndcmd(unsigned char dat1, unsigned char dat2){ unsigned char temp[3]; temp[0] = 0x07; temp[1] = dat1; temp[2] = dat2; if (write(fd, temp, 3) != 3) return -1; usleep(1000); return 0;}/*********************************************************************************************************** Function name: zlg7290_disp** Descriptions: control zlg7290 to driver led** 控制zlg7290驅(qū)動(dòng)led** Input: unsigned char *buf: display buffer unsigned char num: display number** Output: NULL** Created by: Ming Yuan Zheng 鄭明遠(yuǎn) ** Created Date: 2006-01-09 **-------------------------------------------------------------------------------------------------------** Modified by:** Modified Date: **------------------------------------------------------------------------------------------------------********************************************************************************************************/void zlg7290_disp(unsigned char *buf, unsigned char num){ unsigned char i; unsigned char temp[3]; for(i=0; i<num; i++) { temp[0] = 0x07; temp[1] = 0x60+i; temp[2] = *buf; buf++; write(fd, temp, 3); usleep(5000); }}/**************************************************************************** End Of File**************************************************************************/
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -