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

📄 utils.c

📁 可以实现44B0的底层所有通信
💻 C
字号:

//created by chenzhi

#include <string.h>
#include "..\inc\44b0x.h"
#include "..\INC\44blib.h"
#include "..\INC\MassOp.h"
#include "..\INC\led.h"
#include "..\INC\SL811.h"
#include "..\inc\hpi.h"
#include "..\INC\def.h"
#include "..\INC\option.h"
#include "..\INC\utils.h"

extern U8 INOUTBUF[INOUTBUF_LENGTH];	//#define INOUTBUF_LENGTH 65536

//---------------------------------------------------------------
//time=hour*2048+minute*32+second+2  --edited by tianjh 05.03.29
//----------------------------------------------------------------
U16 CalTimePara4FAT(U8 hour,U8 minute,U8 second)
{
	U16 result;
	result=hour*2048+minute*32+second+2;
	return result;
}
//---------------------------------------------------------------
//date=(year-1980)*512+monthe*32+day  --edited by tianjh 05.03.29
//----------------------------------------------------------------

U16 CalDatePara4FAT(U16 year,U8 month,U8 day)
{
	U16 result;
	result=(year-1980)*512+month*32+day;
	return result;
}

///////////////////////////////////////////////////
/************************************************/
//*****  *argv      --->the filename from outside
//*****  *pBuffer   --->the pointer point to the fileName
//           
/***************************************************/

U8 FatPro(char *argv,U8 *pFileName)
{
	U8 i,len,bAfterComma,pointer;
	char flag='.';
	
	len=strlen(argv);
	bAfterComma=0;
	pointer=0;
	
	if(len==0)
		return FALSE;
	// init buffer
	for(i=0;i<11;i++)
		*(pFileName+i)=0x20;		//0x20---->space  空白键		
	
	for(i=0;i<len;i++)
		{
		if(*(argv+i)==flag)		//flag---->'.'
			{
			if(i==0)
				return FALSE;
			bAfterComma=1;
			pointer=0;
			}
		else if((bAfterComma==0)&&(pointer<8))
			{					
			*(pFileName+pointer)=*(argv+i);		
			pointer++;
			}
		else
			{
			if(pointer<3) 	
				{				 //  copy the ExtensionName   ---edited by tianjh 05.03.29
				*(pFileName+8+pointer)=*(argv+i);
				pointer++;
				}
			}
		}
	
	//convert to the upper case
	
	for(i=0;i<11;i++)
	{
	if (*(pFileName+i) >= 'a' && *(pFileName+i) <= 'z')
		*(pFileName+i)=*(pFileName+i)-'a'+'A';
			
	}
	return TRUE;
}

//  create file ------------edited by tianjh 05.03.14----/
//
//  pBuffer == filename[]="0123456789.dat"; 
//  
//	
//
int tianjh_Createfile(char *pBuffer)
{
	U16 i,retStatus;
	U8 *pFileItem;
	U8 fileName[11];	
	DIR_STRUC itemToCreate;    
	
	retStatus=FatPro(pBuffer,fileName);		//  ------preprocess the filename     ---edited by tianjh  05.03.28
	//;;;;;;return the fileName;;;
	if(!retStatus)
		{
		Uart_Printf("Bad Command.\n");
		return 0;
		}
	for(i=0;i<11;i++)						//  copy the filename   ---edited by tianjh 05.03.28
		*(itemToCreate.DIR_Name+i)=fileName[i];	
	itemToCreate.DIR_Attr=0;		//ATTR_DIRECTORY;
	itemToCreate.DIR_NTRes=0;		//0 fixed
	itemToCreate.DIR_CrtTimeTenth=0;
	itemToCreate.DIR_CrtTime=CalTimePara4FAT(5,43,21);		// 	the create-time of the file		edited by tianjh   05.03.28
	itemToCreate.DIR_CrtDate=CalDatePara4FAT(2004,9,28);	//	the create-date of the file     edited by tianjh   05.03.28 
	itemToCreate.DIR_LstAccDate=0;							//  the last Acc-Date of the file	edited by tianjh   05.03.28
	itemToCreate.DIR_FstClusHI=0;	//FAT16:0				//  the file-type of file           edited by tianjh   05.03.28 
	itemToCreate.DIR_WrtTime=CalTimePara4FAT(5,43,21);		//	the write-time of the file		edited by tianjh   05.03.28
	itemToCreate.DIR_WrtDate=CalDatePara4FAT(2004,9,28);	//	the write-date of the file		edited by tianjh   05.03.28
	itemToCreate.DIR_FstClusLO=0;	//Will be modified by the CreateFile() function
	itemToCreate.DIR_FileSize=0;	//Will be modified by the CreateFile() function
	
	//----- search for the file with the same name ----------------
	
	pFileItem=(U8 *)itemToCreate.DIR_Name;
	
	retStatus=CreateFile(pFileItem);
	  	
	if(retStatus==0x1)
    	{
    		Uart_Printf("File create SUCCESS!\n");
    		return 1;
    	}
    		
  
    Uart_Printf("File create FAILED!\n");
    return 0;
}

int tianjh_Write(int argc, char *argv[])
{
	U16 i,retStatus;
	U16 *wpBuf,len;

	if(argc!=2) 
		{
		Uart_Printf("Bad Command.\n");
		return 0;
		}
			
	wpBuf=(U16 *)INOUTBUF;		//  the length of INOUTBUF---->65536	16bit --be able to collect the data from FIFO -edited by tianjh 05.03.14---
	
	//-------------------------------------------------------------------
	len = strtodec(argv[1],strlen(argv[1]));		//  get the length---convert the string to the number -----edited by tianjh   05.03.28 
	//******************************
	for(i=0;i<len/2;i++)		//
	
			*(wpBuf+i)=0x1234;		//---------copy the data  edited by tianjh 05.03.30-------
	
	// ------   len 		--->the length of data
	// ------	INOUTBUF	--->the buffer to write 
	// ------ 	bEnd		--->the end-flag of file, to define whether to end the file  
	// ------	INOUTBUF	--->U8 INOUTBUF[INOUTBUF_LENGTH];	INOUTBUF_LENGTH----> 65536
	retStatus=WriteFile(len,INOUTBUF,1);
											
	if(retStatus==1){
			Uart_Printf("File write SUCCESS! Total %d bytes written!\n",len);
			return 1;
			}
	
	else {
    		Uart_Printf("File write FAILED!\n");
    		return 0;
    		}
	
}


/*   string to decical   */
unsigned long strtodec(char *str, int cnt)
{
	unsigned long i, data = 0;	
	
	for(i=0; i<cnt; i++)
	{
		data *= 10;
		if(str[i]<'0'||str[i]>'9')		//ERROR   ---edited by tianjh 05.03.28
			return -1;
		data += str[i]-'0';			
	}	
	return data;
}


void HaltUndef(void)
{
    Uart_Printf("Undefined instruction exception!!!\n");
    while(1);
}

void HaltSwi(void)
{
    Uart_Printf("SWI exception!!!\n");
    while(1);
}

void HaltPabort(void)
{
    Uart_Printf("Pabort exception!!!\n");
    while(1);
}

void HaltDabort(void)
{
    Uart_Printf("Dabort exception!!!\n");
    while(1);
}

void Isr_Init(void)
{
    U32 i;
    
    pISR_UNDEF=(unsigned)HaltUndef;
    pISR_SWI  =(unsigned)HaltSwi;
    pISR_PABORT=(unsigned)HaltPabort;
    pISR_DABORT=(unsigned)HaltDabort;
    
     for(i=_RAM_STARTADDRESS;i<(_RAM_STARTADDRESS+0x20);i+=4)
    {
	*((volatile unsigned *)i)=0xEA000000+0x1FFE;
    }

    //rINTCON=0x1;	  // Vectored Int. IRQ enable,FIQ disable    
    rINTCON=0x5;	  // Non-vectored,IRQ enable,FIQ disable    

    rINTMOD=0x0;	  // All=IRQ mode
    rINTMSK|=BIT_GLOBAL|BIT_EINT3;	  // All interrupt is masked.
}

#define	MaxBeepFreq	20000
#define	MinBeepFreq	20
int pwmcnt;
void __irq TIMER0_int(void)
{
 rI_ISPC=BIT_TIMER0;
 pwmcnt++;
 return ;
}
void SetBeepPwm(U16 Freq)		//通过定时器2来设置BEEP。即通过定时器2来设置PWM。
{
	U16 div;
	
	
	
	rTCON  	&= 0xffff0fff;				// clear manual update bit, stop Timer2,仅设置定时器2。
	
	rTCFG0 	&= 0xffff00ff;					//clear Presclar 	
	rTCFG0 	|= 0xffff04ff;					// set Timer 2&3 prescaler 0;将定时器2/3的预分频值清零。0xffff00ff
	
	rTCFG1 	&= 0xfffff0ff;					//定时器2的MUX值清为零。
	rTCFG1  |= 0x00000000;					// set Timer 2 MUX 1/16  MUX值为1/16
	
	div = MCLK/(Freq*10*1000);
	
	rTCNTB2	= div;
	
	rTCMPB2 = 1	;							//if set inverter on,  when TCNT2<=TCMP2, TOUT is low,  TCNT2>TCMP2, TOUT is high
	
	rTCON	|= 0x00002000;					// manual update。
	
	rTCON	&= 0xffff0fff;	  				// clear manal update bit
	
	rTCON   |= 0x0000d000;					// auto reload, inverter on, start Timer 2
}

void StopBeepPwm(void)
{
	rTCON &= ~0x0001;			//stop timer0
}


/****************************************
 *	PWM (BEEP) test		*
 ****************************************/
void Test_Pwm(void)
{
    int  beginf, endf;
    int  i, j;
    Uart_Printf("[pwm (beep)  Test]\n");
   
	beginf=2700;
	endf=3000;
    Uart_Printf("beginf= %d ; endf=%d : ", beginf, endf );
     	 
    rINTCON = 5; //允许IRQ使用非向量模式,允许IRQ中断
    rINTMOD = 0; //中断以IRQ模式来处理	
    rINTMSK=~(BIT_GLOBAL|BIT_TIMER2);//开放TIMER2中断
    pISR_TIMER0= (unsigned  int)TIMER0_int;   //(unsigned)TIMER0中断函数入口地址
    rPDATE = 0x157;				//Beep = 10
    rPCONE = 0x55ab;      // tout0				
    rPUPE  = 0xff;		  //禁止上拉电阻连接到对应脚

  	for (i=beginf; i<endf; i+=50)
    {
        pwmcnt=0;
		SetBeepPwm(i);
		for (pwmcnt=0,j=0; pwmcnt<5;)
        {
        	Uart_Printf("j=%d\n",j++);
        }
	/* for (j=0; j<5;)
	{ 
	  while (READ_COUNT2>=1)
	  {
          
	   }
	  j++;
	 } */

    }
   	StopBeepPwm();
}



⌨️ 快捷键说明

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