If you can't read please download the document
Upload
voduong
View
354
Download
8
Embed Size (px)
Citation preview
AVR - Chapter 5
I/O (GPIO)
I/0
ATmega128 I/O
I/O PORT
8/// I/O (PORT A ~PORT F) 6
5/// I/O (PORT G) 1
I/O PORT
Read-modify-Write
40mA
(Pull-up resistor)
I/O (GPIO)
I/O
3 I/O(DDRx, PORTx, PINx)
.
DDRA(Data Direction Register)
PORT A /
SET(1) CLEAR(0)
I/O (GPIO)
PORTA(Data Register)
PINA(Input Pins Address)
I/O (GPIO)
SFIOR(Special Function I/O Register)
Bit2. PUD(Pull-up Disable)
1= Disable / 0=Enable
PORT /
PORT
DDRx 1
PORTx 1
1
PORTx 0
0 .
I/O
I/O I/O
.
PORT A
PORT B
/
SPI
PORT C
I/O
PORT D
/
USART1
TWI
PORT E
USART0
I/O
PORT F
A/D
JTAG
PORT G
/
PORT void port_init(void){
PORTA = 0x00;DDRA = 0x00; // PORTB = 0x00;DDRB = 0xFF; // PORTC = 0x00; //m103 output onlyDDRC = 0x00;PORTD = 0xFF; //PORTD 1 DDRD = 0x00;PORTE = 0x00;DDRE = 0x00;PORTF = 0x0F; // (4~7) , (0~3) DDRF = 0x0F; // (4~7) 0, (0~3) 1 PORTG = 0x00;DDRG = 0x00;
}
void ktm128_init(void)
{
EX_SS_DATA=0x00;
EX_SS_SEL=0x00;
EX_DM_SEL=0x00;
EX_DM_DATA=0x00;
EX_LED=0x00;
EX_STEPPING=0x00;
}
.
-1#ifndef __KTM128__H__
#define __KTM128__H__
#define EX_LCD_DATA (*(volatile unsigned char *)0x8000)
#define EX_LCD_CONTROL (*(volatile unsigned char *)0x8001)
#define EX_SS_DATA (*(volatile unsigned char *)0x8002)
#define EX_SS_SEL (*(volatile unsigned char *)0x8003)
#define EX_DM_SEL (*(volatile unsigned int *)0x8004)
#define EX_DM_DATA (*(volatile unsigned int *)0x8006)
#define EX_LED (*(volatile unsigned char *)0x8008)
#define EX_STEPPING (*(volatile unsigned char *)0x8009)
#define EX_DCMOTOR (*(volatile unsigned char *)0x800A)
#define EX_SERVO (*(volatile unsigned char *)0x800B)
-2void ktm128_init(void)
{
EX_SS_DATA=0x00;
EX_SS_SEL=0x00;
EX_DM_SEL=0x00;
EX_DM_DATA=0x00;
EX_LED=0x00;
EX_STEPPING=0x00;
}
void s_delay(int cnt){
int i, j;
for(i=0; i < cnt;i++){
for(j=0; j < 265; j++)
;
}
}
-3void delay(int cnt){
int i, j;
for(i=0; i < cnt;i++){
for(j=0; j < 1000; j++)
;
}
}
void l_delay(int cnt){
int i, j;
for(i=0; i < cnt;i++){
for(j=0; j < 2650; j++)
;
}
}
#endif __KTM128__H__
-4
KTM128.h .
ICC AVR include
()
ICCAVR 6.0 : C:\icc\include
ICCAVR 7.0 : C:\iccv7avr\include
#include .
LED
LED( )
,
.
LED
LED (Vd) 0.7[V]
.
LED 13.03[mA] .
PORTD LED
LED
1 LED .
PORTD LED
PORTD LED
void main(void)
{
init_devices();
while(1){
PORTD = 0x80;
delay(100);
PORTD = 0x40;
delay(100);
}
}
4 x 4 LED
4 x 4 LED .(0 )
A733YC Low Active
4 x 4 LED
LED
PORTx 0
PORTx 1
PORTx 2
PORTx 3
PORTx 4
PORTx 5
PORTx 6
PORTx 7
4 x 4 LED
LED
PORTx 0
PORTx 1
PORTx 2
PORTx 3
PORTx 4
PORTx 5
PORTx 6
PORTx 7
LED
4 x 4 LED
LED
PORTx 0
PORTx 1
PORTx 2
PORTx 3
PORTx 4
PORTx 5
PORTx 6
PORTx 7
1
0
1
1
1 0 1 1
LED
4 x 4 LED
1 1 0 1 1 1 0 1
D D0x
0b
4 x 4 LED
LED (Low Active )
PORTx 0
PORTx 1
PORTx 2
PORTx 3
PORTx 4
PORTx 5
PORTx 6
PORTx 7
0
1
0
0
0 1 0 0
LED
4 x 4 LED
0 0 1 0 0 0 1 0
2 20x
0b
4 x 4 LED
void main(void)
{
init_devices();
while(1){
PORTF = 0xDD;
}
}
FND
7-Segment LED
7-segment Display
Multi- Segmented Display
FND
FND
FND (1)
FND (2)
PORTF.4
PORTF.3
PORTF.2
PORTF.1
PORTF.0
PORTF.5
PORTF.6PORTF.7
Vcc
Gnd
FND
FND
const char digit[] =
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7c,0x07,0x
7f,0x67};
void dis(unsigned char num)
{
PORTF =~digit[num]; //NOT
}
FND
FND
FND
const char digit[] =
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7c,0x07,0x
7f,0x67};
void runSevenSegment(char i,char num)
{
EX_SS_SEL = 0x0f;
EX_SS_DATA = digit[num];
EX_SS_SEL = ~(0x01
10X10, LED : 100
LED
Data
Sel1
0
.
Sel2 0
.
10
.
0x1C6
() : 0x8004 (Write )
Sel[0]Sel[1]Sel[2]Sel[3]Sel[4]Sel[5]Sel[6]Sel[7]
01234567
() : 0x8005 (Write )
Sel[8]Sel[9]
01234567
Sel[n] n (H:Enable, L:Disable)
() : 0x8006 (Write )
Data[0]Data[1]Data[2]Data[3]Data[4]Data[5]Data[6]Data[7]
01234567
() : 0x8007 (Write )
Data[n] (H:On L:Off)
Data[8]Data[9]
01234567
0x8006, 0x8004 Integer Pointer 10bit .
#define DM_SEL (*(volatile unsigned int *)0x8004)#define DM_DATA (*(volatile unsigned int *)0x8006)
Data 0x8006 unsigned int
Sel 0x8004 unsigned int
.
volatile
.
//
int dm_data[10] = {0x000, 0x0cc, 0x132, 0x201, 0x205, 0x10a, 0x084, 0x048, 0x030, 0x000};
int dmi=0; //
//
void runDotMatrix(void)
{
EX_DM_SEL = 0;
EX_DM_DATA = dm_data[dmi]; // dmi
EX_DM_SEL = 1
void main(void)
{
while(1){
runDotMatrix();
delay(10);
}
}
1
.
( )
Data
( )
( )
( )
( )
( )
( )
( )
( )
( )
0x001
0x002
0x004
0x008
0x010
0x020
0x040
0x080
0x100
0x200
Sel
2
LED .
-> -> -> ->
.
.
.
(KD-128)
(KT-M128)
1 .
0 0 0 0 0 0 1 0
0 20x
0b
void main(void)
{
volatile unsigned char i, count=0;
volatile unsigned char *sw_in;
volatile unsigned char *led_out;
init_devices();
sw_in=(volatile unsigned char *)0x36;
led_out=(volatile unsigned char *)0x8008;
printf("\n\r\n\nKT-M128 V01\n\r");
printf("Trainning Board Lab5 Example.\n\r");
while(1){
*led_out = *sw_in;
delay(200);
printf("count : %d\n\r", count);
count++;
}
}
1 LED , 1
.
.
.
.
,
while ,
if(PINB & 0b00010000){
while(PINB & 0b00010000)
;
//
}
1 ,
.
while
if(PINB & 0b00010000){
//
while(PINB & 0b00010000)
;
}
4 x 4 ()
4 x 4 ()
PINx 0
PINx 1
PINx 2
PINx 3
PINx 4
PINx 5
PINx 6
PINx 7
4 x 4 ()
PINx 0
PINx 1
PINx 2
PINx 3
PINx 4
PINx 5
PINx 6
PINx 7
1
0
1
1
1 0 1 1
4 x 4 ()
PINx 0
PINx 1
PINx 2
PINx 3
PINx 4
PINx 5
PINx 6
PINx 7
1
0
1
1
1 0 1 1
1. 4 .
4 x 4 ()
PINx 0
PINx 1
PINx 2
PINx 3
PINx 4
PINx 5
PINx 6
PINx 7
1
0
1
1
1 0 1 1
2. 4
4 x 4 () unsigned char PORTC_KEY(){
unsigned char left, right, result;
DDRC = 0x0F;
PORTC = 0x0F;
left = PINC;
DDRC = 0xF0;
PORTC = 0xF0;
right = PINC;
result = left | right;
return result;
}
() LED ,
LED LED
. (KD-128)
() 2 x 2
, FND 22 .(KT-M128)