Professional Documents
Culture Documents
اصول میکرو کامپیوترها PDF
اصول میکرو کامپیوترها PDF
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
PIC microcontroller
.
)core(
Baseline Core 12
Mid-Range Core 14
High End Core 16
PIC
ATMEL AVR
ATMEL .
8 64...
32 816
AVR
Application-specific AVR
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 .
.
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 .
PB1 ()T1
PB2 ()INT2/AIN0
2 .
OPAMP
PB3 ()OC0/AIN1
. PWM PWM
. OPAMP
PB4 ()SS
PB5 ()MOSI
PB6 ()MISO
PB7 ()SCK
SPI
)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 = 1001
32.768
XTAL2XTAL1
RC
RC
8MHz4MHz2MHz 1MHz
5 25
3 10
1MHz
CKSEL3..0
2
CKSEL3..0 ; 0000
ATmega16
Flash
ATmega16
MCUCSR
0x0000 .
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;
for
{) ; ; for(
;
}
unsigned char i;
unsigned int w;
for(i=0;i<25;i++){
W+=5;
}
for(;;){
{
do-while
{do
;
;)( }while
while
{)(while
;
}
;unsigend char a,b
{)while(a>b
;)a=(a/b*2
}
{)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
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
.
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
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
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 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
void _lcd_ready(void)
#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 /)
)WGM01WGM00(:63
COM01 COM00
CTC
FASTPWM
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
#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
#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
UCSRC
:UCPOL .
: UCSZ1:0 UCSZ2 .
:USBS STOP ( 0 9
:UPM1:0
::UMSEL 1. 0
:URSEL UCSRC UBRRH .
UCSCR
UBRRHUBBRL
UBBRH UCSRC
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
printf
.
),,2, 1 Printf(
;)Printf(avr micro