عذر می خوام حواسم نبود :دی .امیدوارم اینیکی خوب باشه.
کد:
#define Mode 0x08
#define Conf 0x10
#define Data 0x18
#define Offset 0x30
#define FScale 0x38
void ADWR(char reg){
CS=0;
spi_write(0x00 | reg);
spi_write(DATA_REG[0]);
spi_write(DATA_REG[1]);
spi_write(DATA_REG[2]);
CS=1;
}
void setparam(char p1,char p2,char p3){
DATA_REG[0]=p1;
DATA_REG[1]=p2;
DATA_REG[2]=p3;
}
void ADRST(){
CS=0;
//adc reset
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
CS=1;
__delay_ms(1);
}
void ADRD(char reg){
CS=0;
spi_write(0x40 | reg);
DATA_REG[0]=spi_write(0x00);
DATA_REG[1]=spi_write(0x00);
DATA_REG[2]=spi_write(0x00);
CS=1;
__delay_ms(1);
}
void single_mode(){
ADRD(Mode);
DATA_REG[0] &=0x0F;
DATA_REG[0] |=0X20;
ADWR(Mode);
}
void cont_mode(){
ADRD(Mode);
DATA_REG[0] &=0x0F;
ADWR(Mode);
}
void idle_mode(){
ADRD(Mode);
DATA_REG[0] &=0x0F;
DATA_REG[0] |=0X40;
ADWR(Mode);
}
char get_status(){
char tmp;
CS=0;
spi_write(0x40);
tmp=spi_write(0x00);
CS=1;
return tmp;
}
void AD7190_init(){
ADRST();
__delay_ms(10);
setparam(0x00,0x01,0x00); //Chop:Disable,Refin1(+)&Refin1(-),CH1,No burn,No ref det,no BUF,Bipolar,Gain:1
ADWR(Conf);
setparam(0x8C,0x85,0x60); //Internal Zero-Scale,Internal 4.92 MHz,SINC3,No ENPAR,NO Single,REJ60:EN
ADWR(Mode);
while((get_status()&0x80)!=0x80); //wait to Calibraion
cont_mode();
}
این هم کد برنامه:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 12000000
#define RS LATD0
#define EN LATD1
#include "spi.c"
#include "lcd.c"
#define CS LATA5
char DATA_REG[3];
#include "ad7190.c"
char temp;
static void interrupt
isr(){
if(TMR2IF==1){
TMR2IF=0;
}
else if(INT0IF==1){
INT0IF=0;
LATB3=~LATB3;
}
}
void main(){
unsigned long xc;
double x1;
__delay_ms(10);
lcd_init();
lcd_cmd4(0x01); //clear LCD
spi_init();
TRISB |=0x01;
//Enable RB0/INT
INTCON2=0x80;
RCON=0x00;
INTCON=0xD0;
//tmr2
TMR2=0;
PR2=0xFF;
T2CON=0x7F;
TMR2IE=1;
PEIE=1;
GIE=0; //Disable Global Interrupt
AD7190_init();
LATB3=0;
while(1){
while(1){
if((get_status() & 0x80)!=0x80)break; //wait to RDY
}
LATB3=1;
ADRD(Data);
xc=DATA_REG[0]*65536;
xc+=(DATA_REG[1]*256);
xc+=(DATA_REG[2] & 0xF0);
//bipolar
x1=xc*2.495; //2.495 Vref
x1/=8388608; //2^N-1 where N=24
x1-=2.495;
//unipolar
//x1=xc*2.495;
//x1/=16777216;
x1*=1000; //Convert to mV
lcd_cmd4(0x01); //clear LCD
printf("%4.5f mV",x1);
LATB3=0;
}
}
ویرایش