How to use photoresistor with PIC through AD

Publisher:温柔之风Latest update time:2020-01-08 Source: eefocusKeywords:PIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The analog signal is input through the photoresistor to obtain the corresponding digital signal.

Small set threshold, through the threshold can control the switch of light bulbs and other devices

The analog signal input port is AN0. The analog voltage is generated by the peripheral circuit. The reference voltage is the internal VCC and VSS.

Connect to the host computer through the serial port to display the converted digital signal

How to use photoresistor with PIC through AD

Main Program

#include "p18f4620.h"

// Initialization, not required

void initial()

{

INTCON = 0x00; //Bit 7~0: Disable total interrupt

ADCON1 = 0x07; //Set digital input and output ports

PIE1 = 0;

PIE2 = 0;

}

//Serial port initialization

void EUSART_Init()

{

TXSTA = 0xA4; //Select asynchronous high-speed mode to transmit 8-bit data

RCSTA = 0x90; // Enable the serial port to work

BAUDCON =0x00; //

TRISC = TRISC|0x80; //Set RC7 (RX) to input mode

TRISC = TRISC&0xBF; //Set RC6 (TX) to output mode

SPBRG = 25; //Set the baud rate to 9600 for 4MHZ crystal oscillator

}

//Send data through the serial port

void send_ch(unsigned char d)

{

PIR1bits.TXIF = 0;

TXREG = d;

Nop();

while (TXSTAbits.TRMT == 0); //Wait for sending to complete

}

//AD initialization function

void AD_Init(void)

{

ADCON1 = 0x0E; //The reference voltage is VCC~VSS, and the analog input ports are configured as AN0 and AN1

ADCON2 = 0xA6; //AD result is right-aligned, sampling time is 8 TAD, conversion clock is selected as Fos/64

ADCON0 = 1; //Start AD

PIR1bits.ADIF = 0; //

TRISAbits.TRISA0 = 1; //Set analog input ports RA0 and RA1 to input mode

TRISAbits.TRISA1 = 0;

}

//Read the AD acquisition data of the specified channel

unsigned int Read_AD(unsigned char ch)

{

unsigned int ad te mp;

PIR1bits.ADIF = 0;

ADCON0 = (ch << 2) | 1; //Analog input channel selection

ADCON0bits.GO = 1;

while (ADCON0bits.GO); //Wait for the end of conversion

PIR1bits.ADIF = 0;

// adtemp = ADRESL + (ADRESH << 8);

adtemp = ADRESH; //Read the conversion value of AD

adtemp = (adtemp << 8) + ADRESL;

return adtemp;

}

//Software delay function

void wait(unsigned char t)

{

unsigned char i;

unsigned int j;

for(i=0;i for(j=0;j<10000;j++);

}

//Main control function

void main()

{

unsigned int adtempdat;

// TRISAbits.TRISA0=0;

// LATAbits.LATA0=1;

ini TI al();

EUSART_Init();

AD_Init();

while(1)

{

adtempdat = Read_AD(0); //Select channel 0

send_ch(0xFF); //Send interval flag

send_ch(adtempdat); //send the lower eight bits of the number

send_ch (adtempdat >> 8); // send the high eight bits of the number

if (adtempdat < 0x0100) //The light switch threshold can be adjusted

{

LATAbits.LATA1 = 1; //If the brightness is less than the threshold, turn on the light, otherwise turn off the light

}

else

{

LATAbits.LATA1 = 0;

}

Nop();

wait(10);

}

}

Keywords:PIC Reference address:How to use photoresistor with PIC through AD

Previous article:Design of PIC microcontroller controlling the programmer circuit
Next article:How to use PIC microcontroller to debug LCD controller

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号