Olá, eu to lendo há alguns dias já hahaha
Obrigado por me responder!
Estou usando um PIC18F4550
http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf
e o acelerômetro MM845X: http://chiselapp.com/user/jschimpf/repository/MM8451/doc/tip/Docs/MMA8451Q.pdf
Já configurei as saídas direitinho (acredito eu) e to vendo no osciloscópio a comunicação.
Porém ao tentar escrever o valor no LCD, não aparece nada, isso implica que o PIC não tá lendo a informação ou que o acelerômetro não está enviando.
Eu acredito que não configurei o acel apropriadamente e por isso estou lendo mais.
No datasheet fala isso:
Single Byte Read
The MMA8451Q has an internal ADC that can sample, convert and return sensor data on request. The transmission of an
8-bit command begins on the falling edge of SCL. After the eight clock cycles are used to send the command, note that the data
returned is sent with the MSB first once the data is received. Figure 11 shows the timing diagram for the accelerometer 8-bit I2C
read operation. The Master (or MCU) transmits a start condition (ST) to the MMA8451Q, slave address ($1D), with the R/W bit
set to “0” for a write, and the MMA8451Q sends an acknowledgement. Then the Master (or MCU) transmits the address of the
register to read and the MMA8451Q sends an acknowledgement. The Master (or MCU) transmits a repeated start condition (SR)
and then addresses the MMA8451Q ($1D) with the R/W bit set to “1” for a read from the previously selected register. The Slave
then acknowledges and transmits the data from the requested register. The Master does not acknowledge (NAK) the transmitted
data, but transmits a stop condition to end the data transfer.
E foi o que eu tentei fazer:
I2C_Master_Start(); //Start condition
I2C_Master_Write(0b38); //7 bit address + Read
I2C_Master_Write(0b01); //7 bit address + Read
I2C_Master_RepeatedStart();
I2C_Master_Write(0b39); //7 bit address + Read
val = I2C_Master_Read(0); //Read + Acknowledge
I2C_Master_Stop(); //Stop condition
Mas não está rolando infelizmente.
Alguma dica?
#include <p18f4550.h>
#include “lcd.c”
#include <delay.h>
#include <stdio.h>
unsigned int val=0;
unsigned int vez=0;
#define _XTAL_FREQ 20000000
void I2C_Master_Init(const unsigned long c)
{
SSPCON1 = 0b00101000;
SSPCON2 = 0;
SSPADD = (_XTAL_FREQ/(4*c))-1;
SSPSTAT = 0;
TRISB0 = 1; //Setting as input as given in datasheet
TRISB1 = 1; //Setting as input as given in datasheet
}
void I2C_Master_Wait()
{
while ((SSPSTAT & 0x04) || (SSPCON2 & 0x1F));
}
void I2C_Master_Start()
{
I2C_Master_Wait();
SEN = 1;
}
void I2C_Master_RepeatedStart()
{
I2C_Master_Wait();
RSEN = 1;
}
void I2C_Master_Stop()
{
I2C_Master_Wait();
PEN = 1;
}
void I2C_Master_Write(unsigned d)
{
I2C_Master_Wait();
SSPBUF = d;
}
unsigned short I2C_Master_Read(unsigned short a)
{
unsigned short temp;
I2C_Master_Wait();
RCEN = 1;
I2C_Master_Wait();
temp = SSPBUF;
I2C_Master_Wait();
ACKDT = (a); //0:1;
ACKEN = 1;
return temp;
}
void main()
{
I2C_Master_Init(100000); //Initialize I2C Master with 100KHz clock
TRISD=0; // Configura como saída
TRISC=0; // Configura como saída
TRISE=0; // Configura como saída
LATC=0; // Seta pra zero
LATD=0; // Seta pra zero
LATE=0; // Seta pra zero
lcd_init();
lcd_clear();
lcd_goto(0,0);
lcd_puts(" TESTE: ");
while(1)
{
PORTDbits.RD2 = 1; // Liga pra ver se o código travou (cria PWM)
I2C_Master_Start(); //Start condition
I2C_Master_Write(0b38); //7 bit address + Read
I2C_Master_Write(0b01); //7 bit address + Read
I2C_Master_RepeatedStart();
I2C_Master_Write(0b39); //7 bit address + Read
val = I2C_Master_Read(0); //Read + Acknowledge
I2C_Master_Stop(); //Stop condition
lcd_goto(2,5);
printf ("%u", val);
delay_ms(1000);
PORTDbits.RD2 = 0; // Desliga pra ver se o código travou (cria PWM)
delay_ms(1000);
}
}