Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8668
Скачиваний: 0
578 |
Chapter 16 |
;ON EXIT:
;output variables asc100, asc10, and asc1 have
;three ASCII decimal digits
;Routine logic:
;The value 100 is subtracted from the source operand
;until the remainder is < 0 (carry cleared). The number
;of subtractions is the decimal hundreds result. 100 is
;then added back to the subtrahend to compensate
;for the last subtraction. Now 10 is subtracted in the
;same manner to determine the decimal tenths result.
;The final remainder is the decimal units result.
;Variables:
; |
inNum |
storage for source operand |
;asc100 storage for hundreds position result
; |
asc10 |
storage for tenth position result |
||
; |
asc1 |
storage for unit position result |
||
; |
thisDig |
Digit counter |
||
bin2asc: |
||||
movwf |
inNum |
; Save copy of source value |
||
clrf |
asc100 |
; Clear hundreds storage |
||
clrf |
asc10 |
; Tens |
||
clrf |
asc1 |
; Units |
||
clrf |
thisDig |
|||
sub100: |
||||
movlw |
.100 |
|||
subwf |
inNum,f |
; Subtract 100 |
||
btfsc |
STATUS,C |
; Did subtract overflow? |
||
goto |
bump100 |
; No. Count subtraction |
||
goto |
end100 |
|||
bump100: |
||||
incf |
thisDig,f |
;increment digit counter |
||
goto |
sub100 |
|||
; Store 100th digit |
||||
end100: |
||||
movf |
thisDig,w |
; Adjusted digit counter |
||
addlw |
0x30 |
; Convert to ASCII |
||
movwf |
asc100 |
; Store it |
||
; Calculate tenth position value |
||||
clrf |
thisDig |
|||
; Adjust minuend |
||||
movlw |
.100 |
; Minuend |
||
addwf |
inNum,f |
; Add value to minuend to |
||
; compensate for last |
||||
operation |
||||
sub10: |
||||
movlw |
.10 |
|||
subwf |
inNum,f |
; Subtract 10 |
||
btfsc |
STATUS,C |
; Did subtract overflow? |
||
Analog to Digital and Realtime Clocks |
579 |
|
goto |
bump10 |
; No. Count subtraction |
goto |
end10 |
|
bump10: |
||
incf |
thisDig,f |
;increment digit counter |
goto |
sub10 |
|
; Store 10th digit |
||
end10: |
||
movlw |
.10 |
|
addwf |
inNum,f |
; Adjust for last subtract |
movf |
thisDig,w |
; get digit counter contents |
addlw |
0x30 |
; Convert to ASCII |
movwf |
asc10 |
; Store it |
; Calculate and store units digit |
||
movf |
inNum,w |
; Store units value |
addlw |
0x30 |
; Convert to ASCII |
movwf |
asc1 |
; Store digit |
return |
||
;========================================================== ; ADC0831 procedures ;========================================================== ;============================
;procedure to read and
;convert analog line ;============================
;ON ENTRY:
;Code assumes that the ADC0831 DO line is initialized for
;input, while CLK and CS lines are output
;From ADC0831 wiring diagram. All lines in Port-B
; |
DO |
= |
RB0 |
==> INPUT |
|
; |
CLK |
= |
RB1 |
<== |
OUTPUT |
; |
CS |
= |
RB2 |
<== |
OUTPUT |
;ON EXIT:
;Returns 8-bit digital value in the register rcvdata
ana2dig:
;Clear data register and init counter for 8 bits
clrf |
rcvdata |
; Clear register |
movlw |
0x08 |
; Initialize counter |
movwf |
bitCount |
|
; Prepare to read analog line |
||
bcf |
PORTB,CS ; CS pin low to enable ADC |
|
nop |
; Delay for 4Mhz clock |
|
bsf |
PORTB,CLK |
; Set CLK high |
nop |
||
bcf |
PORTB,CLK |
; Reset CLK to start |
conversion |
||
nop |
||
580 Chapter 16
nextB:
; Pulse CLK line to read bit from ADC
bsf |
PORTB,CLK |
; CLK high |
nop |
||
bcf |
PORTB,CLK |
; CLK low |
nop |
||
; Read analog line and store data, bit by bit |
||
movf |
PORTB,w ; Read all Port-B bits |
|
movwf |
store1 |
; Store value for later |
rrf |
store1,f ; Rotate bit into carry flag |
|
rlf |
rcvdata,f |
; Rotate carry flag into |
result |
||
; register |
||
decfsz |
bitCount,f |
; Bump counter, skip next |
; if counter zero |
||
goto |
nextB |
|
; Value read is stored in rcvdata register |
||
bsf |
PORTB,CLK |
; Final clock pulse |
nop |
||
bcf |
PORTB,CLK |
|
nop |
||
bsf |
PORTB,CS ; Turn off ADC |
|
call |
long_delay |
; Time to settle |
return |
||
end
16.4.2 A2DinLCD Program
;File name: A2DinLCD.asm
;Last revision: June 2, 2006
;Author: Julio Sanchez
;Processor: 16F877
;Description:
;Program to demonstrate use of the Analog to Digital
;Converter (A/D) module on the 16F877. 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.
;
;WARNING:
;Code assumes 10Mhz clock. Delay routines must be
;edited for faster clock. Clock speed is also used to
;set up the A/D converter clock.
;
;===========================
; 16F877 switches
582 |
Chapter 16 |
||
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 .64; For 2400 baud on 10Mhz clock |
||
;Note: The constants that define the LCD display
;line addresses have the high-order bit set
;so as to meet the requirements of controller
;commands.
;=====================================================
; variables in PIC RAM
;=====================================================
;Reserve 20 bytes for string buffer cblock 0x20
strData endc
;Reserve three bytes for ASCII digits cblock 0x34
asc100
asc10
asc1 endc
;Data
cblock 0x37 |
; Start of block |
|||
count1 |
; Counter # 1 |
|||
count2 |
; |
Counter |
# |
2 |
count3 |
; |
Counter |
# |
3 |
pic_ad |
||||