Monday, April 28, 2008

Arrays


Single Dimensional (1D).


Key in the code below. I know it is long…But there is a point to this. This is to show you how to eliminate redundant code. After keying in the code and compiling it and correcting your typing errors; burn it to your PIC. Connect a common cathode (or common anode) 7 segment LED to portB. When the PIC is powered up, you will see the number 0, 1, ..9 displayed in a endless loop.


Lesson 4 Ver 1 Page 1

Lesson 4 Ver 1 Page 2

Lesson 4v1 compiled



Actually this is an example of bad programming. The code is large, it’s time consuming and the chances of making typos is high. I will now demonstrate arrays. This is how to define arrays:-

char name[30]; //This is an array of 30 characters.

double tempC[10]; // Array of 10 floating point numbers

int x[5]; // An array of 5 elements that can hold 5 signed int.

It like defining 5 variables.
x[0] can store a value x[1] can store another and so on.
x is a variable or const, and [0] is the location where the value is stored.


Also important to note:

the [] in arrays do not mean optional, as it usually does in documentation standard.
the first element of array is always 0.
arrays can be of any valid C data type, ie, float, double, int or char.


If you want to load the array with known values, do this when you declare you array:-

char base2[6] = {1, 2, 4, 8, 16, 32};

This array holds 6 elements, base2[0] is 1, base2[1] is 2, etc. etc. etc..

Or you can do this:-

unsigned int base2[]={1,2,4,8,16,32,64,128,256,512,1024};

When you don’t specify number of elements, and when you compile the, compiler will count then number of element and assign 0 to the first element, 1 to the next one and so on.

Following me so far….Excellent!

Firstly let’s use a variable cc7s as an array of chars. I set is as constant because the values will not change.

The point to this is that the code can be modified using arrays. Using the same code (new project, copy and paste), then change with the modifications in listing below.


Lesson 4 Ver 2 Page 1

Lesson 4v2 compiled



OK, All i have done is replaced the variables with arrays and if() with case. Some what smaller code. But wait! I see a pattern! when count==0, cc7s[0] is displayed, when count==1, cc7s[1] is displayed and so on. Then it loops back and starts over. So, lets redo the Version 1 and 2 code. Type in the listing below.

Note: switch..case will be explained in a later chapter..


Lesson 4 Ver 3 compiled



And that my dear students, is how you use arrays. It compiled to 83 bytes, as compared to 395 bytes in version 1. That’s almost 5 times smaller.







Let’s see if you understood the above lesson.

Skill Check!

1. Declare an array called DOW and initialize it’s elements with first 3 characters of day of week beginning with Monday.

2. What would need to do:-

a) To display decimal point with ALL the numbers.

b) To display decimal point ONLY with odd numbers.

c) To display decimal point ONLY with even number.

Only display decimal point, not to blink it.

Hint: You’ll need to refer to previous lessons.

3. Write a program to display number from 0 to 9 in an endless loop. Turn on the decimal point for 150ms and then off it for 150ms for every number.

4. Without changing the values of the arrays in the version 3 program, modify the program to display the numbers from 0 to 9 on a common anode 7 segment LED. (If you are already using a common anode, then do for common cathode).

Hint: Common cathode is opposite of common anode.

8 comments:

9W2BSR said...

Ha ha..
You've used 'switch & case', not yet covered in lessons...

BTW, another way of doing it is
...
for(count=0;count<10;count++){
portb = cc7s(count);
delay_ms(...);
};
...

9w2gu said...

what do you mean? That is what i did in lesson version 3!

9w2gu said...

Why ! WHY! WHY! Why is it displaying version 1 code. Let me try to update it again.

9w2gu said...

OK fixed problem. I was in hurry in the morning and uploaded the wrong image. But photobucket has a bug. If i delete an image, and upload a modified one with the same name it displays the old deleted one. Sorry guys for the mistake.

9w2dtr said...

:(( wait lah... why so fast... syntax also still learning, ADC also still learning, Port config also still learning, ADCON also blurr... now array??

Unknown said...

Hamid, why use photobucket? You can upload photo direct to blog la...

Dexter, okay lor... we'll stop the lesson temporarily... but to keep the site alive, may be we can discuss your project here... real case study rather than theory... we can discuss the electronics and coding aspect here... others can learn from it also...

9w2dtr said...

OK. I will try to do my project. I write the code on my own effort as exercise and you guys will point out any correction need to be done... sure plenty one lah!

9w2gu said...

OK...I will prepare other lessons on Open Office and will upload when you are hungry for more. Basir did Kin Hou mail you his basic code to debug?