#include<htc.h>
__CONFIG (0x3F72); //configured fuses
//define ports for easier reading
#define LEDS PORTB
void delay(unsigned char itime);
void main(void)
{
//8bit data-type variables
unsigned char x; //ADC low byte
unsigned char y; //ADC high byte
unsigned int z; //final result
TRISB=0x00; //output
TRISA=0xFF; //inputs
LEDS=0xFF; //clear/switch off LEDS
while(1)
{
//ADFM=1, all i/ps analog, +VREF enabled
//Configure the functions of the Port bits
ADCON1=0b10000001;
//clock/channel select & enable bits
//controls the operation of the A/D module
ADCON0=0b11000001;
delay(1);
//Start a2d conversion
//Set GO bit (ADCON0=ADCON0|1;)
GODONE=1;
//wait end-of-conversion (conversion complete)
while(GODONE==1);
x=ADRESL; //store low byte
x=x>>1; //shift 1 bit to the right
y=ADRESH; //store high byte
y=y<<7; //shift left 7 bit position
z=x|y; //combine together
LEDS=~z; //output on leds
}
}
void delay(unsigned char itime)
{
unsigned char i,j;
for(i=0;i<itime;i++)
for(j=0;j<1;j++);
}
/*--- Initialise A/D convertor ---*/
void init_ad(void)
{
ADCON1 = 0x20U; /* Conversion clock Fosc/32 */
ADCON0 = 0x80U; /* 10 bit Right justified result, Vdd as ref */
}
/*--- Read A/D conversion ---*/
int read_ad(unsigned char channel)
{
int result;
unsigned char acquisition_time = 3U;
ADCON0 = 0x01U; /* Turn on A/D */
ADCON0 |= (channel << 2U); /* Select channel */
while(acquisition_time--){ /* Sample channel */
;
}
GODONE = 1U; /* Start conversion */
while(GODONE){ /* Wait for conversion end */
;
}
result = ADRESH;
result <<= 8U;
result += ADRESL;
return result;
}
#define LEDS_8 LATB
#define LEDS_2 LATC
/*--- Read A/D conversion ---*/
void read_ad(unsigned char channel)
{
unsigned char acquisition_time = 3U;
ADCON0 = 0x01U; /* Turn on A/D */
ADCON0 |= (channel << 2U); /* Select channel */
while(acquisition_time--){ /* Sample channel */
;
}
GODONE = 1U; /* Start conversion */
while(GODONE){ /* Wait for conversion end */
;
}
LEDS_8 = ADRESL;
LEDS_2 &= 0xfc;
LEDS_2 |= ADRESH;
}
if(value>=200){
ledR=!ledR;
}
else if(value>=400){
ledG=!ledG;
}
else if(value>=600){
ledB=!ledB;
}
if((value >= 200) && (value < 400)){
ledR=!ledR;
}
else if((value >= 400) && (value < 600)){
ledG=!ledG;
}
else if(value >= 600){
ledB=!ledB;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?