A discussion group for the PICMicro microcontroller. Also called the Microchip PIC, this list is dedicated to the use and abuse of this fine, simple, microcontroller. Close to topic posts are welcome, ie. general electronics.
Dear frens,
I am using digital accelerometer with PIC18f452 to read data of
acceleration through spi interface.
Here I am getting problem in writing conversion command for three axis
accelerations into digital output.
I am new to embedded but I have written a C program by referring from
datasheet of accelerometer.
I am writing the code here so any one tell me where I went wrong and
help me please.
[code]
#include<18f452.h>
#include
#use delay (clock=6000000)
#use rs232 ( baud= 9600, xmit=PIN_C6, rcv= PIN_C7 )
#include
//#device PIC18F452
//#include
//#define spi_master 0x20
//#define spi_slave 0x24
//#include
#define FF_int_w 0x06
#define FF_delay_w 0x07
#define MOT_int_w 0x08
#define MOT_delay_w 0x09
#define FF_int_r 0x86
#define FF_delay_r 0x87
#define MOT_int_r 0x88
#define MOT_delay_r 0x89
#define XOUT_H 0x80
#define XOUT_L 0x81
#define YOUT_H 0x82
#define YOUT_L 0x83
#define ZOUT_H 0x84
#define ZOUT_L 0x85
#define CTRL_REGA 0x8c
#define CTRL_REGC 0x0a
#define CTRL_REGB 0x0b
//#define spi_master 0x20
//#define spi_master 0x20
//INCLUDE
// #byte PORTC=0b10010xxx
//#byte TRISC=ob11111111
#byte TRISC=0b10010xxx
//TRISC0=1
//TRISC1=0
//TRISC2=0
//TRISC3=1
//TRISc4=0
#byte SSPSTAT=0b1100000x
#byte SSPCON1=0b00110001
#byte TXSTA=0b1011-010
#byte RCSTA=0b1011-11x
#byte SPBRG=0b00001001
//TRISB6=1
//TRISB7=1
//void enableinterrupts(void)
void main()
{
unsigned int a, b, XOUT, YOUT, ZOUT;
unsigned int spi_write, spi_read;
//setup_SSP(SPI_MASTER ,SPI_SLAVE )
setup_spi(spi_master, spi_slave)
//setupspi_master(0x20)
//setupspi_slave(0x24)
void spi_io(void);
{
CS=0 ; // chip slect low to write to registers
// a=0x8b
spi_write(0x0b); // intiates write to control regb
spi_write(0x86); // write operational mode byte to
control regb
// spi_write(0x0b && 0x86)
CS=1;
delay_us(130000);
CS=0;
spi_read(0x8b);
a=spi_read(0x86); // intiates the read from control reg b
CS=1;
delay_us(130000);
CS=0;
// b=0x8a
spi_write(0x0a); // intiates write to control regc
spi_write(0x00); // write operational mode byte to
control regc
// spi_write(0x0a && 0x00)
CS=1;
delay_us(130000);
CS=0;
spi_read(0x8a);
b=spi_read(0x00); //intiates the read from control reg c
CS=1;
delay_us(130000); //chip select high before the next data
request
CS=0;
//spi_write(0x06 && 0x14)
spi_write(FF_int_r);
spi_write(0x14);
delay_us(130000);
// spi_write(0x07 && 0x14)
spi_write(FF_delay_r);
spi_write(0x14);
delay_us(130000);
// spi_write(0x08 && 0x4d)
spi_write(MOT_int_r);
spi_write(0x4d);
delay_us(130000);
// spi_write(0x09 && 0x14)
spi_write(MOT_delay_r);
spi_write(0x14);
delay_us(130000);
// Axis conversion command
CS=0
XOUT=spi_read(0x80); // read
first 8 bits
SCLK=0;
delay_us(200);
XOUT=XOUT_H<<8; // shift
the first 8 bits to the left
XOUT!=spi_read(0x80); // read the
second 8 bits
XOUT= spi_read(0x80)>>4 // right
shift by 4 bits to go from 16 to 12 bits
CS=1
delay_ms(130000)
CS=0
YOUT=spi_read(0x82); // read
first 8 bits
SCLK=0;
delay_us(200);
YOUT=XOUT_H<<8; // shift
the first 8 bits to the left
YOUT!=spi_read(0x80); // read the
second 8 bits
YOUT= spi_read(0x80)>>4 // right
shift by 4 bits to go from 16 to 12 bits
CS=1
delay_ms(130000)
CS=0
ZOUT=spi_read(0x84); // read
first 8 bits
SCLK=0;
delay_us(200);
ZOUT=XOUT_H<<8; // shift
the first 8 bits to the left
ZOUT!=spi_read(0x80); // read the
second 8 bits
ZOUT= spi_read(0x80)>>4 // right
shift by 4 bits to go from 16 to 12 bits
CS=1
delay_ms(130000)
CS=0
/*spi_write(0x80);
SCLK=0;
delay_us(200);
XOUT_H=spi_read(0x80);
XOUT_L=spi_read(0x81);
CS=1;
delay_us(130000);
CS=0;
spi_write(0x82);
SCLK=0;
delay_us(200);
YOUT_H=spi_read(0x82);
YOUT_L=spi_read(0x83);
CS=1;
delay_us(130000);
CS=0;
spi_write(0x84);
SCLK=0;
delay_us(200);
ZOUT_H=spi_read(0x84);
ZOUT_L=spi_read(0x85)
CS=1;
delay_us(130000);*/
}
}
[code]
The data sheet of accelerometer is given below plz refer it if any
doubt.
http://www.kionix.com/accelerometers/accelerometer-KXP84.html

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )