Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 8572

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Communications

421

;Keypad switch wiring (values are scan codes):

;—- KEYPAD —

;

0

1

2

3

<= port B0 |

;

4

5

6

7

<= port B1 |—- ROWS = OUTPUTS

;

8

9

A

B

<= port B2 |

;

C

D

E

F

<= port B3

|

;

|

|

|

|

;

|

|

|

|_____ port B4

|

;

|

|

|_________ port B5

|—- COLUMNS = INPUTS

;

|

|_____________ port B6

|

;|_________________ port B7 |

;Operations:

;1. Key press action generates a scan code in the range

;0x0 to 0xf.

;2. Scan code is converted to an ASCII digit and displayed

;on the LCD. LCD lines are scrolled as end-of-line is

;reached.

;3. Characters typed on the keypad are also transmitted

;through the serial port.

;4. Serial port is polled for received characters. These

;are displayed on the LCD.

;

;WARNING:

;Code assumes 4Mhz clock. Delay routines must be

;edited for faster clock. Clock speed also determines

;values for baud rate setting (see spbrgVal constant).

;===========================

;16F877 switches

;===========================

; Switches used in __config directive:

;

_CP_ON

Code protection ON/OFF

; *

_CP_OFF

;

*

_PWRTE_ON

Power-up timer ON/OFF

;_PWRTE_OFF

;

_BODEN_ON

Brown-out reset enable ON/OFF

; *

_BODEN_OFF

;

*

_PWRTE_ON

Power-up timer enable ON/OFF

;_PWRTE_OFF

;

_WDT_ON

Watchdog timer

ON/OFF

; * _WDT_OFF

;

_LPV_ON

Low voltage IC

programming enable ON/OFF

; * _LPV_OFF

;

_CPD_ON

Data EE memory

code protection ON/OFF

;* _CPD_OFF

;OSCILLATOR CONFIGURATIONS:

;

_LP_OSC

Low power crystal oscillator


422

;_XT_OSC

;* _HS_OSC

;_RC_OSC

;|

Chapter 14

External parallel resonator/crystal oscillator High speed crystal resonator Resistor/capacitor oscillator

(simplest, 20% error)

;|

;|_____ * indicates setup values presently selected

processor 16f877 ; Define processor

#include <p16f877.inc>

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WDT_OFF & _LVP_OFF & _CPD_OFF

;__CONFIG directive is used to embed configuration data

;within the source file. The labels following the directive

;are located in the corresponding .inc file. ;============================================================

; M A C R O S

;============================================================ ; Macros to select the register banks

Bank0

MACRO

; Select RAM bank 0

bcf

STATUS,RP0

bcf

STATUS,RP1

ENDM

Bank1

MACRO

; Select RAM bank 1

bsf

STATUS,RP0

bcf

STATUS,RP1

ENDM

Bank2

MACRO

; Select RAM bank 2

bcf

STATUS,RP0

bsf

STATUS,RP1

ENDM

Bank3

MACRO

; Select RAM bank 3

bsf

STATUS,RP0

bsf

STATUS,RP1

ENDM

;=====================================================

;

constant definitions

;

for PIC-to-LCD pin wiring and LCD line addresses

;=====================================================

#define E_line 1

;|

#define RS_line 0

;| — from wiring diagram

#define RW_line 2

;|

; LCD line addresses (from LCD data sheet)

#define LCD_1

0x80

; First LCD line constant

#define LCD_2

0xc0

; Second LCD line constant


Communications

423

#define LCDlimit .20; Number of characters per line

#define spbrgVal .25; For 2400 baud on 4Mhz clock

;Note: The constants that define the LCD display

;line addresses have the high-order bit set

;so as to meet the requirements of controller

;commands.

;

;=====================================================

;

variables in PIC RAM

;=====================================================

; Local variables

cblock

0x20

; Start of block

count1

; Counter # 1

count2

; Counter # 2

count3

; Counter # 3

J

; counter J

K

; counter K

store1

; Local storage

store2

; For LCDscroll procedure

LCDcount ; Counter for characters per line

LCDline

; Current display line (0 or 1)

; Keypad processing variables

keyMask

; For keypad processing

rowMask

; For masking-off key rows

rowCode

; Row addend for calculating scan code

rowCount ; Counter for key rows (0 to 3)

scanCode ; Final key code

newScan

; 0 if no new scan code detected

; Communications variables

newData

; not 0 if new data received

ascVal

errorFlags

endc

;============================================================

; P R O G R A M

;============================================================

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x08

main:

;Wiring:

;LCD data to Port D, lines 0 to 7

;E line -> port E, 1

;RW line -> port E, 2

;RS line -> port E, 0


424

Chapter 14

;Set PORTE D and E for output

;Data memory bank selection bits:

; RP1:RP0

Bank

;

0:0

0

Ports A,B,C,D, and E

;

0:1

1

Tris A,B,C,D, and E

;

1:0

2

;

1:1

3

; First, initialize Port-B by clearing latches

clrf

STATUS

clrf

PORTB

; Select bank 1 to tris Port D for output

bcf

STATUS,RP1

; Clear banks 2/3 selector

bsf

STATUS,RP0

; Select bank 1 for tris

; registers

;Tris Port D for output. Port D lines 4 to 7 are wired

;to LCD data lines. Port D lines 0 to 4 are wired to LEDs. movlw B’00000000’

movwf

TRISD

; and Port D

;By default Port-A lines are analog. To configure them

;as digital code must set bits 1 and 2 of the ADCON1

;register (in bank 1)

movlw

0x06

; binary 0000 0110 is code to

; make all Port-A lines digital

movwf ADCON1

;Port-B, lines are wired to keypad switches, as follows:

;7 6 5 4 3 2 1 0

;| | | | |_|_|_|_____ switch rows (output)

;|_|_|_|_____________ switch columns (input)

;rows must be defined as output and columns as input

movlw

b’11110000’

movwf

TRISB

; Tris

port E for output

movlw

B’00000000’

movwf

TRISE

; Tris port E

; Enable Port-B pullups for switches in OPTION register

;

7

6

5

4

3

2 1 0 <= OPTION bits

;

|

|

|

|

|

|__|__|_____

PS2-PS0 (prescaler bits)

;

|

|

|

|

|

Values for Timer0

;

|

|

|

|

|

000

= 1:2

001 = 1:4

;

|

|

|

|

|

010

= 1:8

011 = 1:16

;

|

|

|

|

|

100

= 1:32

101 = 1:64

;

|

|

|

|

|

110

= 1:128

*111 = 1:256

;

|

|

|

|

|______________

PSA

(prescaler assign)

;

|

|

|

|

*1

=

to WDT

;

|

|

|

|

0

=

to Timer0

;

|

|

|

|_________________

TOSE (Timer0 edge select)

;

|

|

|

*0

=

increment on low-to-high

;

|

|

|

1

=

increment in high-to-low


Communications

425

;

|

| |____________________

TOCS (TMR0 clock source)

;

|

|

*0

= internal clock

;

|

|

1

= RA4/TOCKI bit source

;

|

|_______________________

INTEDG (Edge select)

;

|

*0

= falling edge

;|__________________________ RBPU (Pullup enable)

;

*0

= enabled

;

1

= disabled

movlw

b’00001000’

movwf

OPTION_REG

; Back to bank 0

bcf

STATUS,RP0

;Initialize serial port for 9600 baud, 8 bits, no parity

;1 stop

call

InitSerial

; Test serial transmission by sending “RDY-”

movlw

‘R’

call

SerialSend

movlw

‘D’

call

SerialSend

movlw

‘Y’

call

SerialSend

movlw

‘-’

call

SerialSend

movlw

0x20

call

SerialSend

; Clear all output lines

movlw

b’00000000’

movwf

PORTD

movwf

PORTE

; Wait and initialize HD44780

call

delay_5

; Allow LCD time to initialize itself

call

initLCD

; Then do forced initialization

call

delay_5

; (Wait probably not necessary)

; Clear character counter and line counter variables

clrf

LCDcount

clrf

LCDline

; Set display address to start of second LCD line

call

line1

;============================================================

; scan keypad

;============================================================

; Keypad switch wiring:

;

x

x

x

x

<= port B0

|

;

x

x

x

x

<= port B1

|—- ROWS = OUTPUTS

;

x

x

x

x

<= port B2

|

;

x

x

x

x

<= port B3

|

;

|

|

|

|