Δομή Προγράμματος C pre-processor directives global declarations function prototypes main() local variables to function main ; statements associated with function main ; f1() local variables to function 1 ; statements associated with function 1 ; f2() local variables to function f2 ; statements associated with function 2 ;
Ένα απλό πρόγραμμα C #include <stdio.h> #define MAX 50 int sum(int,int); /* pre-processor directives */ /* (library use and constant definitions) */ /*Definition of function prototype */ main() int i,j,s; s = sum (i,j); printf("the sum is: %d\n,s); /* the standard start for all C programs function main() */ /*Local variable definitions */ /* Function call */ /* Display result on screen */ int sum(int a, int b) int result; result = a + b; return result; /* Function definition */
Τύποι μεταβλητών There are five basic data types associated with variables: * int - integer: a whole number. * float - floating point value: ie a number with a fractional part. * double - a double-precision floating point value. * char - a single character. * void - valueless special purpose type Τhe range of values and the amount of storage that each of these types takes is not defined by the standard. Ιn each case the 'natural' choice is made for each type of machine. Moreover there are long, short, signed and unsigned numbers
ΤΕΛΕΣΤΕΣ Αριθμητικοί τελεστές Addition + Subtraction - Multiplication * Division / Modulo % Λογικοί τελεστές AND && OR Δυαδικοί τελεστές (Bitwise) Apply to char, int, long int One's complement ~ Left Shift << Right Shift >> AND & XOR ^ OR Let x=5, y=2 x&&y = true (x,y are non zero) x&y = false (101 and 010)
Δομές επανάληψης (while loop) while(condition) compound statement ; #include <stdio.h> main() int count; count=0; while (count < 100) ++count; printf("hello\n"); do compound statement while(condition); #include <stdio.h> main() int count; count=0; do ++count; printf("hello\n"); while (count < 100)
Δομές επανάληψης (for loop) for ( counter=start_value; counter <= finish_value; ++counter ) compound statement #include <stdio.h> main() int fahr; for ( fahr = 0 ; fahr <= 300 ; fahr = fahr + 20) printf("%4d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
Conditional Statements if (condition) statement1 ; else statement2 ; #include <stdio.h> Τελεστές Σύγκρισης > (greater than) < (less than) >= (for greater than or equal to ) <= (for less than or equal to) == (to test for equality) main () int num1, num2; printf("\nenter first number "); scanf("%d",&num1); printf("\nenter second number "); scanf("%d",&num2); if (num2 ==0) printf("\n\ncannot devide by zero\n\n"); else printf("\n\nanswer is %d\n\n",num1/num2);
Pointers Pointer (Δείκτης) είναι μία μεταβλητή η οποία περιέχει τη διεύθυνση μίας άλλης μεταβλητής ή συνάρτησης ή δομής δεδομένων. Ο pointer δηλώνεται όπως όλες οι μεταβλητές με το πρόθεμα * int *p1 /* define a pointer to integer */ int a,b /* define integer variables */ float *p2 /* define a pointer to float */ a=4; p1 = &a; /* p1 points to a */ b = *p1 + *p1 ; /* b = 2a */ ΠΡΟΣΟΧΗ p1 = a δεν παράγει μύνημα σφάλματος αλλά ο p1 παίρνει την τιμή του a (στο παράδειγμα o p1 θα δείχνει τη θέση μνήμης 4) b = p1 +p1 θα δώσει στο b τo διπλάσιο της διεύθυνσης του a Η μεταβολή των pointers γίνεται ανάλογα με το μέγεθος της μεταβλητής που δείχνουν p1++ ή p1+1 δείχνει τον επόμενο ακέραιο p2-- δείχνει τον προηγούμενο float
Pointers as function parameters Οι pointers δίνουν τη δυνατότητα επιστροφής πολλαπλών τιμών από συναρτήσεις. (Το return επιστρέφει μόνο μία τιμή) void swap (int *a, int *b) int temp; temp = *a; *a = *b; *b = temp main() int i1,i2; swap (&i1, &i2);
Arrays Array (πίνακας) είναι ένα διατεταγμένο σύνολο ομοειδών μεταβλητών int a[10]; /* array of ten integers a[0], a[1],...a[9] */ char str[50]; /* array of fifty characters (string) */ int f[20][5] /* 2-D array of 100 integers (20 rows x 5 columns) */ array initialization int ii[5] = 10,20,30,40,50 ; addressing an array element b=ii[2]; το όνομα του array χωρίς δείκτη είναι ένας pointer στο πρώτο στοιχείο του πίνακα c=*ii; είναι ισοδύναμο με c=ii[0]; b=*ii+2; δεν είναι ισοδύναμο με b=(*ii)+2; Δεν υπάρχει μηχανισμός ελέγχου του μεγέθους ενός πίνακα και η εγγραφή σε στοιχείο που δεν υπάρχει μπορεί να προκαλέσει προβλήματα ii[6] = 8 * ii[4]; /* γίνεται εγγραφή 7 θέσεις μετά την αρχή του πίνακα ii */
#include <stdio.h> Printing a string const char s[24] = This is a thermometer!! ; char i; char *p; main() p=s; for (i=0; i<22; i++) putchar(p[i]); (ή putchar(*p++);) ενναλακτικά while (*p!= '\0') putchar(*p++);
Variables and Memory Δίνεται η δυνατότητα να ορίσουμε τη μνήμη του AVR στην οποία θα τοποθετηθούν οι μεταβλητές του προγράμματος μας με το κατάλληλο πρόθεμα. Οταν δεν έχει πρόθεμα η μεταβλητή αποθηκεύεται σε καταχωρητή ή την SRAM. flash int a /* integer variable in FLASH */ flash char s[6]= hello /* string of six characters in FLASH */ eeprom int threshold = 120 /* integer variable in eeprom */ register unsigned char c1 /* character variable in register */ volatile float number /* float variable in SRAM */
I/O Registers Ορισμός Ι/Ο καταχωρητών (περιέχεται στο header file xxxx.h) sfrb for 8-bit register sfrb TCNT1L=0x2c; sfrb TCNT1H=0x2d; sfrw for 16-bit register sfrw TCNT1=0x2c; Eγγραφή/ανάγνωση καταχωρητή Ι/Ο ldi r16,high(0x1234) TCNT1 = 0x1234; out TCNT1H,r16 ldi r16,low(0x1234) out TCNT1L,r16 unsigned char a; a=pina; in r16,pina Bit Level instructions (SBI,CBI,SBIS,SBIC) DDRA.1 = 1; PORTA.1 = 0; if (PINB.1 == 0) if (!PINB.1) statements statements
INTERRUPTS Ορισμός των interrupt vectors (περιέχεται στο header file xxxx.h) #define EXT_INT0 2 #define TIM0_OVF 8 Η ρουτίνα εξυπηρέτησης διακοπής (ISR) είναι μία συνάρτηση (function) που ορίζεται με το keyword interrupt interrupt [EXT_INT0] void external_int0(void) statements interrupt [TIM0_OVF] void timer0_overflow(void) statements Επειδή η ρουτίνα διακοπής καλείται από οποιδήποτε σημείο του προγράμματος δεν είναι δυνατό να επιστρέψει τιμή (void) ούτε να δεχθεί ορίσματα (void). To interrupt vector που ακολουθεί το keyword interrupt πρέπει να είναι μεγαλύτερο από 2 γιατί το 1 αντιστοιχεί στο reset.
Period Measurement #include <mega16.h> sfrw ICR1=0x26; unsigned char ov_counter; unsigned int starting_edge, ending_edge; unsigned long clocks, period; interrupt [TIM1_OVF] void timer1_ovf_isr(void) ov_counter++; interrupt [TIM1_CAPT] void timer1_capt_isr(void) ending_edge = ICR1; clocks = (unsigned long) ending_edge + ((unsigned long) ov_counter * 65536) - (unsigned long) starting_edge; period = clocks / 125 ; /* period in milliseconds */ starting_edge = ending_edge; PORTC = (char) ~period;
Period Measurement main () DDRC=0xFF; TCCR1A=0; TCCR1B=0x02; TIMSK=0x24; #asm( sei ) while(1) ;