2
: C .
: , , , #include, .
1.
1.1. C
C 70- г, Bell Labs. , .
, , . :
<> <>([])
{
[]
}
, . void , .
. , . , . . C ( sin Sin ).
, . . . .
, . .
? call. ret.
? Keil R5-R7. , . , R5-R7.
1. a2, .
char a2(char a)
{
return a*a;
}
2. send, .
void send(int addr, char value)
{
P0 = addr;
P1 = addr >> 8;
P2 = value;
WR = 1;
WR = 0;
}
, , , , . . :
init();
b = a2(a);
send(1, b);
main().
void main()
{
}
, main() . ҳ , , , , . while.
void main()
{
//
while (1)
{
//ҳ
}
}
C . , : //. /* */. . :
|
|
a = 2; //
/*b = a+3;
*/
C , . ().
1 C
# | ij | ||
- | -a | 䒺 a | |
+ - * / % | a+b a%2 | , , , a b | |
++a | ++a | . a | |
a++ | a++ | . a a. | |
--a | --a | . a | |
a-- | a-- | . a a. | |
! | !a | a | |
> < >= <=!= == | a>b, a!=b | a b | |
&& || | a && b a || b | ||
~ | ~a | a | |
& | ^ | a & b a^0x0F | , | |
<< >> | a<<b a>>8 | a b | |
= | a = b | a b b | |
+= -= /= *= %= &= |= ^= | a+=b a+=4 a&=0x0f | a b a .a+=b a=a+b | |
& | ptr = &a | ||
* | a = *ptr | ||
sizeof() | sizeof(a)sizeof(int) | ||
?: | max = (a<=b)?b:a | op1? op2: op3 op1 , op3 op2 |
#include
C - .h. #include.
#include < > #include . . . :
#include <ADUC841.H>
#include <intrins.h>
#include "stend.h"
ADuC841 ADUC841.H.
#include <ADUC841.H>
, 0x7A .
WR = 1; //0: .
P0 = 0x7A; //1:
P2 = 7; //2: CS7 "0"
P2 = 0; //3: CS7 "1".
|
|
, P2. , . .
WR = 1; //0: .
P0 = 0x7A; //1:
P2 &= 0xf0; // 2
P2 |= 7; //2: .
P2 = 0; //3: CS7 "1".
2.
1. Keil ADuC841. .
2. main.c.
3. main.c
void write(unsigned char Addr, Data) | Data Addr |
void CS(unsigned char Addr) | |
void LED(unsigned char Data) | ³ Data (1 ) |
void ClearLatches() | 1..8 ( 0xFF) |
4. main(), , 0xAA, .
5. ³ . . .
3.
1. void?
2. ?
3. ?
4. ?
5. C?
6. ?
7. ?
8. ?
9. ?
10. ?
11. ?
12. #include?
3
: , , ', . .
1.
1.1. C
C
[const] <> [ ] <>;
<> -, .
<> , . 1.
[ ] , . , Keil 2. , , data.
[const] . . code, .
1
, | ij | ||
signed char,char | -128..127 | ||
unsigned char | 0..255 | ||
signed int, int | -32768..32768 | ||
unsigned int | 0..65535 | ||
signed long | -2147483648.. 2147483647 | ||
unsigned long | 0.. 4294967295 | ||
float | 1.5E-45..3.4E38 7-8 |
2
code | . MOVC @A+DPTR |
data | (0..127). . |
idata | . 0..255 |
bdata | . |
xdata | . MOVX @DPTR |
far | ( 16). |
pdata | MOVX @Rn |
, - . , . a . b c main().
|
|
const unsigned char code a = 5;
void main()
{
unsigned char b;
int c;
= b*a;
}
.
< > [ ] <>[<>]
.
const < > [ ] <>[n] = { 0, 1,..., n-1}
:
unsigned char D[16]; // 16 unsigned char
const float code coefs[3] = {0.1, 0.25, -0.01}; //
. . :
D[0] = 1;
D[1] = D[0]+1;
y = coefs[i]*x;
1.2.
1
1..4. 0 : PGFEDCBA2. , 7 4 111110002= F816. , . .
unsigned char code LEDD[16] =
{0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x98, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E};
4 , , , .
void Static(unsigned int A)
{
write(4, LEDD[A & 0xF]);
write(3, LEDD[(A >> 4) & 0xF]);
write(2, LEDD[(A >> 8) & 0xF]);
write(1, LEDD[(A >> 12) & 0xF]);
}
2.
1. ³ , .
2.
void Static(unsigned int A) | ³ |
void StaticL(unsigned char A) | ³ |
void StaticH(unsigned char A) | ³ |
void ClearStatic() | ( 1..4 0xFF) |
3. main() , 0x1234.
4. ³ . .
3.
1. C?
2. ?
3. ?
4. ?
4
:
: , , switch, .
1.
1.1.
, C .
{
[ , , ]
...
[];
...
}
. , , . , {}, i .
|
|
void main()
{
int a, b;
char i;
...
if (...)
{
int c, i;
...
}
...
};
1.2.
. :
if () 1; else [2;]
. , 1, 2. . :
if (a>b)
max = a;
Else
max = b;
! = == . , if (x=0) C, 䳿 x , . ==
if (x==0)
...
. . :
if (key!=255)
{
if (key==0)
{
ProcessKey(Readkey());
}
Else
ProcessKey(key);
};
1.3. switch
switch , . :
switch ()
{
[]
[ case _1:][1]
[ case _2:][2]
...
[ case _N:][N]
[ default: []]
}
, switch C, . ҳ switch case . switch . default, . , break.
switch .
1. , switch.
2. .
3. , .
4. , break.
5. , , default.
switch
Key = Readkey();
switch (Key)
{
case 10:
GoBack();
break;
case 11:
Execute();
break;
default:
UpdateCommand(Key);
}
1.4.
P3.2 (INT0) P3.3 (INT1) . ³ INT0 INT1 . :
if (INT0)
LED(0);
Else
LED(1);
3.
1. ?
2. C ?
3. ?
4. ?
5. switch?
6. switch?
7. default?
8. break?
9. .
5
: . .
: for, while, do while, break continue. .
1
. C for, while, do while. 䳿 goto.
? goto , . , , , . , , .
for
for . for . . . (), , . , . .
|
|
ҳ . , {}. 䳿 , . .
for . write . i 1 8. ϳ , i 9, , (i!=9), ( 0) .
for (i=1; i!=9; i++)
write(i, 0xff);
-, . , break. :
for (;;)
{
...
if (a>b) break;
...
}
. :
for (i=0,j=last;i<=j;i++,j--)
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
:
for (j=0; a[j]!=0; j++);
while
while . while . . , , . ҳ . , 32768:
while (sum<32768)
sum += GetADC(0);
, :
while ((sum+=GetADC(0))<32768)
. "" , break. :
while (1)
{
...
if (errCode!=0) break;
...
}
䳿 . T0:
while (!T0);
do-while
for while, do... while . , . do, , while . ϳ , , . , .
Key:
Do
Key = ReadKey();
while (Key!=255)
break continue
break continue . break . continue . while do... while continue . for .
, 䒺 :
for (i=0; i!=last; i++)
{
if (a[i]<0) continue;
s += a[i];
}
:
for (i=0; i!=last; i++)
if (a[i]>=0)
s += a[i];
1.5.
ҳ . . :
unsigned char i, j;
for (i=0; i!=255; i++)
for (j=0; j!=255; j++);