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

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

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

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

Добавлен: 14.06.2025

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

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

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

568

Chapter 16

16.4 Sample Programs

The following sections contain the sample programs discussed in this chapter.

16.4.1 ADF84 Program

;File name: ADCF84.asm

;Last Update: June 8, 2006

;Author: Julio Sanchez

;Processor: 16F84A

;Description:

;Program to demonstrate use of the ADC0831 Analog to

;Digital converter with the 16F84 PIC. Program reads the

;value of a potentionmeter connected to Port-A, line 0

;and displays resistance in the range 0 to 255 on the

;attached LCD.

;Circuit:

;

ADC0831

16F84

CIRCUIT

; PIN LINE

;6 DO ------------- RB0

;7 CLK ------------- RB1

;1 CS ------------- RB2

;2 Vin+ ------------------------------ POT2

;3 Vin- ------------------------------ GND

;5 Vref ------------------------------ +5v

;8 Vcc ------------------------------ +5v

;For LCD display parameters see the LCDTest2 program.

;WARNING:

;Code assumes 4Mhz clock. Delay routines must be

;edited for faster clock

;

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

;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

oscillator

; * _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


Analog to Digital and Realtime Clocks

569

; |

(simplest, 20% error)

;|

;|_____ * indicates setup values presently selected

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

processor

16f84A

include

<p16f84A.inc>

__config

_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

errorlevel -302

; Suppress bank-related warning

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

; M A C R O S

;============================================================ ; Macros to select the register banks in 16F84

Bank0

MACRO

; Select RAM bank 0

bcf

STATUS,RP0

ENDM

Bank1

MACRO

; Select RAM bank 1

bsf

STATUS,RP0

ENDM

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

;

constant definitions

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

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

#define E_line 1

;|

#define RS_line 2

;| — from circuit wiring diagram

#define RW_line 3

;|

; LCD line addresses (from LCD data sheet)

#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

;Defines from ADC0831 wiring diagram

;all lines in Port-A

#define

DO

0

;|

#define

CLK

1

;| — from circuit wiring diagram

#define

CS

2

;|

;

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

;

variables in PIC RAM

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

; Reserve 16 bytes for string buffer


; Start of block ; Counter # 1
; Counter # 2
; Counter # 3
; Storage for start of text area ; counter J
; counter K
; Index into text table (also used ; for auxiliary storage)
; Local temporary storage ; Storage # 2
; Received data

570

Chapter 16

cblock 0x0c strData

endc

;Reserve three bytes for ASCII digits cblock 0x1d

asc100

asc10

asc1 endc

;Continue with local variables

cblock 0x20 count1 count2 count3 pic_ad

J

K index

store1

store2 rcvdata bitCount

; Storage for ASCII decimal conversion and digits

inNum

;

Source operand

thisDig

;

Digit counter

endc

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

; program

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

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x08

main:

Bank1

movlw

b’00000000’

movwf

TRISA

movlw

b’00000001’

movwf

TRISB

Bank0

movlw

b’00000000’

movwf

PORTA

movwf

PORTB

; Wait and initialize HD44780 call delay_5

;All lines to output

;in Port-A

;B line 0 to input

;All outputs ports low

;Allow LCD time to initialize

;itself


Analog to Digital and Realtime Clocks

571

call

delay_5

call

initLCD

; Then do forced

initialization

call

delay_5

; Wait again

; Store base address of text buffer in PIC RAM

movlw

0x0c

; Start address for buffer

movwf

pic_ad

; to local variable

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

;first LCD line ;======================

;Store 16 blanks in PIC RAM, starting at address stored

;in variable pic_ad

call blank16

;Call procedure to store ASCII characters for message

;in text buffer

movlw

d’0’

; Offset into buffer

call

storeMS1 ; Store message text in buffer

; Initialize ADC0831

nextAna:

call

ana2dig

; Read analog line

call

delay_125

; Display result

movf

rcvdata,w

call

bin2asc

; Conversion routine

;At this point three ASCII digits are stored in local

;variables. Move digits to display area

movf

asc1,w

; Unit digit

movwf

.26

; Store in buffer

movf

asc10,w

; same with other digits

movwf

.25

movf

asc100,w

movwf

.24

;Display line

;Set DDRAM address to start of first line

call line1

; Call procedure to display 16 characters in LCD call display16

call long_delay goto nextAna

;============================================================ ; initialize LCD for 4-bit mode ;============================================================ initLCD:

;Initialization for Densitron LCD module as follows:

;4-bit interface

;2 display lines of 16 characters each

;cursor on


572

Chapter 16

;left-to-right increment

;cursor shift right

;no display shift ;=======================|

;

set command mode

|

;=======================|

bcf

PORTA,E_line

; E line low

bcf

PORTA,RS_line

; RS line low

bcf

PORTA,RW_line

; Write mode

call

delay_125

; delay 125 microseconds

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

;

FUNCTION SET

|

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

movlw

0x28

; 0 0 1 0 1 0 0 0 (FUNCTION SET)

call

send8

; 4-bit send routine

; Set 4-bit mode command must be repeated

movlw

0x28

call

send8

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

; DISPLAY AND CURSOR ON |

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

movlw

0x0e

; 0 0 0 0 1 1 1 0 (DISPLAY ON/OFF)

call

send8

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

;

set entry mode

|

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

movlw

0x06

; 0 0 0 0 0 1 1 0 (ENTRY MODE SET)

call

send8

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

; cursor/display shift

|

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

movlw

0x14

; 0 0 0 1 0 1 0 0 (CURSOR/DISPLAY

SHIFT)

call

send8

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

;

clear display

|

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

movlw

0x01

; 0 0 0 0 0 0 0 1 (CLEAR DISPLAY)

call

send8

; Per documentation

call

delay_5

; Test for busy

return

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

;

Procedure to delay