Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ECED3204: Microprocessor
Part V--Communication and Interface
Jason J. Gu
Department of Electrical and Computer Engineering
Dalhousie University
Outline
1
11/3/2015
Outline
2
11/3/2015
USART interface
Primarily designed to transfer data in an asynchronous
mode that utilizes the industrial standard TIA-232
protocol, established in 1960 (originally called RS-232,
established 1960)
Uses two (asynchronous mode) or three (synchronous
mode) wires
3
11/3/2015
4
11/3/2015
5
11/3/2015
6
11/3/2015
7
11/3/2015
8
11/3/2015
9
11/3/2015
10
11/3/2015
MEGA 644A
RXD0/PD0,TXD0/PD1
11
11/3/2015
12
11/3/2015
13
11/3/2015
Controlled by registers
UCSRnA, UCSRnB, and UCSRnC: Mega device
CTRLA, CTRLB, and CTRLC: Xmega device (skip)
14
11/3/2015
15
11/3/2015
USART initialization
Load appropriate values into the baud rate registers
and three control registers
Set the directions of USART-related pins
16
11/3/2015
17
11/3/2015
18
11/3/2015
19
11/3/2015
20
11/3/2015
21
11/3/2015
22
11/3/2015
Outline
23
11/3/2015
24
11/3/2015
SPI-Related Registers
SPI-Related Registers
25
11/3/2015
SPI-Related Registers
SPI-Related Registers
26
11/3/2015
Notes: 1. Mega device uses CPOL and CPHA bits to select SPI transfer mode
whereas the XMega device uses MODE[1:0] bits to set SPI transfer
mode.
2. Group configuration applies to XMega device only.
27
11/3/2015
28
11/3/2015
29
11/3/2015
Void initSPI (void)
{
char tmp;
SPCR =1<<SPE | 1<< MSTR | 1<< CPOL | 1<< SPR0;
SPSR =1<<SP12X;
tmp =SPDR;
DDRB =1<<PB0 | 1<<PB1 | 1<<PB2;
}
30
11/3/2015
; SPI master calls this subroutine to send a byte to the SPI slave
putcSPI_master:
out SPDR,r16
waitTX: in r20,SPSR
sbrs r20,SPIF ;wait until SPIF is set
rjmp wairTX
in r20,SPDR ;clear the SPIF by reading SPDR
ret
; SPI master calls this subroutine to get a byte from the SPI slave
getcSPI_master:
out SPDR,r16
waitRX: in r20,SPSR
sbrs r20,SPIF ;wait until SPIF is set
rjmp waitRX
in r22,SPDR ;fetch data and clear the SPIF
ret
31
11/3/2015
; SPI slave calls this subroutine to send a byte to the SPI master
putcSPI_slave:
out SPDR,r16
waitTX: in r20,SPSR
sbrs r20,SPIF ;wait until SPIF is set
rjmp wairTX
in r22,SPDR ;clear the SPIF by reading SPDR
ret
; SPI slave calls this subroutine to receive a byte from the SPI master
getcSPI_slave:
waitRX: in r20,SPSR
sbrs r20,SPIF ;wait until SPIF is set
rjmp wairRX
in r22,SPDR ;clear the SPIF by reading SPDR
ret
32
11/3/2015
33
11/3/2015
SPI-Compatible Chips
34
11/3/2015
35
11/3/2015
36
11/3/2015
37
11/3/2015
38
11/3/2015
Example:
Set up the circuit connection between AVR Mega and
TC72 for digital temperature reading, write a c function
to read the temperature every 100 ms.
f_PER is assumed as 16 MHz.
MEGA644 PIN
assignment
PB4:SS
PB5:MOSI
PB6:MISO
PB7:SCK
39
11/3/2015
40
11/3/2015
41
11/3/2015
42
11/3/2015
43
11/3/2015
44
11/3/2015
45
11/3/2015
46
11/3/2015
47
11/3/2015
UBRR0 =0;
DDRE =0x06;
UCSR0B =1<<RXEN0|1<<TXEN0;
UCSR0C =1<<UMSEL01 |UMSEL00 |
0<<UCPHA0 | 0<<UCPOL0;
UBRR0 =baud;
}
48
11/3/2015
49
11/3/2015
50
11/3/2015
Operation of
MC14489
51
11/3/2015
23456
34567
45678
56789
67890
78901
89012
90123
01234
52
11/3/2015
Example: 0x01,0x00,0x00,0x01,0x00,0x00,0x45
53
11/3/2015
Operation of
MC14489
54
11/3/2015
Outline
55
11/3/2015
Outline
56
11/3/2015
57
11/3/2015
58
11/3/2015
59
11/3/2015
60
11/3/2015
61
11/3/2015
Handshaking
Clock synchronization: can be used as a handshaking
in data transfer
Data transfer format
Related to the slave addressing method
Seven-bit addressing: directly implemented in AVR’s
hardware
10-bit addressing: requires the software intervention in
addition to hardware support
62
11/3/2015
63
11/3/2015
64
11/3/2015
Characteristics
Master and slave operations supported
Implements slew-rate limited output drivers
Provides noise suppression circuitry
Address recognition feature
65
11/3/2015
66
11/3/2015
67
11/3/2015
68
11/3/2015
69
11/3/2015
70
11/3/2015
71
11/3/2015
72
11/3/2015
73
11/3/2015
74
11/3/2015
75
11/3/2015
; ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
; This subroutine writes a block of bytes into the AT24C08B. The control byte with block
; number, starting address to write, the number of bytes to write, and the pointer to the data
; block in data memory to write are passed in r16, r17, r18, and Z.
; ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
writeBlockEE:
call startTWI ; generate a START condition
call sendByteTWI ; send out control byte to EEPROM
mov r16,r17 ; send out address of the starting location to
call sendByteTWI ; written
wloop: ld r16,Z+ ; fetch a byte to be written
rcall sendByteTWI ; send to EEPROM
dec r18 ; decrement byte count
brne wloop
rcall stopTWI ; generate a STOP condition
call writePoll ; wait for internal write to complete
ret
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// This function performs write poll to make sure that EEPROM internal write operation is
complete.
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
WritePoll: call startTWI ;generate start condition
call sendByteTWI ; send out control byte+page number + R/W bit
lds r21,TWSR ;check status register
andi r21,0xF8 ;mask out low three bit
cpi r21,0x18 ; has AVR received ACK? See table 15.1
breq wc
jmp Writepoll ;EEPROM returned NACK, continue to poll
Wc: ret
76
11/3/2015
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// This function writes a block of data into the EEPROM. The control byte, the starting
// address in EEPROM, number of bytes to write, and the pointer to the data memory
// that holds the data to be written are passed to this function.
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
void writeByteEE(unsigned char SLA, unsigned char addr, unsigned char bytes, char *ptr)
{
startTWI();// generate START condition
sendByteTWI(SLA); // send out SLA + page number + /W
sendByteTWI(addr); // send out memory address to be written
while (bytes){
sendByteTWI(*ptr++); // send out data byte and move pointer
bytes‐‐;
}
stopTWI(); // generate STOP condition
writePoll(SLA); // wait until internal write operation is completed
}
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// This function uses ACK polling to make sure that EEPROM internal write operation is complete.
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
void writePoll (unsigned char SLA)
{
char busy = 1;
while(busy){ // wait while EEPROM returns ACK
startTWI();
sendByteTWI(SLA);
if ((TWSR & 0xF8) == 0x18)
busy = 0;
}
}
77
11/3/2015
78
11/3/2015
79
11/3/2015
80
11/3/2015
81
11/3/2015
82
11/3/2015
83
11/3/2015
Operation
Positive conversion
Truncate the lowest 4 bits.
divide the upper 12 bit by 16
Negative conversion
compute the two’s complement of the conversion results
Truncate the lowest 4 bits
divide the upper 12 bit by 16
84
11/3/2015
Figure 15.26 Typical circuit connection between the Mega2560 and DS1631A
85
11/3/2015
Example: Use the interrupt‐driven approach
and write a C function to set up the TH or the
TL value, the prototype of this C function is
void setupTHoTL(char val_hi, char val_lo char
choice), where val_hi and val_lo are the high
byte and low byte to be written into the
DS1631A and choice specifies whether TH
(choice == 1) or TL (choice == 0) should be set
up. The circuit connection of the DS1631A is
shown in Figure 15.26.
Electrical and Computer Engineering
Dalhousie University 172
86
11/3/2015
Complete C code
87
11/3/2015
Outline
88
11/3/2015
89
11/3/2015
90
11/3/2015
91
11/3/2015
92
11/3/2015
93
11/3/2015
94
11/3/2015
95
11/3/2015
96
11/3/2015
97
11/3/2015
98
11/3/2015
99
11/3/2015
100
11/3/2015
101
11/3/2015
102
11/3/2015
Solution: The assembly subroutine that performs the specified setting is as follows:
initADC: ldi r20,0x21 ; select ADC1 as input, result left‐justified
sts ADMUX,r20 ; "
lds r20,DDRF ; configure ADC1/PF1 pin for input
andi r20,0xFD ; “
sts DDRF,r20 ; “
ldi r20,0x06 ; use Timer1 overflow as trigger source
sts ADCSRB,r20 ; "
ldi r20,0xA7 ; enable ADC, enable auto triggering, set ADC clock
sts ADCSRA,r20; prescaler to 128
ret
The C language version of the function is as follows:
void initADC(void)
{
ADMUX = 0x21; // select ADC as input, result left‐justified
DDRF &= 0xFD; // configure PF1/ADC1 pin for input
ADCSRB = 0x06; // select Timer1 overflow as trigger source
ADCSRA = 0xA7; // enable ADC, auto‐triggering, set prescaler to 128
103
11/3/2015
104
11/3/2015
105
11/3/2015
106
11/3/2015
Example
107
11/3/2015
108
11/3/2015
Example
109