[SOLVED] Basic Help with ADC pic16f877

Status
Not open for further replies.

abmoncivais

Newbie level 2
Joined
Dec 17, 2014
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Victoria de Durango, Mexico
Visit site
Activity points
18
Hi, i hope that someone can help me with a basic ADC program.

I'm trying to read 3 analogic pins in the portA, then the program have to compare the 3 inputs, and turn on a pin in the PORTB.

The action is for the lowest value in the portA


The program just turns on random leds in the portB

I'm using CCS and proteus, PIC16F877





#include <16F877A.h>
#device adc=10
#FUSES XT, NOWDT, NOPROTECT, PUT
#use delay (clock=4000000)
#BYTE TRISA=0X85
#BYTE PORTA=0X05
#BYTE PORTB=0X06


void main()
{
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_tris_b(0x00);
set_tris_a(0xFF);
while(1)
{
int16 x;
int16 y;
int16 z;

delay_ms(100);
set_adc_channel(0);
delay_ms(100);
x=read_adc();
delay_ms(100);
set_adc_channel(1);
delay_ms(100);
y=read_adc();
delay_ms(100);
set_adc_channel(3);
delay_ms(100);
z=read_adc();
delay_ms(100);



if (((x<y) && (x<z)))
{
PORTB=0b10000000;
delay_ms(100);
}
else
{
if (((y<x) && (y<z)))
{
PORTB=0b01000000;
delay_ms(100);
}
else
{
if(((z<x) && (z<y)))
{
PORTB=0b00100000;
delay_ms(100);
}
}
}
delay_ms(100);
}
}
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (((x<y) && (x<z)))
{
PORTB=0b10000000;
delay_ms(100);
}
else if (((y<x) && (y<z)))
{
 
PORTB=0b01000000;
delay_ms(100);
}
else if(((z<x) && (z<y)))
{
 
PORTB=0b00100000;
delay_ms(100);
}



try this may work way you want
 

Thanks!

Actually, i used to have more things wrong in the code

This is my final code

Code:
#include <16f877a.h>
#device adc=10

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected

#use delay(clock=8000000)

#byte PORTB = 0x06 //Definimos PORTB con la dirección del registro PORTB.



void main()
{

int16 x=0;
int16 y=0;
int16 z=0;

////Configuración///
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);
//setup_pp(PMP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);

// unsigned int16 tension=0;
//Variable para almacenar el valor de tensión leido por el ADC
//unsigned int8 valor; //Variable temporal.

set_tris_b(0x00); //0b0000000// Configuramos el puerto B como salida.





while(1){

set_adc_channel(0); //elegimos el canal que vamos a leer.
delay_us(10); // esperamos 10uS necesarios para el ADC
x=read_adc(); //Leemos la tensión en el canal que elegimos.

delay_us(10);
set_adc_channel(1);
delay_us(10);
y=read_adc();

delay_us(10);
set_adc_channel(3);
delay_us(10);
z=read_adc();





delay_ms(1); //Esperamos un poco.
//valor=tension/127;
//Calculamos a cuantos LEDs encendidos corresponde la tensión leida.

      
      
         if (((x<y) && (x<z)))      
         {
            PORTB=0b10000000;
            delay_ms(100);
         }
            else if (((y<x) && (y<z)))
         {
 
            PORTB=0b01000000;
            delay_ms(100);
         }
            else if(((z<x) && (z<y)))
         {
 
            PORTB=0b00100000;
            delay_ms(100);
         }

   }
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…