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

SPI Accelerometer (Freescale MMA7455L) and 16F886
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
vascotech88



Joined: 10 Mar 2009
Posts: 33
Location: Aveiro, Portugal / Lodz, Polska

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon Jan 17, 2011 7:16 pm     Reply with quote

It works!!! It was the incorrect lines! Thank you so much!!

I will not forget again :p
_________________
-----
Vasco Jose Dias Baptista
http://vascotech88.pt.vu/
RoGuE_StreaK



Joined: 02 Feb 2010
Posts: 73

View user's profile Send private message

PostPosted: Sun Feb 20, 2011 8:08 pm     Reply with quote

PCM programmer wrote:

Code:

while(1)
  { 
   Xdata = Acc_Read_Register(0x06); //Read X,Y,Z outputs from Sensor
   Ydata = Acc_Read_Register(0x07);
   Zdata = Acc_Read_Register(0x08);
   delay_ms(500);
   printf("XYZ:  %U, %U, %U \n\r", Xdata, Ydata, Zdata);
  }


Sorry to revive the thread, but figured I'd keep queries about SPI for the MMA7455L in the one place;
PCM programmer, you've got this routine outputting every 0.5 seconds; any idea how far you can reduce this, especially if the RS232 speed is increase to say 57600?
I'd like to get as many samples per second that I can, but at a rate that is capable of being output via RS232; so I can study some example figures before tweaking threshholds for various functions. Two per second is a bit lower than I would hope for, I'm aiming for maybe 10?

These SPI/I2C chips are getting so damn cheap there doesn't seem to be any point using analogue-out ones?

Thanks
Lee
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 20, 2011 11:05 pm     Reply with quote

The 500 ms loop delay is just to limit the display's update rate so it can
be read without it scrolling off the screen too quickly.

To answer your question, download the data sheet:
http://www.freescale.com/files/sensors/doc/data_sheet/MMA7455L.pdf
Look in this section on page 9:
Quote:

Measurement Mode

It tells you the number of samples per second that the chip can do for
the default setting of 62.5 BW. (The code posted earlier uses this setting). Look in that section and find it.

For the serial output speed, with a baud rate of 57600, you can send
5760 characters per second.
tong_tong_68



Joined: 30 May 2011
Posts: 4

View user's profile Send private message

Can anybody tell me about MMA7455L with PIC18F25K20
PostPosted: Mon May 30, 2011 12:43 pm     Reply with quote

i'm new student, and many thank with your forum that useful to me very, i take code from this forum and little adopt as below code


I connected this MMA7455L with PIC18F25K20, but does't work.
here is my code,
and i need to take xdata read from MMA7455L to view on PC throug RS 232......... thank

Code:

#pragma config FOSC = INTIO67    // Set intenal clock 1MHz
#pragma config PWRT = ON       //Power up timer is ON
#pragma config WDTEN = OFF       // Watchdog timer is OFF
#pragma config LVP = ON       // Low Voltage Programming ON
#pragma config BOREN = OFF       //Brown out reset is OFF
#pragma config DEBUG = ON       //Debug is ON                      // CONFIG1H
/** I N C L U D E S **************************************************/
#include "p18f25k20.h"
#include "delays.h"
#include "REGMMA7455L.h"
/** D E C L A R A T I O N S *******************************************/

void main(void);
void initSPI(void);
char SPI_Read(char reg);
char Read_Accelerometer(char reg);
void SPI_Write(char reg);
void Write_Accelerometer(char reg, char data);

#pragma udata
   char LED_Number,data,xdata,ydata,zdata;
   int number = 5;
   int i;
#pragma code

void initial_uart(void);
void send(unsigned char data);
unsigned char recieve(void);

void main (void)
{

   initSPI();          //SPI init
   CS = 1;    //Set Chip Select High
   SDO = 1;    //Set Serial Data Out High
   SCL = 0;    //Set Serial Clock Low
   Write_Accelerometer(MCTL, 0x05);

   initial_uart();
   while (1)
   {   
   //-----------------Read and Print Value Gx,Gy,Gz 8bit  Rang 2G or 4G or 8G  -----------------

    while(DRDY != 1) {;};                       
   // Step 3.Check Status (Pin-int1) measurement data Ready (1 = data Ready)

   //while(DRDY != 1) {;};
   xdata = Read_Accelerometer(XOUT8);          // Step 4.-Read Value Gx
   ydata = Read_Accelerometer(YOUT8);          //        -Read Value Gy
   zdata = Read_Accelerometer(ZOUT8);          //        -Read Value Gz

   }
}
void initSPI(void)
{
   CST = 0;
   SCLT = 0;
   SDOT = 0;
   SDIT = 1;
   DRDYT = 1; /

   CS = 0;
   SCL = 0;
   SDI = 0;
   SDO = 0;
   DRDY = 0;

   //Delay1KTCYx(50);
}
char SPI_Read(char reg)
{
   char loop,data = 0;

   //------- Write reg. for read (SDO)----------
   for(loop=0;loop<8;loop++)      //Loop 8 bits
   {    
      if((reg & 0x80) == 0x80)   //check bit8 is 0 or 1 ?
         SDO = 1;            
      else
         SDO = 0;
      SCL = 1;
      SCL = 0;
      reg <<= 1;
   }
   //------------ Read data (SDI)---------------

   for(loop=0;loop<8;loop++)      //Loop 8 bits
   {    
      SCL = 1;
      data <<= 1;
      if(SDI)                  //check PORTC.4 is 0 or 1 ?
         data |= 0x01;            
      SCL = 0;
   }
   return data;                //return our data
}
char Read_Accelerometer(char reg)
{
   char tmp;
   CS = 0;                         //Chip Select Low
   tmp = SPI_Read(((reg & 0x3F) << 1));
   CS = 1;                         //Chip Select High
   return tmp;                      //Return tmp
}
void SPI_Write(char reg)
{
   char loop;
   //------- Write data (SDO)----------
   for(loop=0;loop<8;loop++)      //Loop 8 bits
   {    
      if((reg & 0x80) == 0x80)   //check bit8 is 0 or 1 ?
         SDO = 1;            
      else
         SDO = 0;
      SCL = 1;
      SCL = 0;
      reg <<= 1;
   }
}

void Write_Accelerometer(char reg, char data)
{
   CS = 0;                         //Chip Select Low
   SPI_Write(((reg & 0x3F)<<1)|0x80);       //Write our address
   SPI_Write(data);                   //Write our data
   CS = 1;                         //Chip Select High
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 30, 2011 1:07 pm     Reply with quote

Your program is for the Microchip C18 compiler. This forum is for the
CCS compiler. They are not compatible.

You need to ask your questions on the C18 compiler forum. Here is the link:
http://www.microchip.com/forums/tt.aspx?forumid=3
tong_tong_68



Joined: 30 May 2011
Posts: 4

View user's profile Send private message

many thank to PCM programmeconnect MMA7455L with PIC18F25K20
PostPosted: Tue May 31, 2011 1:05 am     Reply with quote

many thank for your reply, could you please show me your sample code with CCS compiler, i think will change to ccs compiler, because most of people tell ccs compiler is easy to understanding ........... so please show me your sample code for MMA7455L connect with PIC18FxxKxx

thanks,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 31, 2011 11:07 am     Reply with quote

Any code that I have is posted in the earlier pages of this thread.
wangine



Joined: 07 Jul 2009
Posts: 98
Location: Curtea de Arges, Romania

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Sat Jun 04, 2011 5:20 pm     Reply with quote

I tried to use the same accelerometer in I2C connection.
I did this right?
PIC18F4520 processor is used but we did not configure anything.
I tried to receive the serial but unsuccessful, receiving only FF.
The code is below.
Code:
void init_acc()
   {
   i2c_start();
   i2c_write(0x3A);
   i2c_write(0x16);
   i2c_write(0x49);
   i2c_start();
   i2c_write(0x3A);
   i2c_write(0x07);
   i2c_start();
   i2c_write(0x3B);
   }

   while(TRUE)
   {
//   static int i = 0b11101001;

//         medie();
//   for(i=0;i<=128;i++)
//         display_number(total);
//   for(i=0;i<=254;i++)
//   {delay_ms(10);}
   if (input_state(pin_b0) == 1)
      {
//   output_high(PIN_C0);


   i2c_start();
   x = i2c_read(0x06);
   delay_ms(100);
   y = i2c_read(0x01);
   delay_ms(100);
   z = i2c_read(0x00);

   fputc(x);
   delay_ms(100);
   fputc(y);
   delay_ms(100);
   fputc(z);
      }

//*******************************************************
   
  }


The circuit is the pic

http://www.hexhoby.ro/download/MMA%207455%20accelerometer%20I2C.JPG
Do not know where is the mistake, I tried to write and read registers in any way but without success.
Thanks in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 05, 2011 2:27 pm     Reply with quote

Your i2c protocol is not correct.

Look at Figures 7 and 9 in the MMA7455L data sheet, which show how
to do the single-byte read and single-byte write operations:
http://www.freescale.com/files/sensors/doc/data_sheet/MMA7455L.pdf
Then look at the i2c code in the post below. He's following the protocol
shown in the data sheet:
http://www.ccsinfo.com/forum/viewtopic.php?t=38728
Though I would have made the read byte and write byte code into
two routines, instead of using inline code like he has. Using routines
allows you to call them, and put them into a separate driver file which
can be #include'd into your program. Also, I would have used a while(1)
continuous loop instead of the ASM-like 'goto'.
wangine



Joined: 07 Jul 2009
Posts: 98
Location: Curtea de Arges, Romania

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Tue Jun 07, 2011 7:22 pm     Reply with quote

Thanks for the quick response.
I made a separate file for I2C driver MMA7455L.
Code:

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//#include <33FJ64GS406.h>
#use delay(clock=80000000)
#use i2c(Master,Slow,sda=PIN_G3,scl=PIN_G2, force_HW)

extern int8 x_ax, y_ax, z_ax;
   int1 flag_x, flag_y, flag_z;

void start_acc_I2C()
   {

i2c_start();
i2c_write(0x3A); //0x1D shifted in with a 0 had been 0x3A
i2c_write(0x16); //MODE CONTROL REGISTER
i2c_write(0x05); //CONTINOUS MEASUREMENT
i2c_stop();
delay_ms(30);
   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int8 acc_i2c_read(int8 reg)
   {
int8 data;
   delay_ms(1);
i2c_start();
i2c_write(0x3A);
i2c_write(reg); //8 bits output value Z
i2c_start();
i2c_write(0x3B); //0x1D shifted in with a 1 had been 0x3B
data=i2c_read(0);
i2c_stop();
   delay_ms(1);

return(data);
   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   
// from interrupts
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void X_axis()
   {
   if(flag_x == false)
      {
      i2c_start();
      i2c_write(0x3A);
      i2c_write(0x06); 
      i2c_start();
      i2c_write(0x3B);
      x_ax = i2c_read(0);
      i2c_stop();
   
   flag_x = true;
      }   
   }
//===============================
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Y_axis()
   {
   if(flag_y == false)
      {
      i2c_start();
      i2c_write(0x3A);
      i2c_write(0x07); 
      i2c_start();
      i2c_write(0x3B);
      y_ax = i2c_read(0);
      i2c_stop();
   
   flag_y = true;
      }
   }
//===============================
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Z_axis()
   {
   if(flag_z == false)
      {
      i2c_start();
      i2c_write(0x3A);
      i2c_write(0x08); 
      i2c_start();
      i2c_write(0x3B);
      z_ax = i2c_read(0);
      i2c_stop();
   
   flag_z = true;
      }
   }
//===============================

Datasheet that I have it was incomplete.
I have some noise on it, but I use a digital filter to remove it
Code:
XYZ:  -2, -19, 75

XYZ:  -2, -17, 76

XYZ:  -3, -19, 76

XYZ:  -1, -18, 76

XYZ:  -1, -19, 76

XYZ:  -2, -19, 76

XYZ:  -2, -17, 77

XYZ:  -3, -19, 76

XYZ:  -3, -17, 76

XYZ:  -3, -18, 77

XYZ:  -2, -17, 76

Full Support for dsPIC33Fxx when you think it will be?
I still bother to write to SFR addresses in the header register but do not know how accurate it is.
We have not really tested whether it is correct what I wrote.
Syntax error is not, but do not know if that address is correct.
I tried to insert object files from DSP library but without success. I miss something.
Code:

//extern volatile unsigned int  UART1("sfr:U1MODE");

 struct tagU1MODEBITS {
  union {
    struct {
      unsigned STSEL:1;
      unsigned PDSEL:2;
      unsigned BRGH:1;
      unsigned URXINV:1;
      unsigned ABAUD:1;
      unsigned LPBACK:1;
      unsigned WAKE:1;
      unsigned UEN:2;
      unsigned :1;
      unsigned RTSMD:1;
      unsigned IREN:1;
      unsigned USIDL:1;
      unsigned :1;
      unsigned UARTEN:1;
    };
    struct {
      unsigned :1;
      unsigned PDSEL0:1;
      unsigned PDSEL1:1;
      unsigned :5;
      unsigned UEN0:1;
      unsigned UEN1:1;
    };
  };
} U1MODEBITS;
//#define U1MODE(((*U1MODEBITS)0x0220)
#byte U1MODEBITS = getenv("sfr:U1MODE")

The reason I chose CCS compiler was first forum support, and help very well placed.
Now a year for the C30 compiler got an answer of 50 because of compiler bugs, neither of them unresolved, once compiled code went another time the same code did not work, nothing changed.
Thanks again


Last edited by wangine on Tue Jun 07, 2011 7:56 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 07, 2011 7:52 pm     Reply with quote

I don't have the PCD compiler so I can't help with compiler bugs.
Quote:

#use i2c(Master,Slow,sda=PIN_G3,scl=PIN_G2, force_HW)

If you have doubts about the i2c working, then don't use FORCE_HW.
Let the compiler do software i2c. I think it's more likely to work.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
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