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

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

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

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

Добавлен: 14.06.2025

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

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

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

666

Appendix D

;

(for code testing)

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

long_delay

movlw

D'200' ; w = 200 decimal

movwf

J

; J = w

jloop:

movwf

K

; K = w

kloop:

decfsz

K,f

; K = K-1, skip next if zero

goto

kloop

decfsz

J,f

; J = J-1, skip next

if zero

goto

jloop

return

end


Supplementary Programs

667

;File name: TestStr.asm

;Date: April 19, 2006

;Author: Julio Sanchez

;Description:

;Program to test sending strings to LCD memory directly

;Program uses delay loops for interface timing.

;WARNING:

;Code assumes 4Mhz clock. Delay routines must be

;edited for faster clock

;Displays: Minnesota State, Mankato

;

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

;switches ;===========================

;Switches used in __config directive:

;

_CP_ON

Code protection ON/OFF

; *

_CP_OFF

;

*

_PWRTE_ON

Power-up timer ON/OFF

;_PWRTE_OFF

;

_WDT_ON

Watchdog timer ON/OFF

; * _WDT_OFF

;

_LP_OSC

Low power crystal

occilator

; * _XT_OSC

External parallel

resonator/crystal oscillator

;

_HS_OSC

High speed crystal resonator (8 to

10 MHz)

;

Resonator: Murate

Erie CSA8.00MG =

8 MHz

;

_RC_OSC

Resistor/capacitor oscillator (simplest, 20%

error)

;|

;|_____ * indicates setup values

;========================= ; setup and configuration ;=========================

processor 16f84A

include

<p16f84A.inc>

__config

_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

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

;

constant definitions

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

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

#define E_line 1

;|

#define RS_line 2

;| -- from wiring diagram

#define RW_line 3

;|

; LCD line addresses (from LCD data sheet)


668

Appendix D

#define LCD_1

0x80

;

First LCD line constant

#define LCD_2

0xc0

;

Second LCD line constant

;Note: The constants that define the LCD display line

;addresses have the high-order bit set in

;order to facilitate the controller command

;

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

; PIC register equates

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

porta

equ

0x05

Portb

equ

0x06

fsr

equ

0x04

Status

equ

0x03

indf

equ

0x00

z

equ

2

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

; variables in PIC RAM

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

;Reserve 16 bytes for string buffer cblock 0x0c

strData endc

;Leave 16 bytes and Continue with local variables

cblock

0x1d

; Start of block

count1

; Counter # 1

count2

; Counter # 2

count3

; Counter # 3

pic_ad

; Storage for start of text area

; (labeled strData) in PIC RAM

J; counter J

K; counter K

index

;

Index into text table (also used

;

for auxiliary storage)

endc

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

; program

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

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x08

main:

movlw

b'00000000' ; All lines to output

tris

porta

; in port A

tris

portb

; and port B

movlw

b'00000000' ; All outputs ports low

movwf

porta


Supplementary Programs

669

movwf

portb

; 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)

; Store base address of text buffer in PIC RAM

movlw

0x0c

; Start address of text buffer

movwf

pic_ad

; to local variable

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

;test routine ;=========================

;Set DDRAM address to start of first line

call line1

; Store characters and send directly

movlw

'H'

movwf

portb

call

pulseE

movlw

'e'

movwf

portb

call

pulseE

movlw

'l'

movwf

portb

call

pulseE

movlw

'l'

movwf

portb

call

pulseE

movlw

'o'

movwf

portb

call

pulseE

call

delay_5

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

;

done!

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

loopHere:

goto

loopHere ;done

;************************************************************ ; INITIALIZE LCD PROCEDURE ;************************************************************ initLCD

;Initialization for Densitron LCD module as follows:

;8-bit interface

;2 display lines of 16 characters each

;cursor on

;left-to-right increment


670

Appendix D

;cursor shift right

;no display shift ;***********************|

;

COMMAND MODE

|

;***********************|

bcf

porta,E_line

; E line low

bcf

porta,RS_line

; RS line low for command

bcf

porta,RW_line

; Write mode

call

delay_125

;delay 125

microseconds

;***********************|

;

FUNCTION SET

|

;***********************|

movlw

0x38

; 0 0

1 1

1

0 0 0 (FUNCTION SET)

;

| | |

|__ font select:

;

| | |

1 = 5x10 in 1/8 or 1/11

;

| | |

0 = 1/16 dc

;

| | |___ Duty cycle select

;

| |

0 = 1/8 or 1/11

;

| |

1 = 1/16

;

| |___ Interface width

;

|

0 = 4 bits

;

|

1 = 8 bits

;

|___ FUNCTION SET COMMAND

movwf

portb

;0011

1000

call

pulseE

;pulseE and

delay

;***********************|

;

DISPLAY OFF

|

;***********************|

movlw

0x08

; 0 0

0 0 1

0 0 0 (DISPLAY ON/OFF)

;

|

| | |___ Blink character

;

|

| |

1 = on, 0 = off

;

|

| |___ Cursor on/off

;

|

|

1 = on, 0 = off

;

|

|____ Display on/off

;

|

1 = on, 0 = off

;

|____ COMMAND BIT

movwf

portb

call

pulseE

;pulseE and

delay

;***********************|

; DISPLAY AND CURSOR ON |

;***********************|

movlw

0x0e

; 0 0

0 0 1

1 1 0 (DISPLAY ON/OFF)

;

|

| | |___ Blink character

;

|

| |

1 = on, 0 = off