Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8589
Скачиваний: 0
406 |
Chapter 14 |
;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 |
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 |
||
; | |
(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
;============================================================ ; M A C R O S ;============================================================
Communications |
407 |
||
; Macros to select the register banks |
|||
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 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 |
||
#define LCDlimit .16; Number of characters per line
;4800 baud clock countdown values
;Code reduces rate to 2400 baud by entering a minimal
;presclaer to TRM0
#define halfBaud .152 ; For one-half bit time
#define fullBaud .48 ; For one full bit time
;
;Note: The constants that define the LCD display line
;addresses have the high-order bit set in
;order to facilitate the controller command
;
;=====================================================
;buffer and variables in PIC RAM ;=====================================================
;Create a 16-byte storage area
cblock |
0x0c |
; Start of first data block |
lineBuf |
; buffer for text storage |
|
endc |
||
; Leave 16 bytes and Continue with local variables |
||
cblock |
0x1c |
; Second data block |
count1 |
; Counter # 1 |
|
count2 |
; Counter # 2 |
|
J |
; counter J |
|
K |
; counter K |
|
store1 |
; Local temporary storage |
|
store2 |
; Storage # 2 |
|
; For LCDscroll procedure |
||
LCDcount ; Counter for characters per line |
||
LCDline |
; Current display line (0 or 1) |
|
bufPtr |
; Buffer pointer |
|
408 Chapter 14
; Variables for serial communications
tempData ; Temporary storage for bit manipulations
rcvData |
; Final storage for received character |
|
bitCount ; Bit counter |
||
sendData ; Character to send |
||
endc |
||
;========================================================= |
||
; |
m a i n |
p r o g r a m |
;========================================================= |
||
org |
0 |
; start at address |
goto |
main |
|
; Space for interrupt handlers |
||
org |
0x08 |
|
main: |
||
Bank1 |
||
movlw |
b’00010001’ |
; Port-A lines I/O setup |
; RA0 = RS232 input (R2out) |
||
; RA4 = Pushbutton SW # 1 |
||
movwf |
TRISA |
|
movlw |
b’00000000’ ; Port-B lines as follow: |
|
;RB4-RB7 ===> LCD data lines 4 to 7 (output)
;RB0 =======> MAX202 T2in line (output) movwf TRISB
Bank0
;Clear bits in Port-A output lines
bcf |
PORTA,1 |
|
bcf |
PORTA,2 |
|
bcf |
PORTA,3 |
|
movlw |
b’00000000’ |
; All outputs ports low |
movwf |
PORTB |
|
; Wait and initialize HD44780 |
||
call |
delay_5 |
; Allow LCD time to initialize |
; itself |
||
call delay_5 call initLCD
initialization
call delay_5 ; Wait again
; Set Port-B, line 0 high so start bit is detected bsf PORTB,0
;============================
;wait for start command ;============================
;Program waits until pushbutton number 1 is pressed
;to continue execution. Pushbutton 1 is active low
;and wired to RA4
pb1Wait:
btfsc |
PORTA,4 |
; Test Port-A, line 4 |
Communications |
409 |
|
goto |
pb1Wait |
; Loop if not clear |
;============================ |
||
;display and send “Ready-” ;============================
;Set LCD base address
call line1
; Initialize system for UART emulation at 2400 baud call initTTY
;Display on LCD and test serial transmission by sending
;the string “Ready-”
movlw |
‘R’ |
||
movwf |
sendData ; Store in send register |
||
call |
send8 |
; Local LCD display procedure |
|
call |
sendTTY |
; Local send procedure |
|
movlw |
‘e’ |
||
movwf |
sendData ; Store in send register |
||
call |
send8 |
; Local LCD display procedure |
|
call |
sendTTY |
; Local send procedure |
|
movlw |
‘a’ |
||
movwf |
sendData ; Store in send register |
||
call |
send8 |
; Local LCD display procedure |
|
call |
sendTTY |
; Local send procedure |
|
movlw |
‘d’ |
||
movwf |
sendData ; Store in send register |
||
call |
send8 |
; Local LCD display procedure |
|
call |
sendTTY |
; Local send procedure |
|
movlw |
‘y’ |
||
movwf |
sendData ; Store in send register |
||
call |
send8 |
; Local LCD display procedure |
|
call |
sendTTY |
; Local send procedure |
|
movlw |
‘-’ |
||
movwf |
sendData ; Store in send register |
||
call |
send8 |
; Local LCD display procedure |
|
call |
sendTTY |
; Local send procedure |
|
; Init |
character counter and line counter variables for |
||
; LCD line scroll procedure |
|||
movlw |
0x06 |
; 6 characters already |
|
displayed |
|||
movwf |
LCDcount |
||
clrf |
LCDline |
; LCD line counter |
|
;============================ |
|||
; |
monitor RS232 line |
||
;============================ |
|||
nextChar: |
|||
call |
rcvTTY |
; Receive character |
|
;Store character in local line buffer using indirect
;addressing
;16-byte buffer named lineBuf starts at address 0x0c
410 |
Chapter 14 |
|
; Register variable bufPtr holds offset into buffer |
||
movlw |
0x0c |
; Buffer base address |
addwf |
bufPtr,w ; Add pointer in w |
|
movwf |
FSR |
; Value to index register |
movf |
rcvData,w |
; Character into w |
movwf |
INDF |
; Store w in [FSR] |
incf |
bufPtr,f ; Bump pointer |
|
; Send character (still in w) |
||
call |
send8 |
; Display it |
call |
LCDscroll |
; Scroll display lines |
goto |
nextChar ; Continue |
|
;============================================================ ; 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
;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) |
||||
; |
| |
| | |__ 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 |
||||||
call |
send8 |
; 4-bit |
send routine |
|||||
; Set 4-bit mode command must be repeated movlw 0x28