You are on page 1of 42

‫‪Islamic Azad University‬‬

‫‪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

60H(or 100H)+Data Stack size+Global Var. size


HSTACKEND
Hardware Stack
SP initial value
_HEAP_START_
Heap
RAMEND
Codevision SRAM

R31 R22 R15 R1 R0 

R14 R2 


Project/Configure/C Compiler/Code Generation/Bit Variable Size

char R31 R16 


int
Codevision SRAM

CPU 

،#pragma savereg+ 
Y

R31 R30 R27 R22 R15 R1 R0 SREG

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

 const type name=value;  const float a=23.2;


 flash type name=value;  flash int b=12;
 #define name value  #define C 100
#define flash const

 Do NOT use memory  Do use memory


 Text substitution  Variable
 Substitute the text in  Read from memory in
preprocessing (compile) execution time
time

 #define NUMBER 5 + 2  const int NUMBER = 5 + 2;


 int x = 4 * NUMBER;  int x = 4 * NUMBER;

 #define NUMBER 5 + 2;  flash int NUMBER = 5 + 2;


 int x = NUMBER + 7;  int x = NUMBER + 7;
EEPROM

eeprom type name;


eeprom float Alpha;


IO

 Register’s name = i;  out Register’s Address, Rn


 i = Register’s name;  in Rn, Register’s Address

 sfrb Register’s name = Register’s address;


 sfrb PORTA = 0x1b;
 sfrw TCNT1 = 0x2c;
codevision

auto do if size of void


case double long static volatile
char else int struct while
break enum register switch inline
const extern return typedef
continue float short union
default For singned unsigned

bit defined eeprom


flash funcused interrupt
sfrw sfrb

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 !=

0b00000000 0b00110101 & Bitwise AND &


0b11001010
0xFF 0b00110101 | Bitwise OR |
0b11001010
oxFF 0b00110101 ^ Bitwise XOR ^
0b11001010
True && True = True 0b00110101 && Logical AND &&
0b11001010
True || True = True 0b00110101 || Logical OR ||
0b11001010
a >> n >>
a << n <<

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

comma acts as separator int a=1, b=2, c=3, i;


i=2 i = (a, b);
i=1 i = a, b;
a=6, i=8 i = (a += 5, a + b);
a=11, i=11 i = a += 5, a + b;
i=11 i = a, b, c;
i=3 i = (a, b, c);
 Conditional operator or inline if (iif):
◦ e1 ? e2 : e3
◦ a = (b > c)? x : y;
 type name[size];
 int a[10];


 Physical address = Base address + Displacement

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;

flash struct tank{ // or const struct tank{


int pressure;
char tanktype; } x;
collections of named members that share
the same memory space in SRAM.
 The space allocated to the union in memory is
equal to the size of the largest member.

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.

enum days {Sunday /* =0 */, Monday, …


,Saturday} days_of_week;

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>

#if condition | #ifdef name | #ifndef name


statement;
[#elseif condition
statement; …]

[#else
statement; …]
#endif
#define
.. pi 3.141592
.
#undef pi
pi

#error macro not define


c
#asm
..
.
assembly
.. statements
.
#endasm
#pragma warn-
#pragma warn+
#pragma opt-
#pragma opt+
#pragma optsize-
#pragma optsize+
#pragma savereg+ R15 R1 R0
R27 R26 R25 R24 R23 R22
SREG R31 R30

#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-

Project/Configure/C Compiler/Code Generation


SRAM

void pokeb(unsigned int addr, addr data


unsigned char data)
void pokew(unsigned int addr, data
unsigned int data) data addr
addr+1
unsigned char peekb(unsigned addr
int addr)
unsigned int peekw(unsigned
int addr) addr
addr+1
void delay_us(unsigned int n) n
void delay_ms(unsigned int n) n

BCD

unsigned char bcd2bin(unsigned char n) BCD


n
unsigned char bin2bcd(unsigned char n) BCD
n
int atoi(char *str) str

long int atol(char *str) str


void itoa(int n, char *str) str n
void ltoa(long int n, char n
*str) str
void ftoa(float n, n
unsigned char decimals, str
char *str)
float atof(char *str) str
int rand(void)
UART

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

void sprintf(char *str, char str


flash *fmtstr [arg0, arg1, …])

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

void _lcd_ready(void) LCD


void lcd_init(unsigned char LCD
lcd_columns)
void lcd_clear(void) LCD
void lcd_gotoxy(unsigned
char x, unsigned char y)
void lcd_putchar(char c) LCD c
void lcd_puts(char *str) LCD SRAM str

void lcd_putsf(char flash *str) LCD flash str

You might also like