0% found this document useful (0 votes)
4 views2 pages

New Text Document

This document contains C code for a PIC16F18456 microcontroller that initializes and reads from an ADC (Analog-to-Digital Converter). It continuously reads the ADC value from a specific input pin (RC2) and controls a relay (RELAY_1) based on the ADC value. If the ADC value is less than or equal to 2000, the relay is activated; otherwise, it is deactivated.

Uploaded by

CHARUMATHI M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

New Text Document

This document contains C code for a PIC16F18456 microcontroller that initializes and reads from an ADC (Analog-to-Digital Converter). It continuously reads the ADC value from a specific input pin (RC2) and controls a relay (RELAY_1) based on the ADC value. If the ADC value is less than or equal to 2000, the relay is activated; otherwise, it is deactivated.

Uploaded by

CHARUMATHI M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#include<stdlib.h>
#include<xc.h>
#include<pic16f18456.h>
#include "config.h"

#define _XTAL_FREQ 32000000

#define RELAY_1 RA1


#define INPUT RC2 /***RC2 0X12***/

unsigned long int Temp_Value1;


unsigned int temp1,temp2,temp3,temp,i,Adc_Value;

void ADC_Init();
void ADC_Read();
void Delay(unsigned int);

void main()
{
PORTA = 0X00;
LATA = 0X00;
ANSELA = 0X00;
WPUA = 0X00;
ODCONA = 0X00;
SLRCONA = 0X00;
INLVLA = 0X00;
TRISA = 0X00;

PORTB = 0X00;
LATB = 0X00;
ANSELB = 0X00;
WPUB = 0X00;
ODCONB = 0X00;
INLVLB = 0X00;
TRISB = 0X00;

PORTC = 0X00;
LATC = 0X00;
ANSELC = 0X04;
WPUC = 0X00;
ODCONC = 0X00;
SLRCONC = 0X00;
INLVLC = 0X00;
TRISC = 0X04;

ADC_Init();
Delay(65000);

while(1)
{
ADC_Read();
Delay(65000);
}
}

void ADC_Init()
{
ADCON0bits.CS = 1;
ADCON0bits.FM = 1;
ADCON0 = 0X94;
}

void ADC_Read()
{
unsigned int temp3=0,temp1=0,temp4=0;
unsigned int temp2=0,i=0;
ADPCH = 0X12; // 0001 0010 RC2

for(i=0; i<10; i++)


{
ADCON0bits.GO_nDONE = 1;
while(ADCON0bits.GO_nDONE == 1);
temp1 = ADRESH;
temp1 = temp1<<8;
temp2 = ADRESL;
temp3 = temp1 | temp2;
temp4 = temp3;
temp1 = temp2=temp3=0;
Temp_Value1 += temp4;

Adc_Value= (Temp_Value1/10);//>>1;
Temp_Value1=0;
if(Adc_Value<=2000)
{
RELAY_1 = 1;
}
else if(Adc_Value>=2000)
{
RELAY_1 = 0;
}
}

void Delay(unsigned int x)


{
while(x--);
}

You might also like