Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8667
Скачиваний: 0
726 |
Appendix D |
; Send STOP. Wait for ACK |
|
bsf |
SSPCON2,PEN ; Send STOP condition |
call |
WaitI2C ; Wait for I2C operation to complete |
; WRITE operation has completed successfully. Bank0
return
;============================
;I2C read procedure ;============================
;Procedure to read one byte from 24LC04B EEPROM
;Steps:
; |
1. |
Send START |
; |
2. |
Send control. Wait for ACK |
; |
3. |
Send address. Wait for ACK |
; |
4. |
Send RESTART + control. Wait for ACK |
; |
5. |
Switch to receive mode. Get data. |
; |
6. |
Send NACK |
; |
7. |
Send STOP |
; |
8. |
Retreive data into w register |
;STEP 1: ReadI2C
;Send RESTART. Wait for ACK Bank1
bsf |
SSPCON2,RSEN ; RESTART Condition |
call |
WaitI2C ; Wait for I2C operation |
;STEP 2:
;Send control byte. Wait for ACK
movlw |
LC04READ ; Control |
byte |
call |
Send1I2C ; Send Byte |
|
call |
WaitI2C ; Wait for |
I2C operation |
; Now check to see if I2C EEPROM is ready Bank1
btfsc goto
;STEP 3:
;Send address. Wait for ACK
Bank0 |
|||
movf |
EEMemAdd,w |
; Load from address register |
|
call |
Send1I2C |
; Send Byte |
|
call |
WaitI2C |
; Wait for I2C operation |
|
Bank1 |
|||
btfsc |
SSPCON2,ACKSTAT ; Check ACK Status bit |
||
goto |
FailI2C |
; failed, skipped if successful |
|
;STEP 4:
;Send RESTART. Wait for ACK
bsf |
SSPCON2,RSEN ; Generate RESTART Condition |
Supplementary Programs |
727 |
|
call |
WaitI2C ; Wait for I2C operation |
|
; Send output control. Wait for ACK |
||
movlw |
LC04WRITE ; Load CONTROL BYTE (output) |
|
call |
Send1I2C |
; Send Byte |
call |
WaitI2C |
; Wait for I2C operation |
Bank1 |
||
btfsc |
SSPCON2,ACKSTAT ; Check ACK Status bit |
|
goto |
FailI2C ; failed, skipped if successful |
|
;STEP 5:
;Switch MSSP to I2C Receive mode
bsf |
SSPCON2,RCEN ; |
Enable Receive Mode (I2C) |
; Get the data. Wait for ACK |
||
call |
WaitI2C ; Wait |
for I2C operation |
;STEP 6:
;Send NACK to acknowledge Bank1
bsf bsf
;Once ACK or NACK is sent, ACKEN is automatically cleared
;STEP 7:
;Send STOP. Wait for ACK
bsf |
SSPCON2,PEN ; Send |
STOP condition |
call |
WaitI2C ; Wait for |
I2C operation |
;STEP 8:
;Read operation has finished Bank0
movf SSPBUF,W ; Get data from SSPBUF into W
; Procedure has finished and completed successfully. return
;============================
;I2C support procedures ;============================
;I2C Operation failed code sequence
;Procedure hangs up. User should provide error handling. FailI2C
Bank1
bsf |
SSPCON2,PEN ; Send |
STOP condition |
call |
WaitI2C ; Wait for |
I2C operation |
fail:
goto fail
; Procedure to transmit one byte Send1I2C
Bank0
movwf SSPBUF ; Value to send to SSPBUF return
728 |
Appendix D |
;Procedure to wait for the last I2C operation to complete.
;Code polls the SSPIF flag in PIR1.
WaitI2C
Bank0 |
|||
btfss |
PIR1,SSPIF ; |
Check if I2C operation done |
|
goto |
$-1 |
; I2C module is not ready yet |
|
bcf |
PIR1,SSPIF ; |
I2C ready, clear flag |
|
return |
|||
;============================================================
end ; END OF PROGRAM
;============================================================
Supplementary Programs |
729 |
;File name: Key2LCD.asm
;Date: May 11, 2006
;Author: Julio Sanchez
;Description:
;Decode 4 x 4 keypad and display scan code in LCD.
;Program to use 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 wiring and base
;address of each display line is stored in #define
;statements. These statements can be edited to
;accommodate a different set-up.
;Keypad switch wiring (values are scan codes):
;--- KEYPAD --
; |
0 |
1 |
2 |
3 |
<= port B0 | |
|
; |
4 |
5 |
6 |
7 |
<= port B1 |--- ROWS = OUTPUTS |
|
; |
8 |
9 |
A |
B |
<= port B2 | |
|
; |
C |
D |
E |
F |
<= port B3 |
| |
; |
| |
| |
| |
| |
||
; |
| |
| |
| |
|_____ port B4 |
| |
|
; |
| |
| |
|_________ port B5 |
|--- COLUMNS = INPUTS |
||
; |
| |
|_____________ port B6 |
| |
|||
;|_________________ port B7 |
;Program operations:
;1. Key press action generates a scan code in the range
;0x0 to 0xf.
;2. Program converts scan code to ASCII digit and displays
;the digit on the LCD.
;3. When the end of the first LCD line is reached, display
;continues in the second line. When the end of the
;second line is reached, LCD is reset to the first line
;Program uses delay loops for interface timing.
;WARNING:
;Code assumes 4Mhz clock. Delay routines must be
;edited for faster clock
;
;===========================
;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 |
730 |
Appendix D |
|
; |
* _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 |
occilator |
|
; |
_XT_OSC |
External parallel |
resonator/crystal oscillator |
|
; * _HS_OSC |
High speed |
crystal |
resonator |
|
; |
_RC_OSC |
Resistor/capacitor |
oscillator |
|
; | |
(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.
;============================================================
; |
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
;Note: The constants that define the LCD display line
;addresses have the high-order bit set in
;order to facilitate the controller command
;
;============================================================
; |
PIC register equates |
;============================================================
portd |
equ |
0x08 |
porte equ 0x09
Supplementary Programs |
731 |
||
fsr |
equ |
0x04 |
|
status |
equ |
0x03 |
|
indf |
equ |
0x00 |
|
z |
equ |
2 |
|
c |
equ |
0 |
|
;=============================================================
; variables in PIC RAM
;=============================================================
;Reserve 20 bytes for string buffer cblock 0x20
strData endc
;Leave 16 bytes and Continue with local variables
cblock 0x34 count1 count2 count3 pic_ad
J
K index
store1
store2
; For LCDscroll procedure
LCDcount ; Counter for characters per line
LCDline |
; Current display line (0 or 1) |
; Keypad processing variables |
|
keyMask |
; For keypad processing |
rowMask |
; For masking-off key rows |
rowCode |
; Row addend for calculating scan code |
rowCount ; Counter for key rows (0 to 3) |
|
scanCode ; Final key code |
|
newScan |
; 0 if no new scan code detected |
endc |
|
;============================================================
; M A C R O S
;============================================================
;Macros to select the register banks
;Data memory bank selection bits:
; RP1:RP0 |
Bank |
|||
; |
0:0 |
0 |
Ports A,B,C,D, and E |
|
; |
0:1 |
1 |
Tris A,B,C,D, and E |
|
; |
1:0 |
2 |
||
; |
1:1 |
3 |
||
Bank0 |
MACRO |
; Select RAM bank 0 |
||
bcf |
STATUS,RP0 |
|||