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.

Can someone help me combine some code please.

Status
Not open for further replies.

No1Daemon

New Member
Hi all
With the help of a member of this forum the following code was developed.
I want to add the below functionality to it except for the double click.
I can't seem to make it work because of the definitions in the included msecdelay4MHz.inc

I also cannot work out how to easily remove the double click code from the code.

Can someone please have a look for me and tell me the best way to put it in to my code?

The link to the button code I want to add is here
**broken link removed**
I want it to work like so, Switch A short press calls SwAPressed.
Switch A Held for 1 second or more calls LVC_Setting
Switch B short press calls SwBPressed

Also, after I set my low voltage cutoff setting how can I return to my program without powering off the circuit? After the EEPROM write is completed.
And finally the ontime is to activate a relay for a given time, do I need the ontime code at all? If not I will remove it.
Thanks
Steve

Code:
;****************************************************
;SimpleTimer.asm                                     *
;
;14-6-2011                                          *
;****************************************************
;
    list    p=12F683
    radix    dec
    include    "p12f683.inc"
    
        errorlevel    -224    ; Don't complain about tris
        errorlevel    -302    ; Don't complain about BANK 1 Registers


    __CONFIG    _MCLRE_OFF & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.

;_MCLRE_OFF  - master clear must be off for gp3 to work as input pin 

;****************************************************************
; variables - names and files
;****************************************************************

temp1        equ 20h    ;
temp2        equ    21h    ; 
pushes        equ    22h    
flashes        equ    23h
on_time     equ 24h
off_time    equ 25h
minutes     equ 26h
loops       equ 27h
A_Dvalue    equ 28h
delA        equ    29h
delB        equ    30h
;****************************************************************
;Equates
;****************************************************************
status        equ    0x03
rp1            equ    0x06
rp0            equ    0x05
option_reg    equ 81h



Setup            bsf        STATUS, RP0     ;Bank 1            
                   movlw    b'11111100'        ;Set TRIS  GP0,1 out   GP2(AN2), 3,4,5 input
                movwf    TRISIO    
                movlw    b'01010100'
                movwf    ANSEL        ;A/D Clock FOSC/16, AN2(GP2) Set as analogue
     
                BCF        STATUS, RP0        ;bank 0
                movlw   07h             ;turn off Comparator ports
                movwf   CMCON0           ;must be placed in bank 0  
                movlw    b'00001001'
                movwf    ADCON0        ;Result left justified, Ref Vdd, Select AN2, ADC on
        

                            
                    ;read 3 locations in EEPROM                    
        
        bsf        status,rp0            
        clrf    EEADR            ;EEPROM location 0 for flashes of LED                 
        bsf        EECON1,0        ;to show number of 5 mins                        
        movf    EEDATA,w     
        bcf        STATUS,RP0    
        movwf    flashes            ;EEPROM 0 into flashes                
        
        bsf        STATUS,RP0            
        incf    EEADR        ;EEPROM location 1 for on_time                    
        bsf        EECON1,0    ;starts EEPROM read operation storing result in EEDATA                        
        movf    EEDATA,w    ;move read data into w
        bcf        STATUS,RP0    
        movwf    on_time     ;EEPROM 1 into on_time
        
        bsf        STATUS,RP0            
        incf    EEADR        ;EEPROM location 2 for off_time                         
        bsf        EECON1,0                            
        movf    EEDATA,w    
        bcf        STATUS,RP0    
        movwf    off_time    ;EEPROM 2 into off_time 
        goto     Main    
        
        
        
        
;********************
;* Delays             *
;********************

_50uS  movlw   .15
       movwf   delA
       decfsz     delA,f
       goto     $-1
       retlw     00


_327mS    goto    $+1
        decfsz     delA,f
        goto     $-2
        decfsz     delB,f
        goto     $-4    
        retlw     00        
                    
        
;****************************
;* Sub Routines             *
;****************************
            
SwA_Pressed
        
        incf    flashes,f            
        movlw   07
        xorwf   flashes,w     ;if flashes=7
        btfss   STATUS,Z  ;z=1 if flashes=7
        goto    $+3
        clrf    flashes
        goto    $-6    
                    
                    ;put flashes into EEPROM 0
        
        movf    flashes,w    ;put flashes into w    
        bsf        STATUS,RP0    ;select bank1    
        movwf    EEDATA            
        clrf    EEADR             
        bcf        STATUS,RP0    ;select bank0
        call     write
        
                   ;put same value into EEPROM 1 for on_time
                   
        movf    flashes,w    ;put flashes into w    
        bsf        STATUS,RP0    ;select bank1    
        movwf    EEDATA            
        incf    EEADR,1             
        bcf        STATUS,RP0    ;select bank0
        call     write
        
                ;flash LED 1-5 times
        
        movf    flashes,w
        movwf    temp1     ;save flashes in temp1 for decrementing
        bsf        GPIO,0   ;turn on LED    
        call    _327mS
        bcf        GPIO,0   ;turn off LED    
        call    _327mS
        decfsz  temp1,f
        goto    $-5        
        btfss    GPIO,5    ;loop until SwA is released
        goto    $-1
        retlw    00
        
                  ;SwB = minutes = off_time
SwB_Pressed
        
        incf    minutes,f            
        movlw   06
        xorwf   minutes,w     ;if minutes=6
        btfss   STATUS,Z  ;z=1 if minutes=6
        goto    $+3
        clrf    minutes
        goto    $-6    
                    
                    ;put minutes into EEPROM 2 as off_time
        
        movf    minutes,w    ;put minutes into w    
        bsf        STATUS,RP0    ;select bank1    
        movwf    EEDATA    
        movlw   2        
        movwf    EEADR             
        bcf        STATUS,RP0    ;select bank0
        call     write
        
               ;flash LED 1-5 times
        
        movf    minutes,w
        movwf    temp1     ;save minutes in temp1 for decrementing
        bsf        GPIO,0   ;turn on LED    
        call    _327mS
        bcf        GPIO,0   ;turn off LED    
        call    _327mS
        decfsz  temp1,f
        goto    $-5        
        btfss    GPIO,4    ;loop until SwB is released
        goto    $-1
        retlw    00        
        

        
        
        
        ;microcontroller loops this sub-routine producing ON time 
        ;as stored in EEPROM location 1        
        ;look for sw press every 327mS
        
time_on
        movlw    1
        bsf        STATUS,RP0            
        movwf    EEADR                                    
        bsf        EECON1,0    ;starts EEPROM read operation. Result in EEDATA                        
        movf    EEDATA,w    ;move read data into w
        bcf        STATUS,RP0
        movwf    on_time     ;will be 1-5
                            ;convert each value of "on_time" to 5 minutes
                            ;327mS x 16 x 58 = 5 minutes 
        swapf      on_time,f    ;rlf x 4 = 16 = swap nibbles    
        movlw   .58
        movwf    loops                    
        bsf        GPIO,1        ;turn on relay
        call    _327mS
        btfss    GPIO,5        ;test switchA
        goto    SwA_Pressed
        btfss    GPIO,4        ;test switchB
        goto    SwB_Pressed 
        decfsz  loops,f
        goto    $-6          
        decfsz  on_time,f
        goto    $-.09
        retlw   00
        
        ;microcontroller loops this sub-routine producing OFF time 
        ;as stored in EEPROM location 2
        ;look for sw press every 327mS
time_off
        movlw    2
        bsf        STATUS,RP0            
        movwf    EEADR                                    
        bsf        EECON1,0    ;starts EEPROM read operation. Result in EEDATA                        
        movf    EEDATA,w    ;move read data into w
        bcf        STATUS,RP0
        movwf    off_time    ;will be 1-5
                            ;327mS x 3 = 1 second                           
        addwf    off_time,f  ; multiply by 3    
        addwf    off_time,f        
        movlw   .60
        movwf    loops                                            
        bcf        GPIO,1        ;turn off relay
        call    _327mS
        btfss    GPIO,5        ;test switchA
        goto    SwA_Pressed
        btfss    GPIO,4        ;test switchB
        goto    SwB_Pressed 
        decfsz  loops,f
        goto    $-6       
        decfsz  off_time,f
        goto    $-.11
        retlw   00        
        
    
write    bsf        STATUS,RP0    ;select bank1    
        bsf        EECON1,WREN    ;enable write        
        movlw    55h         ;unlock codes
        movwf    EECON2
        movlw    0aah
        movwf    EECON2
        bsf        EECON1,WR    ;write begins
        bcf        STATUS,RP0    ;select bank0        
        btfss    PIR1,EEIF    ;wait for write to complete
        goto    $-1
        bcf        PIR1,EEIF
        bsf        STATUS,RP0    ;select bank1
        bcf        EECON1,WREN    ;disable other writes
        bcf        STATUS,RP0    ;select bank0                    
        retlw    00                
                        
;************************
;* Main                 *
;************************

Main        
        call    time_on
        call    time_off
        
LVC_Setting
        bsf        ADCON0,GO        ;GO = bit 1 = Start A/D conversion
 
        btfsc    ADCON0,GO    
        goto    $-1            ;Wait until A/D conversion is complete
 
        bsf        STATUS,RP0        ;Switch to Bank1
        movf    ADRESL,W        ;Move A/D conversion result to w
        bcf        STATUS,RP0        ;Switch to Bank0
        movwf    A_Dvalue       ;save A/D value in A_Dvalue
        goto    Main
                
;************************
;*EEPROM                 *
;************************
                                
        org        2100h            
        de      1,  2, 3        ;1=flashes   2=time_on   3=time _off    
                            
        END
 
To detect SwA pressed for 1 second, add the following code:

Code:
SwA_Pressed

        call    _327mS
        btfsc    gpio,5    ;test switchA low=Sw pressed
        goto    $+7 
        call    _327mS
        btfsc    gpio,5     ;test switchA low=Sw pressed
        goto    $+4
        call    _327mS
        btfss    gpio,5     ;test switchA low=Sw pressed
        goto    xxxx       ;switch pressed for 1 second        
        incf    flashes,f            
        movlw   07
        xorwf   flashes,w     ;if flashes=7
        btfss   status,z  ;z=1 if flashes=7
        goto    $+3
        clrf    flashes
        goto    $-6
 
Thats nice and short. So If I setup the adc for the low voltage cutoff as a seperate subroutine and then goto it from the switch pressed for 1 second part as you have put it here then that will give me the functionality I am after right?
Also, can you explain the logic behind the time on and time off functions you wrote please.
If I include this code will it work like this
Switch A short press increments timer
Switch A long press sets adc low voltage cutoff
Switch B short press activates 1 minute timer to get it in the water then activates relay for the period set by short pressing Switch A.
Thats what I am aiming for.
Also, what are the 1-5 led flashes for the SwitchB pressed in the code for?
I am not sure of the function off the Ontime
 
Last edited:
When you increment SwB, you have to know what the program has recognised, that's why the LED fhashes.
As I asked you before, you have to set up the program (write the code) to read the adc to see what value it is detecting on Pin5 (AN2). Then reduce the supply voltage and receive another reading.
 
But I didn't want to increment anything with SwB, just activate the timers.
And I do realise that I need to setup the code for the lvc setting, but I need the rest of the code to work first how I planned it to.

Once I get a long press of SwA to trigger the adc reading for the lvc then I can easily hook up a particular voltage and then read the chip again with MPLAB and see what the value is in the EEPROM.

Seems like the easiest way to do it.

Currently the ADC is performed every time the battery is connected therefore causing a new LVC setting to be recorded each time.
 
But I didn't want to increment anything with SwB
Just remove the code for SwB and send the micro to activate the timer. This is not rocket science. It doesn't even need a discussion.
 
You are a pro at this while I am still learning.
I cannot learn anything about this code without understanding your logic behind why you wrote it the way you did.

If you do not wish to help further then that is fine, I thank you for what you did and I will continue to try to find help to get the code the way I originally wanted it to run.

I did not want to just remove part of it as I didn't fully understand why you wrote it that way and wasn't sure if it was needed for any of the other code.

Thanks anyway
Steve
 
You simply have not done a single line of coding or changed anything in any of the code I have presented. We are not here to do everything for you.
You asked for someone to: “help you.” What has turned out is for someone to: “do it for you.”
You haven’t shown any initiative at all.
I asked you to simply "look for SwB pressed" and send the micro to a sub-routine that turns on a relay.
 
Last edited:
Thats not correct Colin. Just because I haven't posted what I have done doesn't mean I havent tried to alter what you did.
It is just an incorrect assumption on your part.
From my point of view you took the code to almost completion but not what I actually asked for without explaining anything about it to me and then left me either faced with a choice of trying to chop parts of it out without knowing exactly how it worked which would probably have rendered it useless or dumping it and starting again.I didn't want to waste your efforts so I attempted to learn what you were trying to achieve with the ontime etc which you still have attempted to explain.

So don't tell me what I want when you don't have a clue
 
So don't tell me what I want when you don't have a clue
Even from your personal messages to me, you are a very rude person.
You have absolutely no idea how to ask for something or how to treat people.
It is obvious that you have not added a single line of code to the program, by the way you have been asking around to help on the project. No-one else would be able to help with the code as it is very difficult to add to someone else’s program.
I have asked you to do simple additions and absolutely nothing has been created.
But the most annoying thing is you don’t follow my explicit instructions.
I have been a teacher for over 30 years and ran a business for many years. If you don’t follow instructions to-the-letter, you will NEVER get anywhere.
 
Last edited:
I am not rude at all Colin. I have repeatedly thanked you and also asked for further help.
It was your decision to write the code possibly because it was of use to you on your talking electronics website and you went ahead and did it without explaining anything. Thats hardly teaching is it which is why I came here in the first place. I don't want it done for me, I want someone to help me learn it.
Some people are not very good teachers.
I asked you to expand on what to do when you gave me those explicit instructions which I didn't know how to implement.
My last post did seem rude but you clearly aren't listening and don't realise your help so far has led to me being stuck.

I don't want to go back and forth with this, you think youve helped me and I am still stuck. Lets leave it at that and I will continue to seek help on the project elsewhere.

And again, I thank you for what you tried to do.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top