Vizitator Mareshal Postat Mai 31, 2011 Partajează Postat Mai 31, 2011 Incerc sa iau temperatura de la un AD5933(http://www.analog.com/static/imported-f ... AD5933.pdf), sa vad macar ca imi functioneaza I2C-ul calumea. Senzorul de temperatura(datasheet pagina 16, reda temperatura pe 16 biti, la dresele 0x92 si 0x93). Aveti un indiciu? Am nevoie de putin ajutor. Momentan primesc: 40 si 255 Asta e codul pana acum: #include <stdio.h>#include <math.h>#include <htc.h>#include "usart.h"#include "i2c.h"#define STATUS 0x00#define PCLATH 0x00#define _XTAL_FREQ 4000000 // Pic osc 4mhz#define startfreq 200 //Start Frequency#define incfreq 100 //Incremental Frequency#define SUCCESS 0xff //Flag //Command#define Start 0xA4#define Stop 0x94#define Trans 0x84#define ACK 0xC4 //AD5933 control codes#define Init 0x10 //Initialize with start Freq#define Sweep 0x20 //Start Frequency Sweep#define IncFreq 0x30 //Increment Frequency#define RepFreq 0x40 //Repeat Frequency#define MeaTemp 0x90 //Measure Temperature#define PowerDown 0xa0 //Power down mode#define Standby 0xb0 //Standby mode#define Range2V 0x00 //Output Voltage range 2V#define Range1V 0x06 //Output Voltage range 1V#define Range400mV 0x04 //Output Voltage range 400mV#define Range200mV 0x02 //Output Voltage range 200mV#define gainx5 0x00 //PGA gain x5#define gainx1 0x01 //PGA gain x1//AD5933 Register addresses#define Control_high 0x80#define Control_low 0x81#define Freq_high 0x82#define Freq_mid 0x83#define Freq_low 0x84#define FreqInc_high 0x85#define FreqInc_mid 0x86#define FreqInc_low 0x87#define NumInc_high 0x88#define NumInc_low 0x89#define NumSettle_high 0x8a#define NumSettle_low 0x8b#define Status 0x8f#define Temp_high 0x92#define Temp_low 0x93#define Real_high 0x94#define Real_low 0x95#define Imag_high 0x96#define Imag_low 0x97/**************************************************************************** Function : unsigned char Byte_write(unsigned char reg_addr, unsigned char data) Write a byte to AD5933.****************************************************************************/unsigned char Byte_write(unsigned char reg_addr, unsigned char data){ i2c_Start(); i2c_PutByte(0x0D); i2c_WriteTo(reg_addr); i2c_PutByte(data); i2c_Stop(); return 1;}/**************************************************************************** Function : unsigned char Byte_read(unsigned char reg_loc) Read a byte from AD5933.****************************************************************************/unsigned char Byte_read(unsigned char reg_loc){ i2c_Start(); i2c_PutByte(0x0D); i2c_ReadFrom(reg_loc); //set the pointer location return i2c_GetByte(I2C_MORE); i2c_Stop();} /**************************************************************************** Function : unsigned long int Data_proc(unsigned char data_high, unsigned char data_low) Calculate the code value from AD5933.****************************************************************************/unsigned long int Data_proc(unsigned char data_high, unsigned char data_low){ unsigned long int data; data=(unsigned long int)data_high*256+data_low; if(data > 0x7fff) { data=0x10000-data; } return data;}/**************************************************************************** Function : void Display(unsigned long int frequency, unsigned long int impedance) A function used to display the measured results.****************************************************************************/void Display(unsigned long int frequency, unsigned long int impedance){ printf("freq: %li, imp: %li", frequency, impedance);} void main(){ TRISA = 0x00; RA0 = 0xFF; //USART INTCON=0; // purpose of disabling the interrupts. init_comms(); // set up the USART - settings defined in usart.h // Declare your local variables here unsigned char i,a,b, t1, t2; unsigned long int R,I,T; //Real and Imag numbers unsigned long int impedance; //Refence Impedance values used in the system unsigned long int ReImpedance[]={200000}; //Analog switches control code float code,Calcode; // I2C Bus initialization//i2c_Start();// Global enable interrupts while(1) { //printf("me "); // Place your code here //set start frequency Byte_write(Freq_low, 0x00); Byte_write(Freq_mid ,0x19); Byte_write(Freq_high, 0x00); //set Incremental frequency Byte_write(FreqInc_low,0x00); Byte_write(FreqInc_mid,0xC0); Byte_write(FreqInc_high,0x80); //Set Num of Inc Byte_write(NumInc_high, 0x00); Byte_write(NumInc_low, 0x63); //Set Num of Settling Time Cycles Byte_write(NumSettle_high, 0x02); Byte_write(NumSettle_low,0x01); //Working mode Byte_write(Control_low, 0x00); Byte_write(Control_high, 0xb0); Byte_write(Control_high, 0x10); Byte_write(Control_high, 0x20); while(!(Byte_read(Status) & 0x02)); // a=Byte_read(Real_high); // b=Byte_read(Real_low); // printf("%i %i", a, b); // R=Data_proc(a, b); // a=Byte_read(Imag_high); // b=Byte_read(Imag_low); // I=Data_proc(a, b); t1=Byte_read(Temp_high); t2=Byte_read(Temp_low); T=Data_proc(t1, t2); printf("t1=%i, t2=%i, T=%i :: ", t1, t2, T); }}void interrupt isr(){} // to avoid bootloader overwrite. Link spre comentariu
Liviu M Postat Mai 31, 2011 Partajează Postat Mai 31, 2011 /**************************************************************************** Function : unsigned char Byte_write(unsigned char reg_addr, unsigned char data) Write a byte to AD5933.****************************************************************************/unsigned char Byte_write(unsigned char reg_addr, unsigned char data){ i2c_Start(); i2c_PutByte(0x0D); i2c_WriteTo(reg_addr); i2c_PutByte(data); i2c_Stop(); return 1;}/**************************************************************************** Function : unsigned char Byte_read(unsigned char reg_loc) Read a byte from AD5933.****************************************************************************/unsigned char Byte_read(unsigned char reg_loc){ i2c_Start(); i2c_PutByte(0x0D); i2c_ReadFrom(reg_loc); //set the pointer location return i2c_GetByte(I2C_MORE); i2c_Stop();}.[/code]La i2c, bitul LSB din adresa indica operatia - 0 pentru scris, 1 pentru citit . Tu si la read si la write trimiti aceeasi (banuiesc) adresa - 0x0D. Daca nu faci prin functii nici un artificiu, s-ar putea ca de aici sa-ti vina o parte din probleme. Link spre comentariu
Vizitator Mareshal Postat Mai 31, 2011 Partajează Postat Mai 31, 2011 Codul original a fost tradus de la un ATMEGA: http://www.avrfreaks.net/index.php?name ... 80&start=0 Cred ca am omis niste chestii Link spre comentariu
Liviu M Postat Mai 31, 2011 Partajează Postat Mai 31, 2011 Probabil te-ai incurcat la Send_adr(SLA_W);pentru scris si Send_adr(SLA_R); pentru citit. Adresa senzorului fiind 0001101 (7 biti), pentru tine SLA_W = 00011010 = 0x1A si SLA_R = 00011011 = 0x1B. Din cauza ca bitul LSB al "adresei" indica operatia, adresa pentru scriere e intotdeauna para, cea de citire impara (asta asa, ca verificare rapida). LE Ca sa vezi cum se socotesc adresele astea poti sa te uiti la pagina 28 din data-sheetul pe care l-ai postat mai sus. Link spre comentariu
Postări Recomandate
Creează un cont sau autentifică-te pentru a adăuga comentariu
Trebuie să fi un membru pentru a putea lăsa un comentariu.
Creează un cont
Înregistrează-te pentru un nou cont în comunitatea nostră. Este simplu!
Înregistrează un nou contAutentificare
Ai deja un cont? Autentifică-te aici.
Autentifică-te acum