Showing posts with label Programming C. Show all posts
Showing posts with label Programming C. Show all posts

Tuesday, December 21, 2010

Control Pattern Converting at Programming C (%)

Character %_ used as pattern conversion operator. Pattern conversion would very useful at the time of we present result to LCD. There are some kinds of pattern conversion along with the function. For example:



a. %d functioning to presents positive integer. example: sprintf(buf,"Angka %d",14);

b. %o functioning to presents integer octal number.

c. %x functions to present number heksa decimal integer.

d. %u useful to presents number decimal without sign.

e. %f useful to presents fractional number.

f. %i functioning to presents integer number.

g. %c presents character posed at ASCII number.

read more "Control Pattern Converting at Programming C (%)"

Control Procedure and Function Programming C



Often we find group of instruction for a certain need often implemented in a program. Group of this instruction can be made as procedure or function. Stepped this will be able to economize memory compared to if the instruction is written repeatedly. Remember, program in microcontroller to have a real limited memory.



Definition of Procedure.

Procedure is a gathering of instruction to do a certain need without returning a value.

Under this is as of cut-off example of way of writing of program at procedure making;



...

void name_procedure (parameter1, parameter2, ... parameter N)

statements;

}

...

Example of in the application of the program is

...

void delay (unsigned char i)

while (i--) {

/* writing for language assembly * /

/* will be studied separate of 8/

#asm

nop

nop

#endasm

};

}

...



Definition of Function

Function is a gathering of instruction to do a certain need with return end result of the value and need. Under this is writing concept of its(the program.

...

Type data name_function ( parameter1, parameter2,... parameter N)

{

Statements;

return variable_result;

}

...

Example:

...

int wide(int width, int height) {

wide = width*height

return wide;

}

...

Denominating of procedure or function of done directly by writing down its(the procedure or function.

example:

...

delay(150); //the way calls procedure

dt = wide(5,10); //the way applies function

}

...

read more "Control Procedure and Function Programming C"

Control Structure Programming C for ATMega8535



Writing structures of language C in general is consisted of 4 blocks:



1. Header.

2. Constanta declaration global and or variable.

3. Function and or procedure (this can be written under principal program).

4. Principal program.



In general, programming C was simplest programming to be done with only writes down main program only, that is:

Void main (void)

{

….

}

The full programming done is:

/*HEADER for call library that will used*/

#include

#include

/*Constanta declaration and or global variable */

Unsigned char dt, xx;

Char buf[33];

/* Function declaration and or procedure */

Unsigned char lamp (unsigned char bitn)

{

PORTA=bitn & 0x3C;

}

/*Main program */

Void main (void);

/* Constanta declaration and or local variable */

Char data;

PORTA=0x00;

DDRA=0xF0;

While (1)

{

};

}

read more "Control Structure Programming C for ATMega8535"

Control Programming C



In last matter of we have tried studies about header, etc. Now we will to continue study about programming structure C. At this structure we study about reserved keywords, operator and ramification.



1. Operator.

Operator is an instruction of fit containing operator and operand. Operand is variable which is the part of statement while operator is a symbol expressing which operation will be done by operand. For example:

c = a+b

There is 3 type operand (a,b and c) and 2 operator (= and +).

Operator in programming of C divided to become 3 group;

a. Unary is operator operating on one operand, example; - nitrogen.

b. Binary is operator operating on two operand, example; a-n.

c. Nitrogen is operator operating with three or more operand, example; a= ( b*c)+d



2. Reserved Keywords.

Reserved keyword is glossary is standard which useless for label, identification or variable. Following the glossary;

break flash signed continue

bit float sizeof if

case for sfrb struct

char funcused sfrw default

const go to static inline

switch do int typedef

double interrupt union unsigned

eeprom long else register

void enum return volatile

extern short while

3. Branching

In doing ramification at programming of C there is 4 form of public.

a. if - then.

b. if - then - else

c. switch - case

d. switch - case - default

We will study one by one to the four instructions.



a. If - then

Form of generally is

if (condition) {

// statement

};

Mean was statement will be implemented if condition full filled. For example is

if (a<0x55)>

PORTC=0x55;

};

In this example PORTC will be sent data 0x55 (remembers in the form of heksa) if value a smaller than 0x50.

b. if – then – else

Form of generally is

if (condition) {

// statement a

}

Else {

// statement b

};

Mean is statement of a will be implemented if condition full filled and statement of b will be implemented if condition is not full filled. For example;

PORTC will be sent data 0x55 if value a smaller than 0x50 and PORTC will be sent data 0x44 if a>0x50.

if (a<0x50)>

// PORTC=0x55;

}

Else {

// PORTC=0x44

};

c. switch – case

Statement switch - case is applied if happened many ramifications. Writing structure in general is;

switch (expression) {

case constant 1:

Statement1

break;

case constant 2:

Statement 2

break;

case constantN:

StatementN

break;

}

For example:

switch (a) {

case 1:

PORTC=0x01;

break;

case 2:

PORTC=0x02;

break;

case3:

PORTC=0x04;

break;

}

PORTC will be sent data 0x01 if value a=1, PORTC will be sent data 0x02 if value a=2 and PORTC will be sent data 0x04 if value a=3.

d. switch – case – default.

Statement switch - case - default much the same to with switch - case. Becoming difference was there is default hence otherwise there is condition of case matching with expression switch hence would towards statement which there is part of default. This writing structure like under this;

switch ( expression) {

case constanta 1:

statement 1

break;

case konstanta 2:

statement 2

break;

case konstanta N:

statement N

break;

default:

statements;

}

For example:

switch (a) {

case 1:

PORTC=0x01;

break;

case 2:

PORTC=0x02;

break;

case 3:

PORTC=0x04;

break;

default:

PORTC=0xFF;

}

PORTC will be sent data 0x01 if value a=1, PORTC will be sent data 0x02 if value a=2 and PORTC will be sent the data 0x04 if value a=3 and if condition of case unmatched to expression hence statement at default will be implemented.

read more "Control Programming C"

Control Programming C Structure



In every programming of C at a microcontroller must fulfill 4 blocks. In tutorial this microcontroller we will study every category which there must be in every programs writing.



1. Header.

Header comprises include file (.hex) that is library which will be applied in programming. For example:

#include

#include

#include

. . .

2. Data type.

In making of this C program we will make applies Code Vision AVR. Following is tables type variable data which can be applied:



3. Constanta.

Writing of constant in programming of C must follow order under this:

a. Integer or long integer can be written with format denary (example; 1,2,3,4), binary with prefix 0b (example; 0b101001), heksadecimal using prefix 0x (example of 0xff) or oktal by using prefix 0 (example; 0777).

b. Unsigned integer is written with suffix U (example; 10000U).

c. Long integer is written by terminated by L (example of 99L).

d. Unsigned long integer is written by terminated by UL (example of 99UL).

e. Floating point is written by terminated [by] F (example; 1234F).

Character constanta must be written down in quotation mark (example; ' a'), while constanta string must in quotation mark 2 (example; "I learn programming C”).



4. Label, Variable and Function.

Identification of Label, variable, and function cans be in the form of letter (A....Z, a....z) and number (0...9), and character underscore(_). Identified can only be started with letter or character underscore. More important is identifying this Case in significant, that is uppercase and different small. For example, character is variable 1 unlike Variable 1. Identification loads 32 characters.



5. Commentary.

Comments can start with marks ‘/*’ and ending with ‘*/’. For example;



/* this is comment */

/* this comment

Multi line */



Comments at one line can give marks ‘//’. For example;

// this is comment too



To be continued at the next tutorial….

read more "Control Programming C Structure"