单片机第三章

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

To read the value from a port pin configured as input, the following code could be used. unsigned char new_PORTB; //new values of //PORTB : : : new_PORTB = PINB; //read PORTB
Port C Data Direction Register – DDRC
Port C Input Pins Address – PINC
Port D Data Register – PORTD
Port D Data Direction Register – DDRD
Port D Input Pins Address – PIND
output
Pull-Up
DB
input
Pull-Up
DB
Pull-Up
DB
General digital I/O
summarizes the control signals for the pin value.
AVR I/O status conversion
DDRx PORTx 0 1
//set PORTA[7:2] as output, PORTA[1:0] //as input (1111_1100) PORTA=0x03; //initialize PORTA[7:2] low, PORTA[1:0] //current source DDRB=0xf0; //PORTB[7:4] as output, set PORTB[3:0] //as input PORTB=0x00; //disable PORTB pull-up resistors DDRC=0xff; //set PORTC as output PORTC=0x00; //initialize low DDRD=0xff; //set PORTD as output PORTD=0x00; //initialize low }
R3
560
R4
560
R5
560
R6
560
R7
560
R8
560
R9
560
R10
560
D1 R2
5.1k
D2
D3
D4
D5
D6
D7
D8
LED-YELLOW LED-YELLOW LED-YELLOW LED-YELLOW LED-YELLOW LED-YELLOW LED-YELLOW LED-YELLOW
U1 C1
25pF 9 12 13 40 39 38 37 36 35 34 33 1 2 3 4 5 6 7 8 RESET XTAL1 XTAL2 PA0/ADC0 PA1/ADC1 PA2/ADC2 PA3/ADC3 PA4/ADC4 PA5/ADC5 PA6/ADC6 PA7/ADC7 PB0/T0/XCK PB1/T1 PB2/AIN0/INT2 PB3/AIN1/OC0 PB4/SS PB5/MOSI PB6/MISO PB7/SCK ATMEGA16 PC0/SCL PC1/SDA PC2/TCK PC3/TMS PC4/TDO PC5/TDI PC6/TOSC1 PC7/TOSC2 PD0/RXD PD1/TXD PD2/INT0 PD3/INT1 PD4/OC1B PD5/OC1A PD6/ICP1 PD7/OC2 22 23 24 25 26 27 28 29 14 15 16 17 18 19 20 21
Common Anode
Common Cathode
Display Character
CC
CA
Display Character
CC
CA
0 1 2 3 4 5 6 7 8 9 A b
0x3F 0x06 0x5B 0x4F 0x 66 0x6D 0x7D 0x07 0x7F 0x6F 0x77 0x7C
N
DISPLAY
○ LED(Light Emitting Diode) Display; ○LCD(Liquid Crystal Display) Display; ○CRT(Cathode Ray Tube) Display.
LED-DISPLAY
7-Segment Display
Common Anode Common Cathode
Directly drive LED
Key scan
Independent keys
Anti-vibration
Software-delay (ms) Hardware
Press key
Key scan N key pressed? Y delay(ms) N Really pressed? Y get key value
Input (Pull-up)
DDRx PORTx 0 0 DDRx PORTx 1 1
Tri-state
Output high
Output low
DDRx PORTx 1 0
II. I/O Register
Port A Data Register –PORTA
Port A Data Direction Register – DDRA
Note the variable used to read the value from the input pins is declared as an unsigned char because both the port and this variable type are 8 bits wide.
0xC0 0xF9 0xA4 0xB0 0x99 0x92 0x82 0xF8 0x80 0x90 0x88 0x83
c D E F P U I Y H L clear
0x39 0x5E 0x79 0x71 0x73 0x3E 0x31 0x6E 0x76 0x38
0xC6 0xA1 0x86 0x8E 0x8C 0xC1 0xCE 0x91 0x89 0xC7 0xFF
loose Y Return
N
//key scan void key_scan(void) { unsigned char key; DDRC= 0x00; PORTC|= 0x07; if ((PINC&0x07)!=0x07) //detect C PIN , IF PINC==0x07, no key //pressed, then exit { DelayMs(20); //anti-vibration if ((PINC&0x07)!=0x07) //detect again { key = PINC; while ((PINC&0x07)!=0x07) ; //wait until loose the key } } }
Port A Input Pins Address – PINA
Port B Data Register – PORTB
Port B Data Direction Register – DDRB
Port B Input Pins Address – PINB
Port C Data Register – PORTC
Each output buffer has symmetrical drive characteristics with both high sink and source capability. The pin driver is strong enough to drive LED displays directly. All port pins have individually selectable pull-up resistors with a supply-voltage invariant resistance.
X1
CRYSTAL
R1
1M
C2
25pF
AREF AVCC
32 30
R11
1k
R12 R13 1k
1k
Matrix keyboard
Key scan N key pressed? Y delay(ms) N Really pressed? Y get key value
loose Y Return
All AVR ports have true Read-Modify-Write functionality when used as general digital I/O ports. This means that the direction of one port pin can be changed without unintentionally changing the direction of any other pin with the Set the pin as Logic One and Clear the pin as Logic Zero instructions. The same applies when changing drive value (if configured as output) or enabling/disabling of pull-up resistors (if configured as input).
III. General I/O Interfaces Applications
Port pins are usually configured at the beginning of a program for either input or output, and their initial values are then set. Usually, all eight pins for a given port are configured simultaneously. A code example is provided below to show how ports are configured.
{ DDRA=0xfc;
Note that because we are using the C programming language with a compiler include file, the register contents are simply referred to by name. Note that the data direction register (DDRx) is first used to set the pins as either input or output, and then the data register (PORTx) is used to set the initial value of the output port pins.
Ch3 I/O System
wk.baidu.com
I/O Interfaces Architectures I/O Register General I/O Interfaces Applications
I. I/O Interfaces Architectures
The Atmel ATmega16 is equipped with four 8-bit general-purpose, digital I/O ports designated PORTA, PORTB, PORTC, and PORTD. All of these ports also have alternate functions, which will be described later. In this section, we concentrate on the basic digital I/O port features.
0x00
Static Display
Display 0-9:
e.g. 1. press k1– increase 1; press k2- decrease 1;
U1
9 12 13 40 39 38 37 36 35 34 33 RESET XTAL1 XTAL2 PA0/ADC0 PA1/ADC1 PA2/ADC2 PA3/ADC3 PA4/ADC4 PA5/ADC5 PA6/ADC6 PA7/ADC7 PB0/T0/XCK PB1/T1 PB2/AIN0/INT2 PB3/AIN1/OC0 PB4/SS PB5/MOSI PB6/MISO PB7/SCK ATMEGA16 PC0/SCL PC1/SDA PC2/TCK PC3/TMS PC4/TDO PC5/TDI PC6/TOSC1 PC7/TOSC2 PD0/RXD PD1/TXD PD2/INT0 PD3/INT1 PD4/OC1B PD5/OC1A PD6/ICP1 PD7/OC2 22 23 24 25 26 27 28 29 14 15 16 17 18 19 20 21
相关文档
最新文档