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.

ADC on PIC18F4520 using c programming

Status
Not open for further replies.

gordon the light

Newbie level 4
Joined
Feb 13, 2012
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,319
Hi, I'm new in programming. I need help in C programming using ADC in PIC18F4520.

First, i will be using channel 0 and I set RA0 as input which is a temperature sensor, LM351. Then the output will be connected to RC2 as buzzer. So i want the buzzer sound when the temperature reach a certain value.

Second, channel 1 will be use and i set RA1 as input, which is a trimmer. The output will be connected to RE1 which is a light bulb. So i want the trimmer to control the brightness of the light bulb.

So the question is, how to interface the channels according to the user choice? Help will be much appreciated. This is my code.



Code:
#include <p18f4520.h>

unsigned char result;
void sensor (void);
void light (void);
void main()

{

TRISA=0b00000011;      //pin AN0/RA0 & AN1/RA1 as input(fire sensor & trimmer)

TRISC=0b11111011;     // RC2 as output(buzzer)
TRISE=0;              // RE1 as output(light bulb)

ADCON2=0b00011100;   // result left justified, 6TAD, fosc/4 



light();

sensor();

}



void light()
{

ADCON0=0b00000101;    // analog channel 1 is used

ADCON1=0b00001101;    // AN0 & (AN1), internal voltage reference.

while(1)
{
ADCON0bits.GO=1;              // start ADC  

 while(ADCON0bits.DONE);      // ADC completed? 

result=ADRESH;                   // store the high byte of result in A

PORTE=result;

}



if (result <50) 
{
  
                    // Pwm program 
CCPR1L=0b00001100;
CCP1CON=0b00101100;//20% duty cycle, frequency 1000Hz
     

}




else if ( result < 100)
{
               // Pwm program 
CCPR1L=0b00011001;
CCP1CON=0b00001100;	//40% duty cycle, frequency 1000Hz
       
}





 else if ( result < 150)
{

                   // Pwm program 
CCPR1L=0b00100101;
CCP1CON=0b00101100;	//60% duty cycle, frequency 1000Hz
     

}




 else if  ( result <200)
{
                // Pwm program 
CCPR1L=0b00110010;
CCP1CON=0b00001100;	//80% duty cycle, frequency 1000Hz
       

}





 else if (result<250)
{
     
                     // Pwm program 
CCPR1L=0b00111110;
CCP1CON=0b00101100;
	//100% duty cycle, frequency 1000Hz
      
}


}

void sensor()
{
ADCON0=0b00000001;    // analog channel 0 is used
ADCON1=0b00001110;    // AN0, internal voltage reference.
while(1)
{

ADCON0bits.GO=1;          //start ADC

while(ADCON0bits.DONE);   //ADC completed? 

result=ADRESH;


if(result>10)
{

PORTC=result;

}
else
{
PORTC=0;

}
}
}

Use CODE tags when posting your code
 
Last edited by a moderator:

you have some syntax and logic errors i observe.
1. in void light the while will never exit it will just sample show the result on portE so you need make the while loop to include your test conditions.
2.in c language the syntax for if else if is

Code:
if(condition)
{//your code
}
else if(condition)
{//your code
}
else if(condition)
{//your code
}
else
{
//your code
}

I hope this will help!

Use CODE tags when posting your code.
 
Code:
if (result <50) 
{

// Pwm program 
CCPR1L=0b00001100;
CCP1CON=0b00101100;//20% duty cycle, frequency 1000Hz


}




else if ( result < 100)
{
// Pwm program 
CCPR1L=0b00011001;
CCP1CON=0b00001100;	//40% duty cycle, frequency 1000Hz

}





else if ( result < 150)
{

// Pwm program 
CCPR1L=0b00100101;
CCP1CON=0b00101100;	//60% duty cycle, frequency 1000Hz


}

Just having had a quick look I dont think this section on code will behave how you think it will. If result is equal to 120 say your code will run through:
Code:
if (result <50) 
{

// Pwm program 
CCPR1L=0b00001100;
CCP1CON=0b00101100;//20% duty cycle, frequency 1000Hz
}
but not through the conditions such as <100 or <150 because C evaluates the first correct condition in and if/else statement only and ignores the rest, you would need to use a statement such as
Code:
         if(result <50) {
         }
         else if(result > 50 && result < 100){
         }

But as ahem0307 said you are going to be constantly stuck in the while(1) loop as the condition for that while loop, i.e the (1) is always true.

Pete
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top