Author Topic: PIC16F877a initialize  (Read 3767 times)

0 Members and 1 Guest are viewing this topic.

Offline BorisTopic starter

  • Newbie
  • Posts: 6
PIC16F877a initialize
« on: June 08, 2012, 01:22:47 pm »
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 problem
here is my code (sorry abit messy its my first asm code i write)
Code: [Select]

TMR0 EQU     1     
STATUS EQU     3   
PC EQU 2       
PORTA EQU     5             
PORTB EQU     6
PORTC EQU 7     
PORTD EQU 8
PORTE EQU 9
TRISA EQU 85H
TRISB EQU 86H
TRISC EQU 87H
TRISD EQU 88H
TRISE EQU 89H
INTCON EQU     0BH   
ZEROBIT EQU     2               
GIE EQU     7     
INTE EQU     4             
INTF EQU     1     
COUNT EQU     20H
COUNT2 EQU 30H   
TMR0_T EQU     21H     
W_TEMP EQU     22H     
STATUS_T EQU     23H
PORTB_T EQU     24H   
COUNTA EQU 25H
COUNTB EQU 26H
COUNT_T EQU 27H
COUNTA_T EQU 28H
COUNTB_T EQU 29H
OPTION_R EQU 81H
;OSCCON EQU 8FH
ADCON1 EQU 9FH
DIGIT1 EQU 2AH
DIGIT2 EQU 2BH
DIGIT3 EQU 2CH
ENTDIGIT EQU 2DH
TEMPDIGIT EQU 2EH
CURRENTSCAN EQU 2FH


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

LIST    P=16F877A
ORG     0             
GOTO    START         
ORG     4
GOTO    ISR           
;*********************************************************

DELAYP001  CLRF    TMR0   
LOOPVS   MOVF    TMR0,W 
SUBLW   .10   
BTFSS   STATUS,ZEROBIT
GOTO    LOOPVS         
RETLW   0

DELAYP01  CLRF    TMR0   
LOOPS   MOVF    TMR0,W 
SUBLW   .20   
BTFSS   STATUS,ZEROBIT
GOTO    LOOPS         
RETLW   0 

DELAYT  CLRF    TMR0   
LOOPA   MOVF    TMR0,W 
SUBLW   .195     
BTFSS   STATUS,ZEROBIT
GOTO    LOOPA         
RETLW   0             


DELAYP1  MOVLW .2
    MOVWF    COUNTB
LOOPP CALL DELAYT
DECFSZ  COUNTB
GOTO    LOOPP         
RETLW   0             


DELAY1  MOVLW .10
    MOVWF    COUNTA
LOOPB CALL DELAYP1
DECFSZ  COUNTA
GOTO    LOOPB         
RETLW   0             

DELAY5  MOVLW .5
    MOVWF    COUNT
LOOPC CALL DELAY1
DECFSZ  COUNT
GOTO    LOOPC         
RETLW   0   


DISPLAYONSCREEN MOVF DIGIT1,W
MOVWF PORTD

MOVLW B'100'
MOVWF PORTE

CALL DELAYP01

MOVF DIGIT2,W
MOVWF PORTD

MOVLW B'010'
MOVWF PORTE

CALL DELAYP01

MOVF DIGIT3,W
MOVWF PORTD

MOVLW B'001'
MOVWF PORTE

CALL DELAYP01
RETLW   0
 

SCAN    NOP
   
COLUMN1 BCF     PORTB,1         ;Clear B0
BSF     PORTB,2         ;Set B1
BSF PORTB,3


CHECK1  BTFSC           PORTB,4         ;Is B3 Clear?
GOTO            CHECK4          ;No
CALL            DELAYP001
CHECK1A CALL DISPLAYONSCREEN
BTFSS           PORTB,4
GOTO            CHECK1A
CALL            DELAYP001
RETLW           .1

CHECK4  BTFSC           PORTB,5         ;Is B4 Clear?
GOTO            CHECK7          ;No
CALL            DELAYP001
CHECK4A CALL DISPLAYONSCREEN
BTFSS           PORTB,5
GOTO            CHECK4A
CALL            DELAYP001
RETLW           .4

CHECK7  BTFSC           PORTB,6         ;Is B5 Clear?
GOTO            CHECK11         ;No
CALL            DELAYP001
CHECK7A CALL DISPLAYONSCREEN
BTFSS           PORTB,6
GOTO            CHECK7A
CALL            DELAYP001
RETLW           .7

CHECK11 BTFSC           PORTB,7         ;Is B6 Clear?
GOTO            COLUMN2         ;No
CALL            DELAYP001
CHECK11A CALL DISPLAYONSCREEN
BTFSS          PORTB,7
GOTO            CHECK11A
CALL            DELAYP001
RETLW           .10

COLUMN2 BSF             PORTB,1         ;Set B0
BCF             PORTB,2         ;Clear B1
BSF             PORTB,3         ;Set B2

CHECK2  BTFSC           PORTB,4         ;Is B3 Clear?
GOTO            CHECK5          ;No
CALL            DELAYP001
CHECK2A CALL DISPLAYONSCREEN
BTFSS           PORTB,4
GOTO            CHECK2A
CALL            DELAYP001
RETLW           .2              ;Yes, output 2.
   
CHECK5  BTFSC           PORTB,5         ;Is B4 Clear?
GOTO            CHECK8          ;No
CALL            DELAYP001
CHECK5A CALL DISPLAYONSCREEN
BTFSS           PORTB,5
GOTO            CHECK5A
CALL            DELAYP001
RETLW           .5              ;Yes, output 5.
     
CHECK8  BTFSC           PORTB,6         ;Is B5 Clear?
GOTO            CHECK0          ;No
CALL            DELAYP001
CHECK8A CALL DISPLAYONSCREEN
BTFSS           PORTB,6
GOTO            CHECK8A
CALL            DELAYP001
RETLW           .8              ;Yes, output 8.
     
CHECK0  BTFSC           PORTB,7         ;Is B6 Clear?
GOTO            COLUMN3         ;No
CALL            DELAYP001
CHECK0A CALL DISPLAYONSCREEN
BTFSS           PORTB,7
GOTO            CHECK0A
CALL            DELAYP001
RETLW           0               ;Yes, output 10.


COLUMN3 BSF             PORTB,1         ;Set B0
BSF             PORTB,2         ;Set B1
BCF             PORTB,3         ;Clear B2

CHECK3  BTFSC           PORTB,4         ;Is B3 Clear?
GOTO            CHECK6          ;No
CALL            DELAYP001
CHECK3A CALL DISPLAYONSCREEN
BTFSS           PORTB,4
GOTO            CHECK3A
CALL            DELAYP001
RETLW           .3              ;Yes, output 3.
   
CHECK6  BTFSC           PORTB,5         ;Is B4 Clear?
GOTO            CHECK9          ;No
CALL            DELAYP001
CHECK6A CALL DISPLAYONSCREEN
BTFSS           PORTB,5
GOTO            CHECK6A
CALL            DELAYP001
RETLW           .6              ;Yes, output 6.
     
CHECK9  BTFSC           PORTB,6         ;Is B5 Clear?
GOTO            CHECK12         ;No
CALL            DELAYP001
CHECK9A CALL DISPLAYONSCREEN
BTFSS           PORTB,6
GOTO            CHECK9A
CALL            DELAYP001
RETLW           .9              ;Yes, output 9.
 
CHECK12 BTFSC           PORTB,7         ;Is B6 Clear?
GOTO            COLUMN1         ;No
CALL            DELAYP001
CHECK12A CALL DISPLAYONSCREEN
BTFSS          PORTB,7
GOTO            CHECK12A
CALL            DELAYP001
RETLW           .10                     ;Yes, output 12.


CHECKCODE
BSF PORTC,7
MOVF DIGIT1,W
SUBLW   .5 
BTFSS   STATUS,ZEROBIT     ;IS NUMBER=1?
GOTO ENDCHECKCODE


MOVF DIGIT2,W
SUBLW   .7 
BTFSS   STATUS,ZEROBIT     ;IS NUMBER=3?
GOTO ENDCHECKCODE


MOVF DIGIT3,W
SUBLW   .1 
BTFSS   STATUS,ZEROBIT     ;IS NUMBER=7?
GOTO ENDCHECKCODE

. BSF     PORTC,6   ;Turn on LED, 137 entered
GOTO GOODCODE

ENDCHECKCODE

BCF PORTC,6
BSF PORTA,1

MOVLW .25
MOVWF COUNT
WRONGBEEP
CALL DISPLAYONSCREEN
DECFSZ COUNT
GOTO WRONGBEEP
BCF PORTA,1




GOODCODE
RETLW 0
;Interrupt Service Routine.

ISR     MOVWF W_TEMP 
SWAPF STATUS,W
MOVWF STATUS_T
MOVF TMR0,W
MOVWF TMR0_T 
MOVF PORTB,W
MOVWF PORTB_T
MOVF COUNT,W
MOVWF COUNT_T
MOVF COUNTA,W
MOVWF COUNTA_T
MOVF COUNTB,W
MOVWF COUNTB_T

;*****

CALL SCAN
MOVWF CURRENTSCAN
SUBLW .10
BTFSC STATUS,ZEROBIT
GOTO SKIPSCAN

BSF PORTA,1
BCF PORTC,7

INCF ENTDIGIT
MOVF ENTDIGIT,W
SUBLW .3
BTFSC STATUS,ZEROBIT
CLRF ENTDIGIT

NEXTDIGIT1 MOVF ENTDIGIT,W
SUBLW .1
BTFSS STATUS,ZEROBIT
GOTO NEXTDIGIT2

MOVLW .10
MOVWF DIGIT2
MOVWF DIGIT3
;INCF DIGIT1
MOVF CURRENTSCAN,W
MOVWF DIGIT1
GOTO ENDSCAN

NEXTDIGIT2 MOVF ENTDIGIT,W
SUBLW .2
BTFSS STATUS,ZEROBIT
GOTO NEXTDIGIT3

MOVLW .10
MOVWF DIGIT3

MOVF DIGIT1,W
MOVWF DIGIT2

MOVF CURRENTSCAN,W
MOVWF DIGIT1
GOTO ENDSCAN




NEXTDIGIT3 MOVF ENTDIGIT,W
; SUBLW .3
; BTFSC STATUS,ZEROBIT
; GOTO NEXTDIGIT1
MOVF DIGIT1,W
MOVWF TEMPDIGIT

MOVF DIGIT2,W
MOVWF DIGIT3

MOVF TEMPDIGIT,W
MOVWF DIGIT2

MOVF CURRENTSCAN,W
MOVWF DIGIT1

ENDSCAN NOP

MOVF ENTDIGIT,W
;SUBLW .0
BTFSC STATUS,ZEROBIT
CALL CHECKCODE

SKIPSCAN NOP

;MOVF DIGIT1,W
;SUBLW .10
;BTFSC STATUS,ZEROBIT
;CLRF DIGIT1   

;INCF DIGIT2
;MOVF DIGIT2,W
;SUBLW .10
;BTFSC STATUS,ZEROBIT
;CLRF DIGIT2   

;INCF DIGIT3
;MOVF DIGIT3,W
;SUBLW .10
;BTFSC STATUS,ZEROBIT
;CLRF DIGIT3 
;CALL DELAYP1



SW_HI CALL DISPLAYONSCREEN
BTFSS PORTB,0
GOTO SW_HI

BCF PORTA,1



;*****

SWAPF    STATUS_T,W     
MOVWF   STATUS                 
MOVF    TMR0_T,W
MOVWF   TMR0                   
MOVF    PORTB_T,W
MOVWF   PORTB                   
MOVF    W_TEMP,W     
MOVF COUNT_T,W
MOVWF COUNT
MOVF COUNTA_T,W
MOVWF COUNTA
MOVF COUNTB_T,W
MOVWF COUNTB           

BCF     INTCON,INTF             
RETFIE                         

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

;CONFIGURATION SECTION

START   BSF     STATUS,5           
MOVLW   B'11110001'
MOVWF    TRISB

MOVLW B'00000110'
MOVWF ADCON1

MOVLW   B'00000000'                     
MOVWF TRISA

MOVLW   B'00000000'                     
MOVWF TRISC

MOVLW   B'00000000'                     
MOVWF TRISD

MOVLW   B'00000000'                     
MOVWF TRISE

; MOVLW B'01100000'
; MOVWF OSCCON
             
MOVLW   B'00000111'       
MOVWF OPTION_R                   
;OPTION


BCF     STATUS,5         
CLRF    PORTA             
CLRF    PORTB     
CLRF PORTD       
CLRF PORTE
CLRF PORTC
BSF     INTCON,GIE       
BSF     INTCON,INTE   

MOVLW .10
MOVWF DIGIT1
MOVWF DIGIT2
MOVWF DIGIT3
CLRF ENTDIGIT

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


BEGIN   CALL DISPLAYONSCREEN
GOTO    BEGIN

END

i 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 :)
 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2187
  • Country: au
Re: PIC16F877a initialize
« Reply #1 on: June 08, 2012, 02:50:14 pm »
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
 

Offline BorisTopic starter

  • Newbie
  • Posts: 6
Re: PIC16F877a initialize
« Reply #2 on: June 08, 2012, 08:24:04 pm »
hey thanks for the reply :) yea i notice that the image doesnt show but actually yes there 2 caps there, 22pf each with a 4MHz crystal

 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2187
  • Country: au
Re: PIC16F877a initialize
« Reply #3 on: June 09, 2012, 01:39:39 am »
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
 

Offline BorisTopic starter

  • Newbie
  • Posts: 6
Re: PIC16F877a initialize
« Reply #4 on: June 09, 2012, 08:11:11 am »
yes they are configured from the IDE i attached  a screenshot of it
 

Offline caroper

  • Regular Contributor
  • *
  • Posts: 193
  • Country: za
    • Take your PIC
Re: PIC16F877a initialize
« Reply #5 on: June 09, 2012, 10:17:30 am »
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 None
Under [Programmer -> Select Programmer] Select PICKit2


Program and it should run with or without the PICKit connected.


Cheers
Chris


Offline BorisTopic starter

  • Newbie
  • Posts: 6
Re: PIC16F877a initialize
« Reply #6 on: June 09, 2012, 10:40:42 am »
i did that and it actually worked xD thanks that was great :))

cheers
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->