PIC16F877a initialize - Page 1
Forum section: electronics / projects

author: Boris
author_rank: Newbie
timestamp: June 08, 2012, 01:22:47 pm
content: hello everybody,am kinda a newbie here, but i did alot of simple project using PIC16F88 and everything went as planned most of the time using microC, how ever now am trying to create a keypad lock using assembly on a pic16f887a, and its working well, until i disconnect the usb cable (from the pickit2) and reconnect it, then it doesnt work, i have to load and write the hex file to the pic again for it to work, i dont know whats the problem and how to fix it, (if i write an already loaded hex it doesnt work i have to load it again then write, and also if i turn the power off from the pickit2 software and then on it doesnt coz the problem, only when disconnecting the cable for some reason) any help of how to fix this problem?now i know its not an EEPROM thing as the code is still written there coz sometimes it doesnt work at all but sometimes it plays up abit trying to work but not properly so am assuming its an initializing problemhere is my code (sorry abit messy its my first asm code i write)Code:TMR0EQU 1STATUSEQU 3PCEQU2PORTAEQU 5PORTBEQU 6PORTCEQU7PORTDEQU8PORTEEQU9TRISAEQU85HTRISBEQU86HTRISCEQU87HTRISDEQU88HTRISEEQU89HINTCONEQU 0BHZEROBITEQU 2GIEEQU 7INTEEQU 4INTFEQU 1COUNTEQU 20HCOUNT2EQU30HTMR0_TEQU 21HW_TEMPEQU 22HSTATUS_TEQU 23HPORTB_TEQU 24HCOUNTAEQU25HCOUNTBEQU26HCOUNT_TEQU27HCOUNTA_TEQU28HCOUNTB_TEQU29HOPTION_REQU81H;OSCCONEQU8FHADCON1EQU9FHDIGIT1EQU2AHDIGIT2EQU2BHDIGIT3EQU2CHENTDIGITEQU2DHTEMPDIGITEQU2EHCURRENTSCANEQU2FH;*********************************************************LIST P=16F877AORG 0GOTO STARTORG 4GOTO ISR;*********************************************************DELAYP001 CLRF TMR0LOOPVS MOVF TMR0,WSUBLW .10BTFSS STATUS,ZEROBITGOTO LOOPVSRETLW 0DELAYP01 CLRF TMR0LOOPS MOVF TMR0,WSUBLW .20BTFSS STATUS,ZEROBITGOTO LOOPSRETLW 0DELAYT CLRF TMR0LOOPA MOVF TMR0,WSUBLW .195BTFSS STATUS,ZEROBITGOTO LOOPARETLW 0DELAYP1 MOVLW.2MOVWF COUNTBLOOPPCALLDELAYTDECFSZ COUNTBGOTO LOOPPRETLW 0DELAY1 MOVLW.10MOVWF COUNTALOOPBCALLDELAYP1DECFSZ COUNTAGOTO LOOPBRETLW 0DELAY5 MOVLW.5MOVWF COUNTLOOPCCALLDELAY1DECFSZ COUNTGOTO LOOPCRETLW 0DISPLAYONSCREENMOVFDIGIT1,WMOVWFPORTDMOVLWB'100'MOVWFPORTECALLDELAYP01MOVFDIGIT2,WMOVWFPORTDMOVLWB'010'MOVWFPORTECALLDELAYP01MOVFDIGIT3,WMOVWFPORTDMOVLWB'001'MOVWFPORTECALLDELAYP01RETLW 0SCAN NOPCOLUMN1 BCF PORTB,1 ;Clear B0BSF PORTB,2 ;Set B1BSFPORTB,3CHECK1 BTFSC PORTB,4 ;Is B3 Clear?GOTO CHECK4 ;NoCALL DELAYP001CHECK1A CALLDISPLAYONSCREENBTFSS PORTB,4GOTO CHECK1ACALL DELAYP001RETLW .1CHECK4 BTFSC PORTB,5 ;Is B4 Clear?GOTO CHECK7 ;NoCALL DELAYP001CHECK4A CALLDISPLAYONSCREENBTFSS PORTB,5GOTO CHECK4ACALL DELAYP001RETLW .4CHECK7 BTFSC PORTB,6 ;Is B5 Clear?GOTO CHECK11 ;NoCALL DELAYP001CHECK7A CALLDISPLAYONSCREENBTFSS PORTB,6GOTO CHECK7ACALL DELAYP001RETLW .7CHECK11 BTFSC PORTB,7 ;Is B6 Clear?GOTO COLUMN2 ;NoCALL DELAYP001CHECK11ACALLDISPLAYONSCREENBTFSS PORTB,7GOTO CHECK11ACALL DELAYP001RETLW .10COLUMN2 BSF PORTB,1 ;Set B0BCF PORTB,2 ;Clear B1BSF PORTB,3 ;Set B2CHECK2 BTFSC PORTB,4 ;Is B3 Clear?GOTO CHECK5 ;NoCALL DELAYP001CHECK2A CALLDISPLAYONSCREENBTFSS PORTB,4GOTO CHECK2ACALL DELAYP001RETLW .2 ;Yes, output 2.CHECK5 BTFSC PORTB,5 ;Is B4 Clear?GOTO CHECK8 ;NoCALL DELAYP001CHECK5A CALLDISPLAYONSCREENBTFSS PORTB,5GOTO CHECK5ACALL DELAYP001RETLW .5 ;Yes, output 5.CHECK8 BTFSC PORTB,6 ;Is B5 Clear?GOTO CHECK0 ;NoCALL DELAYP001CHECK8A CALLDISPLAYONSCREENBTFSS PORTB,6GOTO CHECK8ACALL DELAYP001RETLW .8 ;Yes, output 8.CHECK0 BTFSC PORTB,7 ;Is B6 Clear?GOTO COLUMN3 ;NoCALL DELAYP001CHECK0A CALLDISPLAYONSCREENBTFSS PORTB,7GOTO CHECK0ACALL DELAYP001RETLW 0 ;Yes, output 10.COLUMN3 BSF PORTB,1 ;Set B0BSF PORTB,2 ;Set B1BCF PORTB,3 ;Clear B2CHECK3 BTFSC PORTB,4 ;Is B3 Clear?GOTO CHECK6 ;NoCALL DELAYP001CHECK3A CALLDISPLAYONSCREENBTFSS PORTB,4GOTO CHECK3ACALL DELAYP001RETLW .3 ;Yes, output 3.CHECK6 BTFSC PORTB,5 ;Is B4 Clear?GOTO CHECK9 ;NoCALL DELAYP001CHECK6A CALLDISPLAYONSCREENBTFSS PORTB,5GOTO CHECK6ACALL DELAYP001RETLW .6 ;Yes, output 6.CHECK9 BTFSC PORTB,6 ;Is B5 Clear?GOTO CHECK12 ;NoCALL DELAYP001CHECK9A CALLDISPLAYONSCREENBTFSS PORTB,6GOTO CHECK9ACALL DELAYP001RETLW .9 ;Yes, output 9.CHECK12 BTFSC PORTB,7 ;Is B6 Clear?GOTO COLUMN1 ;NoCALL DELAYP001CHECK12A CALLDISPLAYONSCREENBTFSS PORTB,7GOTO CHECK12ACALL DELAYP001RETLW .10 ;Yes, output 12.CHECKCODEBSFPORTC,7MOVFDIGIT1,WSUBLW .5BTFSS STATUS,ZEROBIT ;IS NUMBER=1?GOTOENDCHECKCODEMOVFDIGIT2,WSUBLW .7BTFSS STATUS,ZEROBIT ;IS NUMBER=3?GOTOENDCHECKCODEMOVFDIGIT3,WSUBLW .1BTFSS STATUS,ZEROBIT ;IS NUMBER=7?GOTOENDCHECKCODE.BSF PORTC,6 ;Turn on LED, 137 enteredGOTOGOODCODEENDCHECKCODEBCFPORTC,6BSFPORTA,1MOVLW.25MOVWFCOUNTWRONGBEEPCALLDISPLAYONSCREENDECFSZCOUNTGOTOWRONGBEEPBCFPORTA,1GOODCODERETLW0;Interrupt Service Routine.ISR MOVWFW_TEMPSWAPFSTATUS,WMOVWFSTATUS_TMOVFTMR0,WMOVWFTMR0_TMOVFPORTB,WMOVWFPORTB_TMOVFCOUNT,WMOVWFCOUNT_TMOVFCOUNTA,WMOVWFCOUNTA_TMOVFCOUNTB,WMOVWFCOUNTB_T;*****CALLSCANMOVWFCURRENTSCANSUBLW.10BTFSCSTATUS,ZEROBITGOTOSKIPSCANBSFPORTA,1BCFPORTC,7INCFENTDIGITMOVFENTDIGIT,WSUBLW.3BTFSCSTATUS,ZEROBITCLRFENTDIGITNEXTDIGIT1MOVFENTDIGIT,WSUBLW.1BTFSSSTATUS,ZEROBITGOTONEXTDIGIT2MOVLW.10MOVWFDIGIT2MOVWFDIGIT3;INCFDIGIT1MOVFCURRENTSCAN,WMOVWFDIGIT1GOTOENDSCANNEXTDIGIT2MOVFENTDIGIT,WSUBLW.2BTFSSSTATUS,ZEROBITGOTONEXTDIGIT3MOVLW.10MOVWFDIGIT3MOVFDIGIT1,WMOVWFDIGIT2MOVFCURRENTSCAN,WMOVWFDIGIT1GOTOENDSCANNEXTDIGIT3MOVFENTDIGIT,W;SUBLW.3;BTFSCSTATUS,ZEROBIT;GOTONEXTDIGIT1MOVFDIGIT1,WMOVWFTEMPDIGITMOVFDIGIT2,WMOVWFDIGIT3MOVFTEMPDIGIT,WMOVWFDIGIT2MOVFCURRENTSCAN,WMOVWFDIGIT1ENDSCANNOPMOVFENTDIGIT,W;SUBLW.0BTFSCSTATUS,ZEROBITCALLCHECKCODESKIPSCANNOP;MOVFDIGIT1,W;SUBLW.10;BTFSCSTATUS,ZEROBIT;CLRFDIGIT1;INCFDIGIT2;MOVFDIGIT2,W;SUBLW.10;BTFSCSTATUS,ZEROBIT;CLRFDIGIT2;INCFDIGIT3;MOVFDIGIT3,W;SUBLW.10;BTFSCSTATUS,ZEROBIT;CLRFDIGIT3;CALLDELAYP1SW_HICALL DISPLAYONSCREENBTFSSPORTB,0GOTOSW_HIBCFPORTA,1;*****SWAPF STATUS_T,WMOVWF STATUSMOVF TMR0_T,WMOVWF TMR0MOVF PORTB_T,WMOVWF PORTBMOVF W_TEMP,WMOVFCOUNT_T,WMOVWFCOUNTMOVFCOUNTA_T,WMOVWFCOUNTAMOVFCOUNTB_T,WMOVWFCOUNTBBCF INTCON,INTFRETFIE;*********************************************************;CONFIGURATION SECTIONSTART BSF STATUS,5MOVLW B'11110001'MOVWF TRISBMOVLWB'00000110'MOVWFADCON1MOVLW B'00000000'MOVWFTRISAMOVLW B'00000000'MOVWFTRISCMOVLW B'00000000'MOVWFTRISDMOVLW B'00000000'MOVWFTRISE;MOVLWB'01100000';MOVWFOSCCONMOVLW B'00000111'MOVWFOPTION_R;OPTIONBCF STATUS,5CLRF PORTACLRF PORTBCLRFPORTDCLRFPORTECLRFPORTCBSF INTCON,GIEBSF INTCON,INTEMOVLW.10MOVWFDIGIT1MOVWFDIGIT2MOVWFDIGIT3CLRFENTDIGIT;*********************************************************BEGIN CALL DISPLAYONSCREENGOTO BEGINENDi attached an image of the board, i dont have a real schematic to attach as am one of those who start wiring stuff without a real plan :$anyways thanks

author: AlfBaz
author_rank: Super Contributor
timestamp: June 08, 2012, 02:50:14 pm
content: Is that a crystal oscillator? If so I don't see any caps hanging off it.Most likely, the PicKit2 is clocking the chip and when you unplug it your oscillator doesn't run.The data sheet will usually give you some guidance as to what value loading caps to use, but then again it depends on the crystal.I'd suggest using 4 pin oscillator package... Vcc, Gnd and clkout

author: Boris
author_rank: Newbie
timestamp: June 08, 2012, 08:24:04 pm
content: hey thanks for the replyyea i notice that the image doesnt show but actually yes there 2 caps there, 22pf each with a 4MHz crystal

author: AlfBaz
author_rank: Super Contributor
timestamp: June 09, 2012, 01:39:39 am
content: I don't see in your code what the config bits are set to. Its been a while since I used MPLAB or a 16F877. If I remember correctly you could set the config bits in the IDE without having to put them in the code, so you may have done that. If not a quick look at the data sheet shows that the default "erased" config bits are all ones which means the oscillator is set to RC oscillator

author: Boris
author_rank: Newbie
timestamp: June 09, 2012, 08:11:11 am
content: yes they are configured from the IDE i attached a screenshot of it

author: caroper
author_rank: Regular Contributor
timestamp: June 09, 2012, 10:17:30 am
content: It has been a while since I used any of the 8 Bit PIC's and assembler so this may not be relevant, But it is critical with C:Under [Project -> Build Configuration] Select Release rather than Debug.Under [Debugger -> Select Tool] Select NoneUnder [Programmer -> Select Programmer] Select PICKit2Program and it should run with or without the PICKit connected.CheersChris

author: Boris
author_rank: Newbie
timestamp: June 09, 2012, 10:40:42 am
content: i did that and it actually worked xD thanks that was great)cheers