Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8666
Скачиваний: 0
700 |
Appendix D |
; Assumes: |
|
; |
2400 baud |
; |
8 data bits |
; |
no parity |
; |
one stop bit |
initTTY: |
;First initialize receiver to RS-232 line parameters
;Disable global and peripheral interrupts
; |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
<= |
INTCON bitmap |
|
; |
| |
? |
| |
? |
? |
? |
? |
? |
(? |
= unrelated bits) |
|
; |
| |
|________________ |
Timer0 interrupt |
on overflow |
|||||||
;|______________________ Global interrupts
bcf |
INTCON,5 |
; Disable TMR0 interrupts |
||||||||||
bcf |
INTCON,7 |
; Disable global interrupts |
||||||||||
clrf |
TMR0 |
; Reset timer |
||||||||||
clrwdt |
; Clear WDT for prescaler |
|||||||||||
; assign |
||||||||||||
Bank1 |
||||||||||||
; Set up the |
OPTION register bit map |
|||||||||||
; |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 <= OPTION bits |
||||
; |
1 |
1 |
0 |
1 |
1 |
0 |
0 |
0 <= setup |
||||
; |
| |
| |
| |
| |
| |
|__|__|_____ |
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 (Pullup enable)
; |
0 |
= enabled |
; |
*1 |
= disabled |
movlw |
b'11010000' ; set up timer/counter |
|
movwf |
OPTION_REG |
|
Bank0 |
||
return |
||
;============================================================
; |
receive character |
Supplementary Programs |
701 |
;============================================================
;Receive a single character through the serial port.
;Assumes: 4800 baud, 8 data bits, no parity, 1 stop bit.
;Receiving line is Port A, line 0
rcvTTY:
movlw |
0x08 |
; Counter for 8 bits |
movwf bitCount
;The start of character transmission is signaled by
;the sender by setting the line low
startBit: |
||
btfsc |
PORTA,0 |
; Test for low on line |
goto |
startBit ; Go if not low |
|
;=========================
;offset to data bit ;=========================
;At this point the receiver has found the falling
;edge of the start bit. It must now wait one and
;one-half the baud rate to synchronize in the center
;of the sender's first data bit
;, as follows:
;|<========= falling edge of START bit
; |
| |
|<========== center of start bit |
|
; |
| |
| |
|<====== center of data bit |
;|-----------|-----|
;_____ |
___________ |
__________ |
||||||
; |
| |
| |
| |
| |
<== SIGNAL |
|||
; |
----------- |
---------- |
||||||
; |
|<-- 208 -->|<104>| <====== ms. for |
4800 baud |
||||||
; |
||||||||
; Clock start count for one-half bit |
= 255 |
- 104 |
= |
151 |
||||
; Clock start count for one full bit = |
255 |
- 208 |
= |
47 |
||||
;One clock cycle is added for the movwf intruction:
;clkHalf = 152 (for one-half bit countdown)
;clkFull = 48 (for one full bit countdown)
movlw |
halfBaud ; Skip one-half bit |
||
movwf |
TMR0 |
; Initialize tmr0 and start count |
|
bcf |
INTCON,2 |
; Clear overflow flag |
|
;============================ |
|||
; |
start bit |
||
;============================ |
|||
wait1: |
|||
btfss |
INTCON,2 |
; Timer count overflow? |
|
goto |
wait1 |
; No, keep waiting |
|
; At this point we are at the center of the start bit |
|||
btfsc |
PORTA,0 |
; Check to see it is still low |
|
goto |
startBit ; No, it is high. False start |
||
;At this point the clock is at the center of the start
;bit. The first data bit must be read one full baud
702 |
Appendix D |
|
; period later |
||
movlw |
fullBaud |
; One full bit delay |
movwf |
TMR0 |
; Start timer |
bcf |
INTCON,2 |
; clear tmr0 overflow flag |
wait2: |
||
btfss |
INTCON,2 |
; End of one full baud period? |
goto |
wait2 |
; Wait if not end of period |
;Timer is now at the center of the first/next data bit
;Timer must be reset immediately so that code will not
;lose synchronization with sender
movlw |
fullBaud ; Skip to next data bit |
|||
movwf |
TMR0 |
; Restart timer |
||
bcf |
INTCON,2 |
; Reset overflow flag |
||
; Now the data bit can be read and stored |
||||
movf |
PORTA,w |
; Read port B |
||
movwf |
tempData ; Store in temporary variable |
|||
rrf |
tempData,f |
; Rotate bit 0 into carry flag |
||
rrf |
rcvData,f |
; Rotate carry flag into storage |
||
; register high-order bit |
||||
decfsz |
bitCount,f |
; End of data? |
||
goto |
wait2 |
; Continue until 8 bits received |
||
;============================ |
||||
; |
stop bit |
|||
;============================ |
||||
stopWait: |
||||
btfss |
INTCON,2 |
; Test time |
||
goto |
stopWait ; Wait |
|||
return |
; Exit |
|||
;============================================================ ; send character ;============================================================
;Procedure to send one character through the RS232 line.
;Assumes: 2400 baud, 8 data bits, no parity, one stop bit
;Sending line is Port B, line 0
;ON ENTRY:
;variable sendData holds character to send
sendTTY:
movlw |
0x08 |
; Init bit counter |
movwf |
bitCount |
|
bcf |
PORTB,0 |
; Low for start bit |
movlw |
fullBaud |
; For one baud space |
movwf |
TMR0 |
; Start timer |
bcf |
INTCON,2 |
; Clear timer flag |
start2snd: |
||
btfss |
INTCON,2 |
; Full baud done? |
goto |
start2snd |
; No |
movlw |
fullBaud |
; Reset for one full bit |
Supplementary Programs |
703 |
|||
; period |
||||
movwf |
TMR0 |
; |
Start |
timer |
bcf |
INTCON,2 |
; |
Clear |
flag |
;At this point the start bit has been sent
;Data follows
sendOut: |
|||
rrf |
sendData,f |
; Rotate bit into carry |
|
bcf |
PORTB,0 |
; Assume data bit is 0 |
|
btfsc |
STATUS,c ; Test if carry set |
||
bsf |
PORTB,0 |
; Change bit to 1 if clear |
|
; Hold bit for 1 baud period |
|||
timeBit: |
|||
btfss |
INTCON,2 |
; Wait for baud period to end |
|
goto |
timeBit |
; Loop if not yet |
|
movlw |
fullBaud |
; Reset timer |
|
movwf |
TMR0 |
; Start timer |
|
bcf |
INTCON,2 |
; Clear flag |
|
; Test for last bit |
|||
decfsz |
bitCount,f |
; Count this bit |
|
goto |
sendOut |
; Continue if not last bit |
|
; Done. Send stop bit |
|||
bsf |
PORTB,0 |
; High for stop bit |
|
stopBit: |
|||
btfss |
INTCON,2 |
; Timer done? |
|
goto |
stopBit |
; No |
|
; Set port B line 0 high back again |
|||
bsf |
PORTB,0 |
||
call |
delay_5 |
; And hold |
|
return |
|||
End
704 |
Appendix D |
;File: Watchdog.asm
;Date: May 2, 2006
;Author: Julio Sanchez
;Description:
;Program to demonstrate the use of the watchdog timer
;in breaking out of an endless loop.
;A LED on port B, line 1, flashes on and off at 1/2
;second intervals for 20 iterations. At that time the
;program enters an endless loop. The watchdog timer
;times-out and restarts the program ;===========================
;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 |
||
;|
;|_____ * indicates setup values
;========================= ; setup and configuration ;=========================
processor 16f84A |
||
include |
<p16f84A.inc> |
|
__config |
_XT_OSC & _WDT_ON & _PWRTE_ON & _CP_OFF |
|
;===================================================== |
||
; |
PIC register equates |
|
;===================================================== |
||
porta |
equ |
0x05 |
Portb |
equ |
0x06 |
status |
equ |
0x03 |
z |
equ |
0x02 |
tmr0 |
equ |
0x01 |
; |
||
;===================================================== |
||
; |
variables in PIC RAM |
|