|
|
View previous topic :: View next topic |
Author |
Message |
andy09
Joined: 20 Jun 2013 Posts: 2
|
accelerometer LSM303D |
Posted: Mon May 11, 2015 7:41 pm |
|
|
I need your help, I am working with the accelerometer sensor, but I can read the output channel (X, Y and Z).
I don't know that I do wrong
This is my code:
Code: | #INCLUDE <18F4550.h>
#FUSES HSPLL, NOWDT, NOPROTECT, NOLVP, NODEBUG, USBDIV, PLL1, CPUDIV1, VREGEN
#USE delay(clock=48000000)
#include <LSM303D.h>
#define USB_HID_DEVICE FALSE //deshabilitamos el uso de las directivas HID
#define USB_EP1_TX_ENABLE USB_ENABLE_BULK //turn on EP1(EndPoint1) for IN bulk/interrupt transfers
#define USB_EP1_RX_ENABLE USB_ENABLE_BULK //turn on EP1(EndPoint1) for OUT bulk/interrupt transfers
#define USB_EP1_TX_SIZE 3 //size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE 2 //size to allocate for the rx endpoint 1 buffer
#include <pic18_usb.h> //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
#include <PicUSB.h> //Configuración del USB y los descriptores para este dispositivo
#include <usb.c> //handles usb setup tokens and get descriptor reports
//Asignacion de variables
#define LEDV PIN_D1
#define LEDR PIN_D0
#define LEDP PIN_D2
#define LED_ON output_high
#define LED_OFF output_low
#define DATAXL envia[0]
#define DATAYH envia[1]
int8 recibe[2]; //tamaño de arreglo por recibir
int8 envia[3]; //tamaño de arreglo por enviar
void main(void)
{
set_tris_d(0x00); //Configuracion para el puerto D como salida
output_d(0x00); //Limpiamos el puerto D
set_tris_B(0x0); //Configuracion para el puerto b como salida
output_B(0x00);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
disable_interrupts(GLOBAL); //deshabilitamos todas las interrupciones
LED_OFF(LEDV); //Apagamos led Verde
LED_ON(LEDR); //Encendemos led Rojo
delay_ms(500);
usb_init(); //inicializamos el USB
LED_ON(LEDV); //Apagamos led Verde
LED_OFF(LEDR);
delay_ms(500);
usb_task(); //habilita periferico usb e interrupciones
LED_OFF(LEDV); //Apagamos led Verde
LED_ON(LEDR);
delay_ms(500);
usb_wait_for_enumeration(); //esperamos hasta que el PicUSB sea configurado por el host
LED_ON(LEDV); //Apagamos led Verde
LED_OFF(LEDR);
delay_ms(500);
i2c_init(90000);
delay_ms(100);
//accel_init();
//escribir_dato(acce_W ,0x21, 0x20); //modo normal a 50Hz
//escribir_dato(acce_W ,REG2_A, 0x80); //
while (TRUE)
{
if(usb_enumerated()) //Si el PicUSB está configurado
{
if (usb_kbhit(1)) //Si el endpoint de salida contiene datos del host
{
LED_ON(LEDP); //Apagamos led Verde
delay_ms(500);
DATAXL=read_acc(OUT_X_L_A);
DATAXH=read_acc(OUT_X_H_A);
usb_put_packet(1,envia,3,USB_DTS_TOGGLE); //enviamos el paquete de tamaño 1byte del EP1 al PC
usb_get_packet(1, recibe, 2); //Cachamos el paquete de tamaño 2bytes del EP1 y almacenamos en recibe
LED_OFF(LEDP); //Apagamos led Verde
delay_ms(500);
}
}
}//While
}//main
|
Library;
Code: | #include <MATH.h>
#use i2c (MASTER, SDA=PIN_B0, SCL=PIN_B1, FORCE_HW) //fast,
//**************** Registros de configuración de acelerometro
#define acce_W 0x3A //dirección de escritura para acelerometro
#define acce_R 0x3B //dirección de lectura para acelerometro
#define TEMP_OUT_L 0x05 //registro de temparatura parte baja
#define TEMP_OUT_H 0x06 //registro de temparatura parte alta
#define WHO_AM_I 0x0F //registro de configuración
#define CTRL0 0x1F //registro de configuración 1
#define REG1_A 0x20 //registro de configuración 1
#define REG2_A 0x21 //registro de configuración 2
#define REG3_A 0X22 //registro de configuración 3
#define REG4_A 0X23 //registro de configuración 4
#define REG5_A 0X24 //registro de configuración 5
#define REG6_A 0X25 //registro de configuración 6
#define REG7_A 0X26 //registro de configuración 6
#define STATUS_A 0x27 //registro de configuración status
//******************* Registros de lectura para IMU acelerometro
#define OUT_X_L_A 0x28
#define OUT_X_H_A 0x29
#define OUT_Y_L_A 0x2A
#define OUT_Y_H_A 0x2B
#define OUT_Z_L_A 0x2C
#define OUT_Z_H_A 0x2D
//******************************************************************************
//**********Función para escribir un dato a los sensores************************
// address + W = dirección del dispositivo+ write
// reg = registro al cual se escribe
// valor = valor que se escribira
void escribir_dato(int direccion,int reg, int valor){
i2c_start(); //Inicia comunicacion
i2c_write(direccion); //Address+W
i2c_write(reg); //Register address (datos para la lectura de las cordenadas x, y)
i2c_write(valor); //Valor a escribir
i2c_stop(); //Fin
}
//******************** funciones manejo de acelerometro*************************
//funcion que inicializa el accelerometro;
void accel_init(){
escribir_dato(acce_W ,CTRL0,0);
escribir_dato(acce_W ,REG1_A,0b01010111); //modo normal a 1600Hz aceleracion en X, Y y Z habilitadas
escribir_dato(acce_W ,REG2_A,0);
escribir_dato(acce_W ,0x2E,0b01000000); //FiFo
//escribir_dato(acce_W ,REG7_A,0b10000000);
//escribir_dato(acce_W ,REG3_A,0);
//escribir_dato(acce_W ,REG4_A,0);
//escribir_dato(acce_W ,REG5_A,0);
//escribir_dato(acce_W ,REG6_A,0);
//escribir_dato(acce_W ,REG7_A,0);
}
int8 read_acc(int8 reg){
int8 dato1;
int St_A;
i2c_start();
i2c_write(acce_W);
i2c_write(reg);
i2c_start();
i2c_write(acce_R);
St_A=i2c_read(0);
i2c_stop();
dato1=St_A;
//dato1=dato1<<8;
return(dato1); //dato leido del acelerometro
} |
I receive the data once, but I can't read another until I reset the system.
I use your code, I can receive something.
Sorry my english is not good.
Thanks in advance. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 11, 2015 8:26 pm |
|
|
Quote: | while (TRUE)
{
if(usb_enumerated()) //Si el PicUSB está configurado
{
if (usb_kbhit(1)) //Si el endpoint de salida contiene datos del host
{
LED_ON(LEDP); //Apagamos led Verde
delay_ms(500);
DATAXL=read_acc(OUT_X_L_A);
DATAXH=read_acc(OUT_X_H_A);
usb_put_packet(1,envia,3,USB_DTS_TOGGLE); //enviamos el paquete de tamaño 1byte del EP1 al PC
usb_get_packet(1, recibe, 2); //Cachamos el paquete de tamaño 2bytes del EP1 y almacenamos en recibe
LED_OFF(LEDP); //Apagamos led Verde
delay_ms(500);
}
}
}//While
}//main
|
Did you look at the USB examples from CCS ? Here is what they say:
c:\program files\picc\examples\ex_usb_hid.c
Quote: |
//// usb_task() must be called periodically ////
//// in your main loop. If it is not called faster than once ////
//// per millisecond, USB may not work (PIC18 and PIC24 only). //// |
c:\program files\picc\examples\ex_usb_kbmouse.c
Quote: |
//// usb_task() must be called periodically ////
//// in your main loop. If it is not called faster than once ////
//// per millisecond, USB may not work (PIC18 and PIC24 only). //// |
c:\program files\picc\examples\ex_usb_serial.c
Quote: | //// usb_task() must be called periodically ////
//// in your main loop. If it is not called faster than once ////
//// per millisecond, USB may not work (PIC18 and PIC24 only). //// |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue May 12, 2015 6:32 am |
|
|
simple hardware question...
Is the PIC running at 5 volts and the sensor at 3 volts? Nowadays most(all) external peripherals seem to be '3 volt' devices so you have to be careful to properly interface them to a '5 volt' PIC.
jay |
|
|
andy09
Joined: 20 Jun 2013 Posts: 2
|
|
Posted: Wed May 13, 2015 5:49 pm |
|
|
I did the program and it worked fine, but the sensor did not sent new information when the acceleration change, so I need your help to understand what register I need to let this happen.
I got the sensor from pololu https://www.pololu.com/picture/view/0J4943 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|