Function: STC12C2052AD AD conversion C program + PWM output function is successfully used.
Application: AD detection voltage is over-voltage protection (relay control) + PWM chopping DC voltage into pulsating DC.
Board function: Charge the phone battery.
For the LM317 for buck, a small current application should suffice. No time to buy a switch tube, just use the 9013 switch.
drawing:
//The following is a successful program. If you need to apply it to your own project, you only need to change io to apply it directly.
/ / The full version of the program download address:
#include //stc special header file for microcontroller
#include
#define uchar unsigned char
#define uint unsigned int
#define AD_SPEED 0x60 //0110,0000 1 1 270 clock cycles are converted,
/************ Hebei is waiting for you! &&&& less fish welcome you! ******************************/
//
Sbit M=P1^5; //Overvoltage indicator
Sbit N=P1^3; // undervoltage indicator
Sbit LED=P1^7; //Working normal light
Sbit CONTRL=P3^4; //output console
Sbit PWM=P3^7;
/************************************************* ***************/
Void pwm();
Void delayms(uint);
Uint ADC();
Void InitADC();
Void baohu();
Float voltage=0.0;
Uint V;
Float VCC=5.05;
Uchar mtab[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
/***8********************************************* *****************/
Void main()
{
CONTRL=0; / / first close the output
Delayms(700);
V=40; //These are added when I am wrong. The purpose is to figure out exactly what AD has converted.
Voltage=4.0;//Practical proof, the replacement value is useless, indicating no AD
LED=0;
CONTRL=1;
Voltage=V*VCC/256.00*5.00;
Delayms(1000);
PWM=1;
CONTRL=1; / / relay work, is the 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 the timer 0 overflow to do PCA pulse
CL = 0x00; //PCA timer low 8 bits Address: E9H
CH = 0x00; //PCA high 8-bit address F9H
CCON=0x00;
CCAP0L = 0x60; //When using 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 enable compare PWM0=1 enable CEX0 pin as pulse width adjustment output
/*********************
PCA module working mode setting (CCAPMn register n = 0-3 four kinds)
7 6 5 4 3 2 1 0
- ECOMn CAPPn CAPNn MATn TOGn PWMn ECCFn
Options: 0x00 no such action
0x20 16-bit capture mode, triggered by CEXn rising edge
0x10 16-bit capture mode, triggered by CEXn falling edge
0x30 16-bit capture mode, triggered by CEXn's transition
0x48 16-bit software timer
0x4c 16-bit high speed output
0x42 8-bit PWM output
Each PCA module additionally has two registers: CCAPnH and CCAPnL. They are used when capturing or comparing
Save 16-bit count value, used to control duty cycle when operating in PWM mode
*******************************/
TMOD=0x02;
TH0=0x06;
TL0=0x06;
CR=1; //Start PCA TImer.
TR0=1;
}
//AD conversion initialization----turn on the ADC power supply
Void InitADC()
{
P1=0xff;
ADC_CONTR|=0x80;
Delayms(80);
/ / These two registers are used to set the four states of the P1 port, each bit corresponds to a P1 pin, combined operation by state
/*****************
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
The same is true for P3M0 P3M0. Because the STC12C2052AD has only two P ports, only the two groups STC12C5410AD and P2M0 P1M0 have three groups.
P1M0 P1M1 high
0 0 Normal I0 port (quasi-bidirectional) P1 register bit 7 6 5 4 3 2 1 0
0 1 Strong push-pull output (20MA current) use P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0
1 0 This mode is available only for input A/D conversion
1 1 open drain, this mode is available for A/D conversion
E.g:
To set P1.2 as the AD input port
Then P1M0=0X02;
P1M1=0X02; open drain
When not using AD, it is best to turn off the ADC power and restore 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, combined operation according to the state
P1M1=0x02; / / set P1.1 to open drain state
}
//AD conversion program
/************************************************* *****
Note: The commands commented in this function are general commands, which can be used for all AD channels. I have identified a channel in P1.1, so directly
/ / Assignment, save "flow"! The problem that afflicts me is the while waiting statement in this function.
While (1) //wait for the end of the A/D conversion
{
If (ADC_CONTR & 0x10) //0001,0000 Test A/D conversion end No
{ break; }
}
This is usable, what I originally wrote is:
While (ADC_CONTR & 0x10==0);
This can not be used to write, say it again: this can not be used! !
As for why, because of the priority, "==" is higher than & priority.
So add a bracket to it.
While ( (ADC_CONTR & 0x10) == 0 );
If you don't use C language often, you won't remember it! ! !
A lesson is learned from this; small problems affect efficiency
Experience: It is often dead to add a bracket and it doesn't seem to consume "flow"! !
*********************************************/
Uint ADC()
{
ADC_DATA = 0; //Clear the result
ADC_CONTR = 0x60; //Switching 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); //Stabilize the input voltage
ADC_CONTR = 0xe9;
// ADC_CONTR |= 0x08; //0000,1000 Let ADCS = 1, start A/D conversion,
While (1) //wait for the end of the A/D conversion
{
If (ADC_CONTR & 0x10) //0001,0000 Test A/D conversion end No
{ 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; / / over voltage protection, turn off the switch tube control end
M=0;
N=1;
LED=1;
}
If(voltage4.62&&voltage
Rectifier bridge is to seal the rectifier tube in a shell. Points full bridge and half bridge. The full bridge connects the four diodes of the connected bridge rectifier circuit together. The half bridge is half of four diode bridge rectifiers, and two half bridges can be used to form a bridge rectifier circuit. One half bridge can also be used to form a full-wave rectifier circuit with a center-tapped transformer. Select a rectifier bridge to consider. Rectifier circuit and operating voltage.
Bridge Rectifier,Original Bridge Rectifier,Full Bridge Rectifier ,Diodes Bridge Rectifier,Single Phase Rectifier Bridge, Three Phase Bridge Rectifier
YANGZHOU POSITIONING TECH CO., LTD. , https://www.cnchipmicro.com