打印

atmega1280单片机驱动AD7682

[复制链接]
1230|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
2071002759|  楼主 | 2023-7-20 14:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "ad7862.h"
#include "spi.h"
#include <util/delay.h>

#define SET_CNV_LOW (PORTB &= ~(1<<PB0))
#define SET_CNV_HIGH (PORTB |= (1<<PB0))


void initAD7862(void)
{
    initSPIMaster();        //SPI串口初始化
    SET_CNV_LOW;        //片选输出低电平
}

void triggerReadCH1(uint16_t* datas, uint8_t times)
{
    uint8_t tmp1;
    uint8_t tmp2;
    uint8_t cnt=0;

        while(times)
        {
                SET_CNV_HIGH;        //片选拉高
                _delay_us(100);        //延时
                SET_CNV_LOW;        //片选拉低完成一次转换

                // send Character
                SPDR  = 0xF1;
                // wait until Char is sent
                while (!(SPSR & (1<<SPIF)));
                tmp1 = SPDR;

                // send Character
                SPDR  = 0x24;
                // wait until Char is sent
                while (!(SPSR & (1<<SPIF)));
                tmp2 = SPDR;       
                datas[cnt] = (tmp1<<8) + tmp2;
                cnt++;
                times--;
        }   
}

void triggerReadCH2(uint16_t* datas, uint8_t times)
{
        uint8_t tmp1;
        uint8_t tmp2;
        uint8_t cnt=0;

        while(times)
        {
                SET_CNV_HIGH;
                _delay_us(100);
                SET_CNV_LOW;
               
                // send Character
                SPDR  = 0xF3;
                // wait until Char is sent
                while (!(SPSR & (1<<SPIF)));
                tmp1 = SPDR;
               
                // send Character
                SPDR  = 0x24;
                // wait until Char is sent
                while (!(SPSR & (1<<SPIF)));
                tmp2 = SPDR;
                datas[cnt] = (tmp1<<8) + tmp2;
                cnt++;
                times--;
        }
}


void triggerReadCH1AndCH2(uint16_t* datas, uint8_t times)
{
        uint8_t tmp1;
        uint8_t tmp2;
        uint8_t tmp3;
        uint8_t tmp4;
        uint8_t cnt=0;

        while(times)
        {
                SET_CNV_HIGH;
                _delay_us(100);
                SET_CNV_LOW;
               
                // send Character
                SPDR  = 0xF1;
                // wait until Char is sent
                while (!(SPSR & (1<<SPIF)));
                tmp1 = SPDR;
               
                // send Character
                SPDR  = 0x24;
                // wait until Char is sent
                while (!(SPSR & (1<<SPIF)));
                tmp2 = SPDR;
               
                // send Character
                SPDR  = 0xF5;
                // wait until Char is sent
                while (!(SPSR & (1<<SPIF)));
                tmp3 = SPDR;
                               
                // send Character
                SPDR  = 0x24;
                // wait until Char is sent
                while (!(SPSR & (1<<SPIF)));
                tmp4 = SPDR;
               
                datas[2*cnt] = (tmp1<<8) + tmp2;
                datas[2*cnt+1] = (tmp3<<8) + tmp4;
                cnt++;
                times--;
        }
}
此例程为什么会将两个通道单独转换后又在一个另一个函数里将这两个通道再一次转换,而且通道2的两次高八位配置不一样?
triggerReadCH1AndCH2(uint16_t* datas, uint8_t times)函数中的datas要传入什么参数?

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

2

帖子

0

粉丝