code:capdac
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
code:capdac [2009/05/19 23:36] – created laurenceb | code:capdac [2009/05/19 23:42] (current) – laurenceb | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== AD7746 demo code ====== | ||
<code c > | <code c > | ||
#include < | #include < | ||
- | |||
#include < | #include < | ||
- | |||
#include < | #include < | ||
- | |||
#include < | #include < | ||
- | |||
#include < | #include < | ||
Line 13: | Line 10: | ||
#define SCL 50000 // | #define SCL 50000 // | ||
- | |||
#define _AK_ (1<< | #define _AK_ (1<< | ||
- | |||
#define _NAK_ (1<< | #define _NAK_ (1<< | ||
#define F_CPU 20000000UL | #define F_CPU 20000000UL | ||
Line 22: | Line 17: | ||
typedef unsigned char u08; | typedef unsigned char u08; | ||
- | |||
typedef unsigned short u16; | typedef unsigned short u16; | ||
Line 28: | Line 22: | ||
#define BAUDRATE (unsigned long)9600 | #define BAUDRATE (unsigned long)9600 | ||
- | |||
#define BAUDDIV | #define BAUDDIV | ||
void init_i2c(void); | void init_i2c(void); | ||
Line 47: | Line 40: | ||
void init_i2c() | void init_i2c() | ||
- | |||
{ | { | ||
- | |||
TWBR=(u08)(((float)F_CPU/ | TWBR=(u08)(((float)F_CPU/ | ||
- | |||
} // | } // | ||
Line 57: | Line 47: | ||
void i2cstart() | void i2cstart() | ||
- | |||
{ | { | ||
- | |||
u16 timeout=1; | u16 timeout=1; | ||
- | |||
TWCR = (1<< | TWCR = (1<< | ||
- | |||
while (!(TWCR & (1<< | while (!(TWCR & (1<< | ||
- | |||
timeout++; | timeout++; | ||
- | |||
if(!timeout) | if(!timeout) | ||
- | |||
I2Cerr|=16; | I2Cerr|=16; | ||
- | |||
TWCR = (1<< | TWCR = (1<< | ||
- | |||
if (((TWSR & 0xF8) != TW_START)&& | if (((TWSR & 0xF8) != TW_START)&& | ||
- | |||
I2Cerr|=1; | I2Cerr|=1; | ||
- | |||
} | } | ||
Line 83: | Line 62: | ||
void i2cstop() | void i2cstop() | ||
- | |||
{ | { | ||
- | |||
u16 timeout=1; | u16 timeout=1; | ||
- | |||
TWCR = (1<< | TWCR = (1<< | ||
- | |||
while (!(TWCR & (1<< | while (!(TWCR & (1<< | ||
- | |||
timeout++; | timeout++; | ||
- | |||
if(!timeout) | if(!timeout) | ||
- | |||
I2Cerr|=64; | I2Cerr|=64; | ||
- | |||
} | } | ||
Line 103: | Line 74: | ||
void i2cwrite(u08 c) | void i2cwrite(u08 c) | ||
- | |||
{ | { | ||
- | |||
u16 timeout=1; | u16 timeout=1; | ||
- | |||
TWDR = c; | TWDR = c; | ||
- | |||
TWCR = (1<< | TWCR = (1<< | ||
- | |||
while (!(TWCR & (1<< | while (!(TWCR & (1<< | ||
- | |||
timeout++; | timeout++; | ||
- | |||
if(!timeout) | if(!timeout) | ||
- | |||
I2Cerr|=32; | I2Cerr|=32; | ||
- | |||
if ( ((TWSR& | if ( ((TWSR& | ||
- | |||
I2Cerr|=2; | I2Cerr|=2; | ||
- | |||
} | } | ||
- | |||
- | |||
u08 i2cread(u08 ak) | u08 i2cread(u08 ak) | ||
- | |||
{ | { | ||
- | |||
u16 timeout=1; | u16 timeout=1; | ||
- | |||
TWCR=ak; | TWCR=ak; | ||
- | |||
while ((!(TWCR & (1<< | while ((!(TWCR & (1<< | ||
- | |||
timeout++; | timeout++; | ||
- | |||
if(!timeout) | if(!timeout) | ||
- | |||
I2Cerr|=8; | I2Cerr|=8; | ||
- | |||
if(((TWSR & 0xF8) !=TW_MR_DATA_ACK) && ((TWSR & 0xF8) !=TW_MR_DATA_NACK)) | if(((TWSR & 0xF8) !=TW_MR_DATA_ACK) && ((TWSR & 0xF8) !=TW_MR_DATA_NACK)) | ||
- | |||
I2Cerr|=4; | I2Cerr|=4; | ||
- | |||
return TWDR; | return TWDR; | ||
- | |||
} | } | ||
- | |||
- | |||
//comms | //comms | ||
- | |||
- | |||
int put_char(char c, FILE * stream) | int put_char(char c, FILE * stream) | ||
- | |||
{ | { | ||
- | |||
while(!(UCSR0A & (1<< | while(!(UCSR0A & (1<< | ||
- | |||
UDR0 = c; | UDR0 = c; | ||
- | |||
return 0; | return 0; | ||
- | |||
} | } | ||
- | |||
- | |||
int get_char(FILE * stream) | int get_char(FILE * stream) | ||
- | |||
{ | { | ||
- | |||
while( !(UCSR0A & (1<< | while( !(UCSR0A & (1<< | ||
- | |||
return(UDR0); | return(UDR0); | ||
- | |||
} | } | ||
- | |||
- | |||
//the main code | //the main code | ||
- | |||
- | |||
int main() | int main() | ||
- | |||
{ | { | ||
- | |||
//setup code | //setup code | ||
- | |||
UCSR0B = (1<< | UCSR0B = (1<< | ||
- | |||
UCSR0C = (1<< | UCSR0C = (1<< | ||
- | |||
UBRR0L = BAUDDIV; | UBRR0L = BAUDDIV; | ||
- | |||
UBRR0H = BAUDDIV>> | UBRR0H = BAUDDIV>> | ||
- | |||
FILE mystdio = FDEV_SETUP_STREAM(put_char, | FILE mystdio = FDEV_SETUP_STREAM(put_char, | ||
- | |||
stdout=& | stdout=& | ||
- | |||
unsigned char command_humidity[]={0x90, | unsigned char command_humidity[]={0x90, | ||
- | |||
unsigned char command_level[]={0x90, | unsigned char command_level[]={0x90, | ||
- | |||
init_i2c(); | init_i2c(); | ||
- | |||
unsigned char n,m,l; | unsigned char n,m,l; | ||
- | |||
unsigned long int b; | unsigned long int b; | ||
- | |||
float humidity, | float humidity, | ||
- | |||
printf(" | printf(" | ||
- | |||
while(1) | while(1) | ||
- | |||
{ | { | ||
- | |||
for(m=0; | for(m=0; | ||
- | |||
{ | { | ||
- | |||
i2cstart(); | i2cstart(); | ||
- | |||
i2cwrite(0xBF); | i2cwrite(0xBF); | ||
- | |||
i2cstop(); | i2cstop(); | ||
- | |||
I2Cerr=0; | I2Cerr=0; | ||
- | |||
i2cstart(); | i2cstart(); | ||
- | |||
for(n=0; | for(n=0; | ||
- | |||
{ | { | ||
- | |||
i2cwrite(command_humidity[n]); | i2cwrite(command_humidity[n]); | ||
- | |||
} | } | ||
- | |||
i2cwrite(255); | i2cwrite(255); | ||
- | |||
do{ // | do{ // | ||
- | i2cstop(); | + | i2cstop(); |
i2cstart(); | i2cstart(); | ||
i2cwrite(0x90); | i2cwrite(0x90); | ||
Line 257: | Line 155: | ||
}while(i2cread(_NAK_)& | }while(i2cread(_NAK_)& | ||
i2cstop(); | i2cstop(); | ||
- | |||
i2cstart(); | i2cstart(); | ||
- | |||
i2cwrite(0x90); | i2cwrite(0x90); | ||
- | |||
i2cwrite(0x01); | i2cwrite(0x01); | ||
- | |||
i2cstart(); | i2cstart(); | ||
- | |||
i2cwrite(0x91); | i2cwrite(0x91); | ||
- | |||
b=0; | b=0; | ||
- | |||
for(n=3; | for(n=3; | ||
- | |||
{ | { | ||
- | |||
b|=((unsigned long int)i2cread(_AK_))<< | b|=((unsigned long int)i2cread(_AK_))<< | ||
- | |||
} | } | ||
- | |||
humidity+=((float)b)/ | humidity+=((float)b)/ | ||
- | |||
b=0; | b=0; | ||
- | |||
for(n=2; | for(n=2; | ||
- | |||
b|=((unsigned long int)i2cread(_AK_))<< | b|=((unsigned long int)i2cread(_AK_))<< | ||
- | |||
b|=(unsigned long int)i2cread(_NAK_); | b|=(unsigned long int)i2cread(_NAK_); | ||
- | |||
temperature+=((((float)b)/ | temperature+=((((float)b)/ | ||
i2cstop(); | i2cstop(); | ||
- | |||
i2cstart(); | i2cstart(); | ||
- | |||
i2cwrite(0xBF); | i2cwrite(0xBF); | ||
- | |||
i2cstop(); | i2cstop(); | ||
- | |||
i2cstart(); | i2cstart(); | ||
- | |||
for(n=0; | for(n=0; | ||
- | |||
i2cwrite(command_level[n]); | i2cwrite(command_level[n]); | ||
- | |||
i2cwrite(190); | i2cwrite(190); | ||
do{ // | do{ // | ||
Line 313: | Line 188: | ||
l=i2cread(_NAK_); | l=i2cread(_NAK_); | ||
}while(l& | }while(l& | ||
- | |||
i2cstop(); | i2cstop(); | ||
- | |||
i2cstart(); | i2cstart(); | ||
- | |||
i2cwrite(0x90); | i2cwrite(0x90); | ||
- | |||
i2cwrite(0x01); | i2cwrite(0x01); | ||
- | |||
i2cstart(); | i2cstart(); | ||
- | |||
i2cwrite(0x91); | i2cwrite(0x91); | ||
- | |||
b=0; | b=0; | ||
- | |||
for(n=2; | for(n=2; | ||
- | |||
b|=((unsigned long int)i2cread(_AK_))<< | b|=((unsigned long int)i2cread(_AK_))<< | ||
- | |||
b|=(unsigned long int)i2cread(_NAK_); | b|=(unsigned long int)i2cread(_NAK_); | ||
- | |||
level+=((float)b)/ | level+=((float)b)/ | ||
- | |||
i2cstop(); | i2cstop(); | ||
- | |||
} | } | ||
putchar(12); | putchar(12); | ||
- | |||
printf(" | printf(" | ||
printf(" | printf(" | ||
- | |||
temperature=0; | temperature=0; | ||
- | |||
humidity=0; | humidity=0; | ||
- | |||
level=0; | level=0; | ||
- | |||
} | } | ||
- | |||
} | } | ||
- | |||
</ | </ |
code/capdac.1242776190.txt.gz · Last modified: 2009/05/19 23:36 by laurenceb