You are on page 1of 27

Experiment 1.

Test Port A Bit 0 ON LED First Experiment we will test Port RA by on RA0 that connect to LED the schematic for Experiment 1is shown below in Figure 1

Figure 1.1 schematic for Experiment 1Test ON LED ON Port A Bit 0 Assemble circuit as figure 1.1 and load Programming to PIC16F628 after that on power supply you will see result it Source Code For Experiment 1 list p=16f628 ; list directive to define processor #include <p16f628.inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _MCLRE_ON & _LVP_OFF org clrf movlw movwf bsf movlw movwf bcf bcf goto END 0x000 PORTA 0x07 CMCON STATUS,RP0 b'11111110' PORTA STATUS,RP0 PORTA,0 $

; Initial PORT A ; output data latches ; Turn Comparator off and ; Enable pins for I/O ; Select Bank 1 ; All PORTA = Input ; RA0 = Output ; Return to Bank 0 ; RA0 = 0 LED "ON" ; end of program'
1

Tools & Components Name List MPLAB PIC16F628 4 MHz Crystal LED 33 pF Capacitors 1K Resistors 4.7K Resistors Push Button Switch Quantity 1 1 1 2 1 1 1

;********************************************************************** ; ; Filename: Experiment 1. Test Port A Bit 0 ON LED ; ; www.electronics-microcontroller.com ; ;********************************************************************** list p=16f628 #include <p16f628.inc> ; list directive to define processor ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _MCLRE_ON & _LVP_OFF org clrf movlw movwf bsf movlw movwf bcf bcf goto END 0x000 PORTA 0x07 CMCON STATUS,RP0 b'11111110' PORTA STATUS,RP0 PORTA,0 $ ; ; ; ; ; ; ; ; Initial PORTA Turn Comparator off and Enable pins for I/O Select Bank 1 All PORTA = Input RA0 = Output Return to Bank 0 RA0 = 0 LED "ON"

; end of program

Experiment 2. Input & Output on Port A


The Experiment 2 we will test Port A is both Input and Output . Assemble circuit as figure 2.1 and Download Program to PIC16F628

Figure 2.1 schematic for Experiment 2 Input & Output on Port A Assemble circuit as figure 2.1 and Down load Programming to PIC16F628

list p=16f628 ; list directive to define processor #include <p16f628.inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _MCLRE_ON & _LVP_OFF org 0x000 clrf PORTA ; Initial POR TA ; output data latches movlw 0x07 ; Turn Comparator off and movwf CMCON ; Enable pins for I/O bsf STATUS,RP0 ; Select Bank 1 movlw b'11111111' ; All PORTA = Input movwf PORTA ; bcf STATUS,RP0 ; Return to Bank 0 btfsc PORTA,0 ; Test Switch Press ? goto $-1 ; If Not Press Loop bsf STATUS,RP0 ; If Press Set RA0 = Output bcf PORTA,0 ; TRISA bcf STATUS,RP0 ; Return To Bank 0 bcf PORTA,0 ; RA0 = 0 LED "ON" goto $ END ; end of program'
3

From The Source Code we can be explain by flowchart as figure 2.2

Figure 2.2 Flowchart of Experiment 2 Tools & Components


Name List MPLAB PIC16F628 4 MHz Crystal LED Quantity 1 1 1 Push Button Switch 1 PC 33 pF Capacitors 4.7K Resistors 1K Resistors 2 2 1 1

Resources ,Tools & Related


;********************************************************************** ; Filename: Experiment 2. Input & Output on Port A ; www.electronics-microcontroller.com ;********************************************************************** list p=16f628 ; list directive to define processor #include <p16f628.inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _MCLRE_ON & _LVP_OFF org clrf movlw movwf bsf movlw movwf bcf btfsc goto bsf bcf bcf bcf goto 0x000 PORTA 0x07 CMCON STATUS,RP0 b'11111111' PORTA STATUS,RP0 PORTA,0 $-1 STATUS,RP0 PORTA,0 STATUS,RP0 PORTA,0 $ END ; ; ; ; ; ; ; ; ; ; ; ; ; Initial PORTA Turn Comparator off and Enable pins for I/O Select Bank 1 All PORTA = Input Return to Bank 0 Test Switch Press ? If Not Press Loop If Press Set RA0 = Output TRISA Return To Bank 0 RA0 = 0 LED "ON" ; end of program

Experiment 3. Sound Generated


The Experiment 3 we will Generated sound (one frequency) by use PIC16F628 . first of all see Timing Diagram as figure 3.1 for to understand

Figure 3.1 Timing Diagram for Sound Generated Assemble circuit as figure 3.2 and load Programming to PIC16F628

Figure 3.2 schematic for Experiment 3 Sound Generated Assemble circuit as figure 3.2 and load Program to PIC16F628 . RA4 Connect with PIEZO , The Frequency can be calculate by formula as below f = 1/ T 1/(768 uSec + 768 uSec) = 651 Hz Source Code For Experiment 3

list p= 16 f 628 ; list directive to define processor #include <p 16 f 628. inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _MCLRE_ON & _LVP_OFF org 0 x 000 clrf PORTA ; Initial PORTA ; output data latches movlw 0 x 07 ; Turn Comparator off and movwf CMCON ; Enable pins for I/O bsf STATUS,RP 0 ; Select Bank 1 movlw b' 11101111 ' ; RA 4 = Output movwf PORTA ; Other = Input bcf STATUS,RP 0 ; Return to Bank 0 Loop1 bsf PORTA, 4 ; High RA 4 768 usec call Delay bcf PORTA, 4 ; Low RA 4 768 usec call Delay goto Loop1 ; Frequency = 1/(768 usec + 768 usec) ; = 651 Hz Delay clrf 0 x 20 ; Delay 768 usec decfsz 0 x 20 ,f goto $-1 return END From The Source Code we can be explain by flowchart as figure 3.3

Figure 3.3 Flowchart of Experiment 3 Tools & Components

Name List MPLAB PIC16F628 4 MHz Crystal Push Button Switch 1 PC 33 pF Capacitors 4.7K Resistors 1K Resistors PIEZO Resources ,Tools & Related

Quantity 1 1 1 2 1 1 1

Download Source Code of Experiment 2. Input & Output on Port A


;********************************************************************** ; ; Filename: Experiment 3. Sound Generated ; ; www.electronics-microcontroller.com ; ;********************************************************************** list p=16f628 #include <p16f628.inc> definitions ; list directive to define processor ; processor specific variable

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _MCLRE_ON & _LVP_OFF org clrf movlw movwf bsf movlw movwf bcf Loop1 bsf call bcf call goto 0x000 PORTA 0x07 CMCON STATUS,RP0 b'11101111' PORTA STATUS,RP0 PORTA,4 Delay PORTA,4 Delay Loop1 0x20,f ; Initial PORTA ; output data latches ; Turn Comparator off and ; Enable pins for I/O ; Select Bank 1 ; RA4 = Output ; Other = Input ; Return to Bank 0 ; High RA4 768 usec ; Low RA4 768 usec ; Frequency = 1/(768 usec + 768 usec) = ; Delay 768 usec 651 Hz

Delay clrf 0x20 decfsz goto $-1 return END

Experiment 4. Sound Generated The previously experiment (Exp 3) we can Generated Sound at one frequency, for this experiment we will apply Exp 3 to Generated Sound more one frequency like melody Assemble circuit as figure 4.1 and Download Program to PIC16F628

Figure 4.1 schematic for Experiment 4 Sound Melody


list p= 16 f 628 ; list directive to define processor #include <p 16 f 628. inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _MCLRE_ON & _LVP_OFF COUNT equ 0 x 20 SETTIME equ 0 x 21 org 0 x 000 clrf PORTA ; Initial PORTA ; output data latches movlw 0 x 07 ; Turn Comparator off and movwf CMCON ; Enable pins for I/O bsf STATUS,RP 0 ; Select Bank 1 movlw 0x0F ; RA 0- RA 3 = Input RA 4 = Output movwf PORTA ; bcf STATUS,RP 0 ; Return to Bank 0 Loop1 bsf PORTA, 4 ; Start Rising Pulse call Delay ; Delay bcf PORTA, 4 ; Falling Pulse call Delay btfsc PORTA, 3 ; Test RA 3 Press goto Check 2 ; If No Continue Check movlw 0 x 20 ; If RA 3 Press Set Time = 20 H movwf SETTIME goto Loop1 ; Continue Check 2 btfsc PORTA, 1 ; Test RA 1 Press goto Check 1 ; If No Continue Check movlw 0 x 55 ; If RA 1 Press movwf TIME ; Set Time = 55 H goto Loop1 ; Continue Check 1 btfsc PORTA, 0 ; Test RA 0 Press goto Loop1 ; If No Begin Pulse Again movlw 0 x 60 ; If RA 0 Press movwf TIME ; Set Time = 60 H goto Loop1 ; Begin Pulse Again Delay movf TIME,w ; Delay Use Change Delay Time movwf COUNT ; With TIME Register decfsz COUNT,f goto $-1 return END ; of program

Tools & Components


Name List MPLAB PIC16F628 4 MHz Crystal Push Button Switch 1 PC 33 pF Capacitors 4.7K Resistors Quantity 1 1 4 2 1 1K Resistors 220 Resistors PIEZO LED 1K Resistors 220 Resistors PIEZO 3 3 1 3 3 3 1

Filename: Experiment 4. Sound Generated list p=16f628 ; list directive to define processor #include <p16f628.inc> ; processor specific variable definitions
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _MCLRE_ON & _LVP_OFF

COUNT SETTIME org clrf movlw movwf bsf movlw movwf bcf LOOP1 bsf call bcf call btfsc goto movlw movwf goto Check2 goto movlw movwf goto Check1 goto movlw movwf goto

equ equ 0x000 PORTA 0x07

0x20 0x21 ; Initial PORTA ; output data latches ; Turn Comparator off and ; Enable pins for I/O ; Select Bank 1 ; RA0-RA3 =Input RA4 = Output ; ; Return to Bank 0 ; Start Rising Pulse ; Delay ; Falling Pulse ; Test RA3 Press ; If No Continue Check ; If RA3 Press Set Time = 20H ; Continue ; Test RA1 Press ; If No Continue Check ; If RA1 Press ; Set Time = 55H ; Continue ; Test RA0 Press ; If No Begin Pulse Again ; If RA0 Press ; Set Time = 60H ; Begin Pulse Again ; Delay Use Change Delay Time ; With TIME Register

CMCON STATUS,RP0 0x0F PORTA STATUS,RP0 PORTA,4 Delay PORTA,4 Delay PORTA,3 Check2 0x20 SETTIME LOOP1 btfsc PORTA,1 Check1 0x55 TIME LOOP1 btfsc PORTA,0 LOOP1 0x60 TIME LOOP1

Delay movf TIME,w movwf COUNT decfsz COUNT,f goto $-1 return END

; of program

Experiment 5. Stepper Motor Drive


The Experiment 5 we will test Use PIC16F628 to Drive Stepper Motor, for Article & Types of Stepping Motors you and see links as Resources ,Tools & Related Menu below This Experiment use Unipolar Motors 9-24 V 7.5 Degree/Step , and use IC ULN2003 for Drive

Unipolar Motors

IC ULN2003

Figure 5.1 schematic for Experiment 5 Stepper Motor Drive Assemble circuit as figure 5.1 and Download Program to PIC16F628 from Figure 5.1 VR 100k use for adjust frequency clock of PIC16F628 when run in mode ER

10

list p= 16 f 628 ; list directive to define processor #include <p 16 f 628. inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _ER_OSC_NOCLKOUT & _MCLRE_ON & _LVP_OFF COUNT equ 0 x 20 DUMMY 1 equ 0 x 21 DUMMY 2 equ 0 x 22 org 0 x 000 ; Reset Vetor Address bsf STATUS,RP 0 ; Set RB 4- RB 7 For Drive Stepper Motor movlw b' 00001111 ' movwf PORTB bcf STATUS,RP 0 Begin movlw .12 ; Loop 12 Time For 7.5 Degree/Step ; 4x12x7.5 = 360 movwf COUNT Loop movlw b' 00010000 ' ; 1 Phase Full Step movwf PORTB call Delay ; Change Delay For Change Speed movlw b' 00100000 ' movwf PORTB call Delay movlw b' 01000000 ' movwf PORTB call Delay movlw b' 10000000 ' movwf PORTB call Delay decfsz COUNT,f ; Check Loop = 12 Time ? goto Loop ; If Not Loop Again movlw .12 ; Reverse Move 12 Time movwf COUNT Loop 2 movlw b' 01000000 ' movwf PORTB call Delay movlw b' 00100000 ' movwf PORTB call Delay movlw b' 00010000 ' movwf PORTB call Delay movlw b' 10000000 ' movwf PORTB call Delay decfsz COUNT,f ; Check Loop = Time ? goto Loop 2 goto Begin ; If Yes Start Again Delay movlw 0 x 20 ; Delay Between Step movwf DUMMY 1 Delay 1 clrf DUMMY 2 decfsz DUMMY 2 ,f goto $-1 decfsz DUMMY 1 ,f goto Delay 1 return END ; end of program

11

;Filename: Experiment 5. Stepper Motor Drive list p=16f628 ; list directive to define processor #include <p16f628.inc> ; processor specific variable definitions
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _ER_OSC_NOCLKOUT & _MCLRE_ON & _LVP_OFF

COUNT DUMMY1 DUMMY2 org bsf movlw movwf bcf

equ equ equ

0x20 0x21 0x22 ; Reset Vetor Address ; Set RB4-RB7 For Drive Stepper Motor

0x000 STATUS,RP0 b'00001111' PORTB STATUS,RP0

Begin movlw .12 movwf COUNT Loop movlw b'00010000' movwf PORTB call Delay movlw b'00100000' movwf PORTB call Delay movlw b'01000000' movwf PORTB call Delay movlw b'10000000' movwf PORTB call Delay decfsz COUNT,f goto Loop movlw .12 movwf COUNT Loop2 movlw b'01000000' movwf PORTB call Delay movlw b'00100000' movwf PORTB call Delay movlw b'00010000' movwf PORTB call Delay movlw b'10000000' movwf PORTB call Delay decfsz COUNT,f goto Loop2 goto Begin Delay movlw 0x20 movwf DUMMY1 Delay1 clrf DUMMY2 decfsz DUMMY2,f goto $-1 decfsz DUMMY1,f goto Delay1 return END

; Loop 12 Time For 7.5 Degree/Step 4x12x7.5 = 360

; 1 Phase Full Step ; Change Delay For Change Speed

; Check Loop = 12 Time ? ; If Not Loop Again ; Reverse Move 12 Time

; Check Loop = Time ? ; If Yes Start Again ; Delay Between Step

; of program 12

Digital thermostat using PIC16F628


Digital thermostat in this project design from microcontroller PIC16F628 .This circuit can control a outside system by driving a relay. For thermometer use DS1820 which is 1Wire Digital Thermometer Feature LCD Temperature Display Can control a outside system by driving a relay Can Adjust temperature Hardware Completed circuit of Digital Thermostat show in figure 1 . From figure 1 PIC16F628 is main device of circuit which keep source for control. DS1820 is thermometer which DS1820 is 1-wire thermometer. For temperature display use LCD Module 16X2 for display. Output can control outside by relay

Figure 1. Schematics of Digital thermostat

;************************************************************************ ; Digital Thermostst * ; WWW.Guidecircuit.com * ;************************************************************************

13

list

p=16f628 #include <p16f628.inc> errorlevel 0,-302

CBLOCK DS_DAT DS_SIGN SEND COUNT NUM1 NUM2 NUM3 DATA1 HALF DUMMY0 DUMMY1 DUMMY2 DUMMY5 DUMMY6 BITS COMPARE SET_TEMP ADDR endc #defineDQ #defineE #defineRS org goto

0x20

; End of definition PORTA,0 PORTB,0 PORTB,3 0x000 Start ; Define text substitution

;********************************************************************** ; LCD Display ;********************************************************************** Room_ addwf PCL,f ; dt "Room Temp " Set_ addwf PCL,f dt "Set Temp " First_ addwf PCL,f dt "<Set Temp First> " ;********************************************************************** ; Main Program * ;********************************************************************** Start call Init_Port ; Set -up port call Init_LCD ; Initial LCD movlw 0x20 ; Set temperature data = 00100000b = 16.0C movwf SET_TEMP
14

bsf PORTA,4 movlw 0x80 movwf ADDR call Set_Addr

; Off relay, first ; ; Select LCD's first line address

clrf COUNT ; Get message from table Send_Lp2 movf COUNT,w ; Begin with counter = 0 call First_ ; Income data position COUNT call Send ; Send data to LCD incf COUNT,f ; Get the next data if counter < 16 movlw .16 subwf COUNT,w ; Test counter < 16 ? btfss STATUS,Z goto Send_Lp2 ; If < 16, still send data Again movlw 0xC5 movwf ADDR call Set_Addr ; Select the center of LCD's second line

clrf HALF ; Save half degree bcf STATUS,C rrf SET_TEMP,w ; Rotate bit data rlf HALF,f ; movwf DATA1 ; Save for conversion call Convert call Send_Temp ; Send temperature value to LCD btfsc goto call incf Check_Run btfsc goto PORTA,1 Check_Run Delay200 SET_TEMP,f PORTA,3 Again ; Check temperature increment switch ; Delay for switch debouncing

; Check running switch ; If not pressed, loop again

movlw 0x01 movwf ADDR call Set_Addr call Delay5

; Send command for clearing display

; Nescessary delay

clrf COUNT ; Get message from table Send_Lp3 movf COUNT,w ; Begin with counter = 0 call Set_ ; Income data position COUNT call Send ; Send data to LCD incf COUNT,f ; Still send next data if counter < 8 movlw .9 subwf COUNT,w ; Test counter < 8 ? btfss STATUS,Z goto Send_Lp3 ; If < 8, try again
15

call

Send_Temp ; Select LCD's second line address

movlw 0xC0 movwf ADDR call Set_Addr

clrf COUNT ; Get message from Table Send_Lp4 movf COUNT,w ; Begin with counter = 0 call Room_ ; Income data position COUNT call Send ; Send data to LCD incf COUNT,f ; Still send next Data if counter < 8 movlw .9 subwf COUNT,w ; Test counter < 8 ? btfss STATUS,Z goto Send_Lp4 ; If < 8, try again OFF_ bsf PORTA,4 call RD_Temp movf DS_DAT,w clrf HALF bcf STATUS,C rrf DS_DAT,w rlf HALF,f movwf DATA1 call Convert movf subwf btfss goto ; Off relay ; Load data ; Clear register for storing a half temp value ; Use 7-bit upper ; Store a half degree (.5) or (.0) ; Data for conversion to decimal ; Convert data to decimal

DS_DAT,w ; Check room temp > setting temp ? SET_TEMP,w STATUS,C ON_ ; If more than, goto on relay ; If not, show room's temp on LCD

movlw 0xC9 movwf ADDR call Set_Addr call call goto ON_ Send_Temp Delay200 OFF_

; Loop again ; On relay ; Set address for LCD ; Show room's temp

bcf PORTA,4 movlw 0xC9 movwf ADDR call Set_Addr call call call movf clrf bcf Send_Temp Delay200 RD_Temp DS_DAT,w HALF STATUS,C

; Send temperature value to LCD ; Read temperature value again ; Load data ; Clear register for storing a half temp
16

rrf DS_DAT,w rlf HALF,f movwf DATA1 call Convert movf addlw subwf btfsc goto goto

; Use 7-bit upper ; Store a half degree (.5) or (.0) ; Data for decimal conversion

DS_DAT,w ; Check temperature again 0x01 ; Temperarure is lower than [setting Temperature - 1] ? SET_TEMP,w STATUS,C ; If yes, off relay OFF_ ; ON_ ; If no, loop again

;********************************************************************** ; SetPort I/O of PIC16F628 * ;********************************************************************** Init_Port clrf PORTA ; Clear PORTA before initial movlw 0x07 ; Set PORTA to digital I/O movwf CMCON bsf STATUS,RP0 movlw b'11101111' ; Set RA0, RA1 and RA3 as input, RA4 as output movwf PORTA movlw b'00000110' ; Use PORTB for LCD movwf PORTB bcf STATUS,RP0 return ;********************************************************************** ; Read Data From DS1820 * ;********************************************************************** RD_Temp call DS_Rx ; Check DS1820 status addlw 0x01 ; 255=ready, 0= ready btfss STATUS,Z ; Zero flag set = ready return ; W register is not zero = not ready Get_Temp call DS_Reset ; Reset chip first btfss STATUS,Z ; Zero flag set = OK goto Error_ ; If not response, exit movlw 0xcc ; Skip ROM command call DS_Tx ; Send command movlw 0xbe ; Read scratch pad command call DS_Tx ; Send command call DS_Rx ; Read 8-bit data movwf DS_DAT ; Save data to register call DS_Rx ; Sign (FF=-VE, 00=+VE) movwf DS_SIGN ; Save 9th bit to register call DS_Reset ; Restart movlw 0xcc ; Skip ROM command call DS_Tx ; Send command movlw 0x44 ; Start convert command call DS_Tx ; Send command
17

movf btfsc goto addlw btfss goto Pass retlw Error_ retlw

DS_SIGN,w STATUS,Z Pass 0x01 STATUS,Z Error_ 0x00 0x01

; Check dign ; Check dign = 00 ? ; If yes, Temp as positive then return ; ; Check sign = FF ? ; If not, end with error ; If Yes, (Temp as negstive then return

;********************************************************************** ; MACRO For DS1820 * ;********************************************************************** DQLOW macro bcf DQ ; DQ bit ready bsf STATUS,RP0 bcf DQ ; Set DQ to output bcf STATUS,RP0 endm DQHIZ bsf bsf bcf endm macro STATUS,RP0 DQ ; Set DQ to input STATUS,RP0

PAUSE macro DELAY movlw DELAY movwf DUMMY0 call DelayAL endm

; Generate delay time

;********************************************************************** ; DS1820 SubRoutine * ;********************************************************************** DelayAL nop ; Use for pausing macro nop decfsz DUMMY0,f goto DelayAL return ;********************************************************************** ; Reset DS1820 * ;********************************************************************** DS_Reset DQLOW PAUSE 0x77 ; 600 microsecond delay DQHIZ PAUSE 0x0c ; Wait 67 microsecond for response bit nop nop movf PORTA,w
18

andlw 0x01 ; Use RA0 only movwf DUMMY1 PAUSE 0x3b ; 300 microsecond delay movf DUMMY1,w ; Response in W register return ;********************************************************************** ; Send Data To DS1820 (8 Bit) * ;********************************************************************** DS_Tx movwf DUMMY2 ; Transmission data movlw 0x08 ; Prepare 8-bit counter for sending data movwf DUMMY1 ; Define loop counter Tx_Loop DQLOW ; Macro of DQ pin to low, This is start bit PAUSE 0x01 ; 10 microsecond delay rrf DUMMY2,f ; Rotate data to Carry flag btfsc STATUS,C ; Test Carry flag bsf DQ ; If Carry flag = "1" , set DQ to high PAUSE 0x0d ; 70 microsecond delay DQHIZ nop decfsz DUMMY1,f ; 8 times ? goto Tx_Loop ; No, send again return ;********************************************************************** ; Recieve Data From DS1820 (8 Bit) * ;********************************************************************** DS_Rx movlw 0x08 ; Recieve 8-bit data movwf DUMMY1 Rx_Loop DQLOW ; Macro of DQ pin to low, this is start bit PAUSE 0x01 ; 10 microsecond delay DQHIZ ; Back to high for receiving nop nop movf PORTA,w ; Read data andlw 0x01 ; Get data bit 0 only addlw 0xff ; Move data bit 0 to Carry flag with addition method rrf DUMMY2,f ; Move data bit 0 to DUMMY bit 7 PAUSE 0x0b ; 60 microsecond delay decfsz DUMMY1,f ; Loop 8 times goto Rx_Loop ; Read again movf DUMMY2,w ; Save data to W register return ;********************************************************************** ; Subroutine For Send Data To LCD * ;********************************************************************** Send_Temp movf NUM3,w ; Read data from DS1820 btfsc STATUS,Z ; Fisrt data = 0 ? movlw 0xe0 ; If yes, convert to blank 0xE0+0x30 =0x20 (" ")
19

addlw 0x30 call Send movf NUM2,w addlw 0x30 call Send movf NUM1,w addlw 0x30 call Send movlw '.' call Send movlw '0' btfsc HALF,0 movlw '5' call Send movlw 'C' call Send return Set_Addr bcf bcf call movf call bsf call return E RS Delay125 ADDR,w Send RS Delay125

; Convert number to ASCII ; Send data to LCD ; Read second digit ; Convert to ASCII ;

; Test half degree = [0] or [5]

; Fix position on LCD ; Send command ; Select address for sending data to LCD

reset_LCD macro movlw 0x38 movwf BITS call Flip call Pulse call Delay125 endm Init_LCD bcf E bcf RS call Delay125 reset_LCD reset_LCD reset_LCD reset_LCD movlw 0x28 movwf BITS call Flip call Pulse call Delay125 movlw 0x28 call Send movlw 0x0c

; Fisrt, send 8-bit mode command

; Set command mode

; Send 4-bit mode command in MSB only

; Set LCD mode to 4 bit mode, 2 line display, 5x7 dot ; Set LCD on, cursor off, cursor not blink
20

call Send movlw 0x01 call Send call Delay5 return Send movwf BITS call Flip call Pulse swapf BITS,f call Flip call Pulse call Delay125 return bcf btfsc bsf bcf btfsc bsf bcf btfsc bsf bcf btfsc bsf return PORTB,4 BITS,4 PORTB,4 PORTB,5 BITS,5 PORTB,5 PORTB,6 BITS,6 PORTB,6 PORTB,7 BITS,7 PORTB,7

; Clear display

; Send 8-bit data to LCD module ; ; Send Upper 4-bit first and lower 4-bit following

Flip

; Copy data from BITS register to RB4-RB7 ; ; Unchange other bit of PORTB ;

Pulse bsf E nop bcf E call Delay125 return Delay125 movlw .42 movwf DUMMY1 decfsz DUMMY1,f goto $-1 return

; Generate enable pluse for LCD

; Delay 125 microsecond

Delay5 movlw .41 ; Delay 5 millisecond movwf DUMMY2 D1 call Delay125 decfsz DUMMY2,f goto D1 return Delay200 clrf DUMMY5 ; Delay ~ 200 mSec For Key Bounce
21

clrf decfsz goto decfsz goto return

DUMMY6 DUMMY6,f $-1 DUMMY5,f $-3

;********************************************************************** ; Convert Hex to 3 Digit Decimal * ;********************************************************************** Convert clrf NUM2 ; Clear register of 10's unit Check movlw 0x0A ; Subtract with 10 until the result lower 10 subwf DATA1,w ; Fisrt subtraction < 10 ? btfss STATUS,C goto Less1 ; If < 10 then return incf NUM2,f ; If > 10 then increase 10's unit movlw 0x0A ; Subtract with 10 subwf DATA1,f ; Test Again. Is it lower 10 ? goto Check Less1 movf DATA1,w movwf NUM1 clrf NUM3 Check2 movlw 0x0A subwf NUM2,w btfss STATUS,C return incf NUM3,f movlw 0x0A subwf NUM2,f goto Check2 end ; If < 10, send data to 1's unit ; ; Clear 100's unit ; 10's unit > 10 ? ; If < 10 then return ; If > 10 then subtraction again ; and check until the result is lower 10

22

Digital Thermometer
Digital Thermometers in this circuit design from microcontroller PIC16F628 of micrichip company.Good point of PIC16F628 such as brown-out reset , low-voltage programming reference voltafe module,USART: Universal Synchronous Asynchronous Receiver Transmitter PWM (CCP : Capture Compare Pulse-width modulation module PIC16F628 For completed circuit of Digital Thermometers show in figure 1 . It can be display 2 digit which you can applied to 3 digit

Figure 1. Schematics of Digital Thermometers (PIC) For temperature sensor use IC DS1820 which The DS18S20 communicates over a 1-Wire bus. You can download data sheet from hardware Support Data.
;************************************************************************ ; Digital Thermometer ; WWW.Guidecircuit.com ;************************************************************************ list p=16f628 #include <p16f628.inc> errorlevel 0,-302 CBLOCK 0x20 DS_DAT DS_SIGN DUMMY0 DUMMY1 DUMMY2 DUMMY3 DUMMY4 SEND COUNT NUM1 NUM2 NUM3 DATA1 HALF endc

23

#define DAT_PIN PORTB,0 #define LATCH PORTB,4 #define CLK PORTB,5 #Define DQ PORTA,0 org goto 0x000 Start

; 74595 DATA pin ; 74595 LATCH pin ; 74595 CLK pin

Table_ addwf PCL,f dt 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f Start clrf movlw movwf bsf movlw movwf bcf call clrf bcf rrf rlf movwf call call goto PORTA 0x07 CMCON STATUS,RP0 b'00000110' PORTB STATUS,RP0 RD_Temp HALF STATUS,C DS_DAT,w HALF,f DATA1 Convert Delay3 Main ; Set port A for digital I/O

; Set port B for displaying via LED 7 segments

Main

; Read data from DS1820 ; A Half digit ; Use 7-bit upper ; Store a half degree ; Data for convert to decimal ; Convert data to decimal ; Display data delay

;********************************************************************** ; Display On 7 Segment * ;********************************************************************** Delay3 clrf DUMMY4 ; Display data delay DelayLp call Disp decfsz DUMMY4,f goto DelayLp return Delay2 clrf DUMMY3 decfsz DUMMY3,f goto $-1 return Disp movf call btfsc addlw call bcf call bsf NUM1,w Table_ HALF,0 0x80 Send_Dat PORTB,6 Delay2 PORTB,6 ; Delay for scanning LED 7 segments

; Load data to DSP1 ; Convert ; Show a half degree on dot-point LED ; Send data to LED 7 segments ; ON DSP1 Display

24

movf call call bcf call bsf return

NUM2,w Table_ Send_Dat PORTB,7 Delay2 PORTB,7

; Load data to DSP2 ; Convert ; Send data to LED 7 segments ; On DSP2 ; Off DSP2

Send_Dat movwf SEND movlw 0x08 movwf COUNT Chk_Bit bcf DAT_PIN btfsc SEND,7 bsf DAT_PIN Clock bsf CLK bcf CLK Rotate rlf SEND,f decfsz COUNT,f goto Chk_Bit Latch bsf LATCH bcf LATCH return

; Load data ; Loop 8 times for sending 8-bit data ; ; Check bit 7 "0" or "1" ; Send bit 7 to shift register ; Clock pulse ; Rotate bit for sending next bit ; Test loop 8 times ? ; If no, send bit again ; Latch data for displaying

;********************************************************************** ; Macro for DS1820 * ;********************************************************************** DQLOWmacro bcf DQ ; DQ bit ready bsf STATUS,RP0 bcf DQ ; Set DQ to output bcf STATUS,RP0 endm DQHIZ macro bsf STATUS,RP0 bsf DQ bcf STATUS,RP0 endm PAUSE macro movlw movwf call endm DELAY DELAY DUMMY0 Delay5

; Set DQ to input

; Generate delay time

;********************************************************************** ; DS1820 1-Wire bus * ;********************************************************************** RD_Temp call DS_Rx ; Check chip status addlw 0x01 btfss STATUS,Z ; Z set = ready 25

return Get_Temp call DS_Reset movlw 0xcc call DS_Tx movlw call call movwf call 0xbe DS_Tx DS_Rx DS_DAT DS_Reset

; W is not zero = not ready

; Skip ROM

; Read scratch pad RAM of DS1820 ; Read 8-bit data

; Restart ; Skip ROM

movlw 0xcc call DS_Tx movlw 0x44 call DS_Tx Delay5 nop nop decfsz DUMMY0,f goto Delay5 return DS_Reset DQLOW PAUSE 0x77 DQHIZ PAUSE 0x0c nop nop movf PORTA,w andlw 0x01 movwf DUMMY1 PAUSE 0x3b movf DUMMY1,w return DS_Tx movwf movlw movwf Tx_Loop PAUSE rrf btfsc bsf PAUSE DQHIZ nop decfsz DUMMY2 0x08 DUMMY1 DQLOW 0x01 DUMMY2,f STATUS,C DQ 0x0d

; Start conversion

; Delay for PAUSE macro

; 600 microcsecond delay ; Wait for response pulse (67 microsecond)

; Read response ; Use RA0 only ; 300 microcsecond delay ; Response in W

; Transmission data ; Prepare 8-bit counter for sending data ; Define loop counter ; Macro of DQ pin to low, This is start bit ; 10 microsecond delay ; Rotate data to Carry flag ; Test Carry flag ; If Carry flag = "1" , set DQ to high ; 70 microsecond delay

DUMMY1,f

; 8 times ? 26

goto Tx_Loop return DS_Rx movlw movwf Rx_Loop PAUSE DQHIZ nop nop movf andlw addlw rrf PAUSE decfsz goto movf return 0x08 DUMMY1 DQLOW 0x01

; No, send again

; Recieve 8-bit data ; Macro of DQ pin to low, this is start bit ; 10 microsecond delay ; Back to high for receiving

PORTA,w 0x01 0xff DUMMY2,f 0x0b DUMMY1,f Rx_Loop DUMMY2,w

; Read data ; Get data bit 0 only ; Move data bit 0 to Carry flag with addition method ; Move data bit 0 to DUMMY bit 7 ; 60 microsecond delay ; Loop 8 times ; Read again ; Save data to W register

;********************************************************************** ; Convert Hex to Decimal(3 Digit) * ;********************************************************************** Convert clrf NUM2 ; Clear register of 10's unit Check movlw 0x0A ; subwf DATA1,w ; Subtract with 10 until lower 10 btfss STATUS,C ; Check subtraction result lower 10 ? goto Less1 ; If yes then return incf NUM2,f ; If no, increase 10's unit value movlw 0x0A ; subwf DATA1,f ; Subtract with 10 until lower 10 and check result again goto Check movf DATA1,w ; movwf NUM1 ; Send data to 1's unit display clrf NUM3 ; Check hundred unit Check2 movlw 0x0A subwf NUM2,w ; btfss STATUS,C ; 10's unit over 10 ? return ; If no then return incf NUM3,f movlw 0x0A ; If yes, subtract again and check until the result is lower 10 subwf NUM2,f goto Check2 ;********************************************************************** end Less1

27

You might also like