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

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

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

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

Добавлен: 14.06.2025

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

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

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.
; RA0 is analog. All others ; ; digital
; Vref+ = Vdd

Analog to Digital and Realtime Clocks

593

movwf

asc10

; Store it

; Calculate and store units digit

movf

inNum,w

; Store units value

addlw

0x30

; Convert to ASCII

movwf

asc1

; Store digit

return

;============================================================ ; Analog to Digital Procedures ;============================================================ ;============================

;init A/D module ;============================

;1. Procedure to initialize the A/D module, as follows:

;Configure the PIC I/O lines. Init analog lines as input

;2. Select ports to be used by setting the PCFGx bits in the

;ADCON1 register. Selects rightor left-justification.

;3. Select the analog channels, select the A/D conversion

;clock, and enable the A/D module.

;4. Wait the acquisition time.

;5. Initiate the conversion by setting the GO/DONE bit in the

;ADCON0 register.

;6. Wait for the conversion to complete.

;7. Read and store the digital result.

InitA2D:

Bank1

;

Select bank

for TRISA register

movlw

b’00000001’

movwf

TRISA

;

Set Port-A,

line 0, as input

;Select the format and A/D port configuration bits in

;the ADCON1 register

;Format is left-justified so that ADRESH bits are the

;most significant

;

0

x

x

x

1

1

1

0

<== value installed in ADCON1

;

7

6

5

4

3

2

1

0

<== ADCON1

bits

;

|

|__|__|__|____ RA0 is analog.

;

|

Vref+

=

Vdd

;

|

Vref-

=

Vss

;|_________________________ 0 = left-justified

;ADCON1 is in bank 1

movlw b’00001110’ movwf ADCON1

;Select D/A options in ADCON0 register

;For a 10Mhz clock the Fosc32 option produces a conversion

;speed of 1/(10/32) = 3.2 microseconds, which is within the

;recommended range of 1.6 to 10 microseconds.

; 1 0 0 0 0 0 0 1 <== value installed in ADCON0


ADCON0,GO ; Test bit convWait ; Wait if not clear

594

Chapter 16

;

7

6

5

4

3

2

1 0 <== ADCON0 bits

;

|

|

|

|

|

|

|____

A/D

function

select

;

|

|

|

|

|

|

1 =

A/D ON

;

|

|

|

|

|

|__________

A/D

status bit

;

|

|

|__|__|_____________

Analog Channel

Select

;

|

|

000

= Chanel

0

(RA0)

;|__|______________________ A/D Clock Select

;

10 = Fosc/32

;

ADCON0 is in bank 0

Bank0

movlw

b’10000001’

movwf

ADCON0

; Channel 0, Fosc/32, A/D

;enabled

;Delay for selection to complete. (Existing routine provides

;more than 20 microseconds required)

call

delayAD

; Local procedure

return

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

;read A/D line ;============================

;Procedure to read the value in the A/D line and convert

;to digital

ReadA2D:

; Initiate conversion

Bank0

;

Bank for ADCON0

register

bsf

ADCON0,GO

;

Set the GO/DONE

bit

; GO/DONE bit is cleared automatically when conversion ends convWait:

btfsc goto

;At this point conversion has concluded

;ADRESH register (bank 0) holds 8 MSBs of result

;ADRESL register (bank 1) holds 4 LSBs.

;In this application value is left-justified. Only the

;MSBs are read

movf

ADRESH,W ; Digital value to w register

return

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

;delay procedure ;=======================

;For a 10Mhz clock the Fosc32 option produces a conversion

;speed of 1/(10/32) = 3.2 microseconds. At 3.2 ms per bit

;13 bits require approximately 41 ms. The instruction time

;at 10Mhz is 10 ms. 4/10 = 0.4 ms per instruction. To delay

;41 ms a 10Mhz PIC must execute 11 instructions. Add one

;more for safety.

delayAD:

movlw

.12

; Repeat 12 machine cycles


Analog to Digital and Realtime Clocks

595

movwf

count1

; Store value in counter

repeat11:

decfsz

count1,f

; Decrement counter

goto

repeat11 ; Continue if not 0

return

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

end

; END OF PROGRAM

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

16.4.3 RTC2LCD Program

;File name: RTC2LCD.asm

;Last Update: June 6, 2006

;Author: Julio Sanchez

;Processor: 16F84A

;Description:

;Program to demonstrate use of the NJU6355 Real Time Clock

;IC. Program uses LCD to display results of hours, minutes,

;and seconds, as follows:

;

;Top LCD line: H:xx M:yy S:zz

;Initialization values are in #define statements that start

;with i, such as iYear, iMonth, etc.

;

;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


596

Chapter 16

; |

(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 realtime clock wiring diagram

;all lines in Port-B

#define

DAT

0

;|

#define

CLK

1

;| — from circuit wiring diagram

#define

CE

2

;|

#define

IO

3

;|

;

; Defines for RTC initialization (values are arbitrary)

#define iYear

.7

#define iMonth

.6


Analog to Digital and Realtime Clocks

597

#define iDay

.5

#define iDoW

.4

#define iHour

.3

#define iMin

.2

#define iSec

.1

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

;

PIC register equates

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

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

;

variables in PIC RAM

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

;Reserve 16 bytes for string buffer cblock 0x0c

strData endc

;Reserve three bytes for ASCII digits cblock 0x1d

asc100

asc10

asc1 endc

;Continue with local variables

cblock 0x20

; Start of block

count1

; Counter # 1

count2

; Counter # 2

count3

; Counter # 3

pic_ad

; Storage for start of text area

J

; counter J

K

; counter K

index

; Index into text table (also used

; for auxiliary storage)

store1

; Local temporary storage

store2

; Storage # 2

; Storage for BCD digits

bcdLow

; low-order nibble of packed BCD

bcdHigh

; High-order nibble

; Variables for Real-Time Clock

year

month

day

dayOfWeek

; Sunday to Saturday (1 to 7)

hour

minutes

seconds

temp1

counter

; Storage for BCD conversion routine

inNum ; Source operand