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.

Need help for ADC problem

Status
Not open for further replies.

swapan

Member
Friends,
I have decided to make an automatic voltage stabilizer using 16F676. While testing a portion of the project a peculiar working of the PIC is faced. Please see my codes. A pot (4.7K) has been used to feed analogue adc supply from 5V rail. when I rotate the pot to and fro at a point towards lower side the RA1 pin sets and clears. It is OK. Rotating the pot to and fro at a point towards higher side, the same RA1 pin sets and clears again. The RA0 pin remains set all along even I rotate the pot towards extreme high i.e. adc input is 5V. Please suggest any way.

Swapan

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

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

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
 
hi,
On a Oshonsoft sim test, your program sets AN0 and AN1 pins as outputs , not analog.:)
 
hi,
Look at the small changes to your prog.
It makes AN0 and AN1 analog.
 

Attachments

  • forum1.asm
    1.7 KB · Views: 134
Thanks Mr. ericgibbs for your cooperation. Actually, I have defined RA0, RA1, RA2 as outputs and RC2/AN6 and RC3/AN7 as AD input.
 
Thanks Mr. ericgibbs for your cooperation. Actually, I have defined RA0, RA1, RA2 as outputs and RC2/AN6 and RC3/AN7 as AD input.

hi,
The way I read your first post was that you are using AN0 and AN1 as analog.?

If you could explain the problem in more detail, I am sure we can help.

BTW: when you post code, select it all and then click on the '#' in the above menu bar, that way the formatting will not be lost.

Makes it easier to read.
 
hi,
Look at this image, you are RESETTING PORTA.1 when you SET PORTA.0
 

Attachments

  • AAesp04.gif
    AAesp04.gif
    6.8 KB · Views: 162
Thank you Sir. 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
 
hi swapan
Try this, now works OK in simulation.

EDIT: a zipped avi file showing the program working using the Oshonsoft simulator.
 

Attachments

  • forum_adc1.asm
    1.1 KB · Views: 125
  • adczip1.zip
    205.7 KB · Views: 104
Last edited:
Thanks Mr. Ericgibbs. I have not yet tested the modified code in practical. I have carefully avoided the instruction bcf and bsf as one of the friends of a forum suggested me not to use them because of Read-Modify-Write feature. Sir, would you spare your valuable time to detail the RMW feature of microcontrollers?

Swapan
 
Certain instructions, such as bcf and bsf, will read the entire register, perform some operation on it, and then write the result back to the register. For example, the instruction bsf REG1,2, as you know, sets bit 2 of REG1. What really happens inside the PIC is that all 8 bits of REG1 are read into an internal work register inside the PIC's ALU, bit 2 is set there, and then all 8 bits are written back into REG1.

The snag that can happen with RMW instructions is when they are used on PORT registers where there are pins set as input. Reading a port register actually reads the state of the pins themselves. Writing a port register writes to the output data latches of the port. A RMW instruction may change bits in the output latch that weren't intended. For example, let's say that RB0 is a pin that's multiplexed and used as both an input and an output. RB7 is an output pin that we want to turn on with a bsf instruction. Last time you set RB0 when it was an output, you set it to a 0, so that's what's in the output latch for that bit. When RB0 is set to output, it will be low. But when it's an input, it will read whatever the pin voltage is at (high or low). So, if you do a bsf PORTB,RB7 and RB0 as an input is high, the output data latch will end up with RB0 = 1 instead of 0.

The solution in this case is to use a shadow register to retain output pin values, do your RMW instructions there, then move the shadow register to the actual port.

Code:
    bsf    portb_shadow,7    ; Set portb to output in shadow
    movfw portb_shadow     ; Get shadow register
    movwf PORTB               ; and write to port B
 
It gives me great pleasure to say that the code is working fine. Really I am very thankful to you. Thank you again.

Swapan
 
Friends,
PL HELP ME...
I have desing to make an automatic voltage stabilizer using 16F676.A tremer

(2K) has been used to feed analogue adc supply from 5V rail in RA0[PIN. 13].
its arrangment in the following manner.

relay 4- RC2/8 .....using as o\p relay.

relay 3- RC1/9......using as fx. relay.

relay 2- RC0/10 .....using as fx.relay.

relay 1- RA1/12......using as fx.relay.

adc in -RA0/13.......feed analogue adc supply from 5V rail

bz -RC3/7.......o\p high volt[250]cut off [relay 4] it sbeep 1\2 sec

led[g] -RC4/6......i\p normal ind.

led[r] -RC5/5......o\p high volt[250]cut off ind.

low volt-RA4/3.......low voltage cut off enebal when it contact GND.

ins start-RA5/2......instant start[offter 3sec relay 4 driven]when it contact

GND.
ONLY ONE I\P (2K) has been used to feed analogue adc supply from 5V rail in

RA0[PIN. 13].
 

Attachments

  • sahu stb micro.zip
    226.2 KB · Views: 107
Last edited:
Friends,
PL HELP ME...
I have desing to make an automatic voltage stabilizer using 16F676.A tremer

(2K) has been used to feed analogue adc supply from 5V rail in RA0[PIN. 13].
its arrangment in the following manner.

relay 4- RC2/8 .....using as o\p relay.

relay 3- RC1/9......using as fx. relay.

relay 2- RC0/10 .....using as fx.relay.

relay 1- RA1/12......using as fx.relay.

adc in -RA0/13.......feed analogue adc supply from 5V rail

bz -RC3/7.......o\p high volt[250]cut off [relay 4] it sbeep 1\2 sec

led[g] -RC4/6......i\p normal ind.

led[r] -RC5/5......o\p high volt[250]cut off ind.

low volt-RA4/3.......low voltage cut off enebal when it contact GND.

ins start-RA5/2......instant start[offter 3sec relay 4 driven]when it contact

GND.
ONLY ONE I\P (2K) has been used to feed analogue adc supply from 5V rail in

RA0[PIN. 13].



It gives me great pleasure to say that if any body repply me.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top