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
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);
}
}
Previous article:Design of PIC microcontroller controlling the programmer circuit
Next article:How to use PIC microcontroller to debug LCD controller
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- [NXP Rapid IoT Review] Rapid IoT Studio Trial
- 【STM32WB55 Review】——by lvxinn2006
- The next generation of wireless connectivity Wi-Fi devices will help you solve three major design challenges
- Hard-core science: This is what radio frequency is all about!
- Feedforward multi-carrier old technology
- 【Smart Network Desk Lamp】10. Esp32-S2 driver UART
- Fun Oscilloscope + Running Polygons
- How to get download points
- A 985 graduate student is unemployed at the age of 37. What should I do?
- GPU Computing Motherboard Study Material No. 735: AGX Xavier GPU Computing Motherboard Based on 3U VPX