Esempio n. 1
0
unsigned short SampleFvrForVdd(void)
{
	unsigned short result = 0;
	unsigned char i;
	CVRCON2bits.FVREN = 1;	// FVR on
	Delay100us();			// Wait for stability
	Delay100us();
	InitAnalog(); 			// adc to default
	ADCON1 = 0b00000000; 	// vdd used for reference
	ADCON2 = 0b10101011; 	// Right Justified, 12Tad sample time, IntRC, 20us Taq
	ADCON0 = 0b00000001; 	// adc on
	ADCON0 |= (FVR_CHANNEL<<2);	// adc channel

	// Sample multiple times
	for(i=0;i<64;i++)
	{
		// Sample sensor - ~30us * 64
		ADCON0bits.GO = 1;
		while (ADCON0bits.GO){;}
		result += ((unsigned short)ADRESH<<8) + ADRESL;
	}

	CVRCON2bits.FVREN = 0; 	// FVR off
	InitAnalog(); 			// Put everything back (Off)
	return result;
}
Esempio n. 2
0
uint8_t LCD1602_WaitBusy()
{
	uint16_t timeout=0;
	
	Delay2ms();

	LCD1602_RS_L;
	LCD1602_RW_H;
	Delay100us();
	LCD1602_EN_H;
	
	while(LCD1602_DB7_READ)//缁涘绶�602鏉╂柨娲栨担搴f暩楠烇拷
	{
		timeout++;
		Delay1us();
		if(timeout>200)
		{
			LCD1602_EN_L;
			return 1;//Still Busy
		}
	}	
	LCD1602_EN_L;

return 0;
}
Esempio n. 3
0
unsigned short SampleLight(void)
{
	// Assembles 16bit ADC value using oversampling
	// Specially delayed to get ~10ms average for 
	// fluorescent lamp compensation @ 50Hz
	unsigned char i;
	unsigned long sum = 0;
	InitAnalog(); 					// adc to default
	ADCON0 = 0b00000001; 			// adc on
	ADCON0 |= (LIGHT_CHANNEL<<2); 	// adc channel
	ADCON0bits.GO = 1;
	// Sample multiple times
	for(i=0;i<64;i++)
	{
		// Sample sensor - ~30us
		ADCON0bits.GO = 1;
		while (ADCON0bits.GO){;}
		sum += ((unsigned short)ADRESH<<8) + ADRESL;
		// Sample sensor - ~30us
		ADCON0bits.GO = 1;
		while (ADCON0bits.GO){;}
		sum += ((unsigned short)ADRESH<<8) + ADRESL;
		// Additional delay - 100us
		Delay100us();
		// Total delay = 64 * 160us = 10.2ms
	}
	// Discard 1 bit (10bits * 64 sample = 16bits)
	sum >>= 1;	
	// Turn off ADC
	ADCON0 = 0b00000000; 	// adc off
	// Return result
	return (sum & 0xffff);
}
Esempio n. 4
0
unsigned char TPSendByte(unsigned char u8Data) {
    unsigned char i, ack;
    unsigned char u8p = 1;
#ifdef TP_DEBUG_LEVEL_1
    DebugStrHex1R("TP:<", u8Data);
#endif

    TPSetClk(0);
    Delay100us();
    TPSetData(0);
    TPSetClk(1);

    for (i = 0; i < 8; i++) {
        TPWaitClock(0);
        TPSetData(u8Data & 0x01);
        u8p += u8Data;
        TPWaitClock(1);
        u8Data >>= 1;
    }
    TPWaitClock(0);
    TPSetData(u8p & 0x01);
    TPWaitClock(1);
    TPWaitClock(0);
    TPSetData(1);
    TPWaitClock(1);
    TOUCH_PAD_DATA_TRIS = 1;
    TPWaitClock(0);

    if (TOUCH_PAD_DATA_PORT == 1) {
#ifdef TP_DEBUG_LEVEL_0
        Debug("L");
#endif
        STP.TPModule.ErrorState.b1TxLErr = 1;
        return 0;
    }
    TPWaitClock(1);
    ack = TPGetByte();
    if (ack != 0xFA) {
        STP.TPModule.ErrorState.b1TxAErr = 1;
        return 0;
    }
    return 1;
}
Esempio n. 5
0
File: main.c Progetto: sebseb7/lun1k
int main(void)
{
	RCC_ClocksTypeDef RCC_Clocks;


	RCC_GetClocksFreq(&RCC_Clocks);
	/* SysTick end of count event each 0.1ms */
	SysTick_Config(RCC_Clocks.HCLK_Frequency / 10000);

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	
	
	//prepare init structure
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
/*
 *      10   11   12
 *
 * E    C7   C5   B12
 * DC   A7   A7   A7
 * RW   C9   C9   C9
 * RST  A6   A6   A6
 * D0   C4   B15  B15
 * D1   C5   B0   B0
 * D2   B0   B1   B1
 * D3   B1   B2   B2
 * D4   B15  B14  B14
 * D5   B14  B13  B13
 * D6   B13  B12  C5
 * D7   B12  C6   C6
 *
 * v11 : E,D0..D7 swapped
 * v12 : E and D6 swapped
 *
 */

	
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_0;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
#if LUN1K_VERSION >= 11
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
#endif
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_12;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_14;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_15;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
#if LUN1K_VERSION == 10
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_4;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
#endif
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_5;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
#if LUN1K_VERSION >= 11
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
#endif
#if LUN1K_VERSION == 10
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
#endif
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	//leds
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2;       
	GPIO_Init(GPIOD, &GPIO_InitStructure);  
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;       
	GPIO_Init(GPIOC, &GPIO_InitStructure);  
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3;       
	GPIO_Init(GPIOC, &GPIO_InitStructure);  
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3;       
	GPIO_Init(GPIOB, &GPIO_InitStructure);  

	//regen
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;       
	GPIO_Init(GPIOA, &GPIO_InitStructure);  


	//ESC Button
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;       
	GPIO_Init(GPIOB, &GPIO_InitStructure);  
	
	//stick button
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13;       
	GPIO_Init(GPIOC, &GPIO_InitStructure);  
	//A
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_4;       
	GPIO_Init(GPIOB, &GPIO_InitStructure);  
	//B
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_0;       
	GPIO_Init(GPIOA, &GPIO_InitStructure);  
	buttonsInitialized=1;
		


	//set RW to 0
	GPIO_ResetBits(GPIOC,GPIO_Pin_9);	

	// 12V power
	GPIOA->ODR           |=       1<<1;


	RNG_Enable();
	srand(RNG_Get());
	RNG_Disable();

	adc_a3_init();

	lcdInit();
	
	n35p112_init();

	
	int current_animation = 0;
	animations[current_animation].init_fp();
	int tick_count = 0;
	

	int loopcount = 0;

	uint8_t count  = 0;
		
	int16_t bat_voltage = adc_a3_get();

	float voltagesample = bat_voltage;

	while(1)
	{
		loopcount++;
		if((loopcount == 55)||(loopcount == 57))
		{
			GPIOC->ODR           |=       1<<1;
			GPIOD->ODR           |=       1<<2;
			GPIOB->ODR           |=       1<<3;
			GPIOC->ODR           |=       1<<3;
		}
		if((loopcount == 56)||(loopcount == 58))
		{
			GPIOC->ODR           &=       ~(1<<1);
			GPIOD->ODR           &=       ~(1<<2);
			GPIOB->ODR           &=       ~(1<<3);
			GPIOC->ODR           &=       ~(1<<3);
			if(loopcount==58)
				loopcount = 0;
		}
		
		uint32_t start_tick = tick;

		get_n35p112(&joy_x,&joy_y);

		animations[current_animation].tick_fp();
/*		int16_t bat_voltage = adc_a3_get();

		fill_8x6(20,20, 5,0,0,0);
		fill_8x6(20,30, 5,0,0,0);
		draw_number_8x6(20,20, bat_voltage, 5, ' ' ,255,255,255);

		voltagesample = voltagesample * 0.98f;
		voltagesample += bat_voltage * 0.02f;


		float tmp2 = voltagesample / (4096.0f / 3.3f);

		float tmp3 = tmp2 * 2.14f;

		draw_number_8x6(20,30, tmp3*1000, 5, ' ' ,255,255,255);

		setLedXY(bat_voltage-2364, count,255,0,0);*/

		lcdFlush();

		uint32_t duration = tick - start_tick;

		//draw_number_8x6(20,20, animations[current_animation].timing - duration, 6, ' ' ,255,255,255);


		if(animations[current_animation].timing > 0)
			Delay100us(animations[current_animation].timing - duration);
	

//		draw_number_8x6(20,30, joy_y, 3, ' ' ,255,255,255);

//		draw_filledCircle(joy_y>>1,joy_x>>1,8,0,0,0);
//		draw_filledCircle(joy_y>>1,joy_x>>1,5,255,255,255);


		count++;
		if(count > 128)
			count=0;

		tick_count++;


		if(get_key_press(KEY_ESC))
		{
			animations[current_animation].deinit_fp();

			current_animation++;
			if(current_animation == animationcount)
			{
				current_animation = 0;
			}
			tick_count=0;
	
			lcdFillRGB(0,0,0);

			animations[current_animation].init_fp();

			//usb_printf("diff: %i , %i (%i) (%i %i %i %i %i %i %i)\n",diff,int_status,i2c_errors,i2c_e[0],i2c_e[1],i2c_e[2],i2c_e[3],i2c_e[4],i2c_e[5],i2c_e[6]);

		}
	}

}
Esempio n. 6
0
unsigned short SampleHumidity(void)
{
/*
	KL 2013: The sensor used is a resisitive element type. This uses a thin
	and fragile metal oxide layer on a ceramic substrate. The layer is easilly 
	and permanently damages by DC current. To compensate this a high pass or 
	dc blocking capacitor is used. One side of the capacitor is energised to 
	a reference voltage briefly. The capacitor charges through the sensors
	resistance and a second series sense resistance. The induced voltage is
	amplified and measured by the ADC. The amplifier is a programmable gain
	amplifier (PGA) with gains of x1,x10 and x50. Measurements must be taken 
	close to the start of the charge transient otherwise the capacitor voltage
	will have risen too high and the circuit will produce unreliable results.
	A 100us delay has been added to allow the amplifier gain to settle.
	Multiple samples are added to reduce noise and increase resolution.
*/
	#define HUMIDITY_LIMIT (1022ul * 8) /*Could be 1023 * 8 but we must catch OOR errors*/

	unsigned char i;
	unsigned long sumHumid;

	// Sample humidity sensor during the transients
	ADCON0 = 0b00000001; 	// adc on
	ADCON0 |= (HUMID_CHANNEL<<2); // adc channel

	HUMIDITY_GAIN_SEL_50();
	Delay100us();
	sumHumid = 0;
	for(i=0;i<8;i++)
	{
		// Pulse sensor output
		HUMIDITY_PULSE = 1;
		// Sample sensor - ~30us
		ADCON0bits.GO = 1;
		while (ADCON0bits.GO){;}
		// Clear pulse
		HUMIDITY_PULSE = 0;	
		// Get values
		sumHumid += ((unsigned short)ADRESH<<8) + ADRESL;
		// Wait for re-settling
		DelayMs(1);
	}
	if(sumHumid < HUMIDITY_LIMIT)
	{
		// Done...
	}
	else // Out of sensor dynamic range, lower gain
	{
		HUMIDITY_GAIN_SEL_10();
		Delay100us();
		sumHumid = 0;
		for(i=0;i<8;i++)
		{
			// Pulse sensor output
			HUMIDITY_PULSE = 1;
			// Sample sensor - ~30us
			ADCON0bits.GO = 1;
			while (ADCON0bits.GO){;}
			// Clear pulse
			HUMIDITY_PULSE = 0;	
			// Get values
			sumHumid += ((unsigned short)ADRESH<<8) + ADRESL;
			// Wait for re-settling
			DelayMs(1);
		}
		if(sumHumid < HUMIDITY_LIMIT)
		{
			// Correct for additional PGA gain
			sumHumid*=5;	// Note: Max value of 40,960
		}
		else // Still out of range
		{
			// Sample at x10 amplification first with settle time for PGA
			HUMIDITY_GAIN_SEL_1();
			Delay100us();
			sumHumid = 0;
			for(i=0;i<8;i++)
			{
				// Pulse sensor output
				HUMIDITY_PULSE = 1;
				// Sample sensor - ~30us
				ADCON0bits.GO = 1;
				while (ADCON0bits.GO){;}
				// Clear pulse
				HUMIDITY_PULSE = 0;	
				// Get values
				sumHumid += ((unsigned short)ADRESH<<8) + ADRESL;
				// Wait for re-settling
				DelayMs(1);
			}
			// In this mode it can't be out of range but the multiply will break the short
			sumHumid*=50;
		}	
	}
	// Adc and PGA off
	HUMIDITY_GAIN_SEL_10(); // Lowest power
	ADCON0 = 0b00000000; 	// Adc off
	return (sumHumid+4)>>3;	// Set back to 10 bit range
}