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 support@ccsinfo.com

Accelerometer + 18F46J50 not 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

Accelerometer + 18F46J50 not work
PostPosted: Tue Aug 23, 2011 1:09 pm     Reply with quote

I'm newbie for CCS. This code compile pass but not output in oscilloscope. Code what's wrong. Can you help me?
Code:

#include <18F46J50.h>
#fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock=20000000)

//************************************************************MMA7455 reg define   
#define XOUT8         0x06     
#define YOUT8         0x07   
#define ZOUT8         0x08   
#define MCTL          0x16     //4g, measurement mode,SPI 4wire  =0x09

//***********************************MMA7455_SPI PIN define
#define MMA7455_CS   PIN_A5   //0
#define MMA7455_CLK  PIN_B4   //0
#define MMA7455_SDI  PIN_B5   //0
#define MMA7455_SDO  PIN_C7   //1
#define MMA7455_INT1 PIN_B0   //1 DRDY

//**************************SPI_write data to MMA7455
void spi_write(char addr)
{
char a;
  for(a=0;a<8;a++)                  // loop 8 bit
  {
   if((addr & 0x80)== 0x80)         //check data bit 8 w=1 r=0
      output_high(MMA7455_SDO);    //Sent 1 to SDO
   else
      output_low(MMA7455_SDO);     //Sent 0 to SDO
      output_high(MMA7455_CLK);
      output_low(MMA7455_CLK);
      addr<<=1;                     //Shift data Next Bit
  }
}

//******************************write_Acc value for set up MMA7455
void write_Acc(char reg,char dat)   //reg(reg for write),dat(set up Acc)
{
   output_low(MMA7455_CS);         //from timing
   spi_write(((reg&0x3F)<<1)|0x80); //write address data for write to SPI(th guide)
   spi_write(dat);                  //Write data to SPI
   output_high(MMA7455_CS);
}

//********************************************SPI_read
char spi_read(char reg)
{
  char a,result=0;
  for(a=0;a<8;a++)                 //write reg for read(SDO)
  {
   if((reg & 0x80)== 0x80)         //check data bit 8 w=1 r=0
      output_high(MMA7455_SDO);
   else
      output_low(MMA7455_SDO);
      output_high(MMA7455_CLK);
      output_low(MMA7455_CLK);
      reg <<= 1;
  }     
  for(a=0;a<8;a++)        //read data(SDI)
  {
    output_high(MMA7455_CLK);
    result <<= 1;
   
    if((MMA7455_SDI && 0x08)== 0x08)    //If SDI = "1"
     {
      result |= 0x01;                   //Keep Result of data-Read
      output_low(MMA7455_CLK);
     }
  }
 
return(result);                  //Return data
}

//******************************read_Acc data
signed char read_Acc(char reg)
{
   signed char dat;
   output_low(MMA7455_CS);
   dat=spi_read(((reg&0x3F)<<1));      //Write address command for read to SPI Accelerate
   output_high(MMA7455_CS);
   
   return(dat);
}

//************************************Check status ready
void data_ready()
{
  while((MMA7455_INT1&&0x01) != 0x01){;}  //Check Data Ready exit loop
}

void Acc_init(void)
{
   set_tris_b(0xFF);
   set_tris_c(0x20);
   
   setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_XMIT_L_TO_H|SPI_CLK_DIV_16);
   
   output_high(MMA7455_CS);
   output_high(MMA7455_SDO);
   output_low(MMA7455_CLK);
   delay_ms(100);
   
   write_Acc(MCTL,0x09);         //4g,meas,4wire
   
}

//*************************main********************
void main()
{
   int Xdata,Ydata,Zdata;
   
   Acc_init();
   delay_ms(100);
   
   while(TRUE)
   {
   data_ready();
   
   Xdata = read_Acc(XOUT8);
   Ydata = read_Acc(YOUT8);
   Zdata = read_Acc(ZOUT8);

   delay_ms(100);
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 23, 2011 1:28 pm     Reply with quote

Look at the .LST file. The compiler is ignoring your spi_write() and
spi_read() functions because you gave them the same names as the
built-in CCS functions. If you want it to use your functions, change
the names slightly. You could call it my_spi_write(), or spi_wrt() and
spi_rd().

Also, if you want to use your own software spi functions, then don't
call the setup_spi() function.

Also, you don't need to set the TRIS. The compiler will do it for you.
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