Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8565
Скачиваний: 0
402 |
Chapter 14 |
||
highBit |
equ |
7 |
; High order bit |
;====================================================== |
|||
; |
variables in PIC RAM |
||
;===================================================== |
|||
cblock 0x0d |
; Start of block |
||
bitCount ; Counter for 8 bits |
|||
dataReg |
; Data to send |
||
workReg |
; Work register for bit shifts |
||
endc |
|||
;=========================================================
; program
;=========================================================
org |
0 |
; start at address |
goto |
main |
|
; Space for interrupt handlers |
||
org |
0x04 |
|
main:
; Port-A is all output Bank1
movlw b’00000000’ movwf TRISA
;Port-B line 0 is input, all others are output movlw b’00000001’
movwf TRISB Bank0
;Make sure Port-A line 2 (clear line) is high movlw b’00000100’
movwf PORTA
;======================== |
||
; read input from 165 IC |
||
;======================== |
||
call |
in165 |
; Local procedure |
;dataReg contains input ;===========================
;call serial output
;procedure ;===========================
call out164 ; Call serial output procedure
;===========================
;wait forever ;=========================== endloop:
goto endloop ;============================================================
;74HC164 procedure to send serial data ;============================================================
;ON ENTRY:
Communications |
403 |
;local variable dataReg holds 8-bit value to be
;transmitted through port labeled serialLN
;OPERATION:
;1. A local counter (bitCount) is initialized to
;8 bits
;2. Code assumes that the first bit is zero by
;setting the data line low. Then the high-order
;bit in the data register (dataReg) is tested.
;If set, the data line is changed to high.
;3. Bits are shifted in by pulsing the 74HC164
;clock line (CLK).
;4. Data bits are then shifted left and the bit
;counter is tested. If all 8 bits have been sent
;the procedure returns.
out164: |
||
; Clear 74HC164 shift register |
||
bcf |
PORTA,clearLN |
; 74HC164 CLR clear low |
bsf |
PORTA,clearLN |
; then high again |
; Init counter |
||
movlw |
0x08 |
; Initialize bit counter |
movwf |
bitCount |
|
sendBit: |
||
bcf |
PORTA,dataLN |
; Set data line low (assume) |
;Using this assumption is possible because the bit is not
;shifted in until the clock line is pulsed.
btfsc |
dataReg,highBit |
; |
test number bit 7 |
|
bsf |
PORTA,dataLN |
; |
Change assumption |
if set |
;=========================
;pulse clock line ;=========================
;Bits are shifted in by pulsing the 74HC164 CLK line
bsf |
PORTA,clockLN |
; CLK high |
|
bcf |
PORTA,clockLN |
; CLK low |
|
;========================= |
|||
; Rotate data bits left |
|||
;========================= |
|||
rlf |
dataReg,f |
; Shift left data bits |
|
decfsz |
bitCount,f |
; Decrement bit counter |
|
goto |
sendBit |
; Repeat if not 8 bits |
|
;========================= |
|||
; |
end of transmission |
||
;========================= |
|||
return |
|||
;============================================================
;74HC165 procedure to read parallel data and send
; |
serially to PIC |
;============================================================
404 |
Chapter 14 |
;OPERATION:
;1. Eight DIP switches are connected to the input
;ports of an 74HC165 IC. Its output line Hout,
;and its control lines CLK and load are connected
;to the PIC’s Port-B lines 0, 1, and 2
;respectively
;2. Procedure sets a counter (bitCount) for 8
;iterations and clears a data holding register
;(dataReg).
;3. Port-B bits are read into w. Only the lsb of
;Port-B is relevant. Value is stored in a working
;register and the meaningful bit is rotated into
;the carry flag, then the carry flag bit is
;then shifted into the data register.
;4. The iteration counter is decremented. If this
;is the last iteration the routine ends. Otherwise
;the bitwise read-and-write operation is repeated.
in165: |
||
clrf |
dataReg |
; Clear data register |
movlw |
0x08 |
; Initialize counter |
movwf |
bitCount |
|
bcf |
PORTB,loadLN |
; Reset shift register |
bsf |
PORTB,loadLN |
|
nextBit: |
||
movf |
PORTB,w ; Read Port-B (only LOB is |
|
; meaningful in this routine) |
||
movwf |
workReg |
; Store value in local |
; register |
||
rrf |
workReg,f |
; Rotate LOB bit into carry |
; flag |
||
rlf |
dataReg,f |
; Carry flag into dataReg |
decfsz |
bitCount,f |
; Decrement bit counter |
goto |
shiftBits |
; Continue if not zero |
return |
; done |
|
shiftBits: |
||
bsf |
PORTB,clk65LN |
; Pulse clock |
bcf |
PORTB,clk65LN |
|
goto |
nextBit |
; Continue |
;========================================================= |
||
; |
end of program |
|
;======================================================== end
14.5.4 TTYUsart Program
;File name: TTYUsart.asm
;Last update: May, 2006
Communications |
405 |
; 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 |
; For one full baud rate delay:
; |
255 - 208 = 47 + 1 = 48 |