//Select RAO as the analog input channel;
/ / Continuous conversion 4 times and then averaged as a conversion result
/ / The final structure only takes the lower 8 bits
/ / The result is sent to the lower 3 digits of the digital tube display
#include
__CONFIG(0x1832);
/ / Chip configuration word, watchdog off, power-on delay on, power-down detection off, low-voltage programming off, encryption, 4M crystal HS oscillation
constcharTABLE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0X82,0XF8,0X80,0X90};
/ / Define a constant 0-9 data table
voidDELAY();//delay function declaration
Voidinit();//I/O port initialization function declaration
Voiddisplay(intx);//Display function declaration
//------------------------------------------------
//The main program starts
Voidmain()
{
Intresult=0x00; / / define conversion result register
While(1)//infinite loop
{
Inti; / / define the loop number control register
Result=0x00;//The conversion result is cleared to 0
For(i=5;i>0;i--)//To find the average of 5 conversion results
{
Init();//call initialization function
ADGO=0X1; / / turn on the conversion process
While(ADGO);//waiting for conversion to complete
Result=result+ADRESL;//accumulated conversion result
}
Result=result/5;//To find the average of 5 results
Display(result);//call display function
}
}
//-----------------------------------------------
/ / initialization function
Voidinit()
{
PORTA=0XFF;
PORTD=0XFF; / / extinguish all displays
TRISA=0X1; / / set RA0 as input, the other is output
TRISD=0X00; / / set D port is all output
ADCON1=0X8E; / / conversion results are left aligned, RA0 is the analog input port, others do ordinary I / O
ADCON0=0X41; / / system clock Fosc / 8, select RA0 channel, allow ADC to work
DELAY();//guaranteed sampling delay
}
//-----------------------------------------------
/ / Display function
Voiddisplay(intx)
{
Intbai, shi, ge, temp; / / define 4 temporary variables
Temp=x;//Scratch AD conversion results
Bai=temp/0x64;//Seeking the hundredth place displayed
Shi=(temp%0x64)/0xa;//Seeking the ten digits displayed
Ge=(temp%0x64)%0xa;//Seeking the displayed ones
PORTD=TABLE[bai];//Check the table to display the code of hundreds of digits
PORTA=0x37; / / RA3 output low level, lighting hundreds of digits display
DELAY();//delay for a certain time to ensure display brightness
PORTD=TABLE[shi];//Check the table to display the code of ten digits
PORTA=0x2F; / / RA4 output low level, light up ten display
DELAY (); / / delay for a certain time, to ensure brightness
PORTD=TABLE[ge];//Seeking the code displayed in one place
PORTA=0x1F; / / RA5 output low level, light up one bit display
DELAY (); / / delay for a certain time, to ensure brightness
}
//----------------------------------------------
/ / delay program
voidDELAY()//delay program
{
Inti; / / define the shaping variable
For(i=0x100;i--;);//delay
}
Fiber Optic Splice Closure,Fiber Optic Splice Case,Fiber Splice Closures,Outdoor Fiber Optic Splice Closure
Cixi Dani Plastic Products Co.,Ltd , https://www.dani-fiber-optic.com