Пример #1
0
void ReadWallSensors(void) {
	
	s16 offSens;
	
	offSens = AdcRead(ADC_CH_WALL_SENS_FR);
	GpioWrteWallSensFrOn(1);
	TimerWait1_333us(10);
	WallSensFr = (s16)AdcRead(ADC_CH_WALL_SENS_FR) - offSens;
	GpioWrteWallSensFrOn(0);

	offSens = AdcRead(ADC_CH_WALL_SENS_LS);
	GpioWrteWallSensLsOn(1);
	TimerWait1_333us(10);
	WallSensLs = (s16)AdcRead(ADC_CH_WALL_SENS_LS) - offSens;
	GpioWrteWallSensLsOn(0);

	offSens = AdcRead(ADC_CH_WALL_SENS_RS);
	GpioWrteWallSensRsOn(1);
	TimerWait1_333us(10);
	WallSensRs = (s16)AdcRead(ADC_CH_WALL_SENS_RS) - offSens;
	GpioWrteWallSensRsOn(0);

	offSens = AdcRead(ADC_CH_WALL_SENS_FL);
	GpioWrteWallSensFlOn(1);
	TimerWait1_333us(10);
	WallSensFl = (s16)AdcRead(ADC_CH_WALL_SENS_FL) - offSens;
	GpioWrteWallSensFlOn(0);

}
Пример #2
0
void main(void)
{ 
  unsigned short Val_Freq; // Valeur analogique de la frequence lue sur le potentiometre

  DebugClear();

  // Initialisation des ports
  PPI_Init();  
  EXPORT_Init();
  LED_Y_Init();
  LED_R_Init();

  // Initialisation de la memoire
  XHeap_init();

  // Mise en marche du convertisseur
  AdcSet(ADC0,ADCDF_128);
  AdcOn();

  while(1) { 
    Val_Freq = AdcRead(); // Acquisition de la valeur

    // affichage des 8 MSB (sur 10) de la valeur de la tension sur les LEDS hautes en SPI
    LED_R(Val_Freq>>2);
   
    Send_1(MsgBox_1, Val_Freq);


    // Affichage de la mesure dans la fenêtre 'ON-The-Fly' du mode Débug
    DebugClear();
    DebugPrint("\nValeur clignotement (0 à 1023) : ");
    DebugSHORT(Val_Freq,DEC);
  }
}
Пример #3
0
/**
* @brief 读取当前温度
* @return 当前温度(0.1度)
*/
int ReadTemperature(void)
{
	int adv = AdcRead(ADC_CHN_TEMP);

	if(TempCalibNum < 2) return CalDefTemp(adv);

	return CalByCalib(TempCalib, TempCalibNum, adv);
}
Пример #4
0
/**
* @brief 读取当前电池电压
* @return 当前电池电压(mv)
*/
int ReadBatVol(void)
{
#define MULTI_RATE		335

	int adv = AdcRead(ADC_CHN_BAT);

	if(BatCalibNum < 2) {
		adv = adv * MULTI_RATE / 100;
		return adv;
	}

	return CalByCalib(BatCalib, BatCalibNum, adv);
}
Пример #5
0
////////////////////////////////////////////////////
// 功能: 
// 输入: 
// 输出:
// 返回: 
// 说明: 
////////////////////////////////////////////////////
static void JzThreadRecordFile(DWORD p)
{
	PAK_OBJECT obj;
	PAK_VFILE vfile;
	PMEDIA_TASK task;
	MESSAGE msg;
	int recsize;
	int samp_size;
	void *buf_in;
	BYTE *fade_buf;
	int msgid;
	int wsize,samp_time;
	int obj_del = 1;
	int fade_len;
	int fade_record = RECORD_FADE;

	// 获取打开信息
	obj = (PAK_OBJECT)p;
	task = &obj->Task;
	vfile = &obj->File;
	if(!task->AChannels)
		task->AChannels = 1;
	if(!task->ASamplerate)
		task->ASamplerate = 8000;
	
	// 写入头信息
	samp_size = 8192 * 2;
	msgid = 0;
	WaveFileHead.bytes_per_samp = task->AChannels * 2;
	WaveFileHead.bytes_per_sec = WaveFileHead.bytes_per_samp * task->ASamplerate;
	WaveFileHead.num_chans = task->AChannels;
	WaveFileHead.sample_rate = task->ASamplerate;
	if( task->MediaType == AK_REC_TYPE_WAVE)
	{
		if(JzCbFileWrite(&WaveFileHead, sizeof(WAVE_HEADER),1,vfile) != sizeof(WAVE_HEADER))
		{
			*obj->bReady = -1;
			goto ThreadEnd;
		}
	}

	// 打开音频编码库
	buf_in = kmalloc(samp_size);
	if(buf_in == NULL)
	{
		*obj->bReady = -1;
		goto ThreadEnd;
	}

	// 打开ADC设备
	obj->hDac = AdcOpen(task->ASamplerate, task->AChannels);
	if(obj->hDac == NULL)
	{
		kdebug(mod_audio, PRINT_ERROR, "ADC device open failed! CH: %d, %dHz\n", task->AChannels, task->ASamplerate);
		kfree(buf_in);
		*obj->bReady = -1;
		goto ThreadEnd;
	}
	AdcSetVolume(obj->hDac, obj->Task.Volume);
	AdcSetEq(obj->hDac, obj->Task.Eq);

	// 发送WM_MEDIA_OPEN消息
	if(task->hWnd)
	{
		msg.hWnd = task->hWnd;
		msg.Data.v = 0;
		msg.hWndSrc = 0;
		msg.MsgId = WM_MEDIA_OPEN;
		msg.Param = 0;
	#if defined(STC_EXP)
		sGuiMsgPutMsg(&msg);
	#else
		GuiMsgPutMsg(&msg);
	#endif
	}
	
	// 等待条件,写入数据
	*obj->bReady = 1;
	recsize = 0;
	fRecodeWav = 1;
	while(!obj->bClose && !obj->bTerminate)
	{
		int i;
		
		// 从DA读取PCM数据
		for(i=0; i<8; i++)
		{
			JzSrvProcMsg(obj);
			wsize = AdcRead(obj->hDac, (short*)((DWORD)buf_in+(i*samp_size)/8), samp_size/8);
			samp_time = (samp_size * 1000 / 8) / (task->ASamplerate * task->AChannels * sizeof(short)) ;

			if( fade_record )
			{	//fade时,不进行时间计算,递减fade时间
				if( fade_record > samp_time )
					fade_record -= samp_time;
				else
					fade_record = 0;
			}
			else	//fade结束,可以进行时间计算
				obj->RecDuration += samp_time;
		}

		if( !fade_record )
			recsize += samp_size;
		
		// 检查录音时间是否结束
		if(obj->Task.Duration && (obj->RecDuration >= obj->Task.Duration))
		{
			recsize -= samp_size;
			obj->bClose = 1;
			break;
		}

		// 直接把PCM数据写入文件
		if( !fade_record )
		{
			wsize = JzCbFileWrite(buf_in, samp_size,1,vfile);
			if(wsize != samp_size)
			{
				if(wsize > 0)
					recsize -= samp_size - wsize;
				else
					recsize -= samp_size;
				obj->bClose = 2;
				break;
			}
		}

		// 处理控制消息
		JzSrvProcMsg(obj);
		while(obj->bRecPause && !obj->bClose && !obj->bTerminate)
		{
			JzSrvProcMsg(obj);
			sTimerSleep(100, NULL);
		}
	}

	//fade时,不保存文件
	if( !fade_record )
	{
		fade_len = (task->ASamplerate * task->AChannels * sizeof(short) * RECODE_SKIP_LEN) / 1000;
		if( task->MediaType == AK_REC_TYPE_WAVE)
		{
			// 录音结束,写入头/尾信息
			JzCbFileSeek(vfile, 0, SEEK_SET);
			WaveFileHead.file_size = recsize + sizeof(WaveFileHead) - 8;
			WaveFileHead.data_length = recsize;
			JzCbFileWrite(&WaveFileHead, sizeof(WAVE_HEADER),1,vfile);

			if( vfile->PlayLength > (fade_len + sizeof(WaveFileHead)) && fade_len )
			{
				fade_buf = (BYTE*)kmalloc(fade_len);
				kmemset(fade_buf,0,fade_len);
				JzCbFileSeek(vfile, recsize + sizeof(WaveFileHead) - fade_len , SEEK_SET);
				JzCbFileWrite(fade_buf, fade_len,1,vfile);
				kfree(fade_buf);
				kdebug(mod_audio, PRINT_INFO, "recode fade: skip wav len = %d\n",fade_len);
			}
		}
		else
		{
			if( vfile->PlayLength > fade_len && fade_len )
			{
				fade_buf = (BYTE*)kmalloc(fade_len);
				kmemset(fade_buf,0,fade_len);
				JzCbFileSeek(vfile, recsize - fade_len, SEEK_SET);
				JzCbFileWrite(fade_buf, fade_len,1,vfile);
				kfree(fade_buf);
				kdebug(mod_audio, PRINT_INFO, "recode fade: skip pcm len = %d\n",fade_len);
			}
		}

	}
	JzCbFileWriteFlush(vfile);
	fRecodeWav = 0;
	
	// 线程结束, 关闭文件和设备
	AdcClose(obj->hDac);

	// 设置线程结束标志	
	if(obj->bTerminate || (obj->bClose == 2))
	{	
		msgid = WM_MEDIA_TERMINATE;
		msg.MsgId = msgid;
		msg.hWnd = task->hWnd;
	}
	else if(obj->bClose)
	{	
		msgid = WM_MEDIA_CLOSE;
		msg.MsgId = msgid;
		msg.hWnd = task->hWnd;
	}
	else
	{
		// 非正常结束
		msgid = 0;
		*obj->bReady = -1;
	}
	
	// 设置返回消息参数	
	if(obj->bTerminate == 2)
		msg.Data.v = 1;
	else if(obj->bClose == 2)
		msg.Data.v = 2;
	else
		msg.Data.v = 0;
	
	// 删除对象
	if(obj->bTerminate == 1)
		obj_del = 0;
	else
		JzSrvDestroyNotify(task);
	
	// 发送WM_MEDIA_CLOSE消息
	if(msgid)
	{
		msg.hWndSrc = 0;
		msg.Param = 0;
	#if defined(STC_EXP)
		sGuiMsgPutMsg(&msg);
	#else
		GuiMsgPutMsg(&msg);
	#endif
	}

	if(buf_in)
		kfree(buf_in);
ThreadEnd:	
	// 设置线程结束标志	
	if(!obj_del)
		obj->bTerminate++;

#if defined(STC_EXP)
	sThreadTerminate(sThreadSelf());
#else
	ThreadTerminate(ThreadSelf());
#endif
}
Пример #6
0
/*
 * This function returns the current value of the potentiometer. It is in the same units as the ADC.
 * A 10-bit unsigned integer scaled into units of MAX_IN_VOLTAGE/1024
 */
uint16_t PotRead(void)
{
    uint16_t value = AdcRead(0x0F);
    return value << 6;
}
Пример #7
0
int main()
{
    // Configure the device for maximum performance but do not change the PBDIV
    // Given the options, this function will change the flash wait states, RAM
    // wait state and enable prefetch cache but will not change the PBDIV.
    // The PBDIV value is already set via the pragma FPBDIV option above..
    SYSTEMConfig(F_SYS, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    // Auto-configure the PIC32 for optimum performance at the specified operating frequency.
    SYSTEMConfigPerformance(F_SYS);

    // osc source, PLL multipler value, PLL postscaler , RC divisor
    OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1);

    // Configure the PB bus to run at 1/4th the CPU frequency, so 20MHz.
    OSCSetPBDIV(OSC_PB_DIV_4);

    // Enable multi-vector interrupts
    INTEnableSystemMultiVectoredInt();
    INTEnableInterrupts();

    // Configure Timer 2 using PBCLK as input. We configure it using a 1:16 prescalar, so each timer
    // tick is actually at F_PB / 16 Hz, so setting PR2 to F_PB / 16 / 100 yields a .01s timer.
    OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_16, F_PB / 16 / 100);

    // Set up the timer interrupt with a medium priority of 4.
    INTClearFlag(INT_T2);
    INTSetVectorPriority(INT_TIMER_2_VECTOR, INT_PRIORITY_LEVEL_4);
    INTSetVectorSubPriority(INT_TIMER_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0);
    INTEnable(INT_T2, INT_ENABLED);

/******************************** Your custom code goes below here ********************************/
    int check;
    OledInit();
    AdcInit();
    LEDS_INIT();
    check = GameInit();

    if(check == STANDARD_ERROR) {
        FATAL_ERROR();
    }
    float currPage;
    float binSize;
    float titleSize;
    float descSize;
    float numPages;
    uint8_t roomExit;
    uint16_t adcValue = 0;

    while(1) {
        roomExit = GameGetCurrentRoomExits();
        LEDS_SET(roomExit);
        while(buttonEvents == 0) {
            descSize = GameGetCurrentRoomDescription(roomData.description);
            titleSize = GameGetCurrentRoomTitle(roomData.title);

            numPages = ((titleSize + descSize) / MAX_OLED_PIXELS);
            binSize = (ADC_MAX_VALUE / numPages);

            if(AdcChanged()) {
                adcValue = AdcRead();
            }

            currPage = (adcValue / binSize);
            if(currPage < 1) {
                char titleArray[TITLE_OLED_SPACE] = {0};
                char descriptionBuffer[FIRST_PG_DESCRIPTION_OLED_SPACE] = {0};

                strncpy(descriptionBuffer, roomData.description, DESCRIPTION_COPY);
                sprintf(titleArray, "%s\n%s", roomData.title, descriptionBuffer);

                OledClear(OLED_COLOR_BLACK);
                OledDrawString(titleArray);
            } else {
                char buffer[MAX_OLED_PIXELS] = {0};
                int buffIndex;
                buffIndex = (int)currPage * MAX_OLED_PIXELS;
                strncpy(buffer, (roomData.description + buffIndex - OFFSET), MAX_OLED_PIXELS);

                OledClear(OLED_COLOR_BLACK);
                OledDrawString(buffer);
            }
            OledUpdate();
        }

        if((buttonEvents & BUTTON_EVENT_4UP) && (roomExit & GAME_ROOM_EXIT_NORTH_EXISTS)) {
            GameGoNorth();
        } else if((buttonEvents & BUTTON_EVENT_3UP) && (roomExit & GAME_ROOM_EXIT_EAST_EXISTS)) {
            GameGoEast();
        } else if((buttonEvents & BUTTON_EVENT_2UP) && (roomExit & GAME_ROOM_EXIT_SOUTH_EXISTS)) {
            GameGoSouth();
        } else if((buttonEvents & BUTTON_EVENT_1UP) && (roomExit & GAME_ROOM_EXIT_WEST_EXISTS)) {
            GameGoWest();
        }
        buttonEvents = BUTTON_EVENT_NONE;
    }



/**************************************************************************************************/
    while (1);
}