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.

A2D conversion question

Status
Not open for further replies.

lake_of_fire

Newbie level 5
Joined
Feb 8, 2012
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,342
Hi,

I have been using a PIC16F87, I have analogue to digital conversion working correctly, i am currently using it to check for a 0-5v range on the input

Code:
void A2D(void)
{
    float VLVL = 1.5;   //sets A2D alarm level
    if(getVIN()>VLVL)
    {  
    RA1 = 1;  //turn LED on
    }
    else
    {
    RA1 = 0;  //turn LED off
    }
    __delay_ms(200);
}

getVIN() is just a function that reads the A2D input, this works fine however when the program leaves this function (A2D) the LED is switched off RA1 goes low, until this function is called again. How can I get the RA1 to remain on once the function has been left? (I assumed maybe wrongly that RA1 should stay high)

I thought the LED on RA1 should be staying on

Thanks
 

Check it out ra1 maybe tri state buffer, so u have to decide what will be the logic level when not needed
 

I can see nothing wrong with your function.
RA1 must be being cleared in some other part of your program?
 

At last before closing braket try
while(1)
instruction may be it works but not sure
 

I recommend you post your entire program, it appears the issue is outside of the A2D() routine.

BigDog
 

I recommend you post your entire program, it appears the issue is outside of the A2D() routine.

BigDog


Hi, Ive included the whole code now thanks

Code:
#include  <htc.h>
#define _XTAL_FREQ 16000000

__CONFIG(FOSC_HS);

void A2D_setup(void)
{
    ADFM = 1;               //right justified
    ADCS2 = 1;              //sets the conversion ratio with ADCS1 and 0
    ADCS1 = 0;              //sets the conversion ratio
    ADCS0 =0;               //sets the conversion ratio
    PCFG3:PCFG0 = 1110;     //sets AN0 as an analogue input
    CHS2:CHS0 = 000;        //selects the analogue channel (AN0)
    ADON = 1;               //enables A/D converter

}

float getVIN(void)
{
    ADCON0bits.GO = 1;       //when ADON1 = 1, this bit starts the A/D conversion starts the conversion
    while(ADCON0bits.GO==1); //waits for completion of A/D conversion when done GO is cleared
    return(((ADRESH*256)+ADRESL)*0.00489);
}

void A2D(void)
{
    float VLVL = 1.5;   //sets A2D alarm level
    if(getVIN()>VLVL)
    RA1 = 1;  //turn light on
    else
    RA1 = 0;  //turn light off
    __delay_ms(200);
}

int main(void)
{
    TRISA1 = 0;
    TRISA5 = 0;
    A2D_setup();
    while(1)
    {
    CLRWDT();

    A2D();
    RA5 = 1;
    __delay_ms(200);
    RA5 = 0;
    __delay_ms(200);
    }
}

I have just put another LED on RA5 to test, when RA5 flashes on and off RA1 turns off independent of the input to the A2D (RA0).

I have also tested using an LED on PORTC not using RA5, this works as expected(unlike the above code), so I feel the problem may now be in how I setup the ports in A2Dsetup.
 

Just glancing at your code, one issue maybe the Watchdog Timer (WDT), I would always suggest disabling the WDT within the Configuration Bit Settings when developing your code.

In its current configuration the WDT appears it maybe expiring long before making it out of A2D() on the first pass.

Reference: PIC16F87/88 Datasheet, Section: 15.12.1 WDT OSCILLATOR, pg. 143

15.12.1 WDT OSCILLATOR

The WDT derives its time base from the 31.25 kHz
INTRC. The value of WDTCON is ‘---0 1000’ on all
Resets. This gives a nominal time base of 16.38 ms,
which is compatible with the time base generated with
previous PIC16 microcontroller versions.

Also the use of _delay() or its associated macros __delay_ms() or __delay_us() with the WDT feature can produce unpredictable results:

Reference: HI-TECH C Compiler for PIC10/12/16 MCUs Version 9.83 Release Notes, Section:5.1 General, pg. 12

5.1 General

Delays and the watchdog timer Note that if the _delay feature is used to insert a simple delay sequence or
loop, the CLRWDT instruction may be used in this sequence. If the watchdog timer is not being used, this
is of no concern. If the watchdog timer is enabled, then be aware that delays may reset its timer.

BigDog
 

Hi

I have now disabled the WDT in the configuration bits, the problem does still remain

Thanks for your help
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top