سلام دوستان
من میخوام سنسور HP03SA رو که یک سنسور فشار و دما با خروجی دیجیتال و با ارتباط I2C هست رو راهآاندازی کنم.
برنامهآی خواندن ضرایب از EEPROM داخلی سنسور و خواندن دیتا از واحد ADC سنسور رو نوشتم(فقط فشار رو نیاز داشتم).علاوه بر این برنامه محاسباتی فشار نهایی رو هم نوشتم اما صد درصد از برنامهآای که نوشتم مطمئن نیستم و یه قسمتآهایی داره که متوجه نمیشم باید چکار کنم مثل پایهآهای MCLK و XCLR .دوستان امیدوارم کمک کنن ^
^
دیتاشیت سنسور رو میذارم
http://www.datasheetlib.com/datashee...html#datasheet
اینم برنامهآای که نوشتم
من میخوام سنسور HP03SA رو که یک سنسور فشار و دما با خروجی دیجیتال و با ارتباط I2C هست رو راهآاندازی کنم.
برنامهآی خواندن ضرایب از EEPROM داخلی سنسور و خواندن دیتا از واحد ADC سنسور رو نوشتم(فقط فشار رو نیاز داشتم).علاوه بر این برنامه محاسباتی فشار نهایی رو هم نوشتم اما صد درصد از برنامهآای که نوشتم مطمئن نیستم و یه قسمتآهایی داره که متوجه نمیشم باید چکار کنم مثل پایهآهای MCLK و XCLR .دوستان امیدوارم کمک کنن ^
^دیتاشیت سنسور رو میذارم
http://www.datasheetlib.com/datashee...html#datasheet
اینم برنامهآای که نوشتم
کد:
#include <mega32.h>
// I2C Bus functions
#asm
.equ __i2c_port=0x1B ;PORTA
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
#include <i2c.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h>
#include <delay.h>
#include <math.h>
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
}
// Declare your global variables here
signed int C1,C2,C3,C4,C5,C6,C7,A,B,C,D,D1,D2;
long float DUT,OFF,SENS,X,Press;
float MD1,MD2,MD3;
char z[30];
unsigned char read_cal_data(void);
unsigned int readPressureAD(void);
long float CalculatePressure(unsigned int);
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=0 State2=0 State1=T State0=T
PORTA=0x00;
DDRA=0x0C;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// 2 Wire Bus initialization
// Generate Acknowledge Pulse: On
// 2 Wire Bus Slave Address: 0h
// General Call Recognition: Off
// Bit Rate: 62.500 kHz
TWSR=0x00;
TWBR=0x00;
TWAR=0x00;
TWCR=0x44;
// I2C Bus initialization
i2c_init();
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
read_cal_data();
while (1)
{
// Place your code here
readPressureAD();
CalculatePressure();
delay_ms(300);
};
}
unsigned char read_cal_data(void) // tabe khandan C1,C1,...,C,D. ravesh khandan eeprom bsorat khandan motevali ast
{
unsigned char ucValue;
i2c_start();
i2c_write(0xa0); // device address
i2c_write(16); // word address
i2c_start();
i2c_write(0xa1); // device address
ucValue=i2c_read(1); // meghdar 1 ba dalil sader shodan ACK pas az khandan 1 bay ast(ba tavajoh ba gha'ede khandan motevali)
C1=ucValue;
ucValue=i2c_read(1);
C1<<=8; // 8 bit ka aval daryaft shode ra 8 ta ba chap shift madahim ta MSB shavad
C1|=ucValue; // C1=C1+ucValue
ucValue=i2c_read(1);
C2=ucValue;
ucValue=i2c_read(1);
C2<<=8;
C2|=ucValue;
ucValue=i2c_read(1);
C3=ucValue;
ucValue=i2c_read(1);
C3<<=8;
C3|=ucValue;
ucValue=i2c_read(1);
C4=ucValue;
ucValue=i2c_read(1);
C4<<=8;
C4|=ucValue;
ucValue=i2c_read(1);
C5=ucValue;
ucValue=i2c_read(1);
C5<<=8;
C5|=ucValue;
ucValue=i2c_read(1);
C6=ucValue;
ucValue=i2c_read(1);
C6<<=8;
C6|=ucValue;
ucValue=i2c_read(1);
C7=ucValue;
ucValue=i2c_read(1);
C7<<=8;
C7|=ucValue;
ucValue=i2c_read(1);
A=ucValue;
ucValue=i2c_read(1);
B=ucValue;
ucValue=i2c_read(1);
C=ucValue;
ucValue=i2c_read(1);
D=ucValue;
}
unsigned int readPressureAD(void) // tabe khandan D1 az ADC
{
unsigned char ucData;
i2c_start();
i2c_write(0xee);
i2c_write(0xff);
i2c_write(0xf0);
i2c_stop();
delay_ms(40);
i2c_start();
i2c_write(0xee);
i2c_write(0xfd);
i2c_start();
i2c_write(0xef);
D1=i2c_read(1);
D1<<=8; // khandan MSB
ucData=i2c_read(0); // da in halat bayad 0 nevesht chon bayad NACK ersal shvad
i2c_stop();
D1|=ucData;
return D1;
}
long float CalculatePressure(void) // tabe mohasebat
{
if(D2<C5)
{
MD1=D2-C5;
MD2=MD1/pow(2,7);
MD3=(MD2*B)/pow(2,C);
dUT=MD1-(MD2*MD3);
}
else // D2>=C5
{
MD1=D2-C5;
MD2=MD1/pow(2,7);
MD3=(MD2*A)/pow(2,C);
dUT=MD1-(MD2*MD3);
}
//calculate OFF
OFF=(C2+((C4-1024)*dUT/pow(2,14)))*4;
//calculate SENS
SENS=C1+(C3*dUT)/pow(2,10);
//calculate X
X=(SENS*(D1-7168)/pow(2,14))-OFF;
//calculate Press
Press=(X*10/pow(2,5))+C7;
lcd_init(16);
lcd_gotoxy(0,0);
sprintf(z,"pressure is %6.2f",Press);
lcd_puts(z);
}





oo:
دیدگاه