Documente Academic
Documente Profesional
Documente Cultură
" ##
$
% &' # " (
( # # )
$ *+,-.
! &' # " ##
$
, ' / #
#include Bibliothèques pré-compilées
<stdio.h> Bibliothèques Fichiers (fichiers objet)
en C (texte) header
main() *.c *.h
{ puts(" Bonjour à
* tous ");
}
! " ##
$ Préprocesseur Compilateur C Editeur de lien
Remplace les #define Transforme le fichier C en un LINKER
et effectue les fichier objet (code machine), Lie (donne des adresses
Fichier source aux fonctions) tous les
#include les fonctions pré-compilés sont
C contenant la
déclarées dans les fichiers *.h fichiers objets et crée un
fonction main
fichier exécutable
Fichier de Programme
symboles exécutable
pour debug
4
# ' 0 " 1
#define TVA 19.6
#define taux 1.0/6.55957 Librairies standards : les fichiers « header »
Equivalence : Le pre-processeur #include <stdio.h> *.h contiennent en général des équivalences
#define pi 3.14 remplacera tous les pi par 3.14 #include <stdlib.h> ou les prototypes des fonctions précompilées
#include <stdio.h> char s[20];
ici : stdio pour printf, gets, puts et stdlib pour
Header de la bibliothèque standard in/out. atof
float d,c; (pour printf) float pht;
# #
Type Longueur Domaine de valeurs
signed char 8 bits -128 à 127
unsigned char 8 bits 0 à 255
int main(void)
char a,b,c ;
Tableau de 100 entiers
{ int table[100] ;
int n, m, l;
double d; char tableau[]={10,0x1c,’A’,55,4}
d = 5;
n = (int) 3.4; char *chaine= “bonjour” ;
n = 1;
m = 5; char *p ; Chaîne de 8 caractères (finie par 0)
d = 2;
l = (int) (m / d);
d = n / m; Le symbole * désigne un pointeur sur
un type défini p est un pointeur sur des
d = n / ((double) m);
caractères
d = 'A' * 6.0 – m + 0xA2CL;
return 0;
}
10
& "
#define + ,-.
* ) /
const int i=16569, char c=0x4c ;
#define pi 3.14 const float pi=3.14;
#define fruit pomme near )
far /
* « const »
ram rom
11 12
2 2
+ ,0. *
3 ) /
char a,b=28,c=’A’ ; * /
+ 4 2 " ) %/
« volatile »
static char var;
" 11 2 %
+ *56- " ) %/
volatile char a;
char var;
13 14
% , 3 % 45 &
Condition de départ
int a,i;
i=0;
Condition pour rester dans la boucle
while (i<100)
Évolution d’une variable
For (i=0 ;i<100 ;i++)
{
{ a=a+i;
a=a+i; // calcul la somme des nombres de 0 à 99
} i++ ;
}
char i=20 ;
For (;i<100 ;i++) a=a+i; /* Pas de condition de +
départ*/ 3 /
For( ;;) ; /*une boucle sans fin non
standard*/
15 16
% ! 45 & % 6 7
do 7 $ 8$9$8 $9 $ : $;;$<< =
{
int calc(char c)
a=a+i;
{
i++ ; if (c=='+') s=a+b; else
} if (c=='-') s=a-b; else
while (i<100) if (c=='/') s=a/b; else
if (c=='*') s=a*b;
return(s);
}
17 18
% 8 7 $
int calc(char c) break
{
" $> $ > $ > %
switch (c )
{ continue 4
case '+' : return (a+b);
case '-' : return (a-b); for(i=0 ;i<100 ;i++)
case '*' : return (a*b); { if (i<50) continue else a=a+i;}
case '/' : return (a/b); exit )
default : return(0); " ? %
}
}
19 20
9 # #
+ " / / "
'
+ @ ? /
A B (
/
* ; ) / int a=1,b=2,c;
int *p1,*p2 ;
char *p ; C
p1=&a ;
float *f ; /
Char *fonction(void) )
p2=p1 ;
C c=*p1 ;
void(*fonction)(void)
void(*fonction)(void)=0x8000
DDD p2=&b ;
*p2=*p1
21 22
9 # # &' # / #
int a=1,b=2,c; 1B B1 echange((x,y
echange x,y);
); echange(&x,&y);
echange(&x,&y);
int *p1,*p2 ; 1B 2 B1
void echange(int i ,int j) void echange(int *i ,int *j)
p1=&a ; 1B B1 { {
p2=p1 ; :B B1 int k; int k
c=*p1 ; 1B k=i; k=*i ;
B1 i=j; *i=*j ;
p2=&b ; 1B B1 j=k; *j=k ;
*p2=*p1 } }
1B 4 3 E0+ 2 ( *
$ 4 B1 $ 3 2 (
23 24
; ' < )
A F ( 7 2 C D$
chiffre[0]=0, et chiffre[3]=3
!
int chiffres[]={0,1,2,3,4,5,6,7,8,9}
int TAB[20]={1,12,13} TAB[3] à TAB[19] = 0
25 26
= = 7 #
A
struct {
( / Nouveau type (facultatif) unsigned RB0:1;
unsigned RB1:1;
struct identite { char nom[30]; unsigned RB2:1;
int age; unsigned RB3:1; Utilisation :
} unsigned RB4:1; PORTBbits.RB2=1;
jean,jacques,groupe[20]; unsigned RB5:1; A=PORTBbits.RB2;
unsigned RB6:1;
jean.nom = « bonnot »; unsigned RB7:1;
a = jean.age; } PORTBbits ;
27 28
= 7 =; 0 #
AG5-G F /
volatile near union {
struct { CAST automatiques :
unsigned RE0:1;
unsigned RE1:1; char -> int -> long -> float -> double
PORTEbits.RE0,
unsigned RE2:1; PORTEbits.ALE signed -> unsigned
unsigned RE3:1;
PORTEbits.AN5
unsigned RE4:1;
unsigned RE5:1; désignent le même bit du même registre
unsigned RE6:1; float x ; int a=5 ;
unsigned RE7:1; x=5.0
} ; x=(float)a ;
struct {
unsigned ALE:1;
unsigned OE:1;
unsigned WRL:1; float x=5.6 ; int a ; a=5
unsigned WRH:1; Les bits 4,5,6 ne sont pas déclarés
unsigned :3; a=(int)x ;
unsigned CCP2:1;
} ; Seul le bit 0 est déclaré
struct {
unsigned AN5:1;
} ;
} PORTEbits ;
29 30
/ # > &
void strcpy(char *s, char *t)
{ int i; void strcpy(char *s, char *t)
#define PORTA *(unsigned char *)(0x1000) i=0; {
do while((*s++=*t++) != ‘\0’) ;
{s[i] =t[i] }
i++;
}
while (s[i-1] != ‘\0’);
PORTA est le contenu de cette adresse
}
2 H1+ C $ 0 #
Affiche une chaîne
/ de caractères
33 34
, #
# " $ % %c " %
\n nouvelle ligne
%s " J C $
char a=10 ; float b=3.1412 3 ) K LD% \t tabulation
printf(" décimal %d,
%d hexa %x,
%x reel %f " ,a
a,a
a,b
b) ;
%d " % \b backspace
%u " % \r retour chariot
%x M" \f form feed
0
2 % \' apostrophe
D$ 2 0$ I$ \\ antislash
%f " 2 %
# " J $ $ % %p " % \" double quote
J %" #% \0 nul
35 36
% ) (= #
ctype.h ( 2 " % () Appel de fonction
> [] Indice de tableau
limits.h ) ( ! Négation logique (NOT)
string.h J ~ Complément binaire bit à bit b=~a
math.h ) - Moins unaire b=-a;
stdlib.h " % + Plus unaire b=+a;
" $ % ++ Pré ou postincrément b=a++;
( ) " $ %$ ") % -- Pré ou postdécrément b=a--;
time.h 4 4 & Adresse de b=&a;
* Indirection b=*a;
37 38
# ;
? . B A + " 8 %
: 1 AF 5 " 9 %
@ E & B + " 9 %
7 . @ BF + " 9 %
AA 4 88 FF H " %
BB 4 99 GF " : %
C HN ;
CC HN ) "" P% ;; " %%
D -A 2 O
EE -A ) "" P% <<" %%
E -A <
H6 7 ' " 9 %! "+ 9 ' ' %
39 40
7 9 #
F 0
?F 0 B "
B % *
:F 0 ) 1 "
1 % ) /
IF 0 # " 1 %
@F 0 & " & %
7F 0 @ " @ % E ? ) $
CF 0 HN ; P " ;P% $ I )
DF 0 -A HM O P " OP% /
EF 0 -A < P " < P%
AAF0 88 P " 88P%
BBF0 99 P " 99P%
41 42
9 # 9 #
. 4 N 4
unsigned a = 0x000F; /* 0000 0000 0000 1111 */ unsigned a = 0x000F; /* 0000 0000 0000 1111 */
unsigned b = 0x0010; /* 0000 0000 0001 0000 */ if (a & (1u << 2)) {
unsigned c = a | b; /* 0000 0000 0001 1111 soit 0x001F */ printf("bit 2 = 1");
}
. 4' I else {
unsigned a = 0x000F; /* 0000 0000 0000 1111 */ printf("bit 2 = 0");
unsigned b = 0xFFF7; /* 1111 1111 1111 0111 */ }
unsigned c = a & b; /* 0000 0000 0000 0111 soit 0x0007 */ (%6 5 Q
A 88 D
A 88
A 88
///
43 44
9 # &'
N I 4 P 4D 2
unsigned a = 0x000F; /* 0000 0000 0000 1111 */
if ( (a & (1u << 3)) && (a&(1u<<15))==0 ) {
printf("bit 2 = 1 et bit 15=0");
}
else {
printf("bit 2 = 1 et bit 15=0 n’est pas vérifié");
}
45 46
&' &'
2 R 7 K
J C $ /*
(
47 48
&' &'
R 7 K + 2 J C /
J C $ / * K
) /
J * 6,
* 2 " J C %
K L/S ' J C 4
/
A K ) K L/
) 2
" %/
A K#L 2 /
49 50
&' &'
J M 65 4'
51 52
&' &'
J N 67 3 ) 4 J . 60
J C
53 54
&'
&' # "
(
# # )
55
# # ) # # )
* C C 5T. U $ 4 /
4 % ( = &
1+ M M
* C E7 K D M M HEE
I M M HEE
T P / M M HEE
P I M M HEE
M M HEE
U P M M HEE
M M HEE
V U M M HEE
D 107W M
TA+X M
EH M
I +H*H7N M
10AN-6HH M M
P 1H,,-, M
15G5N M M
U 1+H*H7N M M
@ P .0++H M M
57 58
# # ) # # )
=;3 %& * 4 " 4 D% ) 4 )
D4 U ) K /
!O > !P T ) C 4 /
* 4D ) 4 K ) K Y C
* C C )
) /
% =Q * 4D ) / *K ?
) @ /5 ) 4
4 / + 2
& + Z Z/ ) 4 K ) K
/
! RO'NP-5S
=& & ; * ) 4 K K Z Z Z Z/
; ,&&! * ) 4 $K 4 ) C
T& T # RO'NPU5S
Z Z Y /H $ K
C /
&33 3 5 ) 4 K ) K /
T # RO'NP 5S
( ; *K K K /
=& & ; ( *K K K /
9 ==& 7K E7/
59 60
# # ) # # )
T& T # RO'NPU5S 6
! RO'NP-5S
7 $ )
7 ) KF $ K
/
E7 /
* V:V ) Z* ) 5 Z W 5
U P I D
U P I D
U P I D
61 62
# # ) " R (S
T # RO'NP 5S 6 A 70G )
7 4 / C C "4 )
0 K K $ %/
@ " $ $ ) 5 2 ( )
$ /% $) " #/
2 4 A 70G )
C /
) 2
7K ) K K
) )
K I I " $ /%
K 4 ) ( K " K
K K @ 4 / ) %/
) ) 4
P I D
/
M M T5N 7N,* 5, HG0T*H +H*H7N 5G" % 15G5N 0AN-6HH 1+N,-TH
63 64
" R (S " R (S
7 2 C 4 5 2 2 (
" # E
)
"S & &S S @ D% D 8 S 8 S &@)
"S @ @S S & D% @"S @ & )% 8 S 8 D
" # E
65 66
= # ! O-O.
67 68
! O-O.
69 70
71 72
X
E ? E57
6 /
0 ? E57
$ *+,-.
6 /
E /
74
A E57 ? .57,-7[5E B
E57 E 5 7
• + /
• 7 2 /
• A 3 ,5+7 "3
# %
B >>>/ #/
75 76
,
* E57 6 I @ A E57
/ T @* * *M / 22"*% MM((@'' "H2 6 0%
22 /
/ . @, $) *. /
* N /
I/ [ @H $) *+ MM N(
- 4 9 73 / 7 HE,-. HHE,-./
7, E,-./
66 /
(( 5 /
'' S 2 ) '/
77 78
&' #
$ ? E57 * ) 4
/
6 @ D/
* 2 /
. @, " %/ NC C /
*
6. 6 /
]HT/
N( / * 3 K $ 4
J /
D ' 4 D .[\ 2 /
*
K ) /
79 80
% %
* E57 6 /
* E57 6 F
T 5E "E 5 @ E Q %4
T +-57 "+ - 5 7 S
81 82
* E57 6
. $
. $
E $
, 2 " ) %/
83 84
3 '
* 2
* 2 E57 6
A ) ) 7 2
) " $ $ $=% E /
) " HN$ G $0G $=/ %/
N /
, 4/
5 /
7 /
. /
85 86
9 #
* E57 6 Y
[ C
. /
. /
87 88
9 # 9 #
* W * E57 6 C )
" D %/ WB /
7 4 2 / *K DDDD /
* 7 '
* DDD (
( C " %/
89 90
9 # 9
+ 2
/ . ,0. /
. /
. /,0.
H 2
* +6, " + 6 , %)
? /
* 2 "^E,
^ E , %$ K /
/ . HHE,-. /
0 /
G /
E ) ,0./
- ( C 4 K HH0 , HH 0N0
? 2 ? HH7-G HH7-G /
91 92
9 9
* 2 )
* ) D ,ED
+N0NA+ D/
* ) ,ED
+N0NA+ /
7 ) ) /
* C ) )
+6, "+ 6 , %/
93 94
3 =; ; =
95 96
# #
A ? 2 / G 2
Z ( Z 4 ( /
2 / * C E57 "T
A 2 2 %
E Q
2/
/
E 2 /
97 98
/& :=
* 6 C
E 0 "E-,N0%/
E T "E-,NT%/
# /& :=
99 100
3; ;3 = 3; ;3 =
* 0 E-,N0 P * ,0 2 K
",0D 4 ,0 %/
N N.,D/
* )
N,5+0/ H F
T $%&'( ) * + ,-%$( H1+ 0$
T $%&'( ) . + ,-%$( N N.,D
* 2 4 K ND7+
-EN5-G_,H^/
101 102
3; ;3 = 3;% ;3 =%
,0 H1+ 4 $ K * T E-,NT
" *H ",TD 4 ,TU%/
2 %$ * 4 K
2 S / N,5+T/
+ ,0 4 D$ K .F ) E-,N0/
$ 4 /
H $ ,TD 5GN
+ ,0 4 $K
2 5GN/
$ K `
H $ ) ) ,T 4 ,TU
2 /
K /
LED rouges de 20mA (tension de seuil égale à 1.8V).
103 104
9 Y "
* a C E57
2 F /
E E57$
9 Y " 0 2 S S++$
2 4 ) '$
( C
? 4 /
105 106
5 5
*K F 2 - 4) '
/
*K K 4
) ' K ,7/
0 K 4 '$
) 3 ) K4 $ D D .['
( ? /
$ 2
? F (
E57 /
107 108
5 5
- ,7 [ 2
109 110
5 3
* .7*,
) K $K ( C " %/
) /
0 ) ' .['$
.['$ 2
111 112
&& 3 9 & && 3 9
/ 0 *KHHE,-. ],HG
/ E HH0 , / E HH0 ,
I/ E 4 HH 0N0
/ . , HH7-G 4
/ E D2PP HH7-G
I/ * HH 0N0 P/ E D200 HH7-G
/ K ],
U/ 0 K $ " D % "HH56
], D%
/ , K 4
113 114
! " ## / # X
Projet
Ecriture algorithme
Ecriture
2 Ecriture du source
du
$ Erreurs ?
oui
non
Debug du programme
Test
du logiciel
non
Fonctionnement
sur le OK ?
matériel oui
Programmation
de la carte
Fin du projet
116
, ' / # X
Fichiers source fichier1.c fichier2.c fichier3.asm
Assembleur
lib1.lib
linkage
script.lkr
" #
Programmation
Debug
du composant
117 118
" * " *
- *H TE /
A TE $ *H
DDD /
H Q 7) /
119 120
" M " M
- *H 2
TE TE / A TE $ *H
TE $ *H
/
H Q 7) /
121 122
" N " .
- 4 I " $ - @ E57 6 0
I $P %/ + $ /5
TE bTE $ TE $ TEIc ,0D$ *H ,TU *H 4 TE bTE $
I P / TE $ ///$ TE c/ *
• A TE $ *H $
• A 2C *H /
• E N,5+0 N,5+T
• E N,5+0 N,5+T
N,5+0 "========% $N,5+T "========%
N,5+0 "========% $N,5+T "========%
• H Q 7) /
• H Q 7) /
123 124
" Z = / " Z
- 4 2 0S 0, 4
E57 6 0" % + ) $
N,5+T "=======%
0 4 /,
* ?
K )
) ? E57/
H Q 7) /
125 126
= / " Z " +
- DD 2
2 " %/ H Q 7 )
/
! "
" !
! "
#
127 128
9&3