AnsweredHot!Curiosity PIC16F1619: turn LED on using switch in assembly ..Need Some Help

Author
fengyi
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2017/03/08 21:47:50
  • Location: 0
  • Status: offline
2017/03/10 22:12:53 (permalink)
0

Curiosity PIC16F1619: turn LED on using switch in assembly ..Need Some Help

I am trying to use 8 bit curiosity board with pic16F1619 to turn led on/off by a switch
The setup is following:
INPUT:RC4    ------ on board switch S1
OUTPUT:RC5 ------ on board led D7
when the switch is pressed, the led is on. when the switch is released, led is off.
It looks like I have problem with the input, the input is always 0.
(In the debug, I can turn RC5 on/off from PC) 
I give my assembly code here:
; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR
#include "p16F1619.inc"
 
 __CONFIG _CONFIG1, _FOSC_INTOSC & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
; CONFIG2
; __config 0xFFFF
 __CONFIG _CONFIG2, _WRT_OFF & _PPS1WAY_ON & _ZCD_OFF & _PLLEN_ON & _STVREN_ON & _BORV_LO & _LPBOR_OFF & _LVP_ON
; CONFIG3
; __config 0xFFFF
 __CONFIG _CONFIG3, _WDTCPS_WDTCPS1F & _WDTE_OFF & _WDTCWS_WDTCWSSW & _WDTCCS_SWC
    
    
RES_VECT CODE 0x0000 ; processor reset vector
    GOTO START ; go to beginning of program

; TODO ADD INTERRUPTS HERE IF USED

;On Board LED::: D4:RA5 D5:PGEC D6:RA2 D7:RC5
;On Board Switch:: S1:RC4
;On board Pot POT1:RC0
    
MAIN_PROG CODE ; let linker place main program

START
    ;switch setup RC4, On Board LED: D7:RC5
    BANKSEL ANSELC ;set as digital IO
    ;MOVLW (1<<ANSC4|1<<ANSC5)
    MOVLW 0x30
    COMF WREG
    ANDWF ANSELC,F
    
    BANKSEL TRISC ;RC4 set as INPUT
    MOVLW (1<<TRISC4)
    IORWF TRISC,F
    MOVLW (1<<TRISC5) ;RC5 as output
    COMF WREG
    ANDWF TRISC,F
      
    BANKSEL PORTC
    
LOOP
    MOVFW (1<<RC4)
    ANDWF PORTC,W
    LSLF WREG,W ;RC4 value left shift to RC5
    MOVWF PORTC
    GOTO LOOP ; loop forever
    
    END

By the way I am using internal oscillator and watchdog is disabled.
Thank you for the helo P:
 
post edited by fengyi - 2017/03/11 18:08:40
#1
qhb
Superb Member
  • Total Posts : 6257
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Curiosity PIC16F1619: turn LED on using switch in assembly ..Need Some Help 2017/03/11 22:59:42 (permalink) ☼ Best Answerby fengyi 2017/03/12 11:41:51
+3 (3)
Does that board have a pullup resistor on RC4?
If not, you may need to enable the internal weak pullup, by setting WPUC4 in WPUC, and clearing WPUEN in OPTION_REG.
This seems a bit like overkill.

    BANKSEL ANSELC ;set as digital IO
;MOVLW (1<<ANSC4|1<<ANSC5)
    MOVLW 0x30
    COMF WREG
    ANDWF ANSELC,F

You could just set the whole port to digital with:

        BANKSEL ANSELC ;
        CLRF ANSELC ;set all PORTC to digital

or just set RC4 to digital with
        BANKSEL ANSELC ;
        BCF ANSELC,4 ;clear ANSELC4 to set RC4 to digital


this instruction is not doing what you are expecting:
 MOVFW (1<<RC4)

That is saying to move the contents of an SFR into the W register, and the address of the SFR is (1 << RC4) = 1 << 4 = 10h, which is the PIR1 register.

Try this:
LOOP:
        BANKSEL PORTC
        MOVF PORTC,W ;copy PORTC to W
        BANKSEL LATC
        BTFSS WREG,4 ;test copy of PORTC4 in W
        BCF LATC,5 ;clear LATC5 if PORTC4 was low
        BTFSC WREG,4 ;test copy of PORTC4 in W
        BSF LATC,5 ;set LATC5 if PORTC4 was high
        GOTO LOOP

#2
fengyi
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2017/03/08 21:47:50
  • Location: 0
  • Status: offline
Re: Curiosity PIC16F1619: turn LED on using switch in assembly ..Need Some Help 2017/03/12 11:30:38 (permalink)
0
qhb..You are right.
The problem is
 MOVFW (1<<RC4)

I have changed it to 
MOVLW (1<<RC4)

Everything works as suppose to
I take your advice to use BSF and BCF, and reorganize it. I ll give the working code here
; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR
#include "p16F1619.inc"
 
 __CONFIG _CONFIG1, _FOSC_INTOSC & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
; CONFIG2
; __config 0xFFFF
 __CONFIG _CONFIG2, _WRT_OFF & _PPS1WAY_ON & _ZCD_OFF & _PLLEN_ON & _STVREN_ON & _BORV_LO & _LPBOR_OFF & _LVP_ON
; CONFIG3
; __config 0xFFFF
 __CONFIG _CONFIG3, _WDTCPS_WDTCPS1F & _WDTE_OFF & _WDTCWS_WDTCWSSW & _WDTCCS_SWC
    
    
RES_VECT CODE 0x0000 ; processor reset vector
    GOTO START ; go to beginning of program

; TODO ADD INTERRUPTS HERE IF USED

;On Board LED::: D4:RA5 D5:PGEC D6:RA2 D7:RC5
;On Board Switch:: S1:RC4
;On board Pot POT1:RC0
    
MAIN_PROG CODE ; let linker place main program

START
    ;switch setup RC4, On Board LED: D7:RC5
    BANKSEL ANSELC ;set as digital IO
    CLRF ANSELC ;set all PORTC to digital
    
    BANKSEL TRISC ;slect Bank for TRISC
    BSF TRISC,TRISC4 ;set RC4 to digital input
    BCF TRISC,TRISC5 ;set RC5 to digital output
      
    ;BANKSEL PORTC
LOOP
    ;MOVLW (1<<RC4)
    ;ANDWF PORTC,W
    ;LSLF WREG,W ;RC4 value left shift to RC5
    ;MOVWF PORTC
    
        BANKSEL PORTC
        MOVF PORTC,W ;copy PORTC to W
        BANKSEL LATC
        BTFSS WREG,4 ;test copy of PORTC4 in W
        BCF LATC,5 ;clear LATC5 if PORTC4 was low
        BTFSC WREG,4 ;test copy of PORTC4 in W
        BSF LATC,5 ;set LATC5 if PORTC4 was high
    
    
    GOTO LOOP ; loop forever
    
    END

Thanks for your help qhb
#3
maru
New Member
  • Total Posts : 23
  • Reward points : 0
  • Joined: 2016/03/31 08:26:30
  • Location: 0
  • Status: offline
Re: Curiosity PIC16F1619: turn LED on using switch in assembly ..Need Some Help 2017/08/13 10:12:38 (permalink)
0
Hi,
Found this topic while I was searching for a assembly example (same setup, Curiosity/PIC16F1619) . I made a new project in MPLAB X IDE4.00 and pasted this code in a new .asm file. When I use the "Make and Program Device Main Project" icon I got these errors:
 
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/microchip/PIC_Projects/PIC16F1619_LedSwitch.X'
make -f nbproject/Makefile-default.mk dist/default/production/PIC16F1619_LedSwitch.X.production.hex
make[2]: Entering directory 'C:/microchip/PIC_Projects/PIC16F1619_LedSwitch.X'
make[2]: *** No rule to make target 'build/default/production/mcc_generated_files/pin_manager.o', needed by 'dist/default/production/PIC16F1619_LedSwitch.X.production.hex'. Stop.
make[2]: *** Waiting for unfinished jobs....
"C:\Program Files (x86)\Microchip\MPLABX\v4.00\mpasmx\mpasmx.exe" -q -p16f1619 -l"build/default/production/LedSwitch.lst" -e"build/default/production/LedSwitch.err" -o"build/default/production/LedSwitch.o" "LedSwitch.asm"
Message[302] C:\MICROCHIP\PIC_PROJECTS\PIC16F1619_LEDSWITCH.X\LEDSWITCH.ASM 27 : Register in operand not in bank 0. Ensure that bank bits are correct.
Message[302] C:\MICROCHIP\PIC_PROJECTS\PIC16F1619_LEDSWITCH.X\LEDSWITCH.ASM 30 : Register in operand not in bank 0. Ensure that bank bits are correct.
Message[302] C:\MICROCHIP\PIC_PROJECTS\PIC16F1619_LEDSWITCH.X\LEDSWITCH.ASM 31 : Register in operand not in bank 0. Ensure that bank bits are correct.
Message[302] C:\MICROCHIP\PIC_PROJECTS\PIC16F1619_LEDSWITCH.X\LEDSWITCH.ASM 44 : Register in operand not in bank 0. Ensure that bank bits are correct.
Message[302] C:\MICROCHIP\PIC_PROJECTS\PIC16F1619_LEDSWITCH.X\LEDSWITCH.ASM 46 : Register in operand not in bank 0. Ensure that bank bits are correct.
make[2]: Leaving directory 'C:/microchip/PIC_Projects/PIC16F1619_LedSwitch.X'
nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/microchip/PIC_Projects/PIC16F1619_LedSwitch.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 2s)
 
A little bit of help is appreciated, Thanks for reading 
 
 
 
 
#4
RISC
Super Member
  • Total Posts : 4646
  • Reward points : 0
  • Status: offline
Re: Curiosity PIC16F1619: turn LED on using switch in assembly ..Need Some Help 2017/08/13 11:03:46 (permalink)
0
Hi,
I suggest you read the tutorial on MPLAB X to get started :
http://www.microchipdeveloper.com
you probably did not create the project correctly...and forgot to select the compiler
Regards
#5
maru
New Member
  • Total Posts : 23
  • Reward points : 0
  • Joined: 2016/03/31 08:26:30
  • Location: 0
  • Status: offline
Re: Curiosity PIC16F1619: turn LED on using switch in assembly ..Need Some Help 2017/08/13 12:38:33 (permalink)
0
Hi,
 
Thanks, I just found it has to do with my v4.00 installation of MPLAB version. I've tested in v.3.65 and it all works fine!
 
Another C project in v4.00 gave these errors
In Starter Kit on Board
 
BUILD SUCCESSFUL (total time: 305ms)
Loading code from C:/microchip/PIC_Projects/CuriosityLab_Blink.X/dist/default/production/CuriosityLab_Blink.X.production.hex...
Loading completed
Connecting to programmer...
The programmer could not be started: Could not connect to tool hardware: PKOBSKDEPlatformTool, com.microchip.mplab.mdbcore.pkobskde.PKOBSKDEDbgToolManager
 
I will do a re-install of v4.00  
 
Edit:
Well, I did a fresh installation of v4.00 (after uninstall en reboot). There was a Xc8 warning but after login everything looks fine.  I've tested with both projects (standard Curiosity Blink and the assembly in this topic)...no way,  same errors!
 
I leave version 4.00 for what it is and will go back to MPLABX IDE v.3.65
 
 
post edited by maru - 2017/08/13 13:31:32
#6
RISC
Super Member
  • Total Posts : 4646
  • Reward points : 0
  • Status: offline
Re: Curiosity PIC16F1619: turn LED on using switch in assembly ..Need Some Help 2017/08/14 06:41:46 (permalink)
0
Hi,
maru
Hi,
 
Connecting to programmer...
The programmer could not be started: Could not connect to tool hardware: PKOBSKDEPlatformTool, com.microchip.mplab.mdbcore.pkobskde.PKOBSKDEDbgToolManager
 

this error message has nothing to see with the project...
The issue is that the Curiosity board firmware does not managed to be updated by MPLAB X v4.00.
Whenever a tool (pickit3,...of a board with PKOB) is connected to a new version of MPLAB X or a new PIC family, it needs to update its firmare. Unfortunately this process sometimes fails and you need to do it a couple of times until it finally updates.
I have one HPC curiosity board running with MPLAB X v4.00 and I remember I also had this issue
Regards 
#7
maru
New Member
  • Total Posts : 23
  • Reward points : 0
  • Joined: 2016/03/31 08:26:30
  • Location: 0
  • Status: offline
Re: Curiosity PIC16F1619: turn LED on using switch in assembly ..Need Some Help 2017/08/14 10:20:53 (permalink)
0
RISC
 
this error message has nothing to see with the project...
 



Agree,  I found some related answer in http://www.microchip.com/forums/m1009193.aspx
One forum member opened a ticket.  Perhaps an update will make it work. As I said, code is working well in v3.65
 
#8
Ewerning
Super Member
  • Total Posts : 77
  • Reward points : 0
  • Joined: 2017/09/08 11:02:31
  • Location: Between 1 and 0
  • Status: offline
Re: Curiosity PIC16F1619: turn LED on using switch in assembly ..Need Some Help 2017/10/06 11:59:54 (permalink)
0
qhb
...
Try this:
LOOP:
        BANKSEL PORTC
        MOVF PORTC,W ;copy PORTC to W
        BANKSEL LATC
        BTFSS WREG,4 ;test copy of PORTC4 in W
        BCF LATC,5 ;clear LATC5 if PORTC4 was low
        BTFSC WREG,4 ;test copy of PORTC4 in W
        BSF LATC,5 ;set LATC5 if PORTC4 was high
        GOTO LOOP



A nice and clean piece of code used very often. Another option to perform the same job and that illustrates the usage of GOTO as a viable option for program branches could be
LOOP:
        BANKSEL PORTC
        MOVF PORTC,W ;copy PORTC to W
        BANKSEL LATC
        BTFSS WREG,4 ;test copy of PORTC4 in W
        GOTO CLEARLED ; result was zero, go clear the output
        BSF LATC,5 ;result was one, set LATC5
        GOTO LOOP ;return to the main loop
CLEARLED:
BCF LATC,5 ;clear LATC5
        GOTO LOOP ;return to the main loop


Or even more optimized

CLEARLED:
BCF LATC,5 ;clear LATC5
LOOP:
        BANKSEL PORTC
        MOVF PORTC,W ;copy PORTC to W
        BANKSEL LATC
        BTFSS WREG,4 ;test copy of PORTC4 in W
        GOTO CLEARLED ; result was zero, go clear the output
        BSF LATC,5 ;result was one, set LATC5
        GOTO LOOP ;return to the main loop


And one could argue that the code with many GOTO is more difficult to read.
#9
Jump to:
© 2017 APG vNext Commercial Version 4.5