problemas de ADC PIC16F506 LED de 8 bits que se iluminan entre cada múltiplo de 256/4

0

Estaba teniendo problemas al usar un convertidor de 12 bits para un proyecto (vea mi otra pregunta), así que pensé que intentaría algo similar en un dispositivo de línea de base, PIC16F506, usando su ADC de 8 bits. Abajo está mi código.

/******************************************************************************/
/* Project: PIC16F506 ADC LED State Machine                                   */
/* Author: Ben Thomas                                                         */
/* Date: 03/07/17                                                             */
/* Pin Assignments;                                                           */
/* AN0: Voltage to be measured                                                */
/* RC0-3: Output LEDs (RC3 is MSB, RC0 is LSB)                                */
/******************************************************************************/
#include <xc.h>



/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/

//__CONFIG(MCLRE_ON & CP_OFF & WDT_OFF & IOSCFS_OFF & OSC_IntRC_RB4EN);
// CONFIG
#pragma config OSC = IntRC_RB4EN// Oscillator Selection bits (INTRC With RB4 and 1.125 ms DRT)
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled)
#pragma config CP = OFF         // Code Protect (Code protection off)
#pragma config MCLRE = ON       // Master Clear Enable bit (RB3/MCLR pin functions as MCLR)
#pragma config IOSCFS = OFF     // Internal Oscillator Frequency Select bit (4 MHz INTOSC Speed)
//#define LEDS    PORTC       //output LEDs on RC0-RC3
#define LED1    PORTCbits.RC0 //RED1 LED
#define LED2    PORTCbits.RC1 //RED2 LED
#define LED3    PORTCbits.RC2 //RED3 LED
#define LED4    PORTCbits.RC3 //RED4 LED
#define TRIP1   256/4          //Trip 1 LED1 ON under this value
#define TRIP2   (256/4)*2      //Trip 2 LED2 ON between TRIP 1 and TRIP 2
#define TRIP3   (256/4)*3      //Trip 3 LED3 ON between TRIP 2 and TRIP 3
#define TRIP4   256           //Trip 4 LED4 ON between TRIP 3 and TRIP 4

/* i.e. uint8_t <variable_name>; */
int state = 0;

void initialise(void)
{
    TRISC = 0b110000; //configure RC0-RC3 as outputs (pin7-pin10)
    CM2CON0 = 0;    //Disable comparator 2 (RC0, RC1 digital)
    VRCON = 0;
    state = 0;
    //configure ADC (P53)
    ADCON0 = 0b10110001;
             //10------         AN0 and AN2 are analog inputs
             //--11----         ADC conversion clock = INTOSC/4
             //----00--         Select channel AN0 as ADC channel
             //------0-         ADC go/done status bit (1-go, 0-done)
             //-------1         ADC enable bit, Turn ADC ON
    LED1 = 0;
    LED2 = 0;
    LED3 = 0;
    LED4 = 0;
}
/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/
void main(void)
{
    initialise();

    while(1)
    {
        switch(state)
        {
            case 0:
                //Sample analog input
                ADCON0bits.GO = 1;          //start ADC conversion
                while (ADCON0bits.nDONE)    //wait until conversion finished
                 ;
                state = 1;
                break;
            case 1:         //check ADC value and goto corresponding case for visual indication 
                if(ADRES < TRIP1)  //if ADRES < 256/4 go to RED1 case
                {
                    state = 10;
                    break;
                }
                if (ADRES>TRIP1 && ADRES<TRIP2) // if ADRES between 256/4 and 256/4*2
                {                               //go to RED2 case
                    state = 20;
                    break;
                }
                if (ADRES>TRIP2 && ADRES<TRIP3) //if ADRES between 256/4*2 and 256/4*3
                {                               //go to RED3 case
                    state = 30;
                    break;
                }
                if (ADRES>TRIP3)                //if ADRES greater than 256/4*3
                {                               //go to RED4 case
                    state = 40;
                    break;
                }
                break;

            case 10:        //red 1 zone ADC result less than 64
                LED1 = 1;
                LED2 = 0;
                LED3 = 0;
                LED4 = 0;
                state = 0;
                break;

            case 20:        // red 2 zone ADC result between 64 and 128 
                LED1 = 0;
                LED2 = 1;
                LED3 = 0;
                LED4 = 0;
                state = 0;
                break;

            case 30:        //red 3 zone ADC result between 128 and 192
                LED1 = 0;
                LED2 = 0;
                LED3 = 1;
                LED4 = 0;
                state = 0;
                break;

            case 40:        //red 4 zone ADC result greater than 192
                LED1 = 0;
                LED2 = 0;
                LED3 = 0;
                LED4 = 1;
                state = 0;
                break;


        }





    }

}

También se adjunta el esquema

Tengo un problema donde el LED2 LED se ilumina constantemente si hay 0V, 5V o se aplica un voltaje intermedio al pin 13 AN0 del ADC. El LED solo debe estar alto cuando el resultado del ADC está entre 64 y 128.

¿Hay algo simple que me falta? Tal vez me ayude con mi otra pregunta.

    
pregunta btommo

0 respuestas

Lea otras preguntas en las etiquetas