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

📄 sadc.c

📁 君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图片解码,浏览,电子书,录音,想学ucos,识货的人就下吧 russblock fmradio explore set
💻 C
字号:
#include <jz4740.h>
#include <ucos_ii.h>
#include <jztouch.h>
//#undef printf
//#define printf(x,args...)
#ifdef USE_MIDWARE
#include "midware.h"
#endif

#define TS_AD_COUNT             5
#define M_SADC_CFG_SNUM_5	    ((TS_AD_COUNT - 1) << SADC_CFG_SNUM_BIT)

#define SADC_CFG_INIT (   \
                             (2 << SADC_CFG_CLKOUT_NUM_BIT) |  \
						                SADC_CFG_XYZ1Z2		   		   |  \
									    SADC_CFG_SNUM_5                |  \
									    (1 << SADC_CFG_CLKDIV_BIT)    |  \
						                SADC_CFG_PBAT_HIGH             |  \
									    SADC_CFG_CMD_INT_PEN )
									    
static unsigned char run_flag = 0;
static u16 ts_TimeOut = 0;


#define TASK_STK_SIZE	1024
static OS_STK SADCTaskStack[TASK_STK_SIZE];
static OS_EVENT *sadcEvent;
static OS_EVENT *batteryEvent;
static OS_EVENT *AdKeyEvent;
#define TOUCH_TASK_PRIO	5

static PFN_SADC touchFunction = 0;
static PFN_SADC sadcFunction = 0;
static PFN_SADC batFunction = 0;
static unsigned int batCountperSec = 0;
static unsigned int sadcCountperSec = 0;
static unsigned char dCount = 0;
unsigned short tsxData = -1,tsyData = -1;
static unsigned pendown_flag = 0;
static void start_pbat_adc(void);
static unsigned int jz4740_read_battery(void);

static void ReadTsData()
{
	unsigned int dat;
	unsigned short xData,yData;
	short tsz1Data,tsz2Data;
    
	
	dat = REG_SADC_TSDAT;
	
	xData = (dat >>  0) & 0xfff;
	yData = (dat >> 16) & 0xfff;
	
    //printf("%d -> %d %d\r\n",dCount,xData,yData);		
	dat = REG_SADC_TSDAT;
    tsz1Data = (dat >>  0) & 0xfff;
	tsz2Data = (dat >> 16) & 0xfff;
	//printf("%d -> %d %d\r\n",dCount,tsz1Data,tsz2Data);		
	tsz1Data = tsz2Data - tsz1Data;
	if(pendown_flag != 1)
		return ;
	
	
	if((tsz1Data > 15) || (tsz1Data < -15))
    {
		if(tsxData == (unsigned short)-1)
			tsxData = xData;
		else
			tsxData = (tsxData + xData) / 2;
		
		if(tsyData == (unsigned short)-1)
			tsyData = yData;
		else
			tsyData = (tsyData + yData) / 2;
		
    }

//	printf("0x%08x \r\n",dat);	
    dCount ++;
	
	if(dCount > TS_AD_COUNT - 1)
	{
		if(tsxData != (unsigned short) -1)
		{
			
		    if(touchFunction)
			{
				dat = tsxData + (tsyData << 16);
				touchFunction((unsigned short *)&dat);
			}
			//printf("x: 0x%d y: 0x%d\r\n",tsxData,tsyData);	
			tsxData = -1;
			tsyData = -1;
		}
		
		dCount = 0;
	}
	
}
static void touchTaskEntry(void *arg)
{
		unsigned char state;
		unsigned char sadcstat;
		unsigned char err;
		
		while(run_flag)
		{
			OSSemPend(sadcEvent,ts_TimeOut,&err);
			ts_TimeOut = 0;
			sadcstat = REG_SADC_STATE;
			state = REG_SADC_STATE & (~REG_SADC_CTRL);
			REG_SADC_STATE &= sadcstat;
					
			if(state & SADC_CTRL_PENDM)
			{
				  //printf("pen down irq \r\n");
				  REG_SADC_CTRL &= (~(SADC_CTRL_PENUM |  SADC_CTRL_TSRDYM));
				  REG_SADC_CTRL |= (SADC_CTRL_PENDM);// | SADC_CTRL_TSRDYM);
				  pendown_flag = 1;
			}
			if(state & SADC_CTRL_PENUM)
			{
				//printf("pen up irq \r\n");
				REG_SADC_CTRL &= (~SADC_CTRL_PENDM );
				REG_SADC_CTRL |= SADC_CTRL_PENUM;
				pendown_flag = 0;
				tsxData = -1;
				tsyData = -1;
				if(touchFunction)
				{
					unsigned int dat;
					
					dat = tsxData + (tsyData << 16);
					touchFunction((unsigned short *)&dat);
				}
			    //printf("x: 0x%d y: 0x%d\r\n",tsxData,tsyData);	
			}
			if(state & SADC_CTRL_TSRDYM)
			{
				//printf("touch ad irq 0x%x \r\n",sadcstat);
					ReadTsData();
					
			}
			if(state & SADC_CTRL_PBATRDYM)
			{
				printf("battery ad irq\r\n");
			}
			if(state & SADC_CTRL_SRDYM)
			{
				printf("sad ad irq\r\n");
				
			}
			
			__intc_unmask_irq(IRQ_SADC);
		}
		
}


static void handler(unsigned int arg)
{
	__intc_mask_irq(IRQ_SADC);
//	printf("IRQ_SADC");
	
	OSSemPost(sadcEvent);
}

static int SADC_Init()
{
	REG_SADC_ENA = 0;
	REG_SADC_STATE &= (~REG_SADC_STATE);
	REG_SADC_CTRL = 0x1f;
	
	sadcEvent = OSSemCreate(0);
	//__cpm_start_sadc();
	REG_SADC_CFG =  SADC_CFG_INIT;

	run_flag = 1;
	request_irq(IRQ_SADC, handler, 0);
	
	OSTaskCreate(touchTaskEntry, (void *)0,
		     (void *)&SADCTaskStack[TASK_STK_SIZE - 1],
		     TOUCH_TASK_PRIO);
	REG_SADC_SAMETIME = 1;
	REG_SADC_WAITTIME = 1000; //per 100 HZ
	ts_TimeOut = 0;
	REG_SADC_STATE &= (~REG_SADC_STATE);
	REG_SADC_CTRL &= (~(SADC_CTRL_PENDM | SADC_CTRL_TSRDYM)); // 
	REG_SADC_ENA = SADC_ENA_TSEN; // | REG_SADC_ENA;//SADC_ENA_TSEN | SADC_ENA_PBATEN | SADC_ENA_SADCINEN;
	
}

static int SADC_DeInit()
{
	REG_SADC_ENA = 0;
	__intc_mask_irq(IRQ_SADC);
	OSTimeDly(20);
	run_flag = 0;
	__cpm_stop_sadc();
}


void initTouch(PFN_SADC touchfn)
{
	touchFunction = touchfn;
	//printf("++++++++++++++++++++%s\n", __FUNCTION__);
	
}
void initBattery(int countpersec,PFN_SADC batfn)
{
	batCountperSec = countpersec;
	batFunction = batfn;
}
void initSadc(int countpersec,PFN_SADC sadcfn)
{
	sadcCountperSec = countpersec;
	sadcfn = sadcfn;
}
int TS_init(void)
{
	return SADC_Init();
}

/********* battery start ************************************************/

#ifdef USE_MIDWARE
u16 pbat= MAX_BAT_VAL; //battery voltage
#else
u16 pbat= 2280; //battery voltage
#endif


u16 pbadkey;
u16 pbadkey_bak;
/*
 *  battery voltage int handle
 */
static void handler_battery(unsigned int arg)
{
	unsigned char state;
	unsigned char state1;

	__intc_mask_irq(IRQ_SADC);
	state = REG_SADC_STATE & (~REG_SADC_CTRL);
	state1 = REG_SADC_STATE & (~REG_SADC_CTRL);
	if(state & SADC_CTRL_PBATRDYM)   //read ready and enable interrupt
	{
		pbat = REG_SADC_BATDAT;
		OSSemPost(batteryEvent);
		REG_SADC_STATE = SADC_STATE_PBATRDY;
	}
	if(state1 & SADC_CTRL_SRDYM)   //read ready and enable interrupt
	{
		// pbadkey = REG_SADC_SADDAT;

#if	0	// 2.8	
		pbadkey_bak = pbadkey = REG_SADC_SADDAT; 
		//printf("ADKEY is :%d\n", pbadkey);
		if(pbadkey>=2000 - 200  && pbadkey<=2000 + 200)
			pbadkey=0x20;			//-----up
		else if(pbadkey>= 2500 -200 && pbadkey<=2500 + 200)
			pbadkey=0x08;			//-----down
		else if(pbadkey>=1000 - 150 && pbadkey<=1000 + 150)
			pbadkey=0x04;			//-----left
		else if(pbadkey>=600 - 200 && pbadkey<=600 + 200)
			pbadkey=0x02;			//-----right
		else if(pbadkey>=1500 - 200 && pbadkey<=1500 +200)
			pbadkey=0x04;			//-----left esc
		else if(pbadkey>=3000 - 200 && pbadkey<=3000 +200)
			pbadkey=0x01;			//-----ok ent
		else
			pbadkey=0;		
				
#elif 0 //3.5
		pbadkey_bak = pbadkey = REG_SADC_SADDAT; 
		//printf("ADKEY is :%d\n", pbadkey);
		if(pbadkey>=2980 - 200  && pbadkey<=2980 + 200)
			pbadkey=0x20;			//-----up
		else if(pbadkey>= 590 -200 && pbadkey<=590 + 200)
			pbadkey=0x08;			//-----down
		else if(pbadkey>=2460 - 200 && pbadkey<=2460 + 200)
			pbadkey=0x04;			//-----left
		else if(pbadkey>=1906 - 200 && pbadkey<=1906 + 200)
			pbadkey=0x02;			//-----right
		else if(pbadkey>=1448 - 200 && pbadkey<=1448 +200)
			pbadkey=0x04;			//-----left esc
		else if(pbadkey>=1020 - 200 && pbadkey<=1020 +200)
			pbadkey=0x01;			//-----ok ent
		else
			pbadkey=0;			
#elif 0  //demo
		pbadkey_bak = pbadkey = REG_SADC_SADDAT; 
		//printf("ADKEY is :%d\n", pbadkey);
		if(pbadkey>=2100 - 400  && pbadkey<=2100 + 100)
			pbadkey=0x20;			//-----up
		else if(pbadkey>= 2600 - 400 && pbadkey<=2600 + 100)
			pbadkey=0x08;			//-----down
		else if(pbadkey>=1100 - 300 && pbadkey<=1100 + 100)
			pbadkey=0x04;			//-----left
		else if(pbadkey>=1500 - 300 && pbadkey<=1500 + 100)
			pbadkey=0x02;			//-----right
		else if(pbadkey>=3000 - 200 && pbadkey<=3000 +200)
			pbadkey=0x40;			//-----left esc
		else if(pbadkey>=600 - 200 && pbadkey<=600 +200)
			pbadkey=0x01;			//-----ok ent
		else
			pbadkey=0;
#elif 1 //demo
		pbadkey_bak = pbadkey = REG_SADC_SADDAT; 
		//printf("ADKEY is :%d\n", pbadkey);
		if(pbadkey>=2100 - 400  && pbadkey<=2100 + 100)
			pbadkey=0x20;			//-----up
		else if(pbadkey>= 2600 - 400 && pbadkey<=2600 + 100)
			pbadkey=0x08;			//-----down
		else if(pbadkey>=1100 - 300 && pbadkey<=1100 + 100)
			pbadkey=0x04;			//-----left
		else if(pbadkey>=1500 - 300 && pbadkey<=1500 + 100)
			pbadkey=0x02;			//-----right
		else if(pbadkey>=3000 - 200 && pbadkey<=3000 +200)
			pbadkey=0x40;			//-----left esc
		else if(pbadkey>=600 - 200 && pbadkey<=600 +200)
			pbadkey=0x01;			//-----ok ent
		else
			pbadkey=0;
#elif 0 // 2.4  16
		pbadkey_bak = pbadkey = REG_SADC_SADDAT; 
		//printf("ADKEY is :%d\n", pbadkey);
		/*
		if(pbadkey>=2000 - 200  && pbadkey<=2000 + 200)
			pbadkey=0x20;		//-----up
		else if(pbadkey>= 2600 -200 && pbadkey<=2600 +200)
			pbadkey=0x08;						//-----down
		else if(pbadkey>=1360 - 200&& pbadkey<=1360 + 200)
			pbadkey=0x04;		//-----left
		else if(pbadkey>=670 - 200 && pbadkey<=670 + 200)
			pbadkey=0x02;			//----- right
		else
			pbadkey=0;		
			*/
		if(pbadkey>=2000 - 200  && pbadkey<=2000 + 200)
			pbadkey=0x08;		//-----up
		else if(pbadkey>= 2600 -200 && pbadkey<=2600 +200)
			pbadkey=0x20;						//-----down
		else if(pbadkey>=1360 - 200&& pbadkey<=1360 + 200)
			pbadkey=0x02;		//-----left
		else if(pbadkey>=670 - 200 && pbadkey<=670 + 200)
			pbadkey=0x04;			//----- right
		else
			pbadkey=0;					
#else   //apple3
		pbadkey_bak = pbadkey = REG_SADC_SADDAT; 
		//printf("ADKEY is :%d\n", pbadkey);
		/*
		if(pbadkey>= 1 && pbadkey<=50)
			pbadkey=0x20;			//-----up
		else if(pbadkey>= 180 && pbadkey<=280)
			pbadkey=0x08;			//-----down
		else if(pbadkey>=420 && pbadkey<=550)
			pbadkey=0x04;			//-----left
		else if(pbadkey>=650 && pbadkey<=750)
			pbadkey=0x02;			//-----right
		else
			pbadkey=0;
			*/
			
		if(pbadkey>= 800 - 200 && pbadkey<=800 + 200)
			pbadkey=0x20;			//-----up
		else if(pbadkey>= 1500 - 200 && pbadkey<=1500 + 200)
			pbadkey=0x08;			//-----down
		else if(pbadkey>=2300 - 200 && pbadkey<=2300 + 200)
			pbadkey=0x04;			//-----left
		else if(pbadkey>=3100 - 200 && pbadkey<=3100 + 200)
			pbadkey=0x02;			//-----right
		else
			pbadkey=0;			
#endif	
		
		OSSemPost(AdKeyEvent);
		REG_SADC_STATE = SADC_STATE_SRDY;
		}
	__intc_unmask_irq(IRQ_SADC);
}

/*
 * Read the battery voltage
 */

static unsigned int jz4740_read_battery(void)
{
	if(!(REG_SADC_STATE & SADC_STATE_PBATRDY) ==1)
		start_pbat_adc();
}

/* 
 * set adc clock to 12MHz/div. A/D works at freq between 500KHz to 6MHz.
 */
static void sadc_init_clock(int div)
{
	if (div < 2) div = 2;
	if (div > 23) div = 23;
	
	REG_SADC_CFG &= ~SADC_CFG_CLKDIV_MASK;
	REG_SADC_CFG |= (div - 1) << SADC_CFG_CLKDIV_BIT;
//	REG_SADC_CTRL = 0x1d;  //touch screen
//	REG_SADC_ENA = SADC_ENA_TSEN;
}

static void start_pbat_adc(void)
{
	REG_SADC_CFG |= SADC_CFG_PBAT_HIGH ;   /* full baterry voltage >= 2.5V */
//  	REG_SADC_CFG |= SADC_CFG_PBAT_LOW;    /* full baterry voltage < 2.5V */
  	REG_SADC_ENA |= SADC_ENA_PBATEN;      /* Enable pbat adc */

       REG_SADC_ENA |=  SADC_ENA_SADCINEN; 
	
}

  
int battery_init()
{
	batteryEvent = OSSemCreate(0);
       AdKeyEvent=OSSemCreate(0);
	sadc_init_clock(3);
	request_irq(IRQ_SADC,handler_battery, 0);

}

int read_battery(void)
{
	u16 bat1=0;
	int i;
for(i=0;i<11;i++)
{
	jz4740_read_battery();
	printf("nbat%d is :%d\n",i, pbat);
	bat1+=pbat;
	OSTimeDly (1);
}

	pbat=bat1/i;
//	OSSemPend(batteryEvent,100,&err);
//	printf("read battery delay!---err:%d\n",err);
/*
#ifdef USE_MIDWARE
u16 pbat= MAX_BAT_VAL; //battery voltage
#else
u16 pbat= 2280; //battery voltage
#endif	
*/
	printf("average pbat is :%d\n\n\n", pbat);
	return pbat;
}

static unsigned jz4725_read_keyval(void)
{
	if(!(REG_SADC_STATE & SADC_STATE_SRDY) ==1)
		start_pbat_adc();
}
/*static void sadcin_init_clock(int div)
{
	if (div < 2) div = 2;
	if (div > 23) div = 23;
	
	REG_SADC_CFG &= ~SADC_CFG_CLKDIV_MASK;   //clear AD Converter frequency
	REG_SADC_CFG |= (div - 1) << SADC_CFG_CLKDIV_BIT;  //set AD converter frequency
}*/
/*static void start_pbadkey_adc(void)
{
   	//REG_SADC_CFG |= SADC_CFG_PBAT_HIGH ;   /* full key voltage >= 2.5V */
	//REG_SADC_CFG |= SADC_CFG_PBAT_LOW;    /* full key voltage < 2.5V */
  	//REG_SADC_ENA |=  SADC_ENA_SADCINEN;      /* Enable sadcin adckey */

//}
/*int SADKEY_Init()
{
    AdKeyEvent=OSSemCreate(0);
    sadcin_init_clock(3);
    request_irq(IRQ_SADC,handler_battery,0);

}*/
int Read_Adkey(void)
{
	unsigned char err;
	jz4725_read_keyval();
	OSSemPend(AdKeyEvent,0,&err);
	return pbadkey;
}
//liangzy add 

/************ battery end ************************************************/

⌨️ 快捷键说明

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