You are on page 1of 143

()

5
2
2
3
10


AVR
AVR :
10 AVR :
AVR : ()
11 AVR : ()
CodeVision AVR : ()
AVR C : ()



.
MHz
.


.

4004 1971
2300 200 .

Z80
Z80

zilog 80
.
.


Single 5 Volt power supply
4 MHz (Z80A)
6 MHz (Z80B)
8 MHz (Z80H)

.
.




..

CPU
I /O
RAM
ROM
TIMER
Interrupt

...

MCS-51
PIC
AVR
ARM
Freescale

Intel MCS-51
. 1980

8-bit ALU, Accumulator and 8-bit Registers; hence it is an 8-bit microcontroller

8-bit data bus It can access 8 bits of data in one operation


16-bit address bus It can access 216 memory locations 64 KB (65536
locations) each of RAM and ROM
On-chip RAM 128 bytes (data memory)
On-chip ROM 4 Kbyte (program memory)
Four byte bi-directional input/output port
UART (serial port)
Two 16-bit Counter/timers
Two-level interrupt priority
Power saving mode (on some derivatives)

PIC microcontroller
.

)core(
Baseline Core 12
Mid-Range Core 14
High End Core 16

PIC

ATMEL AVR
ATMEL .

8 64...
32 816

AVR

tinyAVR the ATtiny series

megaAVR the ATmega series

megaAVRs with special features not found on the other members of


the AVR family, such as LCD controller, USB controller, advanced PWM,
CAN etc.

FPSLIC (AVR with FPGA)

16384 kB program memory


4464100-pin package (A4, A3, A1)
Extended performance features, such as DMA, "Event System", and
cryptography support.
Extensive peripheral set with DACs

Application-specific AVR

4256 kB program memory


28100-pin package
Extended instruction set (Multiply instructions and instructions for
handling larger program memories)
Extensive peripheral set

XMEGA the ATxmega series

0.58 kB program memory


632-pin package
Limited peripheral set

FPGA 5K to 40K gates


SRAM for the AVR program code, unlike all other AVRs
AVR core can run at up to 50 MHz [5]

32-bit AVRs

AVR

AT90S



AT90S4434 , AT90S8535, AT90S4433, AT90S2323
AT90S1200, AT90S4414, AT90S8515, AT90S2343

Atiny
24 16 8

ADC
ATtiny10, ATtiny12, ATtiny15, ATtiny25, ATtiny2313, ATtiny45

ATmega


...4064
ATmega48, ATmega603, ATmega164, ATmega128,
ATmega1280, ATmega8515
: 8051 ATMEL
ATmega8515


C ,C++
Basic
Pascal

CPU Cycle

.
CPU

1
osillator frequency

machine cycle


Instructions per second
.

thousand instructions per second (KIPS),
million instructions per second (MIPS),
Giga instructions per second (GIPS),
Million Operations per Second (MOPS).



)Complex Instruction Set Compute(CISC
1980 .
.

)Reduced Instruction Set Compute(RISC


1980 .
10 .
c


RAM (Random Access Memory)
ROM(Read Only Memory)
EPROM(Erasable Programmable Read only Memory)
EEPROM(Electrically Erasable Programmable Read-Only Memory)
Flash memory
NOR memories
NAND memories
MultiMedia Card (MMC)
SD memory

AVR
AVR


C
C
AVRGCC
Codevision

)AVRGCC(winavr
.

AVRSTUDIO
Simulator
codevision

codevision





....

ATmega16

AVR
AVR
SMD DIP


ATmega16L
ATmega16
ATmega16A

ATmega16


RISC

131
32 8
16MHz

16k Flash
512 EEPROM
1KRAM

/ 8
/ 16
RTC
PWM
8 10
TWI
USART
SPI
WATCHDOG


ATmega16

ATmega16

ATmega16
Vcc
ATmega16L
V 2.7 V 5.5 ATmega16 V 4.5 V 5.5 .

GND

PA0) Port A) PA7
A .
8 I / O
. Pull-up
. .
reset ) (Tri-state .

PB0 )Port B(PB7:


I / O 8 Pull-up .

reset .

PB0 ()XCK/TO

USART

PB1 ()T1

PB2 ()INT2/AIN0
2 .

OPAMP

PB3 ()OC0/AIN1

. PWM PWM

. OPAMP

PB4 ()SS

PB5 ()MOSI

PB6 ()MISO
PB7 ()SCK

SPI

PC0 )Port C(PC7:


.
)SCL(PC0
TWI
)SDA(PC1
TWI
)TCK(PC2
)TMS(PC3
)TDO(PC4
)TDI(PC5
JTAG
)TOSC1(PC6
RTC 32.768

)TOSC2(PC7
RTC 32.768

0 )Port D(PD7:

)RXD(PD0

)TXD(PD1

)INT0(PD2

)INT1(PD3

)OC1B(PD4

. PWM PWM
)OC1A(PD5

)ICP(PD6
)OC2(PD7


. PWM PWM


. PWM PWM

RESET

()

reset .
XTAL1

.
XTAL2

.

AVCC
A ADC
Vcc . ADC
Vcc

AREF
..

ATmega16



RC
RC


. .

. .


CKSEL3 , CKSEL2 , CKSEL1 , CKSEL0
.

CKSEL3..0 = 1111 ~ 1010



CKSEL3..0 = 1111


12
22


CKSEL3..0 = 1001
32.768
XTAL2XTAL1

RC

RC


8MHz4MHz2MHz 1MHz

5 25
3 10
1MHz
CKSEL3..0



2
CKSEL3..0 ; 0000

ATmega16


Flash
ATmega16
MCUCSR

0x0000 .

:POWER ON RESET : PORF: 0


VPOT PORF


ATmega16
( :)EXTERNAL RESET
EXTRF
MCUCSR .
.
. 10K






ATmega16
:Watchdog
WDRF MCUCSR

:Brown-out Reset Brown-out
BODEN 2/7 4/5 .
BODLEVEL .
BORF MCUCSR .


LED
LED 1
.


.

AVR

AVR
BASCOM
C Winavr Codevision

BASCOM
.
.
:

C
.


...
winavr codevision

c
c


c

c

;
/* */ //


INC


.h


#include

>#include<atmega16.h
>#include<stdio.h







c 5
void double float char Int

c
: int
16
int
.
; int var_test =50
var_test int
50

c
Char
Float 4
Double 8


;Char x=15
0x
;Char x = 0x15
0b
;char x= 0b11100011

;char x*+ = tehran

;char x = s

:

A-Z a-z



31

:


;

:
.
.
#define

/ * - +

if-else


{)(If
1
{
{else
2
{
if
1
2


unsigned char a,b;
If(a > b){
a++;
b+=10;}
else{
a--;
b-=10;

unsigned char zx,sd;


If(zx==sd) zx++;
else sd++;

for



{) ; ; for(
;
}

unsigned char i;
unsigned int w;
for(i=0;i<25;i++){
W+=5;
}

unsigned char I,j,z=2;


for(i=10;i>0;i--){
for(j=0;j<=50;j++){
z*=10;
if(z==140) z=2;
}
}

for(;;){

{

do-while


{do
;
;)( }while

while


{)(while
;
}
;unsigend char a,b
{)while(a>b
;)a=(a/b*2
}

;unsigned char I,sw,y


{do
;i++
;y=sw*i
;)}while(i<10
{do
;
;)}while (1

{)while(1
;
}

switch

default

break

case
.
{)(Switch
: case 1
;
;break
: case 2
;
;break
default:
;
}

x 1 z 10
26 z 20
z 50.
;unsigned char x,z
{)switch(x
;case 1 : z=10
;break
;case 26 : z=20
;break
;default :z=50
}

break




;Break

continue


;Continue

;unsigned char x=1,i,n


{)While(x
;i++
{)if(n==10
;x=0
;continue
}
}

x
.
n
continue

goto
;goto label

goto
loop:
;
;goto loop

c
c main .
.


.


{) (
;
; return


char int ...
void
void

return

:

{)(void display
;
}
{)(void main
;)(display
;
}

>.h <#include
;


ctype.h

1 0

stdlib.h

math.h


string.h


bcd.h

bcd

delay.h


delay.h
) void delay_us(unsigned int n
n 0 65535

) void delay_ms(unsigned int n


n 0 65535


.


DDRx.n

PORTx.n


PINx.n


A .

A .

PC.0 .

50 B

C SYSTEM

;DDRA = 0xFF
;DDRA = 0x00
;DDRC.0 = 0
;DDRB=0xFF
;PORTB=50

;DDRC = 0X00
;SYSTEM = PINC

:

-1 .


-2


SREG

7 SREG .



)#asm(sei ) #asm(cli





....

-1

atmega16 PB2PD3 PD2


.
5 2
6 0
7 1

GICR 7 SREG

GIFR

.

MCUCR
:
:


MCUCR

:
)ISC11 ISC10( 32


)ISC01 ISC00( 10

MCUCSR
2
)ISC2(6

codevision
codevision
{)( ] void [ interrupt

}


}{ )interrupt [2] void ext_int0_isr(void
}{ )interrupt [EXT_INT0] void ext_int0_isr(void

LED 0

#include<mega16.h>
interrupt [EXT_INT0] void ext_int0_isr(void){
while(PIND.2 ==0);
PORTA.0 =!(PORTA.0);
}
Void main(){
PORTA.0 = 0;
DDRA.0 = 1;
GICR|=0x40;
MCUCR=0X02;
GIFR = 0x40;
#asm(sei)
While(1);
}

1 0

#include<mega16.h>
Flash unsiened char number[]={0x3f,0x06,0x4f,0x66
,0x6d,0x7d,0x07,0x7f,0x6f};
Unsigned char i=0;
interrupt [EXT_INT0] void ext_int0_isr(void){
if(i<9) PORTA = number[++i];
while(PIND.2 == 0);
}
interrupt [EXT_INT1] void ext_int1_isr(void){
if(i>0) PORTA = number[--i];
while(PIND.3== 0);
}
Void main(){
PORTA =number[0];
DDRA.=0xFF;
GICR|=0xC0;
MCUCR=0X0A;
GIFR = 0xC0;
#asm(sei)
While(1);
}

codevision
codevision


c

.....

codevision

new

project

yes

generate ,save and exit


. c

project compile


build

toolschip
program


avrstudio
codevision coff

LCD
LCD
LCD

LCD 2*16
LCD 16

LCD
( )
( )

LCD
codevision LCD
LCD
void lcd_clear(void)

clears the LCD and sets the printing character position at row 0 and
column 0.
void lcd_gotoxy(unsigned char x, unsigned char y)

sets the current display position at column x and row y. The row and
column numbering starts from 0.
void lcd_putchar(char c)

displays the character c at the current display position.


void lcd_puts(char *str)

displays at the current display position the string str, located in RAM.
void lcd_putsf(char flash *str)

displays at the current display position the string str, located in FLASH

unsigned char lcd_read_byte(unsigned char addr);

reads a byte from the LCD character generator or


display RAM.
unsigned char lcd_init(unsigned char lcd_columns)

initializes the LCD module, clears the display and


sets the printing character position at row 0 and
column 0. The numbers of columns of the LCD must be
specified (e.g. 16). No cursor is displayed.
The function returns 1 if the LCD module is detected
and 0 if it is not.
This is the first function that must be called before
using the other high level LCD Functions.

void lcd_write_byte(unsigned char addr, unsigned char data);


writes a byte to the LCD character generator or display RAM.

void _lcd_ready(void)

waits until the LCD module is ready to receive


data.
This function must be called prior to writing data
to the LCD with the _lcd_write_data function.
void _lcd_write_data(unsigned char data)

writes the byte data to the LCD instruction


register.
This function may be used for modifying the LCD
configuration

#include<mega16.h>
#asm
.equ__ lcd_port=0x1b
#endasm
#include<lcd.h>
Void main(){
Lcd_init(16);
Lcd_clear();
Lcd_gotoxy(2,0);
Lcd_putsf(LCD TEST);
Lcd_gotoxy(5,1);
Lcd_putsf(atme16);
Lcd_gotoxy(14,0);
Lcd_putchar(*);
Lcd_gotoxy(14,0);
_lcd_ready();
_lcd_write_data(0x0f);
While(1);
}



.

.
.

#include <mega16.h>
#include <delay.h>
#define c1 PINB.4
#define c2 PINB.5
#define c3 PINB.6
#define c4 PINB.7
flash char row[]={0xfe,0xfd,0xfb,0xf7};
flash char data_key[]={'1','2','3','A,'4','5','6','B',
'7','8','9','C,'*','0','#','D'};
char ac,table;
int r;
//_________________________________
void keypad(){
while (1){
for (r=0;r<4;r++){
ac=4;
PORTB=row[r];
DDRB=0x0f;

if (c1==0) ac=0;
if (c2==0) ac=1;
if (c3==0) ac=2;
if (c4==0) ac=3;
if (!(ac==4)){
table=data_key[(r*4)+ac];
while (c1==0){}
while (c2==0){}
while (c3==0){}
while (c4==0){}
delay_ms(50);
}
}
}
}
//_________________________________
void main(){
PORTB=0xff;
DDRB=0x0f;
keypad();
while(1){
};
}


...

)counter(
.


:)Bottom(

. 0x00
0x0000

(:)MAX

0XFF 8 0XFFFF 16

:TOP


:
. )bottom(
0XFF 0XFFFF

8
/ .
( : TCCR0 /)

)CS02CS01 CS00 (210


( )

)WGM01WGM00(:63

(COM01 COM00 )5,4


/
. /

COM01 COM00
CTC

FASTPWM

PHASE CORRECT PWM

TCNT0
/

OCR0
CTC TOP
PWM

TIMSK

10

)TOIE0( 0
)OCIE0(1

TIFR


0 TOV0 .
.
.
/ /
OCF0 .

1KHZ
PA.0 ( 8 )

T=1/F----------T=1000S
500S 500SON OFF

CS02:0

Ftimer = Fosc/N = 8MHz/64 = 0.125MHz


Ttimer = 1/Ftimer = 8S
8

TCNT0
500
62.5 ; 500/8
FF 256
193.5 ; 256 62.5
TCNT0 FF 256

#include<mega16.h>
void main(){
PORTA.0 = 0;
DDRA.0 = 1;
While(1){
TCNT0 = 0xc1;
TCCR0 = 0X03;
While(!(TIFR & 0x01));
TCCR0 =0x00;
PORTA.0=!(PORTA.0);
TIFR |=0X01;}}


#include<mega16.h>
Interrupt [TIM0_OVF] void timer0_ovf_isr(void){
TCCR0=0X00;
TCNT0=0XC1;
PORTA.0=!(PORTA.0);
TCCR0=0X03;}
Void main(){
PORTA.0 = 0;
DDRA.0 = 1;
TCNT0 = 0xC1;
TIMSK = 0x01;
TCCR0 = 0x03;
#asm(sei)
While(1);
}

16
TCCR1A

TCCR1B

WGM11WGM10TCCR1A
WGM13WGM12TCCR1B

1 .

1

OC1BOC1A


COM1B1COM1B0COM1A1COM1A0
.

CS12CS11 CS10
0

1
1 .

TCNT1

TIMSK
2 5

2 TOIE1
1

TIFR
1

TOV1 .
TOV0

LED PC0 . 1
.

Tpulse = 1000000us
Ftime = Fosc/N = 4/256 = 0.015625Mhz
Ttime = 1/Ftimer = 64us
TCNT1 = 65536 1000000/64 = 49911 = 0xc2f7

#include<mega16.h>
Void main(){
PORTC.0 = 0;
DDRC.0 = 1;
TCCR1A = 0X00;
While(1){
TCNT1H = 0xc2;
TCNT1L = 0xf7;
TCCR1B = 0X04;
While(!(TIFR&0X04));
TCCR1B = 0X00;
PORTC.0 = !(PORTC.0);
TIFR |= 0x04;
}
}

1000
BUZZER 5

TCNT1 = 65536 -1000 = 64536 = 0XFC18

#include<mega16.h>
#include<delay.h>
Interrupt [TIM1-OVF] void timer1_ovf_isr(void){
TCCR1B = 0X00;
TCNT1H = 0XFC;
TCNT1L = 0X18;
PORTC.0 = 1;
delay_ms(5000);
PORTC.0 = 0;
TCCR1B = 0x06;
}
Void main(){
PORTC.0 = 0;
DDRC.0 =1;
TCNT1H = 00xfc;
TCNT1L = 0x18;
TIMSK =0x04;
TCCR1A = 0x00;
TCCR1B = 0x06;
#asm(sei)
While(1);
}


:

.
AVR

:
TXD RXD
.
. Baudrate .
... gsm .



: UDR UDR
USART .

(:UCSRA )A

:MPCM .
: U2X
:)Parity Error(PE .
. UDR
:)Data OverRUN(DOR
. .

:)Frame Error(FE
:UDRE UDR
:TXC
:RXC UDR

UCSRB
:TXB8 9 9

:RXB8 9 9

:UCSZ2 UCSZ1,0 UCSRC


:TXEN .PD1 I/O


:RXEN PD0 I/O
:UDIRE UDR (. UDRE
UCSRA )
:TXCIE (. TXC UCSRA 9
::RXCIE (. RXC UCSRA

UCSRC

:UCPOL .
: UCSZ1:0 UCSZ2 .

:USBS STOP ( 0 9

:UPM1:0

::UMSEL 1. 0
:URSEL UCSRC UBRRH .
UCSCR

UBRRHUBBRL
UBBRH UCSRC

URSEL UCSRC UBBRH

141312
12 UBRR11:0


)(Data =getchar
polling
)putchar(a

)putchar(char c

polling
)Void puts(char *str
ram )\0( null

;char name*+=micro
;)puts(name
)void putsf(char flash *str
flash )\0( null
;Flash char chip*+=micro avr
;)Putsf(chip

)Char gets(char *str ,char len



10
;]Char input[10
;)Gets(input,10

printf
.

),,2, 1 Printf(
;)Printf(avr micro

polling 2400 micro


)8 (
UBRR=Fosc/16*Baud =(8000000/(16*2400))-1
207=0xcf
Baudrate=Fosc/(16*UBRR+1) = 2403.84
ERORR = ((2043.84-2400)/2400 )*100 = 0.2%
#include<mega16.h>
#include<stdio.h>
Void main(){
UCSRB=0X08;
UCSRC=0X86;
UBRRL=0XCF;
Printf(micro);
While(1);
}

You might also like