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

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

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

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

Добавлен: 14.06.2025

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

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

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

Supplementary Programs

705

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

; Local variables

cblock

0x0d

; Start of block

J

; counter J

K

; counter K

count1

; Auxiliary counter

count2

; Second auxiliary counter

old_w

; Context saving

old_status ; Idem

endc

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

;

m a i n

p r o g r a m

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

org

0

; start at address 0

goto

main

;

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

;interrupt handler ;============================= org 0x04 ;=============================

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

;Setting the prescaler to the watchdog timer following

;the sequence recommended by Microchip

movlw

b'10010101'

; Clock source and some

prescaler

option

clrf

tmr0

; Clear timer and prescaler

movlw

b'10111101' ; WDT, do not change prescale

option

; again

;Reset watchdog timer clrwdt

;Final setting of OPTION register

movlw

b'10111000'

; Set up the

OPTION register bit map

;

7

6

5

4

3

2 1 0 <= OPTION bits

;

|

|

|

|

|

|__|__|_____

PS2-PS0 (prescaler bits)

;

|

|

|

|

|

Values for WDT

;

|

|

|

|

|

000

= 1:1

001

= 1:2

;

|

|

|

|

|

010

= 1:4

011

= 1:8

;

|

|

|

|

|

100

= 1:16

101

= 1:32

;

|

|

|

|

|

110

= 1:64 *111 = 1:128

;

|

|

|

|

|______________

PSA

(prescaler assign)

;

|

|

|

|

*1 =

to WDT

;

|

|

|

|

0 =

to Timer0


706

Appendix D

;

|

|

| |_________________

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

movlw

b'00000000' ; Port B is output

tris

portb

; all others are output

clrf

portb

; All port B to 0

;Port A is not used by this program ;============================

;flash LED 20 times ;============================

;Program flashes LED wired to port B, line 2

;5 times before entering the endless loop

movlw

D'5'

; Number of iterations

movwf

count2

; To counter

lights:

movlw

b'00000010'

; Mask with bit 1 set

xorwf

portb,f

; Complement bit 1

call

long_delay

call

long_delay

call

long_delay

decfsz

count2,f

; Decrement counter

goto

lights

clrwdt

; Clear watchdog

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

;

endless loop

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

endless:

goto

endless

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

;

delay sub-routine

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

long_delay

movlw

D'200'

; w = 20 decimal

movwf

J

; J = w

jloop:

movwf

K

; K = w

kloop:

decfsz

K,f

; K = K-1, skip next if zero

clrwdt

goto

kloop


Supplementary Programs

707

decfsz

J,f

; J = J-1, skip next

if zero

clrwdt

goto

jloop

return

end

708

Appendix D

;File name: I2CEEP.asm

;Last revision: May 28, 2006

;Author: Julio Sanchez

;Processor: 16F877

;Description:

;Receive character data through RS-232 line and store in

;24LC04B EEPROM IC, using the I2C serial protocol in the

;PIC's MSSP module. Received characters are echoed on

;the second LCD line. When <Enter> key is detected (code

;0x0d) the text stored in EEPROM memory is retrieved and

;displayed on the LCD. On startup the top LCD line displays

;the prompt: "Receiving:". At that time a message "Rdy- " is

;sent through the serial line so as to test the connection.

;Default serial line setting:

;

2400 baud

;

no parity

;

1

stop bit

;

8

character bits

;

;Wiring:

;24LC04B SDA line is wired to PIC RC4 (MSSP SDA)

;24LC04B SCL line is wired to PIC RC3 (MSSP SCL)

;24LC04B A0-A2 and WP lines are not used (GND)

;

;Program to use 4-bit PIC-to-LCD interface.

;Code assumes that LCD is driven by Hitachi HD44780

;controller and PIC 16F977. Display supports two lines

;each one with 20 characters. The length, wiring and base

;address of each display line is stored in #define

;statements. These statements can be edited to accommodate

;a different set-up.

;

;WARNING:

;Code assumes 10 Mhz clock. Delay routines must be

;edited for a different 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


Supplementary Programs

709

;

*

_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

;

_XT_OSC

External parallel

resonator/crystal oscillator

; * _HS_OSC

High speed

crystal

resonator

;

_RC_OSC

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.

errorlevel -302

; Suppress bank-related warning

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

; 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


710

Appendix D

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

#define LCDlimit .20; Number of characters per line #define spbrgVal .64; For 2400 baud on 10Mhz 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.

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

;constants for I2C initialization ;==========================================================

;I2C connected to 24LC04B EEPROM.

;The MSSP module is in I2C MASTER mode.

#define LC04READ 0xa0 ; I2C value for read control byte #define LC04WRITE 0xa1 ; I2C value for write control byte

;========================================================== ; General Purpose Variables ;==========================================================

;Local variables

;Reserve 20 bytes for string buffer cblock 0x20

strData endc

;Other data

cblock 0x34

; Start of block

count1

; Counter # 1

count2

; Counter # 2

count3

; Counter # 3

J

; counter J

K

; counter K

bufAdd

index

store1

; Local storage

store2

; For LCDscroll procedure