主题 : mini2440控制数码管 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 61772
精华: 0
发帖: 10
金钱: 55 两
威望: 11 点
贡献值: 0 点
综合积分: 20 分
注册时间: 2012-01-06
最后登录: 2013-06-01
楼主  发表于: 2012-06-13 22:09

 mini2440控制数码管


测试程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "stdio.h"
#include "sys/types.h"
#include "sys/ioctl.h"
#include "stdlib.h"
#include "termios.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "sys/time.h"

#define uchar unsigned char
#define uint unsigned int

//uchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//uchar code LED_W[8] = {0,1,2,3,4,5,6,7};

int fd;                      

void choose_num(int seg);
void Delay(uint i);
void choose_site(int site);

#define test 1
int main()
{
   int on = 1;
   int led;
  
   if((fd = open("/dev/wyz_leds",0)) < 0)
   {
       perror("open device false,the problem is what");
       exit(1);
   }
  
   printf("it is ok\n");
  
   #ifdef test
   while(1)
   {
       choose_num(0);   //1
       choose_site(0);  //
       Delay(3);
       choose_num(1);
       choose_site(1);
       Delay(3);
       choose_num(2);   //1
       choose_site(2);  //
       Delay(3);/*
       choose_num(3);
       choose_site(3);
       Delay(3);
       choose_num(4);   //1
       choose_site(4);  //
       Delay(3);
       choose_num(5);
       choose_site(5);
       Delay(3);
       choose_num(6);   //1
       choose_site(6);  //
       Delay(3);
       choose_num(7);
       choose_site(7);
       Delay(3);*/

   }
  
   #else
    while(1)
    {
           for(led=0;led<4;led++)
               {
                 ioctl(fd, on, led);
                 usleep(5000);
                 printf("tell me led is %d\n",led);
               }
          on=!on;
          //sleep(5000);
          printf("it will light\n");
    }    
    
    #endif
   printf("if jump out,is ok?\n");
   close(fd);
   return 0;
}
  
  
  
void choose_num(int seg)
{
    //int on=1;
    //int led;
    int i=0;
    switch(seg)
    {
        case 0:{ //0x3f
              //for(led=0;led<8;led++)
              ioctl(fd , 1 , 0 );
                  ioctl(fd , 1 , 1 );
                    ioctl(fd , 0 , 2 );
                  ioctl(fd , 0 , 3 );
                  ioctl(fd , 0 , 4 );
                  ioctl(fd , 0 , 5 );
                  ioctl(fd , 0 , 6);
                  ioctl(fd , 0 , 7 );
                  do{
              printf("now is zero\n");
              
              }while(i--);
              
              break;
              }
              
         case 1:{ //0x06
              //i=1;
              //for(led=0;led<8;led++)
              ioctl(fd , 1 , 0 );
                  ioctl(fd , 1 , 1 );
                    ioctl(fd , 1 , 2 );
                  ioctl(fd , 1 , 3 );
                  ioctl(fd , 1 , 4 );
                  ioctl(fd , 0 , 5 );
                  ioctl(fd , 0 , 6);
                  ioctl(fd , 1 , 7 );
                  do{
                
              printf("now is one\n");
              printf("%d\n",i);
              //sleep(1);
              }while(i--);
              break;
              }
                  
        case 2:{ //0x5b
              //for(led=0;led<8;led++)
              //i=1;
              ioctl(fd , 1 , 0 );
                  ioctl(fd , 0 , 1 );
                    ioctl(fd , 1 , 2 );
                  ioctl(fd , 0 , 3 );
                  ioctl(fd , 0 , 4 );
                  ioctl(fd , 1 , 5 );
                  ioctl(fd , 0 , 6);
                  ioctl(fd , 0 , 7 );
                  do{
              printf("now is two\n");
              printf("%d\n",i);
              sleep(1);
              }while(i--);
              
              break;
              }
        
        
       case 3:{ //0x4f
              //for(led=0;led<8;led++)
              ioctl(fd , 1 , 0 );
                  ioctl(fd , 0 , 1 );
                    ioctl(fd , 1 , 2 );
                  ioctl(fd , 1 , 3 );
                  ioctl(fd , 0 , 4 );
                  ioctl(fd , 0 , 5 );
                  ioctl(fd , 0 , 6);
                  ioctl(fd , 0 , 7 );
                  do{
              printf("now is three\n");
              }while(i--);
              break;
              }
    
       case 4:{ //0x66
              //for(led=0;led<8;led++)
              ioctl(fd , 1 , 0 );
                  ioctl(fd , 0 , 1 );
                    ioctl(fd , 0 , 2 );
                  ioctl(fd , 1 , 3 );
                  ioctl(fd , 1 , 4 );
                  ioctl(fd , 0 , 5 );
                  ioctl(fd , 0 , 6);
                  ioctl(fd , 1 , 7 );
                  do{
              printf("now is four\n");
              }while(i--);
              break;
              }
              
      case 5:{ //0x6d
              //for(led=0;led<8;led++)
              ioctl(fd , 1 , 0 );
                  ioctl(fd , 0 , 1 );
                    ioctl(fd , 0 , 2 );
                  ioctl(fd , 1 , 3 );
                  ioctl(fd , 0 , 4 );
                  ioctl(fd , 0 , 5 );
                  ioctl(fd , 1 , 6);
                  ioctl(fd , 0 , 7 );
                  do{
              printf("now is five\n");
              }while(i--);
              break;
              }
    
     case 6:{ //0x7d
              //for(led=0;led<8;led++)
              ioctl(fd , 1 , 0 );
                  ioctl(fd , 0 , 1 );
                    ioctl(fd , 0 , 2 );
                  ioctl(fd , 0 , 3 );
                  ioctl(fd , 0 , 4 );
                  ioctl(fd , 0 , 5 );
                  ioctl(fd , 1 , 6);
                  ioctl(fd , 0 , 7 );
                  do{
              printf("now is six\n");
              }while(i--);
              break;
              }
              
     case 7:{ //0x07
              //for(led=0;led<8;led++)
              ioctl(fd , 1 , 0 );
                  ioctl(fd , 1 , 1 );
                    ioctl(fd , 1 , 2 );
                  ioctl(fd , 1 , 3 );
                  ioctl(fd , 1 , 4 );
                  ioctl(fd , 0 , 5 );
                  ioctl(fd , 0 , 6);
                  ioctl(fd , 0 , 7 );
                  do{
              printf("now is server\n");
              }while(i--);
              break;
              }
              
     case 8:{ //0x7f
              //for(led=0;led<8;led++)
              ioctl(fd , 1 , 0 );
                  ioctl(fd , 0 , 1 );
                    ioctl(fd , 0 , 2 );
                  ioctl(fd , 0 , 3 );
                  ioctl(fd , 0 , 4 );
                  ioctl(fd , 0 , 5 );
                  ioctl(fd , 0 , 6);
                  ioctl(fd , 0 , 7 );
                  do{
              printf("now is eight\n");
              }while(i--);
              break;
              
              }
              
      case 9:{ //0x6f
              //for(led=0;led<8;led++)
              ioctl(fd , 1 , 0 );
                  ioctl(fd , 0 , 1 );
                    ioctl(fd , 0 , 2 );
                  ioctl(fd , 1 , 3 );
                  ioctl(fd , 0 , 4 );
                  ioctl(fd , 0 , 5 );
                  ioctl(fd , 0 , 6);
                  ioctl(fd , 0 , 7 );
                  do{
              printf("now is nine\n");
              }while(i--);
              break;
              }
      default:
          break;
          
    }
}  
  
  
  
void Delay(uint i)
{
   uchar x,j;
   for(j=0;j<i;j++)
   for(x=0;x<=148;x++);
}
  
  
void choose_site(int site)
{
    int i=0;
    switch(site)
    {
    case 0:{
                  ioctl(fd , 0 , 8 );
                  ioctl(fd , 0 , 9);
                  ioctl(fd , 0 , 10);  
                  do{  
                  printf("now ths first is choose\n");
                  }while(i--);
                  
                  break;
           }
      
    case 1:{
                  ioctl(fd , 0 , 8 );
                  ioctl(fd , 0 , 9);
                  ioctl(fd , 1 , 10);  
                  do{  
                  printf("now ths second is choose\n");  
                  }while(i--);
                  
                  break;
           }  
    
    case 2:{
                  ioctl(fd , 0 , 8 );
                  ioctl(fd , 1 , 9);
                  ioctl(fd , 0 , 10);    
                  do{
                  printf("now ths third is choose\n");
                  printf("%d\n",i);
                  }while(i--);
                  
                  break;
           }

    case 3:{
                  ioctl(fd , 0 , 8 );
                  ioctl(fd , 1 , 9);
                  ioctl(fd , 1 , 10);    
                  do{
                  printf("now ths fourth is choose\n");
                  }while(i--);
                  
                  break;
           }
        
    case 4:{
                  ioctl(fd , 1 , 8 );
                  ioctl(fd , 0 , 9);
                  ioctl(fd , 0 , 10);    
                  do{
                  printf("now ths fifth is choose\n");
                  }while(i--);
                  
                  break;
           }
          
    case 5:{
                  ioctl(fd , 1 , 8 );
                  ioctl(fd , 0 , 9);
                  ioctl(fd , 1 , 10);  
                  do{
                  printf("now ths sixth is choose\n");
                  }while(i--);
                  
                  break;
           }

    case 6:{
             // i=1;
                  ioctl(fd , 1 , 8 );
                  ioctl(fd , 1 , 9);
                  ioctl(fd , 0 , 10);  
                  do{
                  printf("now ths seventh is choose\n");
                  printf("%d\n",i);
                  }while(i--);
                  
                  break;
           }
          
    case 7:{
              //i=1;
                  ioctl(fd , 1 , 8 );
                  ioctl(fd , 1 , 9);
                  ioctl(fd , 1 , 10);    
                  do{
                  printf("now ths eighth is choose\n");
                  printf("%d\n",i);
                  sleep(1);
                  }while(i--);
                  
                  break;
           }
          
    defult:
         break;
   }
}
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
    
  
级别: 新手上路
UID: 61772
精华: 0
发帖: 10
金钱: 55 两
威望: 11 点
贡献值: 0 点
综合积分: 20 分
注册时间: 2012-01-06
最后登录: 2013-06-01
1楼  发表于: 2012-06-13 22:10
驱动程序如下:
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/gpio.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>


#define DEVICE_NAME "wyz_leds"   //驱动名字




static unsigned long led_table [] =

{  //设置相应GPIO端口到一个数组里,方便做for循环
    S3C2410_GPF(0),
    S3C2410_GPF(1),
    S3C2410_GPF(2),
    S3C2410_GPF(3),
    S3C2410_GPF(4),
    S3C2410_GPF(5),
    S3C2410_GPF(6),
    S3C2410_GPG(0),
    
    S3C2410_GPG(1),
    S3C2410_GPG(3),
    S3C2410_GPG(5),    
};



static unsigned int led_cfg_table [] =

{    //GPIO口输出模式
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,

    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
};



static int sbc2440_leds_ioctl(struct inode *inode,  struct file *file,  unsigned int cmd,  unsigned long arg)

{  //对GPIO实施操作的函数
    switch(cmd)

    {
        case 0:
        case 1:  if (arg > 11)

                     {
                          return -EINVAL;
                     }
                     //在led_table[arg]管脚上输出(!cmd)
                     s3c2410_gpio_setpin(led_table[arg], !cmd);  
             //其中cmd控制LED开关(0、1), arg控制第几个LED(0、1、2、3)    
                     return 0;
        default:  return -EINVAL;
     }
}



static struct file_operations dev_fops =

{    //把驱动程序里的函数(功能)与应用层联系起来

     //常用的还有open、close、read、write等等

     //应用层能够用到的函数,都在这里给出
    .owner = THIS_MODULE,
    .ioctl = sbc2440_leds_ioctl,
};



static struct miscdevice misc =

{    //杂项设备,一种主编号使用10的字符设备
    .minor = MISC_DYNAMIC_MINOR,
    .name = DEVICE_NAME,
    .fops = &dev_fops,
};



static int __init dev_init(void)
{
    int ret;

    int i;

    for (i = 0; i < 11; i++)

   {
        //设置GPIO功能
        s3c2410_gpio_cfgpin(led_table, led_cfg_table);  
        //初始化输出为低电平        
        s3c2410_gpio_setpin(led_table, 0);                                
   }
    //misc_register就是用主标号10调用register_chrdev()
    ret = misc_register(&misc);                              
   //而register_chrdev()是一个字符设备注册函数

    printk (DEVICE_NAME"/tinitialized/n");

    return ret;
}



static void __exit dev_exit(void)
{
    misc_deregister(&misc); //同上
}



module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");            //所遵守协议信息
MODULE_AUTHOR("xiaocaosihan"); //代码作者信息
级别: 新手上路
UID: 61772
精华: 0
发帖: 10
金钱: 55 两
威望: 11 点
贡献值: 0 点
综合积分: 20 分
注册时间: 2012-01-06
最后登录: 2013-06-01
2楼  发表于: 2012-06-13 22:14
比较锉,我就让八个数码管依次显示0--7,效果达不到(只能显示0,然后就是7了,中间的不亮),不知道是处理器处理我的程序太花费时间了,还是怎么的,求好的编程方法和解决方法。
级别: 新手上路
UID: 73474
精华: 0
发帖: 22
金钱: 85 两
威望: 23 点
贡献值: 0 点
综合积分: 44 分
注册时间: 2012-07-10
最后登录: 2014-11-10
3楼  发表于: 2012-09-17 12:42
现在搞定没?我也与遇到类似的情况,个人感觉是驱动的问题,不知道是不是。