gordon the light
Newbie level 4
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.
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: