1. Internally contains 8-channel AD conversion
2. First declare the registers related to AD conversion
Sfr ADC_CONTR =0XBC; //AD conversion control register address
Sfr ADC_RES=0XBD; //AD conversion result storage register
Sfr P1ASF=0x9d; //P1 port analog function control register
//Which position is 1, P1.0-P1.7 is set to AD conversion function
3. To make the program more flexible, each bit of ADC_CONTR can be set individually.
#define ADC_POWER 0X80; //All ADC power control, bit 7
#define ADC_FLAG 0X10; //AD conversion completed flag, bit 4,
#define ADC_START 0X08; //AD conversion start flag, bit 3,
#define ADC_SPEEDLL 0X00; //AD conversion speed control bit, bit 5 and 6
//420 clocks
#define ADC_SPEEDL 0x20;//280 clocks
#define ADC_SPEEDH 0x40;//140 clocks
#define ADC_SPEEDHH 0x60;//70 clocks
4. Initialize ADC
Void initADC(void)
{
P1ASF=0X01; //Set which P1 port to use for ADC
//0X01 P1.0
//0X02 P1.1
//0X03 P1.0 and P1.1
//0XFF all 8 ports
ADC_RES=0; //result register cleared
ADC_CONTR = ADC_POWER | ADC_SPEEDLL // Turn on the ADC power and set the conversion rate
Delay(2); //Software delay 2ms
}
5. Read the ADC result
Uchar getADCresult(uchar ch) //ch is the value of the channel to be read, 0-7
{
ADC_CONTR=ADC_POWER|ADC_SPEEDLL |ADC_START|ch;
//Set the ADC control register, turn on the power , set the conversion rate, turn on the start, and set the conversion channel
_nop_(); //After the statement to set the ADC_CONTR control register is executed, it will take 4 CPU clock delays.
//Its value can be set into the ADC_CONTR control register
_nop_();
_nop_();
_nop_();
while(!(ADC_CONTR & ADC_FLAG));//wait complete flag
ADC_CONTR&=~ADC_FLAG;//close ADC
return ADC_RES;//return ADC result
}
The result of AD conversion is 8-bit data, which is stored in the ADC_RES register.
Its value is: ADC_RES[0:7]=256 X Vin/Vcc
In order to use the converted values to compile other control programs and debug, the converted data can be sent through the serial port and then monitored using the serial port debugging tool . This involves the compilation of the serial port communication program.
Compilation of serial communication program (only send but not receive):
1. To improve the adaptability of the program, you can first define the communication baud rate and crystal oscillator frequency.
#define BAUD 9600 //9600 baud rate
#define FOSC 11059200L //11.0592M crystal
2. Serial port initialization
Void initUART()
{
SCON=0X5A; //8-bit data, no check bit
TMOD=0X20; //Set timer 1 to work in 8-bit auto-reload mode
TH1=TL1=-(FOSC/12/32/BAUD); //T1 initial value
TR1=1; //Start timer 1
}
Of course, this program can also be automatically generated using the baud rate calculator provided by STC, as follows:
void UartInit(void) clock is Fosc/12, i.e. 12T
AUXR &= 0xfe; //Serial port 1 selects timer 1 as the baud rate generator
TMOD &= 0x0f; // Clear timer 1 mode bit
TMOD |= 0x20; //Set timer 1 to 8-bit auto-reload mode
TL1 = 0xFD; //Set the initial timing value
TH1 = 0xFD; //Set the timer reload value
ET1 = 0; //Disable timer 1 interrupt
TR1 = 1; //Start timer 1
}
But be careful to declare the special auxiliary register AUXR
Sfr AUXR=0X8E;
3. Send data
void senddata(fly data)
{
While(!TI); //Wait for the previous data to be sent
TI=0; //TI flag cleared
SBUF=dat; //Send current data
}
In this way, you can see the data in the serial port debugging software as long as you call the send data function.
Of course, you can also use 74HC595 and digital tube to make a display circuit for monitoring . This circuit will be summarized later.
Previous article:DS18B20-64 bit serial code reading program
Next article:Application summary of PWM function of STC12C5201AD microcontroller
- Popular Resources
- Popular amplifiers
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
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- The characteristics of low-power Bluetooth determine that it is worth looking forward to~
- EEWORLD University ---- Industrial Motor Driver - Introduction to Texas Instruments System Solutions
- Updated PYBCN_V2 and PYBNANO_V2 firmware
- IIR filter software implementation (Matlab+C++)
- Invite you to join Microchip's online seminar - Designing energy-efficient solutions for your IoT sensors
- FPGA-based CPU design
- [Synopsys IP Resources] Effectively protect data in PCIe and CXL in cloud computing
- Recruiting part-time DSP and other related professional lecturers or technical support personnel
- [LSM6DSOX finite state machine routine learning 5] - Free Fall
- In programming mode, output sensor data from the USB virtual serial port