STC12C5A60S2 AD conversion sample program

Publisher:人妙果华Latest update time:2018-02-01 Source: eefocusKeywords:STC12C5A60S2 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#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); 


Keywords:STC12C5A60S2 Reference address:STC12C5A60S2 AD conversion sample program

Previous article:stc12c4052 315M wireless decoding program
Next article:STC89LE516AD MCU A/D conversion function program

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号