#include<STC12C5A60S2.H>
#define uint unsigned int
#define uchar unsigned char
sbit CS=P2^0; //LCD12864 serial communication chip select
sbit SID=P2^1; //LCD12864 serial communication data port
sbit SCLK=P2^2; //LCD12864 serial communication synchronization clock signal
sbit PSB=P2^5; //LCD12864 parallel/serial selection: H parallel L serial
unsigned int temp1,sh1,ge1,n1,m1;
unsigned char ad_result_data[10]; //AD conversion high eight bits
unsigned char ad_result_low2[10]; //AD conversion low eight bits
unsigned char ad_result_total[10]; //AD conversion total ten bits
unsigned char ad_average_result; //Average value of ten AD conversions
unsigned char Ain,Vin;
unsigned char b,t,R;
char tp=0;
unsigned char code ma1[6]={0xb5,0xe7,0xd1,0xb9,0xa1,0xc3}; //voltage:
unsigned char code ma2[]={"."};
uchar code disp1[]={"Hint: Press 1 to enter"};
uchar code disp2[]={"Function selection interface."};
unsigned char code num0[]={0xa3,0xb0};
unsigned char code num1[]={0xa3,0xb1};
unsigned char code num2[]={0xa3,0xb2};
unsigned char code num3[]={0xa3,0xb3};
unsigned char code num4[]={0xa3,0xb4};
unsigned char code num5[]={0xa3,0xb5};
unsigned char code num6[]={0xa3,0xb6};
unsigned char code num7[]={0xa3,0xb7};
unsigned char code num8[]={0xa3,0xb8};
unsigned char code num9[]={0xa3,0xb9};
//-------Module delay program---------------------------- 1ms
void delay1ms(uint delay1ms) //STC11F60XE,22.1184M,delay 1ms
{
uint i,j;
for(;delay1ms>0;delay1ms--)
for(i=0;i<7;i++)
for(j=0;j<210;j++);
}
void delay(uint delay) //STC11F60XE,22.1184M,delay 170us
{
uint i,j;
for(;delay>0;delay--)
for(i=0;i<124;i++);
for(j=0;j<124;j++);
}
/********************************************************
AD conversion program
*******************************************************/
void AD_initiate() //initialization function
{
ES=0;
TMOD=0x21; //Timer counter mode control register, "auto reload, 16-bit counter".
SCON=0x50; //Serial control register, convenient for observation in the serial assistant
TH1=0xfa;
TL1=0xfa;
TR1=1;
}
void ADC_Power_On() //AD conversion power
{
ADC_CONTR|=0x80;
delay(5); //Necessary delay
}
void get_ad_result() //Get AD result function, it is a 10-bit AD conversion, averaging every ten times, and finally taking the lower eight bits as AD sampling data
{
uint i,q=0;
for(i=0;i<10;i++)
{
tp=0;
ADC_RES=0; // Clear the upper eight bits of data, the STC12C5A60S2 AD data register name is different from the STC12C54×× series
ADC_RESL=0; // Clear the lower two bits
ADC_CONTR|=0x08; // Start AD conversion
while(!tp) // Determine whether the AD conversion is completed
{
tp=0x10;
tp&=ADC_CONTR;
}
ADC_CONTR&=0xe7;
ad_average_result=ADC_RES;
q=q+ad_average_result;
}
ad_average_result=q/10;
//ad_average_result=ad_average_result*4*5000/1024;
}
/************************AD conversion ends***************************/
void send_ad_result() //Get AD result function and send it to the serial port for debugging
{
SBUF=n1;
while(TI==0);
TI=0;
delay1ms(100);
//SBUF=R>>4;
}
//---------------------Voltage sampling program-------------------------
void caiyangP10() //Measure voltage
{
P1M0|=0x01; //Set P1_0 to open drain mode For example: P1_0= #00000000B
P1M1|=0x01;
ADC_CONTR=0xe0; //Set P1.0 to input AD conversion port
delay(2);
get_ad_result(); //Get conversion dataVin
=ad_average_result;
R=Vin;
}
/*-----------Write control word to LCD12864------------*/
void write_cmd(uchar cmd)
{
uchar i;
uchar i_data;
i_data=0xf8; //Command control word: 11111000 write instruction 11111010 write data 11111100 read status 11111110 read data
CS=1; //Set chip select high to read and write
SCLK=0;
/*----------write command control word----------------*/
for(i=0;i<8;i++) //Loop eight times, read one bit of data each time
{
SID=(bit)(i_data&0x80); //bit means take the highest
bitSCLK=0;
SCLK=1; //Positive jump write instructioni_data
=i_data<<1; //Left shift one bit
}
/*---------------------------------------*/
/*----------Write instruction high four bits-----------------*/
i_data=cmd;
i_data=i_data&0xf0; //Set the lower four bits to 0
for(i=0;i<8;i++) //Loop eight times, read one bit of data each time
{
SID=(bit)(i_data&0x80); //bit means take its highest
bitSCLK=0;
SCLK=1; //Positive jump write instructioni_data
=i_data<<1; //Shift left one bit
}
/*---------------------------------------*/
/*----------Write instruction low four bits----------------*/
i_data=cmd;
i_data=i_data<<4; //Shift left four bits, move the lower four bits of data to the upper four bits, and then set the lower four bits to 0
for(i=0;i<8;i++) //Loop eight times, read one bit of data each time
{
SID=(bit)(i_data&0x80); //bit means take the highest bit
SCLK=0;
SCLK=1; //Positive jump write instruction
i_data=i_data<<1; //Left shift one bit
}
/*-----------------------------------------*/
CS=0; //Set chip select low
delay1ms(5); //The delay is because there is no busy detection, the appropriate delay can avoid busy detection
}
/*-----------------------------------------*/
/*------------Write data to LCD12864-------------*/
void write_dat(uchar dat)
{
uchar i;
uchar i_data;
i_data=0xfa;
CS=1;
for(i=0;i<8;i++)
{
SID=(bit)(i_data&0x80);
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
i_data=dat;
i_data=i_data&0xf0;
for(i=0;i<8;i++)
{
SID=(bit)(i_data&0x80);
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
i_data=dat;
i_data=i_data<<4;
for(i=0;i<8;i++)
{
SID=(bit)(i_data&0x80);
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
CS=0;
delay1ms(5);
}
/*-----------------------------------------*/
/*--------------Display coordinates-------------------*/
void lcd_pos(uchar x,uchar y) //Chinese character display coordinates, x is which row, y is which column
{
uchar pos;
if(x==0)
x=0x80; //first line
else if(x==1)
x=0x90; //second line
else if(x==2)
x=0x88; //third line
else if(x==3)
x=0x98; //fourth line
pos=x+y; //which line (a total of 4 lines) and which vertical line (a total of 8 vertical lines, 1 vertical line for every 16 columns) to display
write_cmd(pos);
}
/*-----------------------------------------*/
/*--------------Display 8 Chinese characters-------------------*/
void disp_hanzi(uchar code *chn)
{
uchar i;
write_cmd(0x30); //Basic instruction operation mode
for(i=0;i<16;i++) //16 columns * 8 Chinese characters = 128 (just right)
write_dat(chn[i]);
}
/*-----------------------------------------*/
/*--------------Display numbers-------------------*/
void disp_num(uchar code *chn)
{
uchar i;
write_cmd(0x30); //Basic instruction operation mode
for(i=0;i<2;i++) //1 number
write_dat(chn[i]);
}
void disp_number(uchar num)
{
switch(num)
{
case 0: disp_num(num0);break;
case 1: disp_num(num1);break;
case 2: disp_num(num2);break;
case 3: disp_num(num3);break;
case 4: disp_num(num4);break;
case 5: disp_num(num5);break;
case 6: disp_num(num6);break;
case 7: disp_num(num7);break;
case 8: disp_num(num8);break;
case 9: disp_num(num9);break;
default: break;
}
}
/*---------- --LCD initialization------------------* /
void lcd_init()
{
PSB=0;
write_cmd(0x30); //Basic instruction
write_cmd(0x02); //Address return
write_cmd(0x06); //Cursor right shift
write_cmd(0x0c); //Overall display
write_cmd(0x01); //Clear screen
}
/*-----------------------------------------*/
void displayP10()
{
float ad1;
//unsigned int temp1,sh1,ge1,n1,m1;
//uchar code dis2[]={0x01,0x02,0x00};
//ad1=x*7.8125; //voltage correction
uchar i;
ad1=Vin*3.9608; //The specific linear parameters are adjusted by the input voltage value. The measurement range of this value is 0-10.00V. The measurement of about 5V is more accurate.
//The maximum error at both ends is 70mv, and the others are generally within 40mv
temp1=(int)ad1;
sh1=temp1/1000; //Tens
ge1=(temp1%1000)/100; //One digit after the decimal point
n1=((temp1%1000)%100)/10; //One digit after the decimal point
m1=((temp1%1000)%100)%10; //Two digits after the decimal point
//write_cmd(0x01);
write_cmd(0x30); //Basic instruction operation mode
lcd_pos(0,0);
for(i=0;i<6;i++) write_dat(ma1[i]);
lcd_pos(0,3);
disp_number(sh1);
lcd_pos(0,4);
disp_number(ge1);
lcd_pos(0,5);
for(i=0;i<2;i++) write_dat(ma2[i]);
lcd_pos(0,6);
disp_number(n1);
lcd_pos(0,7);
disp_number(m1);
/*lcd_pos(2,0);
disp_hanzi(disp1);
lcd_pos(3,0);
disp_hanzi(disp2);*/
}
void main()
{
EA=1;
AD_initiate(); //Initialize
ADC_Power_On(); //Turn on AD power supply
//init();
lcd_init();
delay(10);
while(1)
{
caiyangP10(); //Measure voltage
send_ad_result();
//Vin=Vin*4007;
displayP10();
delay(10);
}
}
Previous article:stc12c4052 315M wireless decoding program
Next article:STC89LE516AD MCU A/D conversion function program
- 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
- 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
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- 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
- Award-winning live broadcast: Microchip&avnet series live broadcast third collection
- [Vicor White Paper Recommendation] Power Averaging Design Method with Voltage Regulation: Ideal Power Supply Solution for Pulse Loads
- Bear Pie Huawei IoT Operating System LiteOS Kernel Tutorial - IoT-Studio Introduction and Installation
- How to use C language to find the corresponding value in the table in the microcontroller
- FPGA Implementation of Digital Down-Conversion
- Wireless bridge dedicated to video transmission
- Implementation Method of Software Serial Interface (SCI) Based on C2000
- Ultra-wideband positioning applications
- Share a book: Crazy STM32 practical lectures
- Design and implementation of Ethernet MII interface expansion based on FPGA