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.

Problem with ARRAY pls help

Status
Not open for further replies.

pawan kumar

Member level 4
Joined
Jan 31, 2012
Messages
73
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Location
Chennai
Activity points
1,894
I have a problem with arrays.I am interfacing LPC2148 with GPS module using both uarts.

I have an array of 70 elements named info[70]. I want another array, lat[9] to store only the first nine elements (as of now) and send them to the UART0.

the logic I ve used is as follows:

void latitude()
{
int i;
i=0;
for(i=0;i<9;i++) //this gets executed 9 times
lat=info;
}

but there is a strange issue : on printing the lat[] in hyperterminal, info[] is also printed along.pls see attached image

I have attached the code. please look through.
Code:
#include<lpc214x.h>
static unsigned char lat[9];
static unsigned char info[70];
void init()				//initializes  both UARTS @9600,8bit, no pairity,1 stopbit
{
U0LCR=0x83;
U0DLL=0x61;
U0DLM=0x00;
U0LCR=0x03;
U1LCR=0x83;
U1DLL=0x61;
U1DLM=0x00;
U1LCR=0x03;
}

void senduart0(unsigned char a)		  //sends a byte through UART0
{
U0THR=a;
while(U0LSR!=0x60);
}
unsigned char recuart1()			 //recieves a byte from UART1
{
unsigned char p;
while ((U1LSR&0x01)!=1);
p=U1RBR;
return p;
}
void  sendstring(const unsigned char *str)	 //A function to send a string on UART0
{  
   while(1)
   {  
      if( *str == '\0' ) break;
      senduart0(*str++);
	  }
	  }
void recgps()						 //Receive the 70 chrs to fill up info[]
{
int count=0;
for(count=0;count<70;count++)
{
info[count]=recuart1();
}
} 
void latitude()		 //to extract the latitude coordinates 
{
int i;
i=0;
for(i=0;i<9;i++)
lat[i]=info[i];
 }
 int main()
{
PINSEL0=0x00050005;
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000000;
init();
recgps();
latitude();
sendstring("latitude\n\r");
sendstring(lat);
sendstring("testing\r\n");
	
}


I am also attaching a screenshot of hyperterminal.


Please tell me where I am wrong.

Pawan
 

Attachments

  • err.png
    err.png
    160.5 KB · Views: 121
Last edited:

One issue with your code is the following statement in your code:

Code:
sendstring(lat);

The routines like the sendstring() usually expect a NULL terminated character string, however lat[] is declared as follows:

Code:
static unsigned char lat[9];

Which can contain nine elements, however the NULL termination would require ten elements, nine characters plus the NULL.

Therefore when the following statement is executed:

Code:
sendstring(lat);

The sendstring() routine, passes all nine elements of the array and when not coming upon a NULL, continues into the following memory storage which happens to be the following:

Code:
static unsigned char info[70];

Which is why the contents of info[] are transmitted as well.

You need to modify the array declarations as follows:

Code:
static unsigned char lat[10];
static unsigned char info[71];

And store a NULL character at the end of each array. There are several ways to accomplish this requirement, one method is to simply initialize the last element of each array:

Code:
lat[9] = '\0';
info[70] = '\0';

Or if the #define exists:

Code:
lat[9] = NULL;
info[70] = NULL;

You could also store the NULL character after each "for" loop used to store the elements.

BigDog
 
Hi Pawan,

How is your GPS project progressing?

I stumbled back on this thread and after reviewing it, if you have not already done so, I would suggest using the Standard C routine strtok() for effectively parsing your GPS sentences. With the correctly selected list of delimiters, strtok() is ideal for tokenizing/parsing various text streams and is used quite often in the construction of compilers.

Code:
char * strtok ( char * str, const char * delimiters );

Split string into tokens

A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.

On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.

To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token.

This end of the token is automatically replaced by a null-character by the function, and the beginning of the token is returned by the function.

Once the terminating null character of str has been found in a call to strtok, all subsequent calls to this function with a null pointer as the first argument return a null pointer.

Parameters:

str
C string to truncate. The contents of this string are modified and broken into smaller strings (tokens).
Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.

delimiters
C string containing the delimiters.
These may vary from one call to another.

Example of strtok(), tokenizing and parsing:
Code:
#include <string.h>
#include <stdio.h>

FILE *f;
int main()
{
char string[1000], seps[] = " \n,( )";
char *p;

f=fopen("Test.dat","r");
if(!f)
return 1;

while(fgets( string, sizeof(string)-1, f) != NULL)
{

/* Break into tokens. */
p = string;
p = strtok( string, seps ); /* Find first token*/

while( p != NULL )
{
printf("Token: %s\n", p);
p = strtok( NULL, seps ); /* Find next token*/
}
}
return 0;
}


Actual File Input:
PROGRAM STATS
VAR
SUM, SUMSQ, I, VALUE, MEAN, VARIANCE : INTEGER
BEGIN
SUM := 0;
SUMSQ := 0;
FOR i := 1 TO 100 DO
BEGIN
READ (VALUE);
SUM := SUM + VALUE;
SUMSQ := SUMSQ + VALUE * VALUE
END;
MEAN := SUM DIV 100
VARIANCE := SUMSQ DIV 100 - MEAN * MEAN;
WRITE (MEAN, VARIANCE)

Output:
Token: PROGRAM
Token: STATS
Token: VAR
Token: SUM
Token: SUMSQ
Token: I
Token: VALUE
Token: MEAN
Token: VARIANCE
Token: :
Token: INTEGER
Token: BEGIN
Token: SUM
Token: :=
Token: 0;
Token: SUMSQ
Token: :=
Token: 0;
Token: FOR
Token: i
Token: :=
Token: 1
Token: TO
Token: 100
Token: DO
Token: BEGIN
Token: READ
Token: VALUE
Token: ;
Token: SUM
Token: :=
Token: SUM
Token: +
Token: VALUE;
Token: SUMSQ
Token: :=
Token: SUMSQ
Token: +
Token: VALUE
Token: *
Token: VALUE
Token: END;
Token: MEAN
Token: :=
Token: SUM
Token: DIV
Token: 100
Token: VARIANCE
Token: :=
Token: SUMSQ
Token: DIV
Token: 100
Token: -
Token: MEAN
Token: *
Token: MEAN;
Token: WRITE
Token: MEAN
Token: VARIANCE

BigDog
 

Hello BigDogGuru,

GPS interface with the controller is absolutely fine. Although I understand this method of parsing is real programming, I have used the array method as of now. I am working with GSM modem interface.

in brief,the project is: Accelerometer+GPS+GSM modem interface with LPC2148 microcontroller.

I am yet to purchase an accelerometer. I ve completed the GPS as you know, and am about to finish the GSM modem interface.

Finally, on the other side, I will be integrating VB with google map to display the location.

I must thank 2012 for being a leap year. All this is to be completed within 20th March.

Anyways, thanks for your support and keep helping me in every stage.

Regards,

Pawan
 

Hi pawan

I am using LPC2148 development board and LSM303DLHC Ecompass. I have completed the following steps.

Step 1 : Connecting the Ecompass with LPC2148
Step 2: Build the program using Keil and downloaded the generated HEX file in FLASH and able to read the XYZ values in the LPC2148 LCD display.
The program used is as given below:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/********************************************************************************/ 
#include <lpc214x.h>           //Includes LPC2148 register definitions
#include <stdlib.h>
#include "LCD.h"
#include "i2c.h"
 
 
/********************Common Definitions***************************************/
#define MAX_BUFFER_SIZE     16
 
#define LED1_ON() IO1SET=(1<<16)        
#define LED2_ON() IO1SET=(1<<17)        //I2C read indicator
#define LED3_ON() IO1SET=(1<<18)        //Comm failure indicator
#define LED4_ON() IO1SET=(1<<19)        
 
#define LED1_OFF() IO1CLR=(1<<16)   
#define LED2_OFF() IO1CLR=(1<<17)
#define LED3_OFF() IO1CLR=(1<<18)
#define LED4_OFF() IO1CLR=(1<<19)
/******************************************************************************/
 
 
/*************************Function Prototypes**********************************/
void Delay_Ticks(unsigned int j);  //Function to generate delay in msec
void __irq I2C0_Status(void);
unsigned char GetXYZAcc(void);
unsigned char GetXYZMag(void);
/******************************************************************************/
 
 
/***********************Global Variables***************************************/
unsigned char I2C_WR_Buf[MAX_BUFFER_SIZE];
unsigned char I2C_RD_Buf[MAX_BUFFER_SIZE];
unsigned char Status=0;
unsigned char Status_Flag=0;
unsigned char I2C_Data=0;
 
signed short int X_Acc,Y_Acc,Z_Acc=0;
signed short int X_Mag,Y_Mag,Z_Mag=0;
float Displacement;
/*****************************************************************************/
 
 
/*****************************************************************************/
void Delay_Ticks(unsigned int j)  //Function to generate delay in msec
{  
   unsigned int  i;
   for(;j>0;j--)
   {
    for(i=0; i<10000; i++);
   } 
}
/*****************************************************************************/
 
 
void InitializeBoard(void)
{
  PINSEL0 = 0x00000000;     // Enable GPIO on all pins
  PINSEL1 = 0x00000000;
  PINSEL2 = 0x00000000;
  IO0DIR = 0x007F0000;      // Set P0.16, P0.17, P0.18, P0.19, P0.20, P0.21, P0.22 as Output, P0.12, P0.13,P0.15 & P0.30 as input
  IO1DIR = (1<<19) | (1<<18) | (1<<17) | (1<<16);       // Set P1.16, P1.17, P1.18, P1.19 as Output
  LED1_OFF();LED2_OFF();LED3_OFF();LED4_OFF();
  I2C_Init();
  Delay_Ticks(100);         //100 mSec Power ON delay for LCD
  LCD_Init();
  LCD_Command(0x01);
  Delay_Ticks(15);
}
 
/*
     This function get Accelerometer XYZ parameters through I2C bus
*/
unsigned char GetXYZAcc(void)
{
  unsigned char Temp=0;
  LED2_ON();    //Read indicator
  if(Read_Multi_Byte(DEVICE_ADDR_ACC,0x80 | STATUS_REG_A))
  {
   LED3_ON();
   while(1);
  }
  else
  {  
   if((I2C_RD_Buf[0] & 0x08)==0x08)
   {
    X_Acc = I2C_RD_Buf[1];
    X_Acc|= (signed short int)I2C_RD_Buf[2] << 8;
          
    Y_Acc = I2C_RD_Buf[3];
    Y_Acc|= (signed short int)I2C_RD_Buf[4] << 8;
        
    Z_Acc = I2C_RD_Buf[5];
    Z_Acc|= (signed short int)I2C_RD_Buf[6] << 8;
     
    I2C_RD_Buf[0] = 0x00;
    Temp = 1;
   }
   else
   {
    Temp = 0;
   }
  }
  LED2_OFF();
  return (Temp);
}
 
/*
     This function get Magnetometer XYZ parameters through I2C bus
*/
unsigned char GetXYZMag(void)
{
  unsigned char Temp=0;
  LED2_ON();    //Read indicator
 
  if(Read_I2C_Byte(DEVICE_ADDR_MAG,SR_REG_MG))  //Check if new data is available
  {
   LED3_ON();
   while(1);
  }
  else
  {
   if((I2C_Data & 0x01)==0x01)    //IF new data is available
   {
    if(Read_Multi_Byte(DEVICE_ADDR_MAG,MR_REG_M))    //Read new data
    {
     LED3_ON();
     while(1);          //if read fails loop here
    }
    else                //Buffer new data
    {  
     X_Mag = I2C_RD_Buf[2];
     X_Mag|= (signed short int)I2C_RD_Buf[1] << 8;
      
     Y_Mag = I2C_RD_Buf[4];
     Y_Mag|= (signed short int)I2C_RD_Buf[3] << 8;
      
     Z_Mag = I2C_RD_Buf[6];
     Z_Mag|= (signed short int)I2C_RD_Buf[5] << 8;
      
     Temp = 1;
    }
   }
   else
   {
    Temp = 0;
   }
  }
  LED2_OFF();
  return (Temp);
}
 
 
int main(void)
{  
 InitializeBoard();
 LCD_String(1,1,"   LSM303DLHC   ");
 LCD_String(2,1,"   Test Setup   ");
 Delay_Ticks(2000);
 LCD_String(1,1,"                ");
 LCD_String(2,1,"X    Y    Z     ");
 
 Send_I2C_Byte(DEVICE_ADDR_ACC,CTRL_REG1_A,0x97);           //Normal Mode, 1344HZ, XYZ enabled 
 Send_I2C_Byte(DEVICE_ADDR_ACC,CTRL_REG4_A,0xA0);           //BDU update, Full scale selection +-8G
 
 Send_I2C_Byte(DEVICE_ADDR_MAG,CRA_REG_M,0x10);             //15Hz Output Rate, Temp Disabled
 Send_I2C_Byte(DEVICE_ADDR_MAG,CRB_REG_M,0x20);             //+-1.3 Guass,
 Send_I2C_Byte(DEVICE_ADDR_MAG,MR_REG_M,0x00);              //Continuous Conversion Mode
 
 while(1)
 {
  /**********************Accelerometer*******************************/
  /*          Accelerometer X,Y,Z sensitivity@ +-8g = 4mg/LSB       */
  /*          i.e. 1g = 250LSB or 1/0.004LSB                        */
  /*                                                                */
  /******************************************************************/
  if(GetXYZAcc())
  {
   if((X_Acc & 0x8000)==0x8000) {LCD_String(2,2,"-\0");}
   else                         {LCD_String(2,2,"+\0");}
   Displacement = ((float)X_Acc * 0.004 * 10.0)/16.0;
   X_Acc = (signed short int)Displacement;
   LCD_Print(2,3,abs(X_Acc),2);
   
   if((Y_Acc & 0x8000)==0x8000) {LCD_String(2,7,"-\0");}
   else                         {LCD_String(2,7,"+\0");}
   Displacement = ((float)Y_Acc * 0.004 * 10.0)/16.0;
   Y_Acc = (signed short int)Displacement;
   LCD_Print(2,8,abs(Y_Acc),2);
 
   if((Z_Acc & 0x8000)==0x8000) {LCD_String(2,12,"-\0");}
   else                         {LCD_String(2,12,"+\0");}
   Displacement = ((float)Z_Acc * 0.004 * 10.0)/16.0;
   Z_Acc = (signed short int)Displacement;   
   LCD_Print(2,13,abs(Z_Acc),2);
  }
  /*********************************************************************/
  
  /*********************   Magnetometer   ***************************/
  /*                                                                */
  /*    X Sensitivity @ +-1.3guass = 1100LSb/Gauss                  */
  /*    Y Sensitivity @ +-1.3guass = 1100LSb/Gauss                  */
  /*    Z Sensitivity @ +-1.3guass = 980LSb/Gauss                   */
  /*                                                                */
  /******************************************************************/  
  if(GetXYZMag())
  {
   if((X_Mag & 0x8000)==0x8000) {LCD_String(1,1,"-\0");}
   else                         {LCD_String(1,1,"+\0");}
   Displacement = ((float)X_Mag / 1100.0) * 100.0;
   X_Mag = (signed short int)Displacement;
   LCD_Print(1,2,abs(X_Mag),3);
   
   if((Y_Mag & 0x8000)==0x8000) {LCD_String(1,6,"-\0");}
   else                         {LCD_String(1,6,"+\0");}
   Displacement = ((float)Y_Mag / 1100.0) * 100.0;
   Y_Mag = (signed short int)Displacement;
   LCD_Print(1,7,abs(Y_Mag),3);
 
   if((Z_Mag & 0x8000)==0x8000) {LCD_String(1,11,"-\0");}
   else                         {LCD_String(1,11,"+\0");}
   Displacement = ((float)Z_Mag / 980.0) * 100.0;
   Z_Mag = (signed short int)Displacement;   
   LCD_Print(1,12,abs(Z_Mag),3);
  }
  /*********************************************************************/
  Delay_Ticks(50);
 }  
}
 
void  __irq I2C0_Status(void)
{ 
  Status_Flag=0xFF;             //update status flag
  Status=I2C0STAT;              //Read Status byte
  I2C0CONCLR=0x28;              
  VICVectAddr = 0x00;           //Acknowledge Interrupt
}
 
************* PROGRAM END **********************************************
 
STEP 4: i have to read the XYZ values in the LCD terminal through the Hyper Terminal and store in a file.. I have struck in this part.
 
 I have started to program to read the values using the following code: Is my approach correct. Because I am not able to read and store the values using hyperterminal:
Please guide me.
 
 
 include<lpc214x.h>
void init()             //initializes  both UARTS @9600,8bit, no pairity,1 stopbit
{
U0LCR=0x83;
U0DLL=0x61;
U0DLM=0x00;
U0LCR=0x03;
U1LCR=0x83;
U1DLL=0x61;
U1DLM=0x00;
U1LCR=0x03;
}
void senduart0(unsigned char a)       //sends a byte through UART0
{
U0THR=a;
while(U0LSR!=0x60);
}

 
Last edited by a moderator:

Hi selvam,

does the LCD output indicate Acceleration in terms of g? and could you suggest an Idea to detect impact/collision.
 

Hi selvam,

does the LCD output indicate Acceleration in terms of g? and could you suggest an Idea to detect impact/collision.



Yes pawan. LCD output indicate Acceleration in terms of g in x,y and z axis in both +ve and -ve Direction.

As of now I am not having idea of impact/Collision.
 

Hi selvam,

I did not notice step 4 previously.


That code is ok. but ll hang when you use rxd also. so better use this :

Code:
void txu0(unsigned char data)				  //Transmit a byte of data through UART0
{
while(!(U0LSR & 0x20));                         // Wait until UART0 ready to send character  
	U0THR = data; 
}

Have you tested the serial communication?

try sending 'a' continuously to the PC using this code:

Code:
#include<lpc214x.h>
void init()
{
U0LCR=0x83;
U0DLL=0x61;
U0DLM=0x00;
U0LCR=0x03;
}
void txu0(unsigned char data)
{
while(!(U0LSR & 0x20));                         // Wait until UART0 ready to send character  
	U0THR = data; 
}
int main()
{
PINSEL0=0x00000005;
PINSEL1=0x00000000;
PINSEL2=0x00000000;
while(1)
{
txu0('a');
}
}

If this works, you may proceed further.

Note: I ve a 12Mhz crystal and this calculation of baudrate is for 9600,No pairity, 1 stopbit.

Use the same setting in hyperterminal and ensure you have connected the serial port in the right direction.


Regards,

Pawan

P.S : how do you give colour to the code?
 
Last edited:

Dear pawan

I have tried transmitting 'a' with ur previous program. I get only blank hyper terminal screen!!!
In your program, you didn't call the init() function. You have just defined only ?

P.S :For giving colours in Keil tool......Goto...... Edit -> Configuration -> colors and fonts. You can choose colors there.

regards
Panneer selvam
 

Hi selvam,

Sorry, it was a typo.. indeed init() is to be called in the main() do it after PINSEL2.

and please post your results.

And thanks for the info.

Regards,

Pawan
 

Thanks pawan

Now transmission of 'a' character is working. i.e., a is continuously transmitted to hyperterminal. Likewise I have to transmit the Ecompass sensor data from LPC2148 to hyperterminal.

Please guide me.

And you have requested to post my results. This is the first output I have got. Henceforth on reaching an productive output I will share my results.
Also if you want any specific results I will post it.

regards
panneer selvam
 

Hi selvam,

Thats good. you have transmitted a single byte. You can now transmit a string or an array easily.

Use this code :
Code:
void sendstring(unsigned char *p)
{
while(1)
{
if(*p=='\0') break;
txu0(*p++);
}
}

first transmit a string like sendstring("hello"); and test the code.

You can transmit arrays using this .

please look at the posts in this thread so that you don't make my mistake.so, store the ecompass data in an array and send it .

Regards,

Pawan
 

Hi pawan

You have seen my Ecompass code in this post. The output of that code gives the values of ecompass sensor in the LCD display of LPC2148.

And I am going to use your code for reading the Ecompass data.

Should I insert your code in Ecompass program in the place of LCD display. i.e.,Instead of displaying to LCD , Should I redirect to UART ?

please guide me.

regards
panneer selvam
 

Hi selvam,

welcome back. I had a glimpse of your code. I guess there are 3 variables named X_acc,Y_acc and Z_acc . below each print-LCD function you have used, send the corresponding data to UART.

like:
LCD_print(***);
txu0(X_acc);
..
..
..
for all the 3 axes.
 

Hi pawan

I have checked for reading X data of Accelerometer. I have written txu0(X_Acc) afte LCD_Print(2,3,abs(X_Acc),2);.

No errors, But I am getting only ASCII values in the hyper terminal. Any idea ?


- Panneer selvam
 

hello sir,
iam using a freescale semiconductor MMA8451Q accelerometer for detecting tilt motions of a humanhand , can any one suggest me to use what 'g' range?

- - - Updated - - -

hello sir,
iam using a freescale semiconductor MMA8451Q accelerometer for detecting tilt motions of a humanhand , can any one suggest me to use what 'g' range?
 

Hi,
The best is to use trial and error to suit your requirement.
Ideally, tilt of a hand would be about 0.5 G. Also check This application note
hello sir thank you ,
I need help regarding Interfacing an accelerometer to LPC2148 via I2C bus, i have understood I2C details and worked out with sample program &segment display using I2C (executed in IAR workbench).. but i need help regarding the accelerometer values(X,Y,Z) to get displayed in LCD in an continuous loop for every tilt movement of an accelerometer.
please help me.. im new to ARM programming and learning it.

- - - Updated - - -

Hi,
The best is to use trial and error to suit your requirement.
Ideally, tilt of a hand would be about 0.5 G. Also check This application note
hello sir thank you ,
I need help regarding Interfacing an accelerometer to LPC2148 via I2C bus, i have understood I2C details and worked out with sample program &segment display using I2C (executed in IAR workbench).. but i need help regarding the accelerometer values(X,Y,Z) to get displayed in LCD in an continuous loop for every tilt movement of an accelerometer.
please help me.. im new to ARM programming and learning it.
 

Hi pawan ,

I am using lpc2148 microcontroller.

I am new for this .So please provide any material regarding basics of this controller or videos etc..

I downloaded datasheet and all. But i need some basic macros for programming and all.....
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top