/************************************
Modified by Wang.HH on 2007.10.20 14:50
*************************************/
#include <avr/io.h>
#include <avr/iom32.h>
#include <string.h>
#include <avr/interrupt.h>
#include <compat/deprecated.h>
//PORT B
#define ADS1255_SYNC PORT3
#define ADS1255_DRDY PORT2
#define ADS1255_CS PORT4
#define SPI_SS ADS1255_CS
#define SPI_MOSI PORT5
#define SPI_MISO PORT6
#define SPI_SCLK PORT7
#define SW_EN PB0
#define Alarm PD4
void init_spi(void);
uint8_t DoByte(uint8_t byte);
void do_n_byte(uint8_t length,uint8_t* pSend);
uint32_t read_data(void);
uint8_t read_register(uint8_t addr);
void write_register(uint8_t addr,uint8_t data);
void set_standby(void);
void set_wakeup(void);
void set_sync(void);
void set_selfcal(void);
uint32_t read_standby(void);
uint32_t tmp=0;
uint8_t i = 0;
uint32_t sum=0,temp=0;max=0,min=0xffffff,p2p=0;
uint32_t result[50];
ISR(INT2_vect)
{
tmp=read_data();
if(tmp&0x00800000)
{
tmp &= 0x007fffff;
}
else
{
tmp &= 0x007fffff;
tmp += 0x7fffff;
}
if(i<50) i++;else i=0;
result[i] = 0;
result[i] = tmp;
sum += result[i];
switch(i)
{
case 50:
sum = sum/51;
temp=sum;
break;
default:
break;
}
}
int main()
{
cli();
init_spi();
DDRB |= (1<<ADS1255_SYNC); / T OUTPUT
DDRB &= ~(1<<ADS1255_DRDY);
PORTB |= (1<<ADS1255_SYNC);
DDRB |= (1<<SW_EN);
PORTB |= (1<<SW_EN);//SW_EN disconnect 10*
DDRD |=(1<<Alarm);
write_register(0,0xf6);
write_register(1,0x01); / t input pin
write_register(2,0x20); / t clk pin, pga gain
write_register(3,0xa1); //1ksps
write_register(4,0x00); / t gpio
tmp = read_register(0);
tmp = read_register(1);
tmp = read_register(2);
tmp = read_register(3);
tmp = read_register(4);
//INT2 initial
cbi(MCUCSR,ISC2);//falling edge
sbi(GICR,INT2);//enable INT2
cbi(GIFR,INTF2);//clear INTF2
/*
PORTB &= ~(1<<ADS1255_SYNC);
PORTB |= (1<<ADS1255_SYNC);
*/
set_sync();
set_wakeup();
PORTD |=(1<<Alarm);
sei();
//loop:
i = 0;
tmp = 0;
max = 0;
min = 0xffffff;
while(1);
{
if(tmp&0x00800000)
{
tmp &= 0x007fffff;
}
else
{
tmp &= 0x007fffff;
tmp += 0x7fffff;
}
result[i] = 0;
result[i] = tmp;
if(tmp>max) max = tmp;
if(tmp<min) min = tmp;
}
// p2p = max-min;
// goto loop;
}
uint32_t read_data(void)
{
uint8_t send[]={0x01,0xff,0xff,0xff};
uint32_t data;
do_n_byte(sizeof(send),send);
// strncpy((uint8_t*)&data,send+1,3);
data = send[1]*65536 + send[2]*256 +send[1];
return data;
}
/*
uint32_t read_data_c(uint8_t num)
{
uint8_t send[]={0x03,0xff,0xff,0xff};
uint32_t data[];
uint8_t j=0;
do_n_byte(sizeof(send),send);
data[0] = send[1]*65536 + send[2]*256 +send[1];
for(;j<num;j++)
{
uint8_t send[]={0xff,0xff,0xff};
do_n_byte(sizeof(send),send);
data[j+1] = send[1]*65536 + send[2]*256 +send[1];
}
return data;
}
*/
uint32_t read_standby(void)
{
uint8_t send[]={0x01,0xff,0xff,0xff,0xfd};
uint32_t data;
do_n_byte(sizeof(send),send);
strncpy((uint8_t*)&data,send+1,3);
return data;
}
void set_selfcal(void)
{
PORTB &= ~(1<<ADS1255_CS);
DoByte(0xf0);/ lfcal command byte(in order to get the best performance)
PORTB |= (1<<ADS1255_CS);
}
void set_wakeup(void)
{
PORTB &= ~(1<<ADS1255_CS);
DoByte(0x00);//wakeup command byte(0xff can too)
PORTB |= (1<<ADS1255_CS);
}
void set_standby(void)
{
PORTB &= ~(1<<ADS1255_CS);
DoByte(0xfd);//standby command byte
PORTB |= (1<<ADS1255_CS);
}
void set_sync(void)
{
PORTB &= ~(1<<ADS1255_CS);
DoByte(0xfc);//sync command byte
PORTB |= (1<<ADS1255_CS);
}
void write_register(uint8_t addr,uint8_t data)
{
uint8_t send[] = {0x50,0x00,0x00};//write 1 register(as used 0x00)
send[0] |= addr;//starting from register addr(as used 0101 "addr"b)
send[2] |= data;//data writed
do_n_byte(sizeof(send),send);
}
uint8_t read_register(uint8_t addr)
{
uint8_t send[] = {0x10,0x00,0xff};//read 1 register(as used 0x00)
send[0] |= addr;//starting from register addr(as used 0001 "addr"b)
do_n_byte(sizeof(send),send);
return send[2];//content saved in the send[2]
}
void init_spi(void)
{
DDRB = (1<<SPI_MOSI) | (1<<SPI_SCLK) | (1<<SPI_SS);
DDRB &= ~(0<<SPI_MISO);
DDRB |= (1<<ADS1255_CS);
SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0) | (0<<CPOL) | (1<<CPHA);
PORTB |= (1<<ADS1255_CS);
}
uint8_t DoByte(uint8_t byte)
{
SPDR = byte;
while (!(SPSR & 0x80));
return SPDR;
}
void do_n_byte(uint8_t length,uint8_t* pSend)
{
PORTB &= ~(1<<ADS1255_CS);
while(length--)
{
*pSend = DoByte(*pSend);
pSend++;
}
PORTB |= (1<<ADS1255_CS);
}
评论0