Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 5278
Скачиваний: 0
11. One Byte at a Time 297
#include |
<16f84.h> |
||
#use |
fast_io(b) |
||
#define PORT_B *(unsigned int *)0x06 |
|||
unsigned int scan_it(void); |
|||
unsigned int get_it(void); |
|||
int main() |
|||
{ |
|||
int ....; |
/* Main’s variable declarations |
*/ |
|
set_tris_b(0xF0); |
/* RB7:4 outputs, RB3:0 inputs |
*/ |
|
port_b_pullups(TRUE); /* PortB pullups active |
*/ |
||
...... |
|||
...... |
|||
} |
|||
Program 11.6 Coding the keypad device driver in C.
unsigned int get_it(void)
{
unsigned int count, old_key, new_key; count = 0;
while(count<255)
{
new_key = scan_it(); if(new_key == old_key)
{ count++;}
else
{
old_key = new_key; count = 0;
}
}
return (old_key);
}
/**************************************************************/
unsigned int scan_it(void) |
|
{ |
|
unsigned int key, pattern; |
|
key=1; pattern = 0xF7; |
/* Initial pattern 11110111b */ |
while(key<13) |
|
{ |
PORT_B = pattern; if(!input(PIN_B7)) {break;} if(!input(PIN_B6)) {key+=3; break;} if(!input(PIN_B5)) {key+=6; break;} if(!input(PIN_B4)) {key+=9; break;} pattern = pattern >>1;
key++;
}
if(key==13) {key = 0xff;} return key;
}
11. One Byte at a Time 299
Assuming each display requires eight lines (seven segments plus decimal point) then a budget of 8 × n parallel lines are required for an n- digit display. The straightforward solution to this problem is shown in Fig. 11.13, where a 3-digit display is driven from three parallel registers on a local bus, in the manner of Fig. 11.10. The principle can be extended to six or more digits using the appropriate number of registers.
VDD |
VDD |
VDD |
|||
4K7 |
4K7 |
4K7 |
|||
Common anode |
Common anode |
Common anode |
|||
f |
a |
f |
a |
f |
a |
b |
b |
b |
|||
g |
g |
g |
|||
e |
c |
e |
c |
e |
c |
dp |
d |
dp |
d |
dp |
d |
180R |
180R |
180R |
|||
RA2
RA1
RA0
RB0
RB1
RB2
RB3
RB4
RB5
RB6
RB7
Fig. 11.14 Scanning a 3-digit 7-segment array.
The displays shown in the diagram are common anode and the appropriate LED is illuminated when the register output is low, with the sink current limited by the series resistance. In practice most logic circuitry can sink more current into a low output as compared to sourcing current from high and because of this common cathode displays are less common. In some larger displays , eg. 5 cm (2"), several LEDs may be paralleled or in series in each segment. In this situation larger anode voltages and/or currents may be needed and suitable drivers required to bu er the register outputs.
An alternative approach, shown in Fig. 11.14, is frequently used with LED-based displays. Instead of using a register for each digit, all readouts are connected in parallel to the one PIC port. Each readout is en-
11. One Byte at a Time 301
Program 11.7 Displaying the decimal equivalent of a binary byte.
; Task 1 |
||
DISPLAY movf |
BINARY,w |
; Get binary byte |
call |
BIN_2_BCD |
; Convert to 3-digit BCD |
; Task 2 |
||
movf |
HUNDREDS,w |
; Get Hundreds nybble |
call |
SVN_SEG |
; Convert to 7-segment code |
movwf |
PORTB |
; Send out to PortB |
bsf |
PORTA,2 |
; Clock into register |
bcf |
PORTA,2 |
|
; Task 3 |
||
movf |
TENS,w |
; Get Tens nybble |
call |
SVN_SEG |
; Convert to 7-segment code |
movwf |
PORTB |
; Send out to PortB |
bsf |
PORTA,1 |
; Clock into register |
bcf |
PORTA,1 |
|
; Task 4 |
||
movf |
UNITS,w |
; Get Units nybble |
call |
SVN_SEG |
; Convert to 7-segment code |
movwf |
PORTB |
; Send out to PortB |
bsf |
PORTA,0 |
; Clock into register |
bcf |
PORTA,0 |
|
(a)
•Copy contents of HUNDREDS into W and convert to 7-segment code.
•Copy 7-segment code to Port B.
• Bring RA2 low \ .
•Delay 10ms.
• Bring RA2 high / .
(b)
•Copy contents of TENS into W and convert to 7-segment code.
•Copy 7-segment code to Port B.
• Bring RA1 low \ .
•Delay 10ms.
• Bring RA1 high / .
(c)
•Copy contents of UNITS into W and convert to 7-segment code.
•Copy 7-segment code to Port B.
• Bring RA0 low \ .
•Delay 10ms.
• Bring RA0 high / .
The coding in Program 11.8 makes use of the 10 ms delay subroutine illustrated in Program 11.5 to regulate the scanning rate. Apart from the length of the enabling pulse the core of the program is identical to our previous situation. However, the code must run continually to give the impression of a constant display. This illustrates the trade o between hardware and software. Reducing the hardware has lead to greater load-