Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

SPI interfacing problem in LPC2148

Status
Not open for further replies.

vikas243

Newbie level 1
Newbie level 1
Joined
Oct 25, 2013
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
6
i am trying to read interface L3G4200D gyroscope with Lpc2148. As i am new to THIS field, i am not understanding what is the problem with my code. please can somebody assist me in this. i am posting code..


Code:
#include <lpc213x.h>
#include <stdio.h>
#define rs (1<<0)					
#define e (1<<1)
#define drdy (1<<7)
#define cs (1<<16)


void delay(unsigned long int h)
{
unsigned long int i,j;
 for(j=0;j<h;j++)
  for(i=0;i<5000;i++);
}

void cmd_lcd(unsigned long int dat)	// funtion to latch command to lcd
{
IOCLR0 = 0X0000ff03;
IOSET0 = dat << 8;
IOCLR0 |= rs;
IOSET0 |= e;
delay(20);
IOCLR0 |= e;
}

void data_lcd(unsigned long int dat)	// function to latch data to lcd
{
IOCLR0 = 0X0000ff03;
IOSET0 = dat << 8;
IOSET0 |= rs;
IOSET0|= e;
delay(20);
IOCLR0|= e;
}


void string_lcd(char *key)			// function to display string on lcd  
{
	
while(*key != '\0')
	{
	 data_lcd(*key++); 
	}
}

void init_lcd()								// fuction to initialize the lcd
{
cmd_lcd(0x38);   	// For specifying the two rows of the LCD
cmd_lcd(0x01);  	//Clear screen
cmd_lcd(0x06);  	//Incrementing display characters from left to right
cmd_lcd(0x0c);  	//making cursor invisible
cmd_lcd(0x80);  	//Setting the initial cursor position 
}		
		
		
		
		
		
		
void spi_init()
{
PINSEL0= ((PINSEL0 & ~(3<<8)) | (1<<8)); // active sck0 line
PINSEL0= ((PINSEL0 & ~(3<<10)) | (1<<10));// active miso0
PINSEL0= ((PINSEL0 & ~(3<<12)) | (1<<12));// active mosi0
	
S0SPCR|=(1<<5); 
S0SPCCR=0x80;
}



void spi_send(int data, int address)
{
	int dummy;
        int value;
        value=address;
		
        S0SPDR = value;
  while (!(S0SPSR & 0x80));         //checks for data flag to go off
	dummy=S0SPSR;
	value=data;
	S0SPDR = value;
  while (!(S0SPSR & 0x80));
	dummy=S0SPSR;	
	
}



int spi_read(int address )
{
static	int data;
	int dummy;
    int value;
        value=address | 0x80;
	
        S0SPDR = value; 
  while (!(S0SPSR & 0x80));         //checks for data flag to go off
	dummy=S0SPSR;
	if((IOPIN0 & drdy)==1)
	{data= S0SPDR;
	return data;}
	else 
	{return 0;}
}



int main()
{
	int x1=0;
	char sout[8]="";
;	
IODIR0=(1<<0)| (1<<1)|(1<<8) | (1<<9) | (1<<10) | (1<<11)|(1<<12) | (1<<13) | (1<<14) | (1<<15);

	IOCLR0 = 0X0000ff03;
	IOCLR0=0xffffffff;
	IOCLR1=0xffffffff;
	IOSET1|= cs;
init_lcd();	

	spi_init();
	delay(100);
 string_lcd("SPI DATA");
	cmd_lcd(0xc0);
	IOCLR1|= cs;
	spi_send(0x0f,0x20);
	spi_send(0x08,0x22);
	delay(200);
	
	 
while(1)
	{
		delay(1000);
		cmd_lcd(0x01);
x1=spi_read(0x0f);	

sprintf(sout,"%x",x1);
string_lcd(sout);
}



}
 

From what I understood from reading the code that you have posted,what the code is doing,is that it is sending and receiving data from the gyroscope through SPI communication protocol,and you are displaying the sensor data on an LCD.
Would you give some more details on the problem,like what part of the system is not working correctly?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top