CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

code accelerometer doesn't work

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Classic



Joined: 22 Aug 2011
Posts: 6
Location: Thailand

View user's profile Send private message

code accelerometer doesn't work
PostPosted: Tue Sep 06, 2011 10:29 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 06, 2011 12:29 pm     Reply with quote

Post a link to the website where you bought the MMA7455L board.
Classic



Joined: 22 Aug 2011
Posts: 6
Location: Thailand

View user's profile Send private message

PostPosted: Tue Sep 06, 2011 12:57 pm     Reply with quote

Shocked thank you very much
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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