Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8524
Скачиваний: 0
396 |
Chapter 14 |
;|
;|_____ * 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
;============================================================ ; 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 pin wiring
;=====================================================
#define readySW 2 |
;| |
|
#define |
readyLED 3 |
;| — from wiring diagram |
#define |
serialLN 0 |
;| |
;=====================================================
; PIC register and flag equates
;=====================================================
c |
equ |
0 |
; |
Carry |
flag |
tmrOVF |
equ |
2 |
; |
Timer |
overflow bit |
; |
;======================================================
; variables in PIC RAM
;=====================================================
cblock 0x0c |
; |
Start of block |
bitCount |
; |
Counter for 8 bits |
rcvReg |
; Data to send |
|
temp |
||
endc |
;=========================================================
; program
;=========================================================
org |
0 |
; start at address |
goto |
main |
|
; Space for interrupt handlers |
||
org |
0x04 |
|
Communications |
397 |
main:
Bank1
; Port-A bits 0 and 2 are input. All others are output
movlw |
b’00000101’ ; Port-A setup |
movwf |
TRISA |
; Port-B is all output |
|
movlw |
b’00000000’ ; Port-B setup |
MOVWF |
TRISB |
Bank0 |
||
; |
Turn off all |
Port-B LEDs |
clrf |
PORTB |
|
; |
And receiver |
register |
clrf |
rcvReg |
;Prepare to set prescaler clrf TMR0 clrwdt
;Setup OPTION register for full timer speed
movlw |
b’11011000’ |
||||||||||
; |
1 |
1 |
0 |
1 |
1 |
0 0 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 |
|||||||
; |
| |
*1 |
= |
raising edge |
|||||||
;|__________________________ RBPU pullups
; |
0 |
= enabled |
|
; |
*1 |
= disabled |
|
option |
|||
; Disable interrupts |
|||
bcf |
INTCON,5 |
; Timer0 overflow disabled |
|
bcf |
INTCON,7 |
; Global interrupts disabled |
|
;=========================
;wait for READY switch
;to be pressed ;=========================
398 |
Chapter 14 |
||
ready2rcv: |
|||
btfsc |
PORTA,readySW |
; Test switch |
|
goto |
ready2rcv |
; loop |
|
; Turn ON the ready-to-receive LED |
|||
bsf |
PORTA,readyLED |
||
;=========================== |
|||
; |
receiving |
||
;=========================== |
|||
call |
rcvData |
; Call serial input procedure |
|
;=========================== |
|||
;data received ;===========================
;Turn ready to receive LED off
bcf |
PORTA,readyLED |
||
; Display received data |
|||
movf |
rcvReg,w |
; Byte received to w |
|
movwf |
PORTB |
; display in Port-B |
|
;=========================== |
|||
; |
wait forever |
||
;=========================== |
|||
endloop: |
|||
goto |
endloop |
||
;============================================================
; |
procedure to receive serial data |
;============================================================
;ON ENTRY:
;local variable dataReg is used to store 8-bit value
;received through port (labeled serialLN)
;OPERATION:
;1. The timer at register TMR0 is set to run at
;maximum clock speed, that is, 256 clock beats.
;The timer overflow flag in the INTCON register
;is set when the timer cycles from 0xff to 0x00.
;2. When the START signal is received, the code
;waits for 128 timer beats so as to read data in
;the middle of the send period.
;3. Each bit (start, data, and stop bits) is read
;at intervals of 256 timer beats.
;4. The procedure tests the timer overflow flag
;(tmrOVF) to determine when the timer cycle has
;ended, that is when 256 clock beats have passed.
;=============================================================
rcvData:
clrf |
TMR0 |
; |
Reset timer |
movlw |
0x08 |
; |
Initialize bit counter |
movwf |
bitCount |
;=========================
Communications |
399 |
; wait for START bit
;=========================
startWait: |
|||
btfsc |
PORTA,0 |
; |
Is port A0 low? |
goto |
startWait |
; |
No. Wait for mark |
;=========================
;offset 128 clock beats ;=========================
;At this point the receiver has found the falling
;edge of the start bit. It must now wait 128 timer
;beats to synchronize in the middle of the sender’s
;data rate, as follows:
; |
|<========= |
falling edge of START bit |
; |
| |
|
; |
|-----|<====== 128 clock beats offset |
|
; |
--------- | |
.--------- |
; |
| |
| <== SIGNAL |
; |
----------- |
|
; |
|<-- 256--->| |
|
; |
||
movlw |
0x80 |
; 128 clock beats offset |
|||
movwf |
TMR0 |
; to TMR0 counter |
|||
bcf |
INTCON,tmrOVF |
; Clear overflow flag |
|||
offsetWait: |
|||||
btfss |
INTCON,tmrOVF |
; timer overflow? |
|||
goto |
offsetWait |
; Wait until |
|||
btfsc |
PORTA,0 |
; Test start bit for error |
|||
goto |
offsetWait |
; Recycle if a false start |
|||
;========================== |
|||||
; |
receive data |
||||
;========================== |
|||||
clrf |
TMR0 |
; Restart timer |
|||
bcf |
INTCON,tmrOVF |
; Clear overflow flag |
|||
; Wait for 256 timer cycles for first/next data bit |
|||||
bitWait: |
|||||
btfss |
INTCON,tmrOVF |
; Timer cycle end? |
|||
goto |
bitWait |
; Keep waiting |
|||
; Timer has counter 256 beats |
|||||
bcf |
INTCON,tmrOVF |
; Reset overflow flag |
|||
movf |
PORTA,w |
; Read Port-A into w |
|||
movwf |
temp |
; Store value read |
|||
rrf |
temp,f |
; Rotate bit 0 into carry flag |
|||
rlf |
rcvReg,f |
; Rotate carry into rcvReg 0 |
|||
decfsz |
bitCount,f |
; 8 bits received |
|||
goto |
bitWait |
; Next bit |
|||
; Wait for one time cycle at end of reception |
|||||
markWait: |
|||||
btfss |
INTCON,tmrOVF |
; timer overflow flag |
|||
400 |
Chapter 14 |
|
goto |
markWait ; keep waiting |
|
;======================== |
||
; |
end of reception |
|
;======================== return
;========================================================= ; end of program ;=========================================================
end
14.5.3 Serial6465 Program
;File name: Serial6465.asm
;Last update: May 7, 2006
;Author: Julio Sanchez
;Processor: 16F84A
;Description:
;Program to exercise serial communications using a
;PIC 16F84A and two shift registers: a 74HC164, and a
;74HC165. The 74HC165 inputs 8 lines from a DIP switch
;and transmits settings to PIC through a serial line.
;PIC sends data serially to an 74HC164 which is wired
;to 8 LEDs that display the received data. A total of
;6 PIC lines are used in interfacing 8 input switches
;to 8 output LEDs.
;Circuit:
;* Port A0 is the serial transmission line which
;comes from the 74HC165.
;* Port A1 is wired to the 74HC164 CLOCK pin
;* Port A2 is wired to the 74HC164 CLEAR pin
;* 74HC164 output pins 0 to 7 are wired to LEDs.
;* Port B0 is wired to the 74HC165 Hout line
;* Port B1 is wired to the 74HC165 CLK line
;* Port B2 is wired to the 74HC165 load line
;* A pushbutton switch is in the 16F84 RESET line
;and serves to restart the program
;Communications protocol:
;Communication between PIC and the 74HC164 and
;74HC165 is synchronous since the shift registers
;clock lines serve to shift in and out the data
;bits.
;
;===========================
;switches ;===========================
;Switches used in __config directive:
; |
_CP_ON |
Code protection ON/OFF |