⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 我们自己做了一个智能充电器,能向串口发送各种电压数据.
💻 C
字号:
/****************************************Copyright (c)**************************************************
**									广州广微电子有限公司
**                          Guangzhou Microwide Electronic Co.,Ltd.
**                                 http://www.micro-wide.com
**
**----------------------------------------File Info----------------------------------------------------
** File name:			main.c
** Last modified Date:  2007-05-04
** Last Version:		1.0
** Descriptions:		32×12 LED 屏测试程序
**						
**
**------------------------------------------------------------------------------------------------------
** Created by:			stars_txx
** Created date:		2007-05-01
** Version:				1.0
** Descriptions:		The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include <rtx51tny.h>
#include <AT89X52.H>
#include <stdio.h>
#include "STC_ADC.h" 

#define INIT      	0               /* task number of task:  init           */
#define CHARG   	1               /* task number of task:  draw pix       */
#define ADC    		2               /* task number of task:  pan scan       */

#define CHARG_PIN	P1_3
#define RUN_LED		P1_7
#define CHARG_LED	P1_6
#define ADC_VRF		2
#define ADC_BAT		1
#define ADC_CHKNUM	100
#define VREF_TR		0x3c
#define SYSCLK		5600000
#define OS_TICK_SEC		46
#define CHARG_ON		1
#define CHARG_OFF	0
#define LED_ON		0
#define LED_OFF		1	

unsigned short 	VRef = 0;
unsigned short 	VBat = 0;
unsigned char 	VBatMax = 0;

sfr P1M0		=0x91;
sfr P1M1		=0x92;

//UART init, CLK=11059200, Baud = 9600bps
void initUart(void)
{
 TMOD|=0x20;
 SCON=0x50;
 PCON|=0x80;
 TH1=0xfa;
 TL1=0xfa;
 TR1=1;
}

char putchar (char c)
{
	//ES=0;
	SBUF=c;
	while(!TI);TI=0;
	//ES=1;
	return c;
}

/******************************************************************************/
/*        Task 0 'init': Initialize                                           */
/******************************************************************************/
void init (void) _task_ INIT  
{ 
/* program execution starts here        */
	ADC_Pin_Config(ADC_VRF);
	ADC_Pin_Config(ADC_BAT);
	Init_ADC_Interrupt(1);
	CHARG_PIN = CHARG_ON;
	CHARG_LED = LED_OFF;
	P1M0 |= 0x01<<3;
	P1M1 |= 0x01<<3;
	initUart();
	os_create_task(ADC);
	os_create_task(CHARG);
 	os_delete_task (INIT);              /* stop init task (no longer needed)  */
}
/******************************************************************************/
/*        Task 1 'charger control':                       */
/******************************************************************************/
void ChargControl (void) _task_ CHARG
{
	unsigned char Status = 0;
	unsigned short MinCout = 0;
	unsigned short TotoleTime = 0;
	unsigned long Index = 0;
	while(1)
	{
		switch(Status)
		{
			case 0:
				if(VRef > VREF_TR)
				{
					Status = 1;
					VRef = 0;
					VBat = 0;
					VBatMax = 0;
					TotoleTime = 0;
					Index = 0;
					printf("%u:Charge ON\n", Index);
				}
				break;
			case 1:
				if(VRef > VREF_TR)
				{
					CHARG_LED = LED_ON;
					CHARG_PIN = CHARG_ON;
					TotoleTime++;
					if(VBatMax < VBat)
					{
						VBatMax = VBat;
						MinCout = 0;
						printf("%u:VBatMax UpData = 0x%x\n", Index,VBat);
					}
					else
					{
						MinCout++;
						if(MinCout == 1800)
						{
							Status = 2;
							printf("%u:Batter is Almost full\n",Index);
							MinCout = 0;
							os_wait2(K_SIG, 1);
							printf("%u:Totole Time = %d S\n", Index,TotoleTime);

						}
						else
						{
							printf("%u:VBatMax Lock = 0x%x\n", Index,VBat);
						}
					}

				}
				else
				{
					CHARG_LED = LED_OFF;
					printf("%u:Charging, Batter Removed\n",Index);
					Status = 0;
				}
				break;
			 case 2:
			 	if(VRef > VREF_TR)
				{
					if(MinCout == 0xff)
					{
						CHARG_LED = LED_OFF;
						CHARG_PIN = CHARG_OFF;
						Status = 3;
						MinCout = 0;
						printf("%u:Charg is Full\n",Index);
					}
					MinCout++;
				}
				else
				{
					CHARG_LED = LED_OFF;
					printf("%u:Charg Almost Full, Batter Removed\n",Index);
					MinCout=0;
					Status = 0;
				}
			 	break;
			 case 3:
			 	if(VBat < 0x10)
				{
					CHARG_PIN = CHARG_ON;
					CHARG_LED = LED_OFF;
					printf("%u:Charg Complete, Batter Removed\n",Index);
					Status = 0;
				}
				else
				{
					printf("%u:Charg Complete\n",Index);
				}
			 	break;
			 case 4:
			 	break;
		}
		Index++;
		os_wait2(K_TMO, OS_TICK_SEC);
		RUN_LED = ~RUN_LED;
	}
}
/******************************************************************************/
/*        Task 2 'ADC Check':                                            */
/******************************************************************************/
void ADC_CHK (void) _task_ ADC 
{
	unsigned short V;
	while(1)
	{
		unsigned char i;
		V = 0;
		for(i=0; i<ADC_CHKNUM; i++)
		{
			ADC_Start_Convt(ADC_VRF,STC_ADC_SPEED_210CLK);
			os_wait2(K_SIG, 100);
			V += GetADC_Reslut(ADC_VRF);
		}
		VRef = V/ADC_CHKNUM;
		V = 0;
		for(i=0; i<ADC_CHKNUM; i++)
		{
			ADC_Start_Convt(ADC_BAT,STC_ADC_SPEED_210CLK);
			os_wait2(K_SIG, 100);
			V += GetADC_Reslut(ADC_BAT);
		}
		VBat = V/ADC_CHKNUM;
		//printf("VRef = 0x%x\n", VRef);
		os_wait2(K_TMO, 10);
		//printf("VBat = 0x%x\n", VBat);
		os_wait2(K_TMO, 10);
	}
	
}

void ADC_Int_Expection(void) interrupt 5
{
	isr_ADC_GetVal();
	isr_send_signal(ADC);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -