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:
//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
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
Previous article:A matrix keyboard program under the stc89c52 microcontroller
Next article:How to implement C language nested assembly language in KEIL
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 【TGF4042 signal generator】+ resolution test
- Transplantation of μC/OS-II Real-time Operating System to 51 Single-Chip Microcomputer
- Free download of "A Brief Introduction to Special and General Relativity"
- MSP432 Wireless Sensor Node
- [Raspberry Pi 3B+ Review] PWM breathing light & control 12V motor
- 【RT-Thread Reading Notes】4. RT-Thread Study Chapter 6 Reading Notes (I)
- Power supply dynamic response test, what kind of waveform is considered qualified?
- EEWORLD University Hall----Live Replay: PI helps high reliability and high efficiency industrial measurement solutions
- AFE77xx EVM Evaluation with TSW14J57 Guide
- Simulate serial port infrared transmitter