Implementation of AD conversion C language program of STC12C2052AD single chip microcomputer

Publisher:二进制游侠Latest update time:2018-03-08 Source: eefocusKeywords:STC12C2052AD Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    Function: STC12C2052AD AD conversion C program + PWM output function was successfully used.

    Application: AD detects voltage for over-voltage and under-voltage protection (relay control) + PWM chops DC voltage into pulsating DC.

    Board function: Charge the mobile phone battery.

    The LM317 used for step-down should be enough for low current applications. I didn't have time to buy a switch tube, so I used a 9013 switch.


    drawing:

   11.jpg

    //The following is a successful program. If you need to apply it in your own project, you only need to change the io to apply it directly
//The complete version of the program download address: http://www.51hei.com/ziliao/file/stc12c2052adde.rar #include // Header file dedicated to
stc microcontroller
#include
#define uchar unsigned char
#define uint unsigned int
#define AD_SPEED 0x60 //0110,0000 1 1 270 clock cycles to convert once, 
/************Hebei Zhengding welcomes you! &&&& Shaozhanyu welcomes you! ******************************/
//
sbit M=P1^5; //Overvoltage indicator 
sbit N=P1^3; //Undervoltage indicator 
sbit  LED =P1^7; //Normal working light 
sbit CONTRL=P3^4; //Output control end 
sbit PWM=P3^7;
/****************************************************************/
void pwm();
void delayms(uint);
uint ADC();
void InitADC();
void baohu();
float voltage=0.0; 
uint V;
float V CC =5.05;
uchar mtab[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
/***8******************************************************************/
 void main()
{  
    CONTRL=0;//Turn off output 
  delayms(700);
 V=40; //These are added when I made a mistake. The purpose is to find out whether the AD conversion is done.
 voltage=4.0;//Practice has proved that changing the value is useless, indicating that there is no AD
 LED=0;
 CONTRL=1;
  voltage=V*VCC/256.00*5.00; 
 delayms(1000);
 PWM=1;
  CONTRL=1;//The relay is working, it is in protection state 
 delayms(1000);
 M=0;
 N=0;
 LED=0;
  delayms(2000);
 M=1;
 N=1;
  LED=1;
    pwm();//Generate PWM waveform    
    delayms(7000);
 
  delayms(100);//Delay 
  InitADC();
  delayms(20);
  V= ADC();
 baohu();
  while(1)
  {
    V= ADC();
  baohu();
 delayms(300);
  }
    
}
//
//
 void pwm()
{  
  //PCA module works in PWM mode C program 
CMOD = 0x04; //Use timer 0 overflow to make PCA pulse
CL = 0x00; //PCA timer low 8-bit address: E9H
CH = 0x00; //PCA high 8-bit address F9H
CCON = 0x00;
CCAP0L = 0x60; //In PWM mode, they are used to control the duty cycle
CCAP0H = 0x60; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow)
CCAPM0 = 0x42; //0100,0010 Setup PCA module 0 in PWM mode
              // ECOM0=1 enables comparison PWM0=1 enables CEX0 pin to be used as PWM output
/************************
PCA module working mode setup (CCAPMn register n= 0-3 four types)
 7 6 5 4 3 2 1 0
 - ECOMn CAPPn CAPNn MATn TOGn PWMn ECCFn
Options: 0x00 No such operation
      0x20 16-bit capture mode, triggered by the rising edge of CEXn
      0x10 16-bit capture mode, triggered by the falling edge of CEXn
      0x30 16-bit capture mode, triggered by the transition of CEXn
      0x48 16-bit software timer
      0x4c 16-bit high-speed output
      0x42 8-bit PWM output
 Each PCA module also corresponds to two registers: CCAPnH and CCAPnL. They are used to
store 16-bit count values ​​when capturing or comparing, and to control the duty cycle when working in PWM mode
**********************************/      
TMOD=0x02;
TH0=0x06;
TL0=0x06; 
CR=1; //Start PCA Timer.
TR0=1;
}

    //AD conversion initialization----turn on ADC power 
void InitADC()
{
P1=0xff;
ADC_CONTR|=0x80;
delayms(80);
//These two registers are used to set the four states of P1 port. Each bit corresponds to a P1 pin and operates according to the state combination 
/********************
 P1M0 and P1M1 register bits 7 6 5 4 3 2 1 0 
                    P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 
 Similarly, P3M0 and P3M0 are also. Because STC12C2052AD has only two P ports, there are only these two groups. STC12C5410AD has more P2M0 P1M0. There are three groups of    
  P1M0 P1M1. High 
    0 0 Ordinary I0 port (quasi-bidirectional) P1 register bit 7 6 5 4 3 2 1 0 
 0 1 Strong push-pull output (20MA current) Try to use as little as possible P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 
 1 0   This mode can be used     only for input A/D
conversion   1 1 Open drain, this mode can be used for A/D conversion 
  
  For example: 
  to set P1.2 as AD input port 
  , P1M0=0X02;
    P1M1=0X02; Open drain is enough. 
  When AD is not used, it is best to turn off the ADC power supply and restore it to the IO port state 
**************************************/
P1M0=0x02;//These two registers are used to set the four states of P1 port. Each bit corresponds to a P1 pin. Operate according to the state combination. 
P1M1=0x02;//Set P1.1 to open drain state 
 }
 
// AD conversion program 
/*******************************************************
Note: The commented commands in this function are general commands and can be used for all AD channels. I have identified P1.1 as a channel, so I directly
//assign values ​​to save some "traffic"! The problem that tortured me is the while waiting statement in this function
 while (1) //Wait for the A/D conversion to end 
    { 
        if (ADC_CONTR & 0x10) //0001,0000 Test whether the A/D conversion is completed or not 
        { break; } 
    } 
This can be used. What I originally wrote was:
 while (ADC_CONTR & 0x10==0);
This writing cannot be used. I say again: This cannot be used! ! 
As for why, because of the priority, “==” has a higher priority than &,
so just add brackets
 while ( (ADC_CONTR & 0x10) == 0 );
If you don’t use C language often, you won’t remember it!!!
From this, I learned a lesson: small problems affect efficiency
experience: adding brackets frequently will kill you, and it doesn’t seem to consume “traffic”! !
*********************************************/
uint ADC() 

    ADC_DATA = 0; //Clear result 
    ADC_CONTR = 0x60; //Conversion speed setting 0x60 fastest speed 
    ADC_CONTR = 0xE0; //1110,0000 Clear ADC_FLAG, ADC_START bit and lower 3 bits 
  ADC_CONTR =0xe1;
  // ADC_CONTR |= 0x01; //Select A/D current channel P1.1 
    delayms(1); //Let the input voltage reach stability 
  ADC_CONTR = 0xe9;
  // ADC_CONTR |= 0x08; //0000,1000 Set ADCS = 1, start A/D conversion,  
    while (1) //Wait for A/D conversion to end 
    { 
        if (ADC_CONTR & 0x10) //0001,0000 Test whether A/D conversion is finished or not 
        { break; } 
    } 
    ADC_CONTR =0xe1;
    //ADC_CONTR &= 0xE7; //1111,0111 clear ADC_FLAG bit, turn off A/D conversion,  
    return ADC_DATA; //Return A/D 10-bit conversion result 

 
//
void baohu()
{
 voltage=V*VCC/256.00*5.00;
 if( voltage>5.25)
  { CONTRL=1;//Overvoltage protection, turn off the switch control terminal 
    M=0;
 N=1;
    LED=1;
  }
if(voltage<4.60)
  { 
  CONTRL=1;//Protection relay is open, normally closed contact is disconnected protection 
    N=0;
 M=1;
 LED=1;
  }
 if(voltage>4.62&&voltage<5.24)
  {
    LED=0;
  M=1;
  N=1;
  CONTRL=0;//Relay is disconnected, normal state 
  }
 }

    void delayms(uint k)
{
uint data i,j;
for(i=0;i


Keywords:STC12C2052AD Reference address:Implementation of AD conversion C language program of STC12C2052AD single chip microcomputer

Previous article:A matrix keyboard program under the stc89c52 microcontroller
Next article:How to implement C language nested assembly language in KEIL

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号