1.32k likes | 1.94k Views
????? : ????? ????? ?? www.atmel.com???? ???? ?????????????? AVR - ???????? ?? - ???? ???? ????? ?? ? ..?????????????? AVR ? ????????? ???? - ???????? ?? - ???? ?? ???????? ??????? : CodeVisionAVRProteusAVR Studio. ???? ??? : 5 ????????? : 3 ????????? : 3 ????????? ??? : 10 ????. Introdu
E N D
1. AVR Microcontrollers Firsrt Session
2. ????? :
????? ????? ?? www.atmel.com
???? ???? ?????????????? AVR - ???????? ?? - ???? ???? ????? ?? ? ..
?????????????? AVR ? ????????? ???? - ???????? ?? - ???? ?? ?????
??? ??????? :
CodeVisionAVR
Proteus
AVR Studio
3. ???? ??? : 5 ????
????? : 3 ????
????? : 3 ????
????? ??? : 10 ????
4. Introduction ATMEL
8 bit Microcontroller
Families
Tiny
90S (CLASSIC)
Mega
LCD AVR
Execute One Instruction Per Cycle (MIPS)
5. ???????? ????? ??? ?????
6. ??? ??????????? ?? ?? ?? ?? ???? ????? ??? ??? ATMEGA 16
ATMEGA 8
ATTINY 28L
ATTINY 28 V
???? ??? ??? ? ??? ????? ? ???? ??? ????? flash ?? ???? ?? ???.
???? ??? ???? low power ? very low power ???. ?? ?? ???? ??? ??? ???? ??? ???:
8. PDIP-Package Mega32
9. TQFP/MLF Package Mega32
11. Architecture RISC: Reduced Instruction Set Computer ==> AVR
CISC: Complex Instruction Set Computer
12. CPU Structures
14. Memory Types Data
SRAM (Static Random Access Memory)
EEPROM (Electrically Erasable Programmable Read Only Memo)
Program
Flash
15. Program Memory Flash (for mega32)
32K Bytes of In-System Self-Programmable Flash Endurance: 10,000 Write/Erase Cycles
16. Data Memories SRAM (for mega32)
2K Byte Internal SRAM- Register file, I/O Register, Data Address.
EEPROM (for mega32)
1024 Bytes EEPROM Endurance: 100,000 Write/Erase Cycles
17. Program Counter (PC)
Instruction Register (IR)
Instruction Decoder (ID)
18. REGISTER FILES
20. X-Y-Z Registers
22. ALU Arithmetic And Logic Unit
Arithmetic
ADD R1,R2
R1?R1+R2
Logic
AND R1,R2
R1?R1 AND R2
23. ???? ??? ?? ????? ?? ????? ????? ?????? ?? ????? ?? ???????? ?? ?? ?????? ????? ???? ?? ???? ? ?? ??? ??? ????? ????? ??????? ???? ?? ??? ??? ???
24. Clock Source
25. Clock Sources
26. PDIP-Package Mega32
27. External Crystal Oscillator
28. Low Frequency Crystal Oscillator
29. External RC Oscillator
30. Calibrated Internal RC Oscillator
31. External Clock
32. Timer/Counter Oscillator
For AVR microcontrollers with Timer/Counter Oscillator pins (TOSC1 and TOSC2), the crystal is connected directly between the pins. No external capacitors are needed. The Oscillator is optimized for use with a 32.768 kHz watch crystal. Applying an external clock source to TOSC1 is not recommended.
33. Clock Distribution
36. AVR Microcontrollers Second Session
37. SRAM Management
38. SRAM Data Memory (mega32)
39. Programming With
CodevisionAVR
40. Prototype #include <mega32.h>
global definition
main()
{
local variable;
statements;
}
function1()
{
local variable;
Statements;
}
function2()
{
local variable;
Statements;
}
41. Prototype ??? ????? ????? ???? ?? ??? ?????? ????? ???? ???? main ?? ???? ???? ???? ???? ?? ???.
#include <mega32.h>
global definition
main()
{
local variable;
???????? ?? ???? ?? ??? ???? ????
While (1)
{
??????? ? ????? ???? ?????? ?? ????? ???? ?? ????
}
}
42. ????? ?? ???? ?????? ????? ?? ??? ????? ????? ???????? ???? ??? ???? ????? header ???
?? ?????? ?? ????? ?? ??? ???? ???? ???? ?? ???
} ? { ?? ????? ? ?????? ?? ????? ????? ???? ? ?? ???? ???? ???? ?? ???
?? // ?? ( /* ? */) ???? ????? ????? ?? ?????? ??????? ?? ???
43. Data Type Type Size (Bits) Range
bit 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
44. ????? ?? ? ???? ?? ????? ???? ????? ???? ??? ????? ??? ????? ????? ???? ???? ???? ?????
eeprom int temp;
flash int temp=10;
eeprom char str;
float a;
int b=30;
??? ????? ?? ?? ????? ?????? ?? ???? ? ????? ????? ??? ??? ????? ????? ?? ??? ???? ???.
45. Constants flash int x=123;
const char y=a;
#define C 100;
46. ????? ?? ?????? ????? ?????? ?? ?? ??? ? ?? ??? ?? ?? ????? ??? ????? ??? ?? ?? ??? ?????
int str[10];
int i[3]= {2,5,6}
????(string):
?? ????? ???? ?? ???? ???? ?? ???? ??? ??? ?? ?????? ???? ?? ?????.
Char t[5]= temp;
47. bit Data type bit x,y;
x=1;
from R2.0 to R14.7 ? max=104
Set in following menu:
Project/Configure/C Compiler/Code Generation/Bit Variable size
48. Operators + -
* /
% ++
-- =
== ~
! !=
< >
<= >=
& &&
| ||
^ ?
<< >>
-= +=
/= %=
&= *=
^= |=
>>= <<=
53. Variables [<storage modifier>] <type definition> <identifier>;
auto int i;
static char x[10];
register int y;
The volatile modifier must be used in order to prevent a variable to be allocated to registers and to warn the compiler that it may be subject to outside change during evaluation.
volatile int abc;
54. Specifying the SRAM Storage Address for Global Variables int a @0x80;
struct x {
int a;
char c;
} alfa @0x90;
55. Structures [<storage modifier>] struct [<structure tag-name>] {
[<type> <variable-name>[,<variable-name>, ...]];
[<type> [<bitfield-id>]:<width>[,[<bitfield-id>]:<width>, ...]];
...
} [<structure variables>];
struct ram_structure {
char a,b;
int c;
char d[30],e[10];
char *pp;
} sr;
56. Accessing the EEPROM eeprom char beta;
int eeprom *ptr_to_eeprom;
Pointers to the EEPROM always use 16 bits.
57. Including Assembly Language in Your Program while (i--) {
/* Assembly language code sequence */
#asm
nop
nop
#endasm
};
}
58. AVR Microcontrollers 3th Session
59. Decision and loops if else
while
do-while
for
Switch - case
68. ????? ? ??? ???? ????? (???? ??? ?????) ??? ???? ??? ???? ?????
}
????? ????? ??? ????
???????
?????????? ????? ????? ?? ???? ????
{
69. ????:?????? ?? ??????? ?? ??? ????? ?? ?? ???? 3 ?????? long int func( int x)
{
long int s;
s=x*x*x;
return s;
}
70. ?????? ????? ????? ????? ?? ?? ?? ?? ?? ???? ????:
1- ??? ?? ???? main
2- ?? ??? ?? ??????
#include<header>
Void func(void)
{
???????
{
Void main()
{
???????
{
71.
#include<header>
Void func(void);
Void main(void)
{
???????
While(1)
{
???????
}
}
Void func()
{
???????
}
73. Libraries math.h
stdio.h
delay.h
Mega16.h
And
74. PORTS
76. Input= 0 Output=1DDRA.0=0; // PA.0 inputDDRA.5=1; // PA.5 outputDDRC=0xff ; // PORTC
77. Output Latch
78. Input register
79. Example-1 ?????? ?? ?????? ?? ?? ??? ???? B ????? 0x28 ?? ??????.
#include <mega32.h>
void main(void)
{
DDRB=0xFF;
while (1)
{
PORTB=0x28;
};
}
80. Example-2 ????? ??????? ?? ?? ???? ???????? ?? ????? ??? ????? ???? ???:
??? : 15?????
???: 3?????
????: 15?????
-------------------------------------------------------------------
??? ???? : trafficsignal
????? : ?????
????? : ?????
??? : PORTA.0 ???: PORTA.1 ???? : PORTA.2
81. Example-2 Header file : delay.h
void delay_us(unsigned int n);
void delay_ms(unsigned int n);
???? : ????? 100 ?????????? ==< delay_us(100)
???? : ????? 5 ????? == < delay_ms(5000)
82. Example-2 #include <mega32.h>
#include <delay.h>
void traficsignal(void);
void main(void)
{
DDRA=0x07;
while (1)
{
traficsignal();
};
}
void traficsignal(void)
{
PORTA.0=1;PORTA.1=0;PORTA.2=0;
delay_ms(15000);
PORTA.0=0;PORTA.1=1;PORTA.2=0;
delay_ms(3000);
PORTA.0=0;PORTA.1=0;PORTA.2=1;
delay_ms(15000);
}
83. Exercise-1 1- ????? ?????? ?? ?? ???? ???????? ?? ?? ?????? ??????? ???? ?????? ???? ??? ?????? ???.
??? : Gdelay ?????
???: Ydelay ?????
????: Rdelay ?????
-----------------------------------
84. Exercise-1 #include <mega32.h>
void traficsignal(int gdelay,int ydeay,int rdelay);
void main(void)
{
DDRA=0x07;
while (1)
{
traficsignal(x,y,z);
};
}
void traficsignal(int gdelay, int ydelay, int rdelay)
{
PORTA.0=1;PORTA.1=0;PORTA.2=0;
delay_ms(gdelay);
PORTA.0=0;PORTA.1=1;PORTA.2=0;
delay_ms(ydelay);
PORTA.0=0;PORTA.1=0;PORTA.2=1;
delay_ms(rdelay);
}
85. Example-3 ????? ??????? ?? ?? ??? ?? ????? ?? ?? ???? A ?????? ? ?? ??? ???? B ???? ???.
#include <mega32.h>
void in_out_port(void);
void main(void)
{
DDRA=0x00;
DDRB=0xff;
while (1)
{
in_out_port();
};
}
void in_out_port(void)
{
unsigned char x;
x=PINA;
PORTB=x;
}
86. 7segment
87. Common Anode
88. Common Cathode
89. Example-4 ?????? ?? ??????? ?? ????? 0 ?? 9 ?? ?? ??? 7segment ??? ????? ?? ????? ?? ????? ???? ??? .
#include <mega32.h>
#include <delay.h>
unsigned char seven_seg_code(unsigned char num);
void main(void)
{
unsigned char i;
DDRA=0xff;
while (1)
{
for (i=0;i<10;i++)
{
PORTA=seven_seg_code(i);
delay_ms(1000);
}
};
}
90. Coding
91. Example-4 unsigned char seven_seg_code(unsigned char num)
{
unsigned char out;
switch(num)
{
case 0:out=0xc0;break;
case 1:out=0xf9;break;
case 2:out=0xa4;break;
case 3:out=0xb0;break;
case 4:out=0x99;break;
case 5:out=0x92;break;
case 6:out=0x83;break;
case 7:out=0xf8;break;
case 8:out=0x80;break;
case 9:out=0x98;break;
}
return out;
}
92. Exercise 2- ???? ??? ?? ???? ?? 7seg ???? ????? ????? ???? ????.
3- ???? ??? ?? ?? ??????? ?? IC ???? BCD ?? 7segment ? 7447 ????? ???? ????.
4- ?? ??????? ?? 7segment ???? ????? ? IC ? 7448 ?? ??????? 0 ?? 9 ?? ????? ????? 1 ????? ??????.
5- ?????? ?? ??????? ?? ???? ?? ?? ???? B ?????? ? ????? 7seg ????? ???.
6- ?? ??????? 0 ?? 99 ?? ????? ????? 1 ????? ??????
93. Double 7Segment
94. AVR Microcontrollers 4th Session
95. LCD
99. Initialization /* the LCD module is connected to PORTC */
#asm
. equ __lcd_port=0x15
#endasm
/* now you can include the LCD Functions */
#include <lcd. h>
Lcd_init(20);
100. PORT ADDRESS
101. LCD Configuration With CodeWizard
102. lcd.h High Level unsigned char lcd_init(unsigned char lcd_columns)
void lcd_clear(void)
void lcd_gotoxy(unsigned char x, unsigned char y)
void lcd_putchar(char c)
void lcd_puts(char *str)
void lcd_putsf(char flash *str)
103. unsigned char lcd_init(unsigned char lcd_columns)
Example:
lcd_init(16)
104. void lcd_clear(void)
???? ?? ?? ?? ?? ??? ?? ???
void lcd_gotoxy(unsigned char x, unsigned char y)
???? ??? ?? ?? ??? ? ???? ?????? ?? ???
Lcd_gotoxy(4,2)
105. void lcd_putchar(char c)
Example :
lcd_putchar('a');
void lcd_putsf(char flash *str)
lcd_putsf(Hello World");
106. void lcd_puts(char *str)
Example:
sprintf(buffer, tempreture= %d, temp);
lcd_puts(buffer);
107. Example - 7 ?? ??? ??? ???? ???? ??????? a ? ?? ??? ??? ???? ??? ????? CodeVisionAVR ?? ?? ??? ?? LCD 2x16 ????? ????.
#include <mega16.h>
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
#include <delay.h>
void main(void)
{
lcd_init(16);
while (1)
{
lcd_clear();
lcd_gotoxy(5,0);
lcd_putchar('a');
lcd_gotoxy(0,1);
lcd_putsf("CodeVisionAVR");
delay_ms(200);
};
}
108. Example-8 ????? ??????? ?? ????? ???? ???? CodeVision ?? ?? ??? LCD ????? ???.
#include <mega16.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
#include <delay.h>
void main(void)
{
int i;
lcd_init(16);
while (1)
{
for (i=0;i<7;i++)
{
lcd_clear();
lcd_gotoxy(i,0);
lcd_putsf("CodeVision");
delay_ms(400);
}
};
}
109. PULL UP
110. example5 ?????? ?? ??????? ?? ??? ???? ???? A ?? pull up ???? ? ????? ?? ?? ?????? ?????? ????? ??? ?? ?? led ?? ???? ?????.
#include <mega32.h>
void main(void)
{
bit x;
DDRA.5=0;
PORTA.5=1;
DDRA.0=1;
PORTA.0=0;
SFIOR=0x00;
while (1)
{
x=PINA.5;
if (x==0)
PORTA.0=1;
else
PORTA.0=0;
};
}
111. Keypad
116. Initialization void initial_keypad_port()
{
SFIOR=SFIOR&0xFB;
}
117.
unsigned char key, butnum;
int s;
unsigned char keytbl[16]={0xee, 0xed, 0xeb, 0xe7, 0xde, 0xdd, 0xdb, 0xd7, 0xbe, 0xbd, 0xbb, 0xb7, 0x7e, 0x7d, 0x7b, 0x77};
DDRC = 0x0f;
PORTC = 0xf0;
delay_us(5);
key = PINC;
DDRC = 0xf0;
PORTC = 0x0f;
delay_us(5);
key = key | PINC;
118. if (key != 0xff)
{
for (butnum=0; butnum<16; butnum++)
{
if (keytbl[butnum]==key) break;
}
119. Key Press int key_press()
{
if ( (PINA & 0xF0)!=0xF0 )
{
delay_ms(T_DEB);
// repeat
if ( (PINA & 0xF0)!=0xF0 )
return 1; // key press
else
return 0; // parasite
}
else
return 0;
}
120. Find Key int find_key()
{
int code, key;
// pressed key is in ROW_1
PORTA=0b11111110;
delay_ms(50);
if ((PINA&0xf0)!=0xf0) goto find;
// pressed key is in ROW_2
PORTA=0b11111101;
delay_ms(50);
if ((PINA&0xf0)!=0xf0) goto find;
// pressed key is in ROW_3
PORTA=0b11111011;
delay_ms(50);
if ((PINA&0xf0)!=0xf0) goto find;
// pressed key is in ROW_4
PORTA=0b11110111;
delay_ms(50);
if ((PINA&0xf0)!=0xf0) goto find;
121. Find Key // create code
find:
code =
( PINA & 0xF0 ) | ( PORTA & 0x0F ); // COLUMNS value Link With ROWS value
switch(code)
{
case 0xEE : key = R1_C1; break;
case 0xDE : key = R1_C2; break;
case 0xBE : key = R1_C3; break;
case 0x7E : key = R1_C4; break;
case 0xED : key = R2_C1; break;
case 0xDD : key = R2_C2; break;
case 0xBD : key = R2_C3; break;
case 0x7D : key = R2_C4; break;
case 0xEB : key = R3_C1; break;
case 0xDB : key = R3_C2; break;
case 0xBB : key = R3_C3; break;
case 0x7B : key = R3_C4; break;
case 0xE7 : key = R4_C1; break;
case 0xD7 : key = R4_C2; break;
case 0xB7 : key = R4_C3; break;
case 0x77 : key = R4_C4; break;
default : key = NO_PRS; break;
};
PORTA=0xF0;
return key;
}
122. Key Release void wait_release()
{
while ((PINA & 0xF0)!=0xF0);
delay_ms (T_DEB);
// release debounce pass after 20ms
}
123. Read Keypad int read_keypad()
{
int key;
initial_keypad_port();
while ( key_press()== 0);
key = find_key();
wait_release();
return key;
}
124. ?Example-9 ?????? ?? ??????? ?? ???? ?? ?? keypad ?????? ????? 7seg????? ???.
125. #include<mega16.h>
#include<delay.h>
int key();
display(int x);
int a;
main()
{
DDRB=0xff;
a=key();
display(a);
}
126. int key()
{
unsigned char key, butnum;
int s;
unsigned char keytbl[16]={0xee, 0xed, 0xeb, 0xe7, 0xde, 0xdd, 0xdb, 0xd7, 0xbe, 0xbd, 0xbb, 0xb7, 0x7e, 0x7d, 0x7b, 0x77};
DDRC = 0x0f;
PORTC = 0xf0;
delay_us(5);
key = PINC;
DDRC = 0xf0;
PORTC = 0x0f;
delay_us(5);
key = key | PINC;
if (key != 0xff)
{
for (butnum=0; butnum<16; butnum++)
{
if (keytbl[butnum]==key) break;
}
s=butnum;
}
else
s=17;
return s;
}
127. display(int x)
{
char str[16]= {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D, 0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
PORTB=str[x];
}
128. ????? ???LCD ?????? ???? ?? ???? ????? ???? ?? ??? ?? ?? ??? ??
129. Programmer
STK 200/300