Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8618
Скачиваний: 0
436 |
Chapter 14 |
|||||
; |
| |
| |
* |
1 |
= |
transmit enabled |
; |
| |
| |
0 |
= |
transmit disabled |
|
; |
| |
|___________________ |
TX9 |
Enable 9-bit Transmit |
||
; |
| |
1 |
= |
9-bit transmission mode |
||
; |
| |
* |
0 |
= |
8-bit mode |
|
;|______________________ CSRC Clock Source Select
; |
Not |
used in asynchronous mode |
||||||||
; |
Synchronous mode: |
|||||||||
; |
1 |
= Master Mode (internal clock) |
||||||||
; |
* 0 |
= Slave mode (external clock) |
||||||||
; Setup value: 0010 0000 = 0x20 |
||||||||||
movlw |
0x20 |
; Enable transmission and high baud |
||||||||
; rate |
||||||||||
movwf |
TXSTA |
|||||||||
Bank0 |
; Bank 0 |
|||||||||
; RCSTA (Receive Status and |
Control Register) bit map: |
|||||||||
; |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
<== |
bits |
; |
| |
| |
| |
| |
| |
| |
| |
|______ RX9D 9th data bit received |
||
; |
| |
| |
| |
| |
| |
| |
| |
? |
(can be parity bit) |
|
; |
| |
| |
| |
| |
| |
| |
|_________ |
OERR Overrun errror |
||
; |
| |
| |
| |
| |
| |
| |
? |
1 = error (cleared by software) |
||
; |
| |
| |
| |
| |
| |
|____________ |
FERR Framing Error |
|||
; |
| |
| |
| |
| |
| |
? |
1 = error |
|||
; |
| |
| |
| |
| |
|_______________ NOT USED |
|||||
; |
| |
| |
| |
|____________ CREN |
Continuous Receive Enable |
|||||
; |
| |
| |
| |
Asynchronous mode: |
||||||
; |
| |
| |
| |
* |
1 = Enable continuous receive |
|||||
; |
| |
| |
| |
0 = Disables continuous receive |
||||||
; |
| |
| |
| |
Synchronous mode: |
||||||
; |
| |
| |
| |
1 = Enables until CREN cleared |
||||||
; |
| |
| |
| |
0 = Disables continuous receive |
||||||
; |
| |
| |
|_______________ SREN |
Single Receive Enable |
||||||
; |
| |
| |
? |
Asynchronous mode = don’t care |
||||||
; |
| |
| |
Synchronous master mode: |
|||||||
; |
| |
| |
1 = Enable single receive |
|||||||
; |
| |
| |
0 = Disable single receive |
|||||||
; |
| |
|__________________ RX9 9th-bit Receive Enable |
||||||||
; |
| |
1 = 9-bit reception |
||||||||
; |
| |
* 0 = 8-bit reception |
||||||||
;|_____________________ SPEN Serial Port Enable
; |
* 1 |
= RX/DT and TX/CK are serial pins |
; |
0 |
= Serial port disabled |
; Setup value: 1001 0000 = 0x90 |
||
movlw |
0x90 |
; Enable serial port and |
; continuous reception |
||
movwf |
RCSTA |
|
; |
||
Communications |
437 |
||
clrf |
errorFlags |
; |
Clear local error flags |
; |
register |
||
return |
;==============================
;transmit data ;==============================
;Test for Transmit Register Empty and transmit data in w SerialSend:
Bank0 |
; Select bank 0 |
|
btfss |
PIR1,TXIF |
; check if transmitter busy |
goto |
$-1 |
; wait until transmitter is |
; not busy |
||
movwf |
TXREG |
; and transmit the data |
return |
;==============================
;receive data ;==============================
;Procedure to test line for data received and return value
;in w. Overrun and framing errors are detected and
;remembered in the variable errorFlags, as follows:
; |
7 |
6 |
5 4 |
3 |
2 |
1 0 |
<== errorFlags |
; |
— |
not |
used |
—— |
| |
|___ |
overrun error |
; |
|______ framing error |
||||||
SerialRcv: |
||
clrf |
newData ; |
Clear new data received register |
Bank0 |
; |
Select bank 0 |
;Bit 5 (RCIF) of the PIR1 Register is clear if the USART
;receive buffer is empty. If so, no data has been received
btfss |
PIR1,RCIF |
; |
Check for received data |
return |
; |
Exit if no data |
;At this point data has been received. First eliminate
;possible errors: overrun and framing.
;Bit 1 (OERR) of the RCSTA register detects overrun
;Bit 2 (FERR) of the RCSTA register detects framing error
btfsc |
RCSTA,OERR |
; Test |
for overrun error |
|
goto |
OverErr |
; |
Error handler |
|
btfsc |
RCSTA,FERR |
; |
Test |
for framing error |
goto |
FrameErr ; Error handler |
|||
;At this point no error was detected
;Received data is in the USART RCREG register
movf |
RCREG,w |
; get received data |
|
bsf |
newData,7 |
; Set bit 7 to indicate new data |
|
; Clear error flags |
|||
clrf |
errorFlags |
||
return |
|||
;========================== |
|||
; |
error handlers |
||
438 |
Chapter 14 |
||
;========================== |
|||
OverErr: |
|||
bsf |
errorFlags,0 |
; Bit 0 is overrun |
error |
; Reset system |
|||
bcf |
RCSTA,CREN |
; Clear continuous |
receive bit |
bsf |
RCSTA,CREN |
; Set to re-enable |
reception |
return |
|||
;error because FERR framing error bit is set
;can do special error handling here - this code simply clears
; and continues |
||
FrameErr: |
||
bsf |
errorFlags,1 |
; Bit 1 is framing error |
movf |
RCREG,W |
; Read and throw away bad data |
return |
||
end |
14.5.6 SerIntLCD Program
;File name: SerIntLCD.asm
;Last revision: May 14, 2006
;Author: Julio Sanchez
;Processor: 16F877
;Interrupt-driven version of the SerComLCD program
;Description:
;Decode 4 x 4 keypad, display scan code in LCD, and send
;ASCII character through the serial port. Also receive
;data through serial port and display on LCD. LCD lines
;are scrolled by program.
;Default serial line setting:
; |
2400 baud |
|
; |
no parity |
|
; |
1 |
stop bit |
; |
8 |
character bits |
; |
||
;Program to uses 4-bit PIC-to-LCD interface.
;Code assumes that LCD is driven by Hitachi HD44780
;controller and PIC 16F977. Display supports two lines
;each one with 20 characters. The length, wiring and base
;address of each display line is stored in #define
;statements. These statements can be edited to accommodate
;a different setup.
;Keypad switch wiring (values are scan codes):
;—- KEYPAD —
; |
0 |
1 |
2 |
3 |
<= |
port |
B0 |
| |
; |
4 |
5 |
6 |
7 |
<= |
port |
B1 |
|—- ROWS = OUTPUTS |
Communications |
439 |
|||||
; |
8 |
9 |
A |
B |
<= port B2 | |
|
; |
C |
D |
E |
F |
<= port B3 | |
|
; |
| |
| |
| |
| |
||
; |
| |
| |
| |
|_____ port B4 |
| |
|
; |
| |
| |
|_________ port B5 |
|—- COLUMNS = INPUTS |
||
; |
| |
|_____________ port B6 |
| |
|||
;|_________________ port B7 |
;Operations:
;1. Key press action generates a scan code in the range
;0x0 to 0xf.
;2. Scan code is converted to an ASCII digit and displayed
;on the LCD. LCD lines are scrolled as end-of-line is
;reached.
;3. Characters typed on the keypad are also transmitted
;through the serial port.
;4. Received characters generate an interrupt. The interrupt
;handler displays received characters on the LCD.
;
;WARNING:
;Code assumes 4Mhz clock. Delay routines must be
;edited for faster clock. Clock speed also determines
;values for baud rate setting (see spbrgVal constant).
;===========================
;16F877 switches
;===========================
; Switches used in __config directive:
; |
_CP_ON |
Code protection ON/OFF |
|
; * |
_CP_OFF |
||
; |
* |
_PWRTE_ON |
Power-up timer ON/OFF |
;_PWRTE_OFF
; |
_BODEN_ON |
Brown-out reset enable ON/OFF |
|
; * |
_BODEN_OFF |
||
; |
* |
_PWRTE_ON |
Power-up timer enable ON/OFF |
;_PWRTE_OFF
; |
_WDT_ON |
Watchdog timer |
ON/OFF |
; * _WDT_OFF |
|||
; |
_LPV_ON |
Low voltage IC |
programming enable ON/OFF |
; * _LPV_OFF |
|||
; |
_CPD_ON |
Data EE memory |
code protection ON/OFF |
;* _CPD_OFF
;OSCILLATOR CONFIGURATIONS:
; |
_LP_OSC |
Low power crystal |
oscillator |
; |
_XT_OSC |
External parallel |
resonator/crystal oscillator |
; * _HS_OSC |
High speed crystal |
resonator |
|
; |
_RC_OSC |
Resistor/capacitor |
oscillator |
440 |
Chapter 14 |
; | |
(simplest, 20% error) |
;|
;|_____ * indicates setup values presently selected
processor 16f877 ; Define processor
#include <p16f877.inc>
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WDT_OFF & _LVP_OFF & _CPD_OFF
;__CONFIG directive is used to embed configuration data
;within the source file. The labels following the directive
;are located in the corresponding .inc file. ;============================================================
; M A C R O S
;============================================================ ; Macros to select the register banks
Bank0 |
MACRO |
; Select RAM bank 0 |
bcf |
STATUS,RP0 |
|
bcf |
STATUS,RP1 |
|
ENDM |
||
Bank1 |
MACRO |
; Select RAM bank 1 |
bsf |
STATUS,RP0 |
|
bcf |
STATUS,RP1 |
|
ENDM |
||
Bank2 |
MACRO |
; Select RAM bank 2 |
bcf |
STATUS,RP0 |
|
bsf |
STATUS,RP1 |
|
ENDM |
||
Bank3 |
MACRO |
; Select RAM bank 3 |
bsf |
STATUS,RP0 |
|
bsf |
STATUS,RP1 |
|
ENDM |
;=====================================================
; |
constant definitions |
; |
for PIC-to-LCD pin wiring and LCD line addresses |
;=====================================================
#define |
E_line 1 |
;| |
|
#define |
RS_line 0 |
;| — from wiring diagram |
|
#define |
RW_line 2 |
;| |
|
; 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 .20; Number of characters per line |
||
#define |
spbrgVal .25; For 2400 baud on 4Mhz clock |
||
; Note: |
The constants that define the LCD display |
||