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

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

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

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

Добавлен: 14.06.2025

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

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

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

Supplementary Programs

683

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

;main program ;============================= main:

;Clear the watchdog timer and reset prescaler clrwdt

;Set up the OPTION register bit map

movlw

b'11010111'

;

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

;

|

|

|____________________

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

option

; Setup ports

movlw

0x00

; Set port B to output

tris

PORTB

clrf

PORTB

; All port B to 0

; Port A is not used in this program

mloop:

incf

PORTB,f

; Add 1 to register value

call

TM0delay

goto

mloop

;******************************

;delay sub-routine

;uses Timer0 ;****************************** TM0delay:

;Initialize the timer register

clrf

TMR0

; Clear SFR for Timer0

;Routine tests the value in the TMR0 register by

;subtracting 0xff from the value in TMR0. The zero flag


684

Appendix D

; is set if TMR0 = 0xff

cycle:

movf

TMR0,w

; Timer to w

; w has TMR0 register value

sublw

0xff

; Subtract max value

; Zero flag is set if value in TMR0 = 0xff

btfss

STATUS,Z ; Test for zero

goto

cycle

; Repeat

return

End

Supplementary Programs

685

;File: TimerTst.ASM

;Date: April 27, 2006

;Author: Julio Sanchez

;Description:

;Using the timer to generate a signal at 1 Mhz ;===========================

;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

processor 16f84A

include

<p16f84A.inc>

__config

_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

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

;

PIC register equates

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

porta

equ

0x05

Portb

equ

0x06

Status

equ

0x03

z

equ

0x02

tmr0

equ

0x01

;

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

;

variables in PIC RAM

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

; Local variables

cblock

0x0d

; Start of block

J

; counter J

K

; counter K

countL

; Auxiliary counter

countH

; ISR counter


686 Appendix D

endc

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

; m a i n p r o g r a m

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

org 0 ; start at address 0

goto main

;

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

; interrupt handler

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

org

0x04

;goto IntServ ;=============================

;main program ;============================= main:

;Clear the watchdog timer and reset prescaler clrwdt

;Set up the OPTION register bit map

movlw

b'11010011'

;

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

;

|

|

|____________________

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

option

; Setup ports

movlw

0x00

; Set port B to output

tris

portb

clrf

portb

; All port B to 0

; Port A is not used in this program mloop:


Supplementary Programs

687

bsf

portb,0

call

TM0delay

bcf

portb,0

call

TM0delay

goto

mloop

;******************************

;delay sub-routine

;uses Timer0 ;****************************** TM0delay:

;Initialize the timer register

clrf

tmr0

; Clear SFR for Timer0

;Routine tests the value in the tmr0 register by

;xoring with a mask of all ones. The operation sets

;the zero flag if tmr0 is zero.

cycle:

movf

tmr0,w

; Timer to w

; w has tmr0 register value

sublw

0xff

; Subtract max value

; Zero flag is set if value in tmr0 = 0xff

btfss

status,z ; Test for zero

goto

cycle

; Repeat

return

end

688

Appendix D

;File name: TTYUsart.asm

;Last update: May, 2006

;Author: Julio Sanchez

;Processor: 16F84A

;Description:

;Program to emulate USART operation in PIC code. Uses

;PIC-to-LCD interface. Display has 2 lines, each with

;16 characters.

;Program operation:

;Characters received from the RS232 line are displayed on

;the LCD. LCD lines scroll automatically. A pushbutton

;activates the send operation by transmitting the text

;string: Readywhich is also displayed on the LCD.

;

;Program communications and LCD parameters are stored in

;#define statements. These statements can be edited to

;accommodate a different set-up. Program uses delay loops

;for interface timing.

;

;WARNING:

;Code assumes 4Mhz clock. Delay routines must be

;edited for faster clock

;

;BAUD RATE CALCULATIONS:

;A 4Mhz clock oscillator has a clock frequency of 1 Mhz:

;Since the baud rate is the number of clock cycles per

;second, for a 4Mhz clock it is:

;

1

;bit time = ------ sec. = 208.33 microseconds

;4,800

;Calculating one half the baud rate allows resetting the

;clock from the edge to the center of a time pulse:

;

;|<======== falling edge of start bit

;

|

|<======== center of bit time

;

>|

|< one-half baud rate

;| |

;__________.

|

.____________.

;

|_____________|

|________

;208/2 = 104

;The PIC clock counts up from 0 to 255. So to implement

;a 104 microsecond delay we must start counting at

;clock beat:

;

255 - 104 = 151

;plus one microsecond for movlw instruction used to

;initialize the clock:

;

151 + 1 = 152


Supplementary Programs

689

; For one full baud rate delay:

;

255 - 208 = 47 + 1 = 48

;The following two constants are stored in #define

;statements:

;

halfBaud

=

152

;

fullBaud

=

48

;Setting the prescaler to TMR0 reduces the baud rate

;to one-half. Other prescaler values will reduce the

;baud rate accordingly.

;

;Wiring diagram:

;RB4-RB7 ===> LCD data lines 4 to 7 (output)

;RB0 =======> MAX202 T2in line (output)

;RA0 =======> MAX202 R2out line (input)

;RA1 =======> LCD E line (output)

;RA2 =======> LCD RS line (output)

;RA3 =======> LCD R/W line (output - not used)

;RA4 =======> Pushbutton switch 1

; (input - active low)

;

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

;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 presently selected

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

processor 16f84A include <p16f84A.inc>

__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

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