Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help to solve ADC problem of 16F676

Status
Not open for further replies.

swapan

Full Member level 4
Full Member level 4
Joined
Feb 20, 2009
Messages
199
Helped
27
Reputation
54
Reaction score
24
Trophy points
1,298
Location
Kolkata
Activity points
2,806
Friends,
I have decided to make an automatic voltage stabilizer using 16F676. What I want to do is driving the electromagnetic relays of an automatic voltage stabilizer using PIC. Before proceeding further I wanted to carry a test of the adc module. For this purpose I have arranged in the following manner. Two relays are driven by RA0 and RA1. RC2/AN6 and RC3/AN7 are used for AD input. A 4.7K Pot is used as potential driver connecting to 5V supply rail and ground so that any value of AD Sampling voltage could be provided (0V to 5V) by rotating the pot. Now according to the code, when the AD input is 5V, both RA0 and RA1 will go low making the relays OFF. At a point where AD input is lower than D'220', RA1 will go high making one of the relays ON. Similarly, at a point where AD input is lower than D'200', both RA0 and RA1 will go high making all the relays ON. In practical, when the AD input is below D'200', both RA0 and RA1 is held high. On gradual rotating the pot, making AD input above D'200', RA1 becomes clear. Upto this, the operation is OK. But rotating the pot further, making AD input above D'220', the RA1 goes again high whereas as per code it should remain low. At AD input above D'240' both RA0 and RA1 should held low. But making the AD input about 5V (D'255') both RA1 and RA0 remain high. Please see if there is any error in my code.

Swapan


_CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _HS_OSC

cblock 0x20
AD_VALUE1
endc

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

MAIN_ON:

                 MOVLW	 b'00011001'
                 MOVWF	 ADCON0
                 BSF	         ADCON0,GO

INIT:	

                 BTFSC	 ADCON0,GO
                 GOTO	 INIT
                 MOVF	 ADRESH,0
                 MOVWF	 AD_VALUE1
                 MOVLW	 D'240'
                 SUBWF	 AD_VALUE1
                 BTFSS	 STATUS,C
                 GOTO	 RL_1
                 MOVLW	 B'00000000'
                 MOVWF	 PORTA

RL_1:

                 MOVLW	 D'220'
                 SUBWF	 AD_VALUE1
                 BTFSC	 STATUS,C
                 GOTO	 MAIN_ON
                 MOVLW	 b'00000001'
                 MOVWF	 PORTA	
                 MOVLW	 D'200'
                 SUBWF	 AD_VALUE1
                 BTFSC	 STATUS,C
                 GOTO	 MAIN_ON
                 MOVLW	 b'00000011'	
                 MOVWF	 PORTA	

GOTO	 MAIN_ON	

END
 

A very late ans:

From instruction set summary

SUBWF Subtract W from f
Syntax: [ label ] SUBWF f,d
Operands: 0 ≤ f ≤ 127
d ∈ [0,1]
Operation: (f) - (W) → (destination)
Status
Affected:
C, DC, Z
Description: Subtract (2’s complement method)
W register from register 'f'. If 'd' is 0,
the result is stored in the W
register. If 'd' is 1, the result is
stored back in register 'f'.

Put ,0 like this
SUBWF AD_VALUE1 ,0
result is stored in w register not back in AD_VALUE1
Code:
  INCLUDE "P16F676.INC"
     __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & 

_CPD_OFF


	cblock 0x20 
AD_VALUE1 
	endc 


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

MAIN_ON: 

                 BSF            ADCON0,GO 

INIT:    

                 BTFSC    ADCON0,GO 
                 GOTO    INIT 
                 MOVF    ADRESH,0 
                 MOVWF    AD_VALUE1 
                 MOVLW    D'240' 
                 SUBWF    AD_VALUE1 ,0
                 BTFSS    STATUS,C 
                 GOTO    RL_1 
                 MOVLW    B'00000000' 
                 MOVWF    PORTA 

RL_1: 

                 MOVLW    D'220' 
                 SUBWF    AD_VALUE1 ,0
                 BTFSC    STATUS,C 
                 GOTO    MAIN_ON 
                 MOVLW    b'00000001' 
                 MOVWF    PORTA    
                 MOVLW    D'200' 
                 SUBWF    AD_VALUE1 ,0
                 BTFSC    STATUS,C 
                 GOTO    MAIN_ON 
                 MOVLW    b'00000011'    
                 MOVWF    PORTA    
	GOTO    MAIN_ON    

	END
[/quote]\]
I tested it . at 5V , both relays are off. When voltage is lowered ,Below 4.3V , RA0 turns on. at 3.9 V RA1 turns on. When voltage is raised RA1 is o, showing hysterisis of 1 bit. While RA0 is turned off at 4.7V.
Actually this code is very basic , contais many flaws. As sampling input is rectified
ac containing ripple voltage so average of many samples at specific intervals will be needed after zero crossing detection. hysterisis should also be incorporated so
relay don't chatter at some voltages. Delay on timer of 3 to 5 minutes should be included. over / under voltage protection ,overload protection control through
software can also be included.
I KNOW WHEN YOU VISIT THIS PAGE SOME DAY YOU MAY SAY ,I'D ALREADY FOUND MY MISTAKE.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top