Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1643909
  • 博文数量: 245
  • 博客积分: 10378
  • 博客等级: 上将
  • 技术积分: 2571
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 08:19
文章分类

全部博文(245)

文章存档

2013年(4)

2012年(8)

2011年(13)

2010年(68)

2009年(152)

分类: LINUX

2009-03-30 17:25:29

#include <stdio.h>  // 通过信号量使键盘读取与gpio输出同步
#include <fcntl.h>  // 按键选择输出
#include <unistd.h>  // --》循环右移
#include <stdlib.h>  // --》循环左移
#include <pthread.h> // --》 频闪
#include <string.h>  // --》 熄灭
#include <sys/types.h>
#include <asm/hardware.h>
#include <asm-arm/arch-s3c2410/gpio.h>
#include <semaphore.h>
#define DELAY_T 4*50000

pthread_t tid1,tid2;
sem_t sem1,sem2;
unsigned char key=0;

unsigned char gpio_rd(int fd);
int gpio_wr(int fd,unsigned char data);

struct gpio  //
{
    int fd;
    int data;
    int time;
};

void *key_func(struct gpio *tmp) //获得键值
{
    struct gpio lem_tmp;
    lem_tmp=*tmp;
    while(1)
    {
        sem_wait(&sem1);  //信号量同步
        key=gpio_rd(lem_tmp.fd);
        sem_post(&sem2);//信号量同步
    }
    pthread_exit(0);
}
void *led_func(struct gpio *tmp)  //输出
{
    static unsigned char i=1,bit=4;
    struct gpio led_tmp;
    led_tmp=*tmp;
    while(1)
    {    
        sem_wait(&sem2);  //信号量同步
        if( key!=0 )      //忽略键盘键值为0(无键按下)情况
            i=key;
        switch(i)
        {
            case 1: //shift left

                {
                    if( bit<8 && bit>3) //高四位循环右移
                    {
                        ioctl(led_tmp.fd,GPIO_SET_PIN,bit);
                        usleep(DELAY_T);
                        ioctl(led_tmp.fd,GPIO_CLR_PIN,bit);
                        usleep(DELAY_T);
                        bit++;
                    }
                    else
                        bit=4;
                }
                break;
            case 2:              //高四位循环左移

                {
                    if( bit<8 && bit>3)
                    {
                        ioctl(led_tmp.fd,GPIO_SET_PIN,bit);
                        usleep(DELAY_T);
                        ioctl(led_tmp.fd,GPIO_CLR_PIN,bit);
                        usleep(DELAY_T);
                        bit--;
                    }
                    else
                        bit=7;
                }
                break;
            case 4://flash

                {
                     unsigned char buf;
                        buf=0xf0;
                        write(led_tmp.fd,&buf,1);
                        usleep(DELAY_T);
                        buf=0x00;
                        write(led_tmp.fd,&buf,1);
                        usleep(DELAY_T);
                }
                break;
            case 8://loop from left to right 

                {
                }
                break;
            default:
                break;
        }
        sem_post(&sem1); //信号量同步
    }
    pthread_exit(0);
    
}
int gpio_open(void) //打开文件 返回文件描述符
{
    int fd;
    if( (fd=open("/dev/gpf/0" ,O_RDWR))<0 )
    {
        perror("open gpio error");
        exit(1);
    }
    return fd;
}

int gpio_set(int fd,unsigned char set_in,unsigned char set_out) //配置IO口的输入输出
{
    printf("set in port ");
    if( ioctl(fd,GPIO_SET_MULTI_PIN_IN,set_in)!=0 )
    {
        perror("gpio ioctl error");
        exit(1);
    }
    printf("[ok] \n set out port ");    
    if( ioctl(fd,GPIO_SET_MULTI_PIN_OUT,set_out)!=0 )
    {
        perror("gpio ioctl error");
        exit(1);
    }
    printf("[ok] \n");
    return 0;
}

unsigned char gpio_rd(int fd) //io写
{
    unsigned char buf=0;
    if( read(fd,&buf,1)==-1 )
    {
        perror("read error");
        exit(1);
    }
    return buf;
}
int gpio_wr(int fd,unsigned char data) //io读
{
    if( write(fd,&data,1)==-1 )
    {
        perror("write error");
        exit(1);
    }
    return 0;
}

int main(void)
{
    int fd;
    char cmd[8];
    struct gpio led;
    sem_init(&sem1,0,1); //信号量初始化
    sem_init(&sem2,0,0);
    fd=gpio_open();//获得文件描述符
    gpio_set(fd,0x0f,0xf0);//配置io口
    led.fd=fd;
    pthread_create(&tid1,NULL,key_func,&led); //创建线程
    pthread_create(&tid2,NULL,led_func,&led);
    while(1)
    {
        scanf("%s",cmd);
    if(    strcmp(cmd,"exit")==0 )
        {
            pthread_cancel(tid1);
            break;
        }
        usleep(20);
    }
    pthread_join(tid1,NULL);
    printf("exit");
    return 0;
    
}

阅读(1231) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-03-30 18:32:40

有点复杂