hi, i need help =(
i'm trying for a weeks ago work with this accelerometer, i used your code, the pic32mx795f512l usb starter kit II, the I/O expansion board and the I2C2, connect SDA2 to pin 36 and SCL2 to pin 38 for J11 connector of expansion board and the i2c is not working i have the pull ops resistor of 4.7 k for SDA and SCL and is not working i dont know what to do... is my first time posting a problem please help me is for my graduation work
i'm trying for a weeks ago work with this accelerometer, i used your code, the pic32mx795f512l usb starter kit II, the I/O expansion board and the I2C2, connect SDA2 to pin 36 and SCL2 to pin 38 for J11 connector of expansion board and the i2c is not working i have the pull ops resistor of 4.7 k for SDA and SCL and is not working i dont know what to do... is my first time posting a problem please help me is for my graduation work
Rich (BB code):
#include "HardwareProfile.h"
#include "dee_emulation/dee_emulation_pic32.h"
#include <stdio.h>
#include <peripheral/i2c.h>
#include <plib.h>
// FreeRTOS includes
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "croutine.h"
// set the configuration fuses for the processor
#pragma config FPLLODIV = DIV_1, FPLLMUL = MUL_20, FPLLIDIV = DIV_2
#pragma config FWDTEN = OFF, FPBDIV = DIV_2, POSCMOD = HS, FNOSC = PRIPLL, CP = OFF
void prvACELEROMETRO_ADC( void *pvParameters)
{
for(;;) {
unsigned char canal;
signed int muestreo;
unsigned char unidad_muestreo_total;
unsigned char muestreo_total;
unsigned int horas;
unsigned int minutos;
unsigned int segundos;
float Xdata;
float Ydata;
float Zdata;
unsigned char dato1, dato2, dato3, dato;
//leer la variable de luz, dividir los bit, si es 1 iniciar el adc guardar los otro en variables y el tiempo total tambien//
//DataEEReadArray (&acelerometroL,5,1);
//DataEEReadArray (&acelerometro_tiempoL,6,1);
//
//canal = acelerometroL >> 7;
//muestreo = (acelerometroL & 0b01111111);
//unidad_muestreo_total = acelerometro_tiempoL >> 6;
//muestreo_total = (acelerometro_tiempoL & 0b00111111);
//segundos = ((muestreo)* 1000);
//
// if(canal == 1){
// while (segundos > 0 ){
void configPIC(){
SYSTEMConfigPerformance(GetSystemClock()); //que e slo mismo que config(system_clk)
}
void configureI2C(void){
I2CConfigure(I2C2, I2C_ENABLE_SLAVE_CLOCK_STRETCHING|I2C_ENABLE_HIGH_SPEED);
I2CSetFrequency (I2C2,80000000ul,4000);
I2CEnable (I2C2, TRUE);
}
void accelwrite(unsigned int PER_ADDR, unsigned int reg_addr, unsigned char *buf)
{
I2CStart(I2C2);
I2CBusIsIdle(I2C2);
I2CSendByte(I2C2, PER_ADDR | 0); //llamo el dispositivo para escribir
I2CBusIsIdle(I2C2);
if (I2C2STATbits.ACKSTAT){return;} //solicitar un ack
I2CSendByte(I2C2,reg_addr); //envio la direccion del registro del periferico
I2CBusIsIdle(I2C2);
I2CSendByte(I2C2,*buf); //escribo el dato almacenado en la variable apuntada por buf
I2CBusIsIdle(I2C2);
if (I2C2STATbits.ACKSTAT){return;} //solicitando un ack
I2CStop(I2C2);
I2CBusIsIdle(I2C2);
while(1)
{
I2CStart(I2C2);
I2CBusIsIdle(I2C2);
I2CSendByte(I2C2,PER_ADDR | 0); //llamo el dispositivo para escribir
I2CBusIsIdle(I2C2);
if (I2C2STATbits.ACKSTAT==0){ break;} //si se devuelve un ack dejo el ciclo
I2CStop(I2C2);
I2CBusIsIdle(I2C2);
}
I2CStop(I2C2);
I2CBusIsIdle(I2C2);
}
void accel_init(){
int inicio = 0x3A;
dato = 0b00000001;
accelwrite (inicio,0x16,&dato);
dato = 0b00000000;
accelwrite (inicio,0x18,&dato);
dato = 0b00000000;
accelwrite (inicio,0x18,&dato);
}
void accelread(unsigned int PER_ADDR, unsigned int reg_addr,unsigned char *buf1,unsigned char *buf2,unsigned char *buf3)
{
I2CStart(I2C2); //comenzar el I2C
I2CBusIsIdle(I2C2); //en espera de un comando
I2CSendByte(I2C2,PER_ADDR | 0); //parte del dispositivo en el cual escribir
I2CBusIsIdle(I2C2);
if (I2C2STATbits.ACKSTAT==1){return;} //se devuelve si recibe un ack si no se reinicia
I2CSendByte(I2C2,reg_addr); //llama el registro interno del periferico
I2CBusIsIdle(I2C2);
if(I2C2STATbits.ACKSTAT==1){ return;} //si retorna un ack si no reinicia el proceidmiento
I2CRepeatStart(I2C2); //hacer un reinicio con el fin de llamar a la funcion de leer de lo contrario se crea un conflicto en el bus
I2CBusIsIdle(I2C2);
I2CSendByte(I2C2,PER_ADDR | 1); //llama al periferico donde se desea la lectura
I2CBusIsIdle(I2C2);
if(I2C2STATbits.ACKSTAT==1) {return;}
I2C2CONbits.RCEN = 1;
while(!I2CReceivedDataIsAvailable(I2C2)); //empezar a recibir datos
*buf1 = I2C2RCV; //pongo el dato en el buffer
I2C2CONbits.ACKDT = 0;
I2C2CONbits.ACKEN = 1;
while(!I2CReceivedDataIsAvailable(I2C2)); //empiezo a recibir datos
*buf2 = I2C2RCV; //pongo el dato en el buffer
I2C2CONbits.ACKDT = 0;
I2C2CONbits.ACKEN = 1;
while(!I2CReceivedDataIsAvailable(I2C2)); //empiezo a recibir datos
*buf3 = I2C2RCV; //pongo el dato en el buffer
I2C2CONbits.ACKDT = 1;
I2C2CONbits.ACKEN = 1;
while(!I2CReceivedDataIsAvailable(I2C2)); //inicio el procedimiento del ack para llegar al final de la transmicion
I2CStop(I2C2);
I2CBusIsIdle(I2C2);
}
void Elabacc ()
{
int inicio=0x3A;
accelread(inicio,0x06,&dato1,&dato2,&dato3); //Z axis 8 bit, only one avaible in measurament mode
Xdata=dato1;
Ydata=dato2;
Zdata=dato3;
}
configPIC();
configureI2C();
accel_init();
Elabacc();
//segundos = segundos - 100;
vTaskDelay (100/portTICK_RATE_MS); // delay por 100 ms
}
}
Last edited by a moderator: