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.

Mal-functioning of PIC 16F676.

Status
Not open for further replies.

swapan

Member
Dear Friends,
I have designed a 16F676 based add-on device that will do some function when added to an inverter. The port assignment is as under.
PORT RA0 & RA1 – Used to drive two relays for changing the taps of Transformer.
PORT RA2 – Used for turning on/off battery charge.
PORT RC0 - Used for activate/deactivate Oscillator of inverter.
PORT RC1 – used for turning on/off change over relay (Inverter/AC Main).
PORT RC4 – used for turning on/off charging state indicator LED.
Port RC5 is so designed that if there is no AC Main, the voltage of it will be 5V otherwise it will be 0V. Rectified and filtered DC of a 12-0-12V step down transformer has been used for sampling of AC Main voltage. This sampling voltage has been given to RA3 also.

While burning the Microcontroller, I have defined the fuse settings as under.
WDT – disable, MCLRE – disable, CODE PROTECT ROM – disable, BANDGAP –highest, PWRTE – enable, BODEN – disable, CODE PROTECT EEP- disable, OSC – HS.

The circuit works well. But some times it is seen that the MCU does not work. Sometimes it is locked in any position. Untill I disconnect supply of the MCU, later connect, it does not start again. Please see my code and suggest any way out.

swapan



Code:
 __CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.

;***** VARIABLE DEFINITIONS (examples)

cblock		0x20
COUNT1
COUNT2
COUNT3
COUNT4
COUNT5
COUNT6
COUNT7
COUNT8
AD_VALUE1
AD_VALUE2
endc


;**********************************************************************
RESET_VECTOR    CODE    0x0000       ; processor reset vector
        goto    start                ; go to beginning of program




MAIN_PROG       CODE

start

		CLRF	PORTA
		CLRF	PORTC
		BSF	STATUS, RP0
		MOVLW	b'00111000'		
		MOVWF	TRISA
		MOVLW	b'00101100'		
		MOVWF	TRISC
		MOVLW	0XC0
		MOVWF	ANSEL
		MOVLW	b'00100000'
		MOVWF	ADCON1
		BCF	STATUS, RP0
		MOVLW	0X07
		MOVWF	CMCON

		CLRF	PORTA
		BTFSS	PORTC, 5	; See if AC Main is present.
		GOTO	AC_ON		; 
INV_ON:	        CLRF	PORTA
		BCF	PORTC,4		; if not, turn off “Charge Full” LED,
		BSF	PORTC,1		; Turn on Change Over Relay,
		
		BCF	PORTC,0		; Cut off Shut Down supply to Osc. 
			               ;  Circuit.
                CALL	DLY_3		; Wait for some moment. 
CONTI:	        BTFSC	PORTA,3	      ; Continue here till RA3 held 
		GOTO	AC_ON		; high on arrival of AC Main.(Inverter will 
		GOTO	CONTI		;  run)

AC_ON:      	MOVLW	D'32'		;  If RA3 is held high on arrival of  
		MOVWF	COUNT3		;  AC Main, take some delay.
LOOP3:	      MOVLW	D'255'		;
		MOVWF	COUNT1		;
LOOP2:	      MOVLW	D'255'		;	
		MOVWF	COUNT2		;
LOOP1:	        BTFSC	PORTC, 5	; Periodically check if RC5 is held
		GOTO	INV_ON		; high on failure of AC Main. If so, turn 		
		DECFSZ	COUNT2,1	; ON Inverter. Otherwise continue 
		GOTO	LOOP1		;	  delay.
		DECFSZ	COUNT1,1	;
		GOTO	LOOP2		;
		DECFSZ	COUNT3,1
		GOTO	LOOP3
		
HI_CUT:	          CALL	ADC_1		; After delay take ADC.
				
		MOVLW	D'250'		;  See if the AC Main voltage is not above
		SUBWF	AD_VALUE1,W	;  250V, If so turn on Inverter.
		BTFSC	STATUS,C	;
		GOTO	INV_ON		;									
				
		MOVLW	D'140'		; Similarly see if AC Main voltage is not 
		SUBWF	AD_VALUE1,W	;  below 140V. If so, turn on Inverter.
		BTFSS	STATUS,C	;  
		GOTO	INV_ON		;  
																				
		CLRF	PORTA		;  Turn off Change Over Relay
		BCF	PORTC,1		;  to AC Main position and 
		BSF	PORTC,0		;  take Shut Down Supply to
					;  Osc. Circuit. (Inverter OFF)
		
		MOVLW	D'64'		;  Again take some delay.
		MOVWF	COUNT3		;
LOOP31:	        MOVLW	D'255'			;
		MOVWF	COUNT1		;
LOOP21:	        MOVLW	D'255'			;	
		MOVWF	COUNT2		;
LOOP11:	        DECFSZ	COUNT2,1		;
		GOTO	LOOP11		;
		CALL	MAIN_CHK		;  Do periodical check, if there is AC 		
						;   Main failure. If so turn on Inverter.
		DECFSZ	COUNT1,1		;
		GOTO	LOOP21		;
		DECFSZ	COUNT3,1
		GOTO	LOOP31
		CALL	MAIN_CHK		;  Do periodical check, if there is AC 			
						; Main failure. If so turn on Inverter.
						;
		
		BSF	PORTA,2		; Turn on Charging relay.
		BCF	PORTA,0   		;  Turn off Tap change Relay.
		BCF	PORTA,1   		;  Turn off Tap change Relay.
MAIN_ON	        CALL	ADC_1   		;
		CALL	MAIN_CHK		;		
		MOVLW	D'250'  		;  See if the AC Main voltage is not above
		SUBWF	AD_VALUE1,W  	;  250V, If so turn on Inverter.
		BTFSC	STATUS,C		;
		GOTO	INV_ON   		;
		
		MOVLW	D'245'  		;  Introduced some amount of
		SUBWF	AD_VALUE1,W  	;  hysterias 
		BTFSC	STATUS,C		;
		GOTO	MAIN_ON		;
							;		
		MOVLW	D'210'   		;
		SUBWF	AD_VALUE1,W   	;
		BTFSS	STATUS,C   		;
		GOTO	RL_1   		;
							;		
		BCF	PORTA,0   		;
		BCF	PORTA,1   		;
		CALL	DLY_3

RL_1		CALL	MAIN_CHK		; 		
		MOVLW	D'205'   		;
		SUBWF	AD_VALUE1,W  	;
		BTFSC	STATUS,C  		;
		GOTO	BAT_CHK1   		;
							;  
		BCF	PORTA,1						
		BSF	PORTA,0		;  If AC Main is below 205V, turn
		CALL	DLY_3		;  on one Tap Change Relay.		
		
RL_2:		CALL	ADC_1
		CALL	MAIN_CHK		;			
		MOVLW	D'190'			;
		SUBWF	AD_VALUE1,W	;
		BTFSC	STATUS,C		;
		GOTO	BAT_CHK1		;  
		CALL	MAIN_CHK		;				
		MOVLW	D'185'			;  
		SUBWF	AD_VALUE1,W	;
		BTFSC	STATUS,C		;
		GOTO	BAT_CHK2		;
		BSF	PORTA,1		; If AC Main is below 185V, turn
		CALL	DLY_3		;  on another Tap Change Relay.
		CALL	MAIN_CHK		;
		MOVLW	D'145'			;  
		SUBWF	AD_VALUE1,W	;  
		BTFSS	STATUS,C		;
		GOTO	LOW_CUT		;
		CALL	MAIN_CHK		;		
		GOTO	BAT_CHK2		;
LOW_CUT	      MOVLW	D'140'			; If AC Main voltage is below 140V, 
		SUBWF	AD_VALUE1,W	;  turn on Inverter.
		
		BTFSS	STATUS,C		;  
		GOTO	INV_ON		;  
		GOTO	BAT_CHK2

BAT_CHK1:
		CALL	MAIN_CHK		; Take Battery voltage sample.		
		MOVLW	b'00011101'
		MOVWF	ADCON0
		CALL	DLY_20
		BSF	ADCON0,GO
WAIT:		BTFSC	ADCON0,GO
		GOTO	WAIT
		CALL	MAIN_CHK		;		
		MOVF	ADRESH,0
		MOVWF	AD_VALUE2
		MOVLW	D'240'			;  Check if battery is fully charged (14V).
		CALL	MAIN_CHK		;		
		SUBWF	AD_VALUE2,W	;
		BTFSC	STATUS,C		;
		
		CALL	CH_FULL		;  If fully charged, turn off charging
							;  relay and turn on Full Charged LED.
		CALL	MAIN_CHK		;		
		MOVLW	D'200'			;  
		SUBWF	AD_VALUE2		;
		BTFSS	STATUS,C
		
		CALL	CH_ON		; If Bat. Voltage is below 12V, turn On Chg &
						; turn Full Charged LED OFF.
		GOTO	MAIN_ON

		
BAT_CHK2:
		CALL	MAIN_CHK		;		
		MOVLW	b'00011101'
		MOVWF	ADCON0
		CALL	DLY_20
		BSF	ADCON0,GO
WAIT_1:	        BTFSC	ADCON0,GO
		GOTO	WAIT_1
		CALL	MAIN_CHK		;		
		MOVF	ADRESH,0
		MOVWF	AD_VALUE2
		MOVLW	D'240'			;  Check if battery is fully charged.
		CALL	MAIN_CHK		;		
		SUBWF	AD_VALUE2,W	;
		BTFSC	STATUS,C		;
		
		CALL	CH_FULL		; If fully charged, turn off charging &
							; Turn Full Charged LED ON.
		CALL	MAIN_CHK		;		
		MOVLW	D'200'
		SUBWF	AD_VALUE2,W
		BTFSS	STATUS,C
		
		CALL	CH_ON		; If Bat. Voltage is below 12V, turn On Chg &
						; turn Full Charged LED OFF.
		GOTO	RL_2

;SUB-ROUTINES ;
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ADC_1:
		MOVLW	b'00011001'
		MOVWF	ADCON0
		CALL	DLY_20
		CALL	MAIN_CHK		;		
		BSF	ADCON0,GO
INIT		BTFSC	ADCON0,GO
		GOTO	INIT
		CALL	MAIN_CHK		;		
		MOVF	ADRESH,0
		MOVWF	AD_VALUE1
		RETURN


MAIN_CHK:	BTFSC	PORTC, 5
		GOTO	INV_ON
		RETURN


DLY_20:	        MOVLW	D'20'
		MOVWF	COUNT4
WAIT_2	        BTFSC	PORTC, 5
		GOTO	INV_ON
		DECFSZ	COUNT4,1
		GOTO	WAIT_2
		RETURN		

CH_FULL:

		BCF	PORTA,2
		BSF	PORTC,4
	    	MOVLW	D'100'
		MOVWF	COUNT6
DL2:		MOVLW	D'255'
		MOVWF	COUNT7
DL_2C:	       DECFSZ	COUNT7,1
		GOTO	DL_2C
		DECFSZ	COUNT6,1
		GOTO	DL2
		RETURN

CH_ON:

		BSF	PORTA,2
		BCF	PORTC,4

		RETURN




DLY_3:	         MOVLW	D'75'
		MOVWF	COUNT8

DL3:		MOVLW	D'255'
		MOVWF	COUNT5
TEST:		DECFSZ	COUNT5,1
		GOTO	TEST
		DECFSZ	COUNT8,1
		GOTO	DL3
		RETURN
; initialize eeprom locations

EE      CODE    0x2100
        DE  0x00, 0x01, 0x02, 0x03

        END                       ; directive 'end of program'
 
It looks like you forgot the processor declaration. You should have "List p=16f676" and "#include <p16f676.inc>" as the first two items in your code.
 
Processor declaration i.e. "List p=16f676" and "#include <p16f676.inc>" are there in the code. But while copying the code for pasting in this forum, the portion has been wrongly left.

swapan
 
While burning the Microcontroller, I have defined the fuse settings as under.
WDT – disable, MCLRE – disable, CODE PROTECT ROM – disable, BANDGAP –highest, PWRTE – enable, BODEN – disable, CODE PROTECT EEP- disable, OSC – HS.
Code:
 __CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
I haven't looked at your code in detail yet, but you should not be setting the fuses both in MPLAB and in code. Every time you recompile your code, the settings in MPLAB will change to match the code. This could confuse you in the future when you recompile and can't figure out why it doesn't work any more. So decide what the CONFIG settings should be and change them in the code and forget about the MPLAB settings for them.
 
Last edited:
Thanks Mr. kchriste. Actually while creating the project, there were some configuration settings in the code by default. I have not changed them. On the contrary, during programming the MCU, I defined the fuse settings in the operating software that was supplied with the programmer. Thus there has been a mismatch in the code and in the functions of the programmer. Is it the source of such erratic operation of the MCU while in circuit? Sir, which fuse setting will prevail - settings in the code or settings in the programmer software?

swapan
 
Well, if you load your HEX file in the programmer, and then set the fuse settings in the programmer, then the programmer settings should prevail. But if you reload the HEX file, then it is possible that the programmer software will change them back to that of the HEX file, forcing you to set the fuse settings in the programmer once again.
It is OK to do it your way when you are experimenting, but once you've determined the proper fuse settings, it is best to update the CONFIG in the code to match.
 
Mr. swapan, can u share it ckt. diagram with me. i want asmabled it.
 
Hi every body
..........happy new year..........
..........happy new year 2010..........
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top