Mobile phone battery charger made of STC12C2052AD

Publisher:心愿达成Latest update time:2012-08-24 Source: 51heiKeywords:STC12C2052AD  Charger Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*******************
C language operator priority level


1 priority left associative
() parentheses [] subscript operator -> point to structure member operator. Structure member operator

level 2 priority right associative
! Logical NOT operator ~ Bitwise negation operator ++ Increment operator -- Decrement operator - Negative sign operator (type) Type conversion operator * Pointer operator & Address and operator sizeof Length operator level

3 priority left associative
* Multiplication operator / Division operator % Remainder operator level

4 priority left associative
+ Addition operator - Subtraction operator level

5 priority left associative
<< Left shift operator >> Right shift operator

level 6 priority left associative
<, <=, >, >= Relational operator

level 7 priority left associative (Note the "equal" operator ==)
== Equality operator != Not equal to operator level

8 priority left associative
& Bitwise AND operator

level 9 priority left associative
^ Bitwise XOR operator

level 10 priority left associative
| Bitwise OR operator level

11 priority left associative
&& Logical AND operator level 12

priority left associative
|| Logical OR operator

level 13 priority right associative
? : Conditional operator

14th level priority right associative (assignment operator)
=+ =- =* =/ =% = >= < <= &= ^= |= All are assignment operators

15th level priority left associative
, Comma operator
**********************************/
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define AD_SPEED 0x60 //0110,0000 1 1 270 clock cycles to convert once,
//Less fish production Hebei Zhengding welcomes you Changsha Aviation Vocational and Technical College 2010 QQ:41165643
//
sbit M=P1^5; //Overvoltage indicator
sbit N=P1^6; //Undervoltage indicator
sbit LED=P1^7; //Full indicator
sbit REF=P1^0;
sbit PWM=P3^7;

bit START =0;

uchar timeL=0x90;
uchar timeH=0x90;
/**********************************************************/
void pwm();
void delayms(uint)
; void ADC();
void InitADC();
//void baohu();

float voltage=0.0;
const float Uref=2.500;

/***8**********************************************************/
void main()
{

PWM=1
; 700);
START=0
; PWM=0;
LED=0; REF
=0; delayms (
9000); delayms (1000 ); M = 0; N =0; LED=0; delayms (7000 ); M =1; N= 1; LED=1; delayms(7000); START=0; while(1) { ADC(); if(START) { pwm(); delayms(2000); } } } // // void pwm() { CR=0; START=0; //PCA module works in PWM mode C program CMOD = 0x02; //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 = timeL; //In PWM mode, they are used to control the duty cycle
CCAP0H = timeH; //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 operation mode setup (CCAPMn register n= 0-3)
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 CEXn rising edge
0x10 16-bit capture mode, triggered by CEXn falling edge
0x30 16-bit capture mode, triggered by CEXn jump
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. When capturing or comparing, they are used to
save the 16-bit count value. When working in PWM mode, they are used to control the duty cycle
****************************/

CR=1; //Start PCA Timer.

}


//AD conversion initialization----turn on ADC power
void InitADC()
{
P1=0xff;
ADC_CONTR|=0x80;
delayms(3);
//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 P3M0 is also. Because STC12C2052AD has only two P ports, so 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 less P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0
1 0 Only input This mode can be used for A/D conversion
1 1 Open drain, this mode can be used for A/D conversion


For example:
To set P1.1 as AD input port
Then P1M0=0X02;
P1M1=0X02; Open drain is OK
When AD is not used, it is best to turn off the ADC power supply and restore it to the IO port state
************************************/
P1M0=0x06; //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.
P1M1=0x06; //Set the open-drain state of P1.1 and P1.2

}



// AD conversion program
void ADC()
{
float V0,V1;

ADC_DATA = 0; //Clear the 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 |= 0x01; //Select A/D current channel P1.1
delayms(1); //Make the input voltage reach stability
ADC_CONTR |= 0x08; //0000,1000 Set ADCS = 1, start A/D conversion,

while(!(ADC_CONTR & 0x10)); //! has a much higher priority than &
// Develop the habit of adding brackets frequently, it does no harm. It does not waste speed
either./***************The
while statement here cannot be changed to while(ADC_CONTR & 0x10==0); it is wrong, because the priority == is higher than &, so you need to add brackets
while( (ADC_CONTR & 0x10) ==0) or negate it while(!(ADC_CONTR & 0x10)); //! has a much higher priority than &
while( (ADC_CONTR & 0x10) ==1) Pay attention to if while statements that judge the truth of logic, when using "==1", pay attention to whether the preceding one is the following 1,
The last 1 is 0x01, so if the first part is 0x02===1, this will not work. But it can be used if you remove the last ==1. Unnecessary actions will only bring troubles
**********************************/
ADC_CONTR &= 0xE7; //1111,0111 clear ADC_FLAG bit, turn off A/D conversion,

V0= ADC_DATA; //Return A/D 10-bit conversion result


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 =0xe2;
// ADC_CONTR |= 0x02; //Select A/D current channel P1.2
delayms(1); //Let the input voltage reach stability
ADC_CONTR = 0xea;
// ADC_CONTR |= 0x08; //0000,1000 set ADCS = 1, start A/D conversion,

while(!(ADC_CONTR & 0x10)); //! has a much higher priority than &.
//It's good to develop the habit of adding brackets frequently. It does not waste speed

ADC_CONTR =0xe2;
//ADC_CONTR &= 0xE7; //1111,0111 clear ADC_FLAG bit, turn off A/D conversion,
V1= ADC_DATA; //Return A/D 10-bit conversion result

voltage=V1/V0*Uref*3.000;



if( voltage>4.180)
{
M=0;//Overvoltage lamp
N=1;
LED=1;
timeL=timeL+0x08;
timeH=timeH+0x08;
START=1;
LED=0;
}
if(voltage<3.601)
{
N=0;//Undervoltage lamp
M=1;
LED=1;
timeL=timeL-0x01;
timeH=timeH-0x01;
START=1;
}

if(voltage>=3.601&&voltage<=4.155)
{
M=1;
N=1;
LED=1;
}

if(voltage>=4.110&&voltage<=4.155)
{
timeL = 0xa2; //When PWM mode, they are used to control the duty
cycletimeH = 0xa2; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow)
START=1;
LED=0;
}

if(voltage>=4.155&&voltage<=4.180)
{
timeL = 0xb2; //When PWM mode, they are used to control the duty
cycletimeH = 0xb2; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow)
START=1;
LED=0;
}

/**************************
if( voltage<3.772&&(timeL!=0xf0))
{
timeL = 0xf0; //When PWM mode, they are used to control the duty cycletimeH
= 0xf0; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow)
START=1;
}

if( voltage<4.052&&voltage>3.772&&(timeL!=0xf2))
{
timeL = 0xf2; //When PWM mode, they are used to control the duty cycle
timeH = 0xf2; //0xc0 64/256=25% duty cycle (overflow) high level time
START=1;
M=1;
N=1;
LED=1;
}

if( voltage>4.052&&voltage<4.167&&(timeL!=0xfd))
{
timeL = 0xfd; //When PWM mode, they are used to control the duty cycle
timeH = 0xfd; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow) START
=1;
M=1;
N=1;
LED=1;
}

if( voltage>4.167&&voltage<4.208&&(timeL! =0x60))
{
timeL = 0x80; //They are used to control the duty cycle in PWM mode
timeH = 0x80; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow)
START=1;
M=1;
N=1;
LED=1;
}

if(voltage>4.2050&&voltage<4.235)
{
timeL = 0x96; //In PWM mode, they are used to control the duty cycle
timeH = 0x96; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow)
START=1;
LED=0;
}

****************************************************/



}


/**********
// Protection function
void baohu()
{

if( voltage>4.231)
{
M=0;//Overvoltage lamp
N=1;
LED=1;
}
if(voltage<3.501)
{
N=0;//Undervoltage lamp
M=1;
LED=1;
}

if( voltage<3.772&&(timeL!=0xcf))
{
timeL = 0xcf; //When PWM mode, they are used to control the duty cycle
timeH = 0xcf; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow)
START=1;
}

if( voltage<4.052&&voltage>3.772&&(timeL!=0x60))
{
timeL = 0x60; //When PWM mode, they are used to control the duty cycle
timeH = 0x60; //0xc0 64/256=25% duty cycle (overflow) high level time
START=1;
}

if( voltage>4.052&&voltage<4.167&&(timeL!=0xb0))
{
timeL = 0xb0; //When PWM mode, they are used to control the duty cycle
timeH = 0xb0; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow)
START=1;
}

if( voltage>4.167&&voltage<4.218&&(timeL!=0xe0))
{
timeL = 0xe0; //In PWM mode, they are used to control the duty cycle
timeH = 0xe0; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow)
START=1;
}

if(voltage>4.220&&(timeL!=0xf2))
{
timeL = 0xf2; //In PWM mode, they are used to control the duty cycle
timeH = 0xf2; //0xff-0xc0=0x3f 64/256=25% duty cycle (overflow)
START=1;
LED=0;
}




}

***************************/

//Delay function
void delayms(uint k)
{
uint data i,j;
for(i=0;i {
for(j=0;j<600;j++)
{;}
}
}

Keywords:STC12C2052AD  Charger Reference address:Mobile phone battery charger made of STC12C2052AD

Previous article:51 single chip multi-byte division
Next article:LCD1602-DS1302 clock program

Recommended ReadingLatest update time:2024-11-16 22:40

Hyundai launches ultra-fast electric vehicle charging device 'Hi-Charger'
According to foreign media reports, Hyundai Motor has set up two ultra-high-speed electric vehicle charging devices "Hi-Charger" in Goyang Motor Studio . This device has a DC fast charging capacity of up to 350kW. Hyundai Motor invited electric vehicle owners and other relevant people on the same day to introduce the
[Automotive Electronics]
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号