Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8531
Скачиваний: 0
Communications |
391 |
ENDM
;=====================================================
; constant definitions for pin wiring
;=====================================================
#define readySW 2 |
;| |
|
#define |
readyLED 3 |
;| — from wiring diagram |
#define |
serialLN 1 |
;| |
;=====================================================
; PIC register flag equates
;=====================================================
c |
equ |
0 |
; |
Carry |
flag |
tmrOVF |
equ |
2 |
; |
Timer |
overflow bit |
;======================================================
; variables in PIC RAM
;=====================================================
cblock 0x0d |
; Start of block |
bitCount ; Counter for 8 bits |
|
dataReg |
; Data to send |
endc |
|
;=========================================================
; program
;=========================================================
org |
0 |
; start at address |
goto |
main |
|
; Space for interrupt handlers |
||
org |
0x04 |
|
main:
; Port-A, bit 2 is input. Rest is output
Bank1 |
||
movlw |
b’00000100’ |
; Port-A bit 2 is input |
; all others are output |
||
movwf |
TRISA |
|
; Port-B is all input |
||
movlw |
b’11111111’ |
|
movwf |
TRISB |
|
Bank0 |
||
bsf |
PORTA,1 |
;Marking bit |
;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 |
392 |
Chapter 14 |
||||||||
; |
| |
| |
| |
| |
| |
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 interupts disabled |
|
; Turn on ready LED |
|||
bsf |
PORTA,3 |
; LED on |
|
;=========================== |
|||
;wait for READY switch
;to be pressed ;=========================== ready2send:
btfsc PORTA,readySW goto ready2send
;===========================
;send serial data ;===========================
;At this point program proceeds to send data through
;the serial port line
;Turn off LED
bcf PORTA,readyLED
; Read switches and store in local variable movf PORTB,w
movwf dataReg ;===========================
;call serial output
;procedure ;===========================
call |
sendData ; call serial output procedure |
|
;=========================== |
||
; |
wait forever |
|
Communications |
393 |
;===========================
endloop:
goto endloop
;============================================================ ; procedure to send serial data ;============================================================
;ON ENTRY:
;local variable dataReg holds 8-bit value to be
;transmitted 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. Each bit (start, data, and stop bits) is sent
;at a rate of 256 timer beats. That is, each bit is
;held high or low for one full timer cycle (256
;clock beats).
;3. The procedure tests the timer overflow flag
;(tmrOVF) to determine when the timer cycle has
;ended, that is when 256 clock beats have passed.
;
sendData:
movlw |
0x08 |
; Setup shift counter |
movwf bitCount
;=======================
;send START bit ;=======================
;Set line low then hold for 256 timer clock beats.
bcf |
PORTA,serialLN |
; Send start bit |
; First reset timer |
||
clrf |
TMR0 |
; Reset timer counter |
bcf |
INTCON,tmrOVF |
; Reset TMR0 overflow flag |
; Wait for 256 timer clock beats |
||
startBit: |
||
btfss |
INTCON,tmrOVF |
; timer overflow? |
goto |
startBit ; Wait until set |
|
; At this point timer has cycled. Start bit has ended |
||
bcf |
INTCON,tmrOVF |
; Clear overflow flag |
;======================== |
||
;send 8 DATA bits ;========================
;Eight data bits are sent through the serial line
;starting with the high-order bit. The data byte is
;stored in the register named dataReg. The bits are
;rotated left to the carry flag. Code assumes the bit
;is zero and sets the serial line low. Then the carry
394 |
Chapter 14 |
;flag is tested. If the carry is set the serial line
;is changed to high. The line is kept low or high for
;256 timer beats.
send8:
rlf |
dataReg,f |
; bit into carry flag |
bcf |
PORTA,serialLN ; 0 to serial line |
|
;Code can assume the bit is a zero and set the line
;low since, if low is the wrong state, it will only
;remain for two timer beats. The receiver will not
;check the line for data until 128 timer beats have
;elapsed, so the error will be harmless. In any case,
;there is no assurance that the previous line state is
;the correct one, so leaving the line in its previous
;state could also be wrong.
btfsc |
STATUS,c |
; test carry flag |
||
bsf |
PORTA,serialLN |
; bit |
is set. Fix error. |
|
bitWait: |
||||
btfss |
INTCON,tmrOVF |
; |
Timer cycled? |
|
goto |
bitWait |
; |
not |
yet |
;At this point timer has cycled.
;Test for end of byte, if not, send next bit
bcf |
INTCON,tmrOVF |
; clear overflow flag |
|
decfsz |
bitCount,f |
; |
Last bit? |
goto |
send8 |
; |
not yet |
;=========================
;hold MARKING state ;=========================
;All 8 data bits have been sent. The serial line must
;now be held high (MARKING) for one clock cycle
bsf |
PORTA,serialLN ; Marking state |
||
markWait: |
|||
btfss |
INTCON,tmrOVF |
; Done? |
|
goto |
markWait ; not yet |
||
;========================= |
|||
; |
end of transmission |
||
;========================= |
|||
return |
; done |
||
;========================================================= ; end of program ;=========================================================
end
14.5.2 SerialRcv Program
;File name: SerialRcv.asm
;Date: May 6, 2006
;Author: Julio Sanchez
Communications |
395 |
;Processor: 16F84A
;Description:
;Two programs to exercise serial communications between
;two PIC 16F84A both running at 4Mhs. One program sends
;data through a single line and the other one receives
;it. This program is the receiver.
;
;Circuit:
;Port A0 is the serial transmission line
;Port A2 is an active-low pushbutton switch that
; |
serves to initiate communications. |
;Port A3 is a LED that is ON when the program is
; |
ready |
to receive data. |
Once data |
starts |
; |
being |
received the LED |
is turned |
OFF. |
;Port-B0-B7 are 8 LEDs that display the data bits
; |
that have been received. |
;A pushbutton switch is in the 16F84 RESET line
; |
and serves to restart the program |
;
;Communications parameters:
;Timer channel TMR0 is used for synchronizing data
;transmission. The timer runs at the maximum rate of
;256 cycles per iteration. In a 4Mhz system the
;timer rate is 1Mhz, thus the bit rate is
; |
1,000,000/256 |
;which is approximately 3,906 microseconds per bit.
;Upon receiving the START bit, the program waits for
;one half a clock cycle (128 timer beats) to
;synchronize with the sender. ;===========================
;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) |
|||