Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic Programming question

Status
Not open for further replies.
Poll just means you have a code loop that reads the I/O lines every so often an do something based upon the results. You can use a timer to execute an interupt routine or a simple main loop which always reads the I/O port at the top of the loop.
 
I've been working on my code see what you guys think. What changes would I need to make to get it where it doesn't jump out of the program when both upper and lower sensors are low

Code:
      list      p=16f628            ; list directive to define processor
       #include <p16f628.inc>        ; processor specific variable definitions
    ;   errorlevel   -302 ;hide banking message
    ;*****   
       __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _LVP_OFF
    ;*****
    ;internal osc settings
    ;*****
    ; '__CONFIG' directive is used to embed configuration data within .asm file.
    ; The lables following the directive are located in the respective .inc file.
    ; See respective data sheet for additional information on configuration word.

    ;***** VARIABLE DEFINITIONS
    w_temp        EQU     0x70       ; variable used for context saving
    status_temp   EQU     0x71       ; variable used for context saving
    Count1        EQU     0X72       ; First Counter for Delay Loops
    Count2        EQU     0X73       ; Second Counter for Delay Loops

    ;**********************************************************************
          ORG     0x000             ; processor reset vector
          goto    Start              ; go to beginning of program
    ;*****
          ORG     0x004             ; interrupt vector location
          movwf   w_temp            ; save off current W register contents
          movf   STATUS,w           ; move status register into W register
          movwf   status_temp       ; save off contents of STATUS register
          movf    status_temp,w     ; retrieve copy of STATUS register
          movwf   STATUS            ; restore pre-isr STATUS register contents
          swapf   w_temp,f
          swapf   w_temp,w          ; restore pre-isr W register contents
          retfie                    ; return from interrupt
          
    ;*****
Start
          clrf   PORTA
          clrf   PORTB
          MOVLW   B'00000111'
          MOVWF   CMCON             ; Turn off comparator
          bsf     STATUS,RP0        ; bank one
          movlw   0xFF
          movwf    TRISA            ; porta all Input
          movlw    0x00 
          movwf    TRISB            ; portb all Output
          bcf     STATUS,RP0        ; return to bank 0
    ;*****               
          movlw   b'00000000'       ;See datasheet for prefered
          movwf   OPTION_REG        ;settings of OPTION_REG
          bsf     PORTB,7           ; Turn on Power LED

          btfss   PORTA,0           ; Check if Upper Sensor = 1
          Goto    Prestart          ; If not go to prestart subroutine
          bsf     PORTB,6
          Goto    RUN
    ;*****


Prestart
          bsf PORTB,5               ; Turn on Warning LED
          bsf PORTB,4               ; Turn on Refill LED
          bsf PORTB,3               ; Turn On Relay Sub-Circuit
          btfsc PORTA,0             ; If Upper Sensor = 1
          Goto Run                  ; Go to Operation Mode
        

Run
          

          btfss PORTA,0             ; If Upper Sensor = 0
          bsf PORTB,5               ; Then turn on Warning LED
          btfss PORTA,1             ; If Lower Sensor = 0
          call Refill               ; Go To Refill Subroutine
          decfsz Count1,1           ; Decrement Counter
          goto Run

Refill
         bsf PORTB,4                ; Turn on Refill LED
         bsf PORTB,3                ; Turn on Refill Sub-Circuit
         btfsc PORTA,0              ; If Upper Sensor = 1
         bcf PORTB,3                ; Turn Off Refill Sub-Circuit
         btfsc PORTA,0              ; If Upper Sensor = 1
         bcf PORTB,4                ; Turn off Refill LED
         btfsc PORTA,0              ; If Upper Sensor = 1
         bcf PORTB,5                ; Turn off Warning LED
         goto Run

     ;*****
          END
 
Firstly you have to set up your program with a Main routine where the micro accesses one of the displays and then accesses the other display to produce a 0-99 display. This is all Main does.
During this display routine the mico is waiting for an interrupt.
When the micro goes to address 04, it is sent to Interrupt Service Routine (isr). At the sub-routine called isr you need to have a set of insructions that firstly see if the particular input has been triggered before.
If not, it looks at the other input and sees if it has been triggered. If not, the counter is incremented. If the second input is triggered and the first input is low, the counter is decremented.
You will also need a delay factor to prevent the counters incrementing or decrementing if any pulses higher than 0.3Hz are received.
 
Last edited:
I'm trying to simplify the code by doing a binary changing of port B

in run mode its Port B is gonna be like this
0 0 0 0 0 0 1 1

With the Upper Sensor low
0 0 0 0 0 1 1 1

Refill Mode
0 0 0 1 1 1 0 1

That should take out a lot of unnecessary code that takes more memory than what my new code will. I should be able to work with it a little better if I can get it working using that coding.
 
Firstly you have to set up your program with a Main routine where the micro accesses one of the displays and then accesses the other display to produce a 0-99 display.

Uhhh I think you need a coffee colin he's just playing with 3-4 led's. What is this talk about 7 segment displays?

Mike
 
Uhhh I think you need a coffee colin he's just playing with 3-4 led's. What is this talk about 7 segment displays?

Mike

LMAO Birdman!! It's 4 LEDs 1 each for Power, Operation, Warning, and Refill. It doesn't seem to be giving me as much trouble since I started using binary switching of PortB, but its still wanting to jump back to the main program before doing other portions of the code. I'm using MPLAB to build my hex file and using Oschonsoft PIC Simulator to test the code. that Errorlevel bit at the beginning kicks the assembler out on Pic Simulator so I chose to use MPLAB to build my hex file. It seems to be ok so far, but time will tell

Here's the code as I have it now

Code:
      list      p=16f628            ; list directive to define processor
       #include <p16f628.inc>        ; processor specific variable definitions
       errorlevel   -302             ;hide banking message
    ;*****   
     __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _LVP_OFF
    ;*****
    ;internal osc settings
    ;*****
    ; '__CONFIG' directive is used to embed configuration data within .asm file.
    ; The lables following the directive are located in the respective .inc file.
    ; See respective data sheet for additional information on configuration word.

    ;***** VARIABLE DEFINITIONS
w_temp        EQU     0x70          ; variable used for context saving
status_temp   EQU     0x71          ; variable used for context saving
Count1        EQU     0X20          ; First Counter for Delay 
Count2        EQU     0X21          ; Second Counter for Delay 
Count3        EQU     0X22          ; Third Counter for Delay

    ;**********************************************************************
          ORG     0x000             ; processor reset vector
          goto    Start             ; go to beginning of program
          ORG     0x004             ; interrupt vector location
          movwf   w_temp            ; save off current W register contents
          movf   STATUS,w           ; move status register into W register
          movwf   status_temp       ; save off contents of STATUS register
          movf    status_temp,w     ; retrieve copy of STATUS register
          movwf   STATUS            ; restore pre-isr STATUS register contents
          swapf   w_temp,f
          swapf   w_temp,w          ; restore pre-isr W register contents
          retfie                    ; return from interrupt
          
Start
          clrf     PORTA
          clrf     PORTB
          MOVLW    B'00000111'
          MOVWF    CMCON            ; Turn off comparator
          bsf      STATUS,RP0       ; bank one
          movlw    0xFF
          movwf    TRISA            ; porta all Input
          movlw    0x00 
          movwf    TRISB            ; portb all Output
          bcf      STATUS,RP0       ; return to bank 0

          movlw   b'00000000'       ;See datasheet for prefered
          movwf   OPTION_REG        ;settings of OPTION_REG
          bsf     PORTB,7           ; Turn on Power LED

          btfss   PORTA,0           ; Check if Upper Sensor = 1
          Call    Prestart          ; If not go to prestart subroutine
          bsf     PORTB,6           ; Turn on Operation LED
          Goto    Run     

Prestart
          movlw b'00011101'
          btfss PORTA,0
          movlw b'00000001'
          movwf TRISB
          btfss PORTA,0
          movlw b'0000001'
          movwf PORTB        
          btfsc PORTA,0             ; If Upper Sensor = 1
          Return                    ; Return to Startup Section
          goto Prestart
Run
          
          btfss PORTA,0             ; If Upper Sensor = 0
          bsf PORTB,5               ; Then turn on Warning LED
          btfsc PORTA,0             ; If Upper Sensor = 0
          decfsz Count1,1           ; Don't Decrement Counter 1
          btfss PORTA,1             ; If Lower Sensor = 0
          Call Refill               ; Go To Refill Subroutine
          goto Run

Refill
          movlw b'00011101'
          btfsc PORTA,0
          movlw b'00000011'
          movfw PORTB
          btfss PORTA,0
          goto Refill
          btfsc PORTA,0
          Return
          
          END
 
Hi there, I'm new to this site and might not have that much experience.

I copied your code into a new MPLAB IDE project and simulated it using MPLAB SIM.

there are a few interesting things that the code is doing and not doing but I want to see if i understand what you are trying to do here first.

As far as i can see you have a tank with 2 pumps, one to water the garden perhaps and the other to fill the tank.

When the tank is not uber full, you give a warning but keep watering anyway.

When its below the bottom water sensor or out of water, you stop your watering and refill the tank.

Think i can see a few things that might be doing something other than what they are supposed to based on the below logic description in one of your first posts.

Upper + Lower Sensor = 1 Then Operation = 1; Warn = 0; Refill = 0
Upper Sensor = 0 + Lower Sensor = 1 Then Operation = 1; Warn = 1; Refill = 0
Upper + Lower Sensor = 0 Then Operation = 0; Warn = Flashing; Refill = 1

EDIT: Trying to figure out your ports from your comments, think this is right but not sure about PORTB,0 as there is no comment and
you didn't set it the same way as the others, (Binary)

1 is on and 0 is off for all I/O including the LEDS? (might be a silly question but my LEDS are backwards so the PIC is sinking the power not supplying)

portA,0 upper sensor
portA,1 lower sensor

portb,0 What?
portB,5 warning LED
portB,6 Operation LED
portB,7 POWER LED
 
Last edited:
The output is for a sub-circuit that I designed that will activate the pump, and the refill control valve of the Hydrogen on Demand system. I'm still working on the cell design, but right now I'm more focused on getting this part working before I start working on the cell design.

Here are the Input and Outputs of my PIC

RA0 = Upper Water Level Sensor
RA1 = Lower Water Level Sensor
RB7 = Power LED
RB6 = Operation LED
RB5 = Warn LED
RB4 = Refill LED
RB3 = Output Connector (To Relay Sub-Circuit)

I've attached a schematic of my relay sub-circuit and the indicator sub-circuit.
 

Attachments

  • Relay Schematic.png
    Relay Schematic.png
    7.8 KB · Views: 143
  • Indicator Schematic.png
    Indicator Schematic.png
    8.8 KB · Views: 153
EUREKA!!!!! I've Done it! I've gotten the program working!!!! I got the Prefill section working and then copied/pasted the prefill section into the refill section and got it working, I've tested it and no errors and no kicking out of the program by the MCU. I've run it at different speeds from step-by-step up to ultimate in PIC Simulator and no problems. I'm attaching the code below

Code:
       list      p=16f628            ; list directive to define processor
       #include <p16f628.inc>        ; processor specific variable definitions
       errorlevel   -302             ;hide banking message
    ;*****   
     __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _LVP_OFF
    ;*****
    ;internal osc settings
    ;*****
    ; '__CONFIG' directive is used to embed configuration data within .asm file.
    ; The lables following the directive are located in the respective .inc file.
    ; See respective data sheet for additional information on configuration word.

    ;***** VARIABLE DEFINITIONS
w_temp        EQU     0x70          ; variable used for context saving
status_temp   EQU     0x71          ; variable used for context saving
Count1        EQU     0X20          ; First Counter for Delay 
Count2        EQU     0X21          ; Second Counter for Delay 
Count3        EQU     0X22          ; Third Counter for Delay

    ;**********************************************************************
          ORG     0x000             ; processor reset vector
          goto    Start             ; go to beginning of program
          ORG     0x004             ; interrupt vector location
          movwf   w_temp            ; save off current W register contents
          movf   STATUS,w           ; move status register into W register
          movwf   status_temp       ; save off contents of STATUS register
          movf    status_temp,w     ; retrieve copy of STATUS register
          movwf   STATUS            ; restore pre-isr STATUS register contents
          swapf   w_temp,f
          swapf   w_temp,w          ; restore pre-isr W register contents
          retfie                    ; return from interrupt
          
Start
          clrf     PORTA
          clrf     PORTB
          MOVLW    B'00000111'
          MOVWF    CMCON            ; Turn off comparator
          bsf      STATUS,RP0       ; bank one
          movlw    0xFF
          movwf    TRISA            ; porta all Input
          movlw    0x00 
          movwf    TRISB            ; portb all Output
          bcf      STATUS,RP0       ; return to bank 0

          movlw   b'00000000'       ;See datasheet for prefered
          movwf   OPTION_REG        ;settings of OPTION_REG
          
          btfss   PORTA,0           ; Check if Upper Sensor = 1
          Call    Prestart          ; If not go to Prestart Subroutine
          Goto    Run               ; Then go to Operation Mode

Prestart
          movlw b'10111000'         ; Set Refill Mode
          btfsc PORTA,0             ; Check If Upper Sensor = 1
          movlw b'11000000'         ; Then Set Operation Mode 
          movwf PORTB               ; Move to PORTB
          btfss PORTA,0             ; Check If Upper Sensor = 1
          goto Prestart             ; If Not Return to Top of subroutine
          Return                    ; Return to Start

Run
          movlw b'11000000'         ; Set Operation Mode
          btfss PORTA,0             ; Check if Upper Sensor = 1
          movlw b'11100000'         ; Set Warning Mode
          movwf PORTB               ; Move to PortB
          btfss PORTA,1             ; Check If Lower Sensor = 1
          Call Refill               ; Go to Refill Subroutine
          Goto Run                  ; Return to Top
          
          
Refill
          movlw b'10111000'         ; Set Refill Mode
          btfsc PORTA,0             ; Check If Upper Sensor = 1
          movlw b'11000000'         ; Then Set Operation Mode 
          movwf PORTB               ; Move to PORTB
          btfss PORTA,0             ; Check If Upper Sensor = 1
          goto Refill               ; If Not Return to Top of subroutine
          Return                    ; Return to Start
          
          END
 
OK cool, I've got a better idea of whats going on. Few things i noticed in your code, and it looks like you have cleaned all but one of them up.

OPTION_REG should be set in Bank1, the default is b'11111111' when i run your code, its still the default value. Think it should look like this

Code:
Start
          clrf     PORTA
          clrf     PORTB
          MOVLW    B'00000111'
          MOVWF    CMCON            ; Turn off comparator
          bsf      STATUS,RP0       ; bank one
          movlw    0xFF
          movwf    TRISA            ; porta all Input
          movlw    0x00 
          movwf    TRISB            ; portb all Output
        
          movlw   b'00000000'       ;See datasheet for prefered
          movwf   OPTION_REG        ;settings of OPTION_REG

          bcf      STATUS,RP0       ; return to bank 0

          btfss   PORTA,0           ; Check if Upper Sensor = 1
          Call    Prestart          ; If not go to Prestart Subroutine
          Goto    Run               ; Then go to Operation Mode

Funnily enough, your project doesn't use the prescaler or weak pull ups on PORTB so this doesn't really affect your project.
 
I'm currently working on a slight improvement of my program. I'm adding a little test subroutine that tests the LEDs prior to running the prefill subroutine or going to the main part of the program. I'm still working on the length of the delay between LED Illumination. I started with the default 255 and then dropped to 05H

Code:
       list      p=16f628            ; list directive to define processor
       #include <p16f628.inc>        ; processor specific variable definitions
       errorlevel   -302             ;hide banking message
    ;*****   
     __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _LVP_OFF
    ;*****
    ;internal osc settings
    ;*****
    ; '__CONFIG' directive is used to embed configuration data within .asm file.
    ; The lables following the directive are located in the respective .inc file.
    ; See respective data sheet for additional information on configuration word.

    ;***** VARIABLE DEFINITIONS
w_temp        EQU     0x70          ; variable used for context saving
status_temp   EQU     0x71          ; variable used for context saving
Count1        EQU     0X20          ; First Counter for Delay 
    ;**********************************************************************
          ORG     0x000             ; processor reset vector
          goto    Start             ; go to beginning of program
          ORG     0x004             ; interrupt vector location
          movwf   w_temp            ; save off current W register contents
          movf   STATUS,w           ; move status register into W register
          movwf   status_temp       ; save off contents of STATUS register
          movf    status_temp,w     ; retrieve copy of STATUS register
          movwf   STATUS            ; restore pre-isr STATUS register contents
          swapf   w_temp,f
          swapf   w_temp,w          ; restore pre-isr W register contents
          retfie                    ; return from interrupt         
Start
          clrf     PORTA
          clrf     PORTB
          MOVLW    B'00000111'
          MOVWF    CMCON            ; Turn off comparator
          bsf      STATUS,RP0       ; bank one
          movlw    0xFF
          movwf    TRISA            ; porta all Input
          movlw    0x00 
          movwf    TRISB            ; portb all Output
          movlw   b'00000000'       ;See datasheet for prefered
          movwf   OPTION_REG        ;settings of OPTION_REG
          bcf      STATUS,RP0       ; return to bank 0
          call    Systest
          btfss   PORTA,0           ; Check if Upper Sensor = 1
          Call    Refill            ; If not go to Refill Subroutine
          Goto    Run               ; Then go to Operation Mode
Systest
          movlw b'10000000'         ; Set PortB,7 High the others Low
          movwf PORTB               
          movlw 0x05                ; Set Count1 to 05H
          movwf Count1             
          call Delay                ; Call Delay Subroutine
          movlw b'01000000'         ; Set PortB,6 High the others Low
          movwf PORTB
          movlw 0x05                ;Reset Counter1 to 05H
          movwf Count1
          call Delay                ;Call Delay Subroutine
          movlw b'00100000'         ; Set PortB,5 High the others Low
          movwf PORTB
          movlw 0x05                ;Reset Counter1 to 05H
          movwf Count1
          call Delay                ; Call Delay Subroutine
          movlw b'00010000'         ; Set PortB,4 High the others Low
          movwf PORTB
          movlw 0x05                ; Reset Counter to 05H
          movwf Count1                
          Call Delay                ; Call Delay Subroutine
          movlw b'11110000'         ; Set All Indicators High
          movwf PORTB
          movlw 0x05                ; Last Counter Reset to 05H
          movwf Count1
          call Delay                ; Call Delay Subroutine
          movlw b'10000000'         ; Turn on Power LED
          movwf PORTB
          RETURN                    ;Return to main program
Run
          movlw b'11000000'         ; Set Operation Mode
          btfss PORTA,0             ; Check if Upper Sensor = 1
          movlw b'11100000'         ; Set Warning Mode
          movwf PORTB               ; Move to PortB
          btfss PORTA,1             ; Check If Lower Sensor = 1
          Call Refill               ; Go to Refill Subroutine
          Goto Run                  ; Return to Top
Refill
          movlw b'10111000'         ; Set Refill Mode
          btfsc PORTA,0             ; Check If Upper Sensor = 1
          movlw b'11000000'         ; Then Set Operation Mode 
          movwf PORTB               ; Move to PORTB
          btfss PORTA,0             ; Check If Upper Sensor = 1
          goto Refill               ; If Not Return to Top of subroutine
          Return                    ; Return to Start
Delay
          decfsz Count1,1           ;Decrement Count1 1
          goto Delay                ;If Count1 > 0 Return to top
          Return                    ;Return to Previous place

          END
 
Might help with your delay routine to clean up the code and make one place to change the time for delay instead of many different places


Declarations
Code:
 ;***** VARIABLE DEFINITIONS
w_temp        EQU     0x70          ; variable used for context saving
status_temp   EQU     0x71          ; variable used for context saving
Count1        EQU     0X20          ; counter
Count1v       EQU     0X21          ; will be value for counter

;*************************************


And the delay routine
Code:
Delay:
    movf	Count1v,w		;Copies preset delay to working register
	movwf	Count1      ;Copies delay to Count1 to save code space
Delay1:		
	decfsz  Count1,1    ;Decrement Count1 1
	goto    Delay1      ;If Count1 > 0 Return to top
    Return              ;Return to Previous place

>< code not formatting properly, still understandable
 
Last edited:
I made those changes and had to tweak it a little bit to get it to work. my program makes 2 calls of each delay subroutine during each step of the indicator test in the systest section and from there the count1v is not used. I've also tweaked the refill section to not kick in the timer until the Lower Sensor goes high. I'm going to keep tweaking it up until I get the circuit and Hydroxy cell built and running to a point where I can test how long I need to set the timer for the Upper Sensor to go high.



Edit:

Oops.I forgot the new source.

Code:
       list      p=16f628            ; list directive to define processor
       #include <p16f628.inc>        ; processor specific variable definitions
       errorlevel   -302             ;hide banking message
    ;*****   
     __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _LVP_OFF
    ;*****
    ;internal osc settings
    ;*****
    ; '__CONFIG' directive is used to embed configuration data within .asm file.
    ; The lables following the directive are located in the respective .inc file.
    ; See respective data sheet for additional information on configuration word.

    ;***** VARIABLE DEFINITIONS
w_temp        EQU     0x70           ; variable used for context saving
status_temp   EQU     0x71           ; variable used for context saving
Count1        EQU     0X20           ; First Counter for Delay 
Count1v       EQU     0X21           ; Second Counter for Delay
    ;**********************************************************************
          ORG      0x000             ; processor reset vector
          goto     Start             ; go to beginning of program
          ORG      0x004             ; interrupt vector location
          movwf    w_temp            ; save off current W register contents
          movf     STATUS,w          ; move status register into W register
          movwf    status_temp       ; save off contents of STATUS register
          movf     status_temp,w     ; retrieve copy of STATUS register
          movwf    STATUS            ; restore pre-isr STATUS register contents
          swapf    w_temp,f
          swapf    w_temp,w          ; restore pre-isr W register contents
          retfie                     ; return from interrupt         

Start
          clrf     PORTA
          clrf     PORTB
          MOVLW    B'00000111'
          MOVWF    CMCON             ; Turn off comparator
          bsf      STATUS,RP0        ; bank one
          movlw    0xFF
          movwf    TRISA             ; porta all Input
          movlw    0x00 
          movwf    TRISB             ; portb all Output
          movlw    b'00000000'       ;See datasheet for prefered
          movwf    OPTION_REG        ;settings of OPTION_REG
          bcf      STATUS,RP0        ; return to bank 0
          call     Systest
          btfss    PORTA,0           ; Check if Upper Sensor = 1
          Call     Refill            ; If not go to Refill Subroutine
          Goto     Run               ; Then go to Operation Mode

Systest
          movlw    0x05
          movwf    Count1v           ; Set Delay Counter to 05H
          call     Delay             ; Call Delay Routine to set Delay time
          movlw    b'10000000'       ; Set PortB,7 High the others Low
          movwf    PORTB               
          call     Delay1            ; Call Delay Subroutine
          call     Delay             ; Call Delay Routine to set Delay time
          movlw    b'01000000'       ; Set PortB,6 High the others Low
          movwf    PORTB
          call     Delay1            ;Call Delay Subroutine
          call     Delay             ; Call Delay Routine to set Delay time
          movlw    b'00100000'       ; Set PortB,5 High the others Low
          movwf    PORTB
          call     Delay             ; Call Delay Routine to set Delay time
          call     Delay1            ; Call Delay Subroutine
          movlw    b'00010000'       ; Set PortB,4 High the others Low
          movwf    PORTB
          call     Delay             ; Call Delay Routine to set Delay time
          Call     Delay1            ; Call Delay Subroutine
          movlw    b'11110000'       ; Set All Indicators High
          movwf    PORTB
          call     Delay             ; Call Delay Routine to set Delay time
          call     Delay1            ; Call Delay Subroutine
          movlw    b'10000000'       ; Turn on Power LED
          movwf    PORTB
          RETURN                     ;Return to main program

Run
          movlw b'11000000'          ; Set Operation Mode
          btfss PORTA,0              ; Check if Upper Sensor = 1
          movlw b'11100000'          ; Set Warning Mode
          movwf PORTB                ; Move to PortB
          btfss PORTA,1              ; Check If Lower Sensor = 1
          Call Refill                ; Go to Refill Subroutine
          Goto Run                   ; Return to Top 

Refill
          movlw    b'10111000'       ; Set Refill Mode
          btfsc    PORTA,0           ; Check If Upper Sensor = 1
          movlw    b'11000000'       ; Then Set Operation Mode 
          movwf    PORTB             ; Move to PORTB
          movlw    0x62              ; Set W Register to 62H
          movwf    Count1            ; Move W Register to Count1
          btfsc    PORTA,1           ; Check If Lower Sensor = 1
          call     Delay1            ; Then Go To Delay Subroutine
          btfss    PORTA,0           ; Check If Upper Sensor = 1
          goto     Refill            ; If Not Return to Top of subroutine
          Return                     ; Return to Start

Delay:
          movfw       Count1v           ;Copies preset delay to working register
      movwf       Count1            ;Copies delay to Count1 to save code space
          Return

Delay1:        
     decfsz    Count1,1          ;Decrement Count1 1
     goto      Delay1            ;If Count1 > 0 Return to top
         Return                      ;Return to Previous place          
         END
 
Last edited:
I've been playing with my program some more and changing some values. I've completely replaced all the binary values within the source code with hexadecimal values. I've also created a decimal/hex/binary table for me to use If I want to change a value from decimal to hex or binary. Working with the hex and binary I've seen the pattern that binary follows going from 0 to 255. I'm thinking about adding a couple more subroutines that would be used to set a particular value for the timers to follow into the EEPROM memory of the PIC. I've created a new thread for that though. I've been reading the data sheet and getting pointers from here and other forums that I'm a member of, to figure out how to read and write to the EEPROM memory. I'm still working on the setup of the subroutines that would be used to set the variable value of the timer for the refill section that would be read from EEPROM when the setup input is low. When the setup input is high, it would read another input and set the value to the Working register until the setup switch returns low. Then it would write the W Register to the EEPROM and continue running the program.

Edit:

I've attached the hex/binary file so any newbies that may see this thread may have a reference when they go to start writing programs for PIC or any other microcontrollers.
 

Attachments

  • Hex-Binary Chart.txt
    26.2 KB · Views: 136
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top