Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8669
Скачиваний: 0
568 |
Chapter 16 |
16.4 Sample Programs
The following sections contain the sample programs discussed in this chapter.
16.4.1 ADF84 Program
;File name: ADCF84.asm
;Last Update: June 8, 2006
;Author: Julio Sanchez
;Processor: 16F84A
;Description:
;Program to demonstrate use of the ADC0831 Analog to
;Digital converter with the 16F84 PIC. Program reads the
;value of a potentionmeter connected to Port-A, line 0
;and displays resistance in the range 0 to 255 on the
;attached LCD.
;Circuit:
; |
ADC0831 |
16F84 |
CIRCUIT |
; PIN LINE
;6 DO ------------- RB0
;7 CLK ------------- RB1
;1 CS ------------- RB2
;2 Vin+ ------------------------------ POT2
;3 Vin- ------------------------------ GND
;5 Vref ------------------------------ +5v
;8 Vcc ------------------------------ +5v
;For LCD display parameters see the LCDTest2 program.
;WARNING:
;Code assumes 4Mhz clock. Delay routines must be
;edited for faster clock
;
;===========================
;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 |
oscillator |
|
; * _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 |
||
Analog to Digital and Realtime Clocks |
569 |
|
; | |
(simplest, 20% error) |
|
;|
;|_____ * indicates setup values presently selected
;========================= ; setup and configuration ;=========================
processor |
16f84A |
include |
<p16f84A.inc> |
__config |
_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF |
errorlevel -302
; Suppress bank-related warning
;============================================================
; M A C R O S
;============================================================ ; Macros to select the register banks in 16F84
Bank0 |
MACRO |
; Select RAM bank 0 |
|
bcf |
STATUS,RP0 |
||
ENDM |
|||
Bank1 |
MACRO |
; Select RAM bank 1 |
|
bsf |
STATUS,RP0 |
||
ENDM |
|||
;===================================================== |
|||
; |
constant definitions |
||
; for PIC-to-LCD pin wiring and LCD line addresses |
|||
;===================================================== |
|||
#define E_line 1 |
;| |
||
#define RS_line 2 |
;| — from circuit wiring diagram |
||
#define RW_line 3 |
;| |
||
; LCD line addresses (from LCD data sheet) |
|||
#define LCD_1 0x80 |
; First LCD line constant |
||
#define LCD_2 0xc0 |
; Second LCD line constant |
||
;Note: The constants that define the LCD display line
;addresses have the high-order bit set in
;order to facilitate the controller command
;Defines from ADC0831 wiring diagram
;all lines in Port-A
#define |
DO |
0 |
;| |
#define |
CLK |
1 |
;| — from circuit wiring diagram |
#define |
CS |
2 |
;| |
; |
|||
;===================================================== |
|||
; |
variables in PIC RAM |
||
;=====================================================
; Reserve 16 bytes for string buffer
Analog to Digital and Realtime Clocks |
571 |
|
call |
delay_5 |
|
call |
initLCD |
; Then do forced |
initialization |
||
call |
delay_5 |
; Wait again |
; Store base address of text buffer in PIC RAM |
||
movlw |
0x0c |
; Start address for buffer |
movwf |
pic_ad |
; to local variable |
;======================
;first LCD line ;======================
;Store 16 blanks in PIC RAM, starting at address stored
;in variable pic_ad
call blank16
;Call procedure to store ASCII characters for message
;in text buffer
movlw |
d’0’ |
; Offset into buffer |
call |
storeMS1 ; Store message text in buffer |
|
; Initialize ADC0831 |
||
nextAna: |
||
call |
ana2dig |
; Read analog line |
call |
delay_125 |
|
; Display result |
||
movf |
rcvdata,w |
|
call |
bin2asc |
; Conversion routine |
;At this point three ASCII digits are stored in local
;variables. Move digits to display area
movf |
asc1,w |
; Unit digit |
movwf |
.26 |
; Store in buffer |
movf |
asc10,w |
; same with other digits |
movwf |
.25 |
|
movf |
asc100,w |
|
movwf |
.24 |
;Display line
;Set DDRAM address to start of first line
call line1
; Call procedure to display 16 characters in LCD call display16
call long_delay goto nextAna
;============================================================ ; initialize LCD for 4-bit mode ;============================================================ initLCD:
;Initialization for Densitron LCD module as follows:
;4-bit interface
;2 display lines of 16 characters each
;cursor on
572 |
Chapter 16 |
;left-to-right increment
;cursor shift right
;no display shift ;=======================|
; |
set command mode |
| |
||
;=======================| |
||||
bcf |
PORTA,E_line |
; E line low |
||
bcf |
PORTA,RS_line |
; RS line low |
||
bcf |
PORTA,RW_line |
; Write mode |
||
call |
delay_125 |
; delay 125 microseconds |
||
;***********************| |
||||
; |
FUNCTION SET |
| |
||
;***********************| |
||||
movlw |
0x28 |
; 0 0 1 0 1 0 0 0 (FUNCTION SET) |
||
call |
send8 |
; 4-bit send routine |
||
; Set 4-bit mode command must be repeated |
||||
movlw |
0x28 |
|||
call |
send8 |
|||
;***********************| |
||||
; DISPLAY AND CURSOR ON | |
||||
;***********************| |
||||
movlw |
0x0e |
; 0 0 0 0 1 1 1 0 (DISPLAY ON/OFF) |
||
call |
send8 |
|||
;***********************| |
||||
; |
set entry mode |
| |
||
;***********************| |
||||
movlw |
0x06 |
; 0 0 0 0 0 1 1 0 (ENTRY MODE SET) |
||
call |
send8 |
|||
;***********************| |
||||
; cursor/display shift |
| |
|||
;***********************| |
||||
movlw |
0x14 |
; 0 0 0 1 0 1 0 0 (CURSOR/DISPLAY |
||
SHIFT) |
||||
call |
send8 |
|||
;***********************| |
||||
; |
clear display |
| |
||
;***********************| |
||||
movlw |
0x01 |
; 0 0 0 0 0 0 0 1 (CLEAR DISPLAY) |
||
call |
send8 |
|||
; Per documentation |
||||
call |
delay_5 |
; Test for busy |
||
return |
||||
;======================= |
||||
; |
Procedure to delay |
|||