Professional Documents
Culture Documents
Najafabad Branch
Computer Department
Ahmad M. Shafiee
شماره صفحات ذکر شده در اسالیدها مربوط به کتاب «برنامهریزی و استفاده از میکروکنترلر »AVR
از انتشارات دانشپژوهان میباشد.
c
Global variables & definitions;
main()
{
local variables;
statements;
}
; .1
.2
// */ /* .3
.4
ANSI C codevision
Type Size (Bits) Range
bit, bool 1 0, 1
char 8 -128 to 127
unsigned char 8 0 to 255
signed char 8 -128 to 127
int 16 -32768 to 32767
short int 16 -32768 to 32767
unsigned int 16 0 to 65535
signed int 16 -32768 to 32767
long int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
signed long int 32 -2147483648 to 2147483647
float 32 ±1.175e-38 to ±3.402e38
double 32 ±1.175e-38 to ±3.402e38
Codevision SRAM
0
Working Registers
20H
I/O Registers
60H(or 100H)
DSTACKEND
Data Stack
Y initial value
60H(or 100H)+Data Stack size
Global Variables
R14 R2
◦
Project/Configure/C Compiler/Code Generation/Bit Variable Size
CPU
،#pragma savereg+
Y
Data Stack
Project/Configure/C Compiler/Code Generation/SRAM/Data Stack Size
Codevision SRAM
SRAM
type name;
int a;
SRAM
type name@address ;
char b@0x01 ;
void main(void) {
unsigned char a=30;
unsigned char b=128;
unsigned int c;
c = a * b; // incorrect result
c = (unsigned int) a * b; }
:FLASH
const
flash
#define
IO
a+b
a-b
a*b
a/b
a%b
0xcc ~(0x33) 1 ~
False !(0x33) NOT
++a a++
a-- --a
b a b = &a
a b = *a *
b
sizeof(char) sizeof()
b a a>b >
b a a<b <
b a a >= b >=
b a a <= b <=
b a a == b ==
b a a != b !=
a=b =
a *= b *=
a /= b /=
a %= b %=
a += b +=
a -= b -=
a &= b AND &=
a ^= b XOR ^=
a |= b OR |=
a >>= b >>=
a <<= b <<=
Comma operator:
evaluates its first operand
discards the result
evaluates the second operand and returns this value
NULL
storing place type *name
codevision
char *p; // SRAM
eeprom int *p1; // EEPROM
flash char *p3; // Flash
EEPROM Flash
SRAM
Tiny ◦
Small ◦
Project/Configure/C Compiler/Memory Model
struct name{
variables of the structure; };
struct tank{
int pressure;
char tanktype; };
struct structure’s name variables;
struct tank x,z;
struct tank{
int pressure;
char tanktype; } x,z;
x.pressure = 39;
z.tanktype = ‘B’;
struct tank{
int pressure;
char tanktype; } x={39,’D’};
struct tank{
int pressure;
char tanktype; } tanks[10];
tanks[3].pressure = 39;
Tanks[7].tanktype = ‘C’;
struct tank{
int pressure;
char tanktype; } x;
struct tank *p;
p = &x;
->
p -> a = 39;
p -> b = ‘A’ ;
storing place struct name{
variables of the structure; };
SRAM
eeprom struct tank{
int pressure;
char tanktype; } x;
union alpha{
unsigned char lsb;
unsigned int word; } data;
data.word = 0x1234;
k = data.lsb; // result: k 0x34
union alpha1{
unsigned char a:1; // bit 0
unsigned char b:4; // bits 0..3
unsigned char c:8; // bits 0..7
} data1;
The enumeration data type can be used in order
to provide mnemonic identifiers for a set of char
or int values.
Enumerations can be stored in SRAM or EEPROM.
Default is SRAM.
void main() {
days_of_week = Saturday; // days_of_week 6
enum
Project/Configure/C Compiler/Code Generation
.1
do/while, while, for
.2
switch, if/else
.3
goto, continue, break
Data Stack codevision
R22 codevision
R31 R30 R23
#define ALFA 0x18
#include <math.h>
#pragma savereg-
#pragma regalloc+
#pragma regalloc- SRAM
#pragma promotechar+ int char
#pragma promotechar- int char
#pragma uchar+ unsigned char char
#pragma uchar- unsigned char char
#pragma used+
#pragma used-
BCD
char getchar(void)
void putchar(char c) c
void puts(char *str) str SRAM
null
void putsf(char *str) str flash
null
void printf(char flash stdout
*fmtstr [arg0, arg1, …])
void scanf(char flash stdin
*fmtstr [arg0 address,
arg1 address, …])
UART
LCD
LCD AVR
RS (pin 4) bit 0
RD (pin 5) bit 1
EN (pin 6) bit 2
DB4 (pin 11) bit 4
DB5 (pin 12) bit 5
DB6 (pin 13) bit 6 #asm(.equ __lcd_port=0x15)
DB7 (pin 14) bit 7 // PORTC
LCD