Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to send conversion axis command to acclerometer thru SPI interface to PIC18f452

Status
Not open for further replies.

anilvadnala

New Member
Dear Frens,

I Anil Vadnala doing work on PIC18f452 first time as all say

I need to interface digital tri axis accelerometer to PIC18f452 through SPI
interface.

I wrote a C program what I understand from data sheet of accelerometer given in 13th page onwards.

**broken link removed**

Code:
#include<18f452.h>
#use delay (clock=6000000)
#use rs232 ( baud= 9600, xmit=PIN_C6, rcv= PIN_C7 )
#include<input.c>
#include<spi.c>
//#include<ACCL.h>
VOID main()
{
unsigned int a,b,FF_int=0x06, FF_delay=0x07, MOT_int=0x08, MOT_delay=0x09
                 XOUT_H, XOUT_L, YOUT_H, YOUT_L, ZOUT_H, ZOUT_L;
unsigned char spi_write, spi_read;
                          //spi_write(0x06 && 0x14)
spi_write(FF_int);
spi_write(0x14);
delay_us(130000);
                           //spi_write(0x07 && 0x14)
spi_write(FF_delay);
spi_write(0x14);
delay_us(130000);
                           //spi_write(0x08 && 0x4d)
spi_write(MOT_int);
spi_write(0x4d);
delay_us(130000);
                           //spi_write(0x09 && 0x14)
spi_write(MOT_delay);
spi_write(0x14);
delay_us(130000);
                           //void spi_io(void);
{
setup_spi(spi_master, spi_slave)

CS=0                           //chip slect low to write to registers
                               //a=0x8b
spi_write(0x0b);               // intiates write to control regb
spi_write(0x06);               // write operational mode byte to control regb
                               //spi_write(0x0b && 0x06)
   CS=1;
   delay_us(130000);
   CS=0;
spi_read(0x8b);
a=spi_read(0x06);                  //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;

                              //  conversion command
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/]


Pleas frens this is my first C program for PIC using SPI so give suggesions where I went wrong.

Thanks in advance
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top