|
Tài trợ cho PIC Vietnam |
Đóng góp ý kiến Nơi tiếp nhận những ý kiến đóng góp của thành viên |
|
Ðiều Chỉnh | Xếp Bài |
31-10-2015, 04:58 PM | #12 |
Nhập môn đệ tử
Tham gia ngày: Apr 2015
Bài gửi: 1
: |
cảm biến MLX90614 đo nhiệt độ non_contact.
mình đang đọc dữ liệu từ con cảm biến MLX90614 bằng PIC18F4620 bằng SMBUS, tuy nhiên bị lỗi khi đọc về toàn là 0xFFFF, ai có biết và kinh nghiệm về phần này có thể cho mình vài tham khảo được không.
đây là code của mình void I2C1_init(void) // init I2C1 as master { TRISCbits.TRISC3 = 1; // LT4100 SCK, Smart battery SCK TRISCbits.TRISC4 = 1; // LT4100 SDA, Smart battery SDA //PIE1bits.SSPIE = 1; SSPCON1 = 0x28; //enable serial port and configure SDA and SCL SSPSTATbits.SMP = 0; //slew rate disable and configered to input level to I2C (< 100k) SSPSTATbits.CKE = 1; // Enable input logic thresholds to SMBus compatable SSPADD = 49; // baud rate fixed to SSPCON2 = 0x00; //SSPCON2bits.ACKEN = 1; } void I2C1_Idle(void) { //while (SSP1STATbits.BF); // Wait for bus Idle (BF - is high while transmission in progress) while ((SSPCON2 & 0x1F) | (SSPSTATbits.R_W)); } void I2C1SSPIFWait(void) { while (PIR1bits.SSPIF != 1); PIR1bits.SSPIF = 0; } void I2C1_start(void) { I2C1_Idle(); // Wait for Bus Idle SSPCON2bits.SEN = 1; I2C1SSPIFWait(); } void I2C1_repeated_start(void) { SSPCON2bits.RSEN = 1; I2C1SSPIFWait(); } void I2C1_stop(void) { SSPCON2bits.PEN = 1; // STOP condition I2C1SSPIFWait(); } void I2C1Ack(void) { SSPCON2bits.ACKDT = 0; /* Acknowledge data bit, 0 = ACK */ SSPCON2bits.ACKEN = 1; /* Ack data enabled */ while(SSPCON2bits.ACKEN); /* wait for ack data to send on bus */ } void I2C1Nak(void) { SSPCON2bits.ACKDT = 1; /* Acknowledge data bit, 1 = NAK */ SSPCON2bits.ACKEN = 1; /* Ack data enabled */ while(SSPCON2bits.ACKEN); /* wait for ack data to send on bus */ } void I2C1RecieveEnable(void) { SSPCON2bits.RCEN = 1; //enable receive enable bit I2C1SSPIFWait(); } //address msb bits. LSB read/write 1/0 bit void I2C1_byte_write(unsigned char dev_add, unsigned char loc_add, unsigned char loc_dat) { I2C1_start(); // SMBus START SSPBUF = dev_add; // temp_add;//device add + write I2C1SSPIFWait(); SSPBUF = loc_add; // Storage Address (command) I2C1SSPIFWait(); SSPBUF = loc_dat; // data I2C1SSPIFWait(); I2C1_stop(); // SMBus STOP } void I2C1_word_write(unsigned char dev_add, unsigned char loc_add, unsigned int loc_dat) { unsigned char loc_dat_msb = 0, loc_dat_lsb = 0; loc_dat_msb = ((loc_dat & 0xFF00) >> 8); loc_dat_lsb = (loc_dat & 0x00FF); I2C1_start(); // SMBus START SSPBUF = dev_add; // device add + write bit I2C1SSPIFWait(); SSPBUF = loc_add; // Storage Address (command) I2C1SSPIFWait(); SSPBUF = loc_dat_lsb; // LSB data I2C1SSPIFWait(); SSPBUF = loc_dat_msb; // MSB data I2C1SSPIFWait(); I2C1_stop(); // SMBus STOP } unsigned char I2C1_byte_read(unsigned char dev_add, unsigned char loc_add) { unsigned char read_dat = 0; unsigned char temp_add = (dev_add | 0x01); I2C1_start(); // SMBus START SSPBUF = dev_add; // Slave Address with Write Bit I2C1SSPIFWait(); //pointing to specific location SSPBUF = loc_add; //Command Code / add location I2C1SSPIFWait(); //I2C1_repeated_start(); SSPBUF = temp_add; //Slave Address + Read Bit I2C1SSPIFWait(); read_dat = SSPBUF; //saving data into some register // SSPCON2bits.ACKDT = 0; // Send an ack bit // SSPCON2bits.ACKEN = 1; // while (SSPCON2bits.ACKEN); // Wait for ack to finish I2C1_stop(); // SMBus STOP return (read_dat); } unsigned int I2C1_Word_read(unsigned char dev_add, unsigned char loc_add) { unsigned int read_dat_msb = 0, read_dat_lsb = 0, read_dat = 0, read_dat_pec = 0; unsigned char temp_add = (dev_add | 0x01); //((dev_add << 1) | 0x01); //Fz_Mod I2C1_start(); // SMBus START SSPBUF = dev_add; // Slave Address with Write Bit I2C1SSPIFWait(); //pointing to specific location SSPBUF = loc_add; //Command Code / add location I2C1SSPIFWait(); I2C1_repeated_start(); // Repeated Start SSPBUF = temp_add; //Slave Address + Read Bit I2C1SSPIFWait(); I2C1RecieveEnable(); // Receive Enable read_dat_lsb = SSPBUF; //saving data into some register I2C1Ack(); // ACK I2C1RecieveEnable(); // Receive Enable read_dat_msb = SSPBUF; //saving data into some register I2C1Nak(); // ACK I2C1RecieveEnable(); // Receive Enable read_dat_pec = SSPBUF; //I2C1Ack(); // NACK I2C1_stop(); // SMBus STOP read_dat_msb = read_dat_msb << 8; read_dat = read_dat_msb | read_dat_lsb; return (read_dat); } |
|
|