zoukankan      html  css  js  c++  java
  • dac7562 应用层实现dac

    /*
     * dac7562 (using spidev driver)
     *
      */

    #include <stdint.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <getopt.h>
    #include <fcntl.h>
    #include <sys/ioctl.h>
    #include <linux/types.h>
    #include <linux/spi/spidev.h>

    #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

    static const char *device = "/dev/spidev0.0";
    static uint8_t mode;
    static uint8_t bits = 24;
    static uint32_t speed = 500000;
    static uint16_t delay;

    static void transfer(int fd, int cmd)
    {
        int ret;
        uint8_t tx[3] ;

        tx[0] = (uint8_t)(cmd >> 24)    // 24bit 命令格式参见表格 12 13 以及 17

        tx[1] = (uint8_t)(cmd >> 16);  //

        tx[2] = (uint8_t)(cmd >> 0);   //
      
        struct spi_ioc_transfer tr = {
            .tx_buf = (unsigned long)tx,
            .rx_buf = NULL,
            .len = 3,
            .delay_usecs = delay,
            .speed_hz = speed,
            .bits_per_word = bits,
        };

        ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
        if (ret < 1)
            printf("can't send spi message");

        puts("");
    }


    int main(int argc, char *argv[])
    {
        int ret = 0;
        int fd;
      

        fd = open(device, O_RDWR);
        if (fd < 0)
            printf("can't open device");

         mode |= SPI_CPOL;

        /*
         * spi mode
         */
        ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
        if (ret == -1)
            printf("can't set spi mode");

       

        /*
         * bits per word
         */
        ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
        if (ret == -1)
            printf("can't set bits per word");

     

        /*
         * max speed hz
         */
        ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
        if (ret == -1)
            printf("can't set max speed hz");

      

        printf("spi mode: %d ", mode);
        printf("bits per word: %d ", bits);
        printf("max speed: %d Hz (%d KHz) ", speed, speed/1000);

        transfer(fd);

        close(fd);

        return ret;
    }

  • 相关阅读:
    Jquery
    day87-Django创建程序步骤,路由系统和项目执行
    day86-Django安装、cmd控制台操作
    day85-Django初识-自己开发的web框架
    day84-bootstrap
    day83-pymysql操作mysql,pycharm安装pymysql的方法(驱动)
    day82-jQuery-事件、动画、each、data、插件
    day81-jQuery-文档操作
    day80-jQuery-属性操作
    day79-jQuery-文本操作
  • 原文地址:https://www.cnblogs.com/zym0805/p/5234985.html
Copyright © 2011-2022 走看看