View previous topic :: View next topic |
Author |
Message |
Classic
Joined: 22 Aug 2011 Posts: 6 Location: Thailand
|
code accelerometer doesn't work |
Posted: Tue Sep 06, 2011 10:29 am |
|
|
I connected this MMA7455L with PIC18F2620 via SPI interface. I am having a problem reading data from the accelerometer. I think it can write to register MMA7455 but can't read. Thank for all comment.
My code
Code: | #include <18F2620.h>
#fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock=20000000)
#use fast_io(c)
//**********************************************************
#define XOUT8 0x06
#define YOUT8 0x07
#define ZOUT8 0x08
#define MCTL 0x16
//***PIC*********************MMA7455************
//CS PIN_A3 connect CS
//CLK PIN_C3 connect CLK
//MOSI PIN_C5 connect SDI
//MISO PIN_C4 connect SDO
//************************************
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
//********************************************
void spi_wr(char val);
void wr_Acc(char reg,char dat) ;
char spi_rd(char reg);
signed char rd_Acc(char reg);
void Acc_init(void);
//******************************************************
void spi_wr(char val)
{
char a;
for(a=0;a<8;a++)
{
if((val & 0x80)== 0x80)
output_high(PIN_C5);
else
output_low(PIN_C5);
output_high(PIN_C3);
output_low(PIN_C3);
val <<= 1;
}
}
//********************************************
void wr_Acc(char reg,char dat)
{
output_low(PIN_A3);
spi_wr(((reg & 0x3F) <<1)| 0x80);
spi_wr(dat);
output_high(PIN_A3);
}
//********************************************
char spi_rd(char reg)
{
char a,result=0;
for(a=0;a<8;a++)
{
if((reg & 0x80)== 0x80)
output_high(PIN_C5);
else
output_low(PIN_C5);
output_high(PIN_C3);
output_low(PIN_C3);
reg <<= 1;
}
for(a=0;a<8;a++)
{
output_high(PIN_C3);
result <<= 1;
if((input(PIN_C4) & 0x08)== 0x08) // If SDI = "1" or 0
result |= 0x01; //Keep Result of Read-data
output_low(PIN_C3);
}
return result;
}
//*******************************************************
signed char rd_Acc(char reg)
{
signed char dat;
output_low(PIN_C3);
dat = spi_rd(((reg & 0x3F) << 1));
output_high(PIN_C3);
return dat;
}
//**************************************************
void Acc_init(void)
{
set_tris_a(0x00);
set_tris_b(0x0F);
set_tris_c(0x10);
delay_ms(100);
output_high(PIN_A3);
output_high(PIN_C5);
output_low(PIN_C3);
}
//********************************************
void main()
{
signed char Xdata, Ydata, Zdata;
Acc_init();
setup_spi(SPI_MASTER|SPI_MODE_0|SPI_CLK_DIV_16);
output_high(PIN_A3);
output_high(PIN_C5);
output_low(PIN_C3);
delay_ms(100);
Wr_Acc(MCTL,0x09);
delay_ms(100);
while(TRUE)
{
Xdata = Rd_Acc(XOUT8);
Ydata = Rd_Acc(YOUT8);
Zdata = Rd_Acc(ZOUT8);
delay_ms(500);
}
} |
My Graph
[img][/img]
[img][/img]
and I learn at
http://www.ccsinfo.com/forum/viewtopic.php?t=37562 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 06, 2011 12:29 pm |
|
|
Post a link to the website where you bought the MMA7455L board. |
|
|
Classic
Joined: 22 Aug 2011 Posts: 6 Location: Thailand
|
|
Posted: Tue Sep 06, 2011 12:57 pm |
|
|
thank you very much |
|
|
|