21ic问答首页 - atmega1280单片机驱动AD7682
atmega1280单片机驱动AD7682
20710027592023-07-20
#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要传入什么参数?
#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要传入什么参数?
您需要登录后才可以回复 登录 | 注册