Beispiel #1
0
int main(void) {
	char str[15];
	//Initialize system
	SystemInit();
	//Initialize Delay library
	TM_DELAY_Init();
	
	//Initialize USART1-> TX: PA9, RX: PA10
	TM_USART_Init(USART1, TM_USART_PinsPack_1, 9600);
	
	TM_USART_Puts(USART1, "ADC example");
	
	//Initialize ADC1 on channel 0, this is pin PA0
	TM_ADC_Init(ADC1, ADC_Channel_0);
	//Initialize ADC1 on channel 3, this is pin PA3
	TM_ADC_Init(ADC1, ADC_Channel_3);
	
	while (1) {
		//							Read ADC1 Channel0					Read ADC1 Channel3
		sprintf(str, "%d: %d\n\r", TM_ADC_Read(ADC1, ADC_Channel_0), TM_ADC_Read(ADC1, ADC_Channel_3));
		TM_USART_Puts(USART1, str);
		
		Delayms(100);
	}
}
int soilhumd_get(void *data)
{
	static uint32_t initialized = 0;
	int ret = 0;
	
	if (!initialized) {
		initialized = 1;
		/* Initialize ADC1 on channel 0, this is pin PA0 */
		TM_ADC_Init(ADC1, ADC_Channel_0);
		TM_ADC_Read(ADC1, ADC_Channel_0);
		while (ret < 0xFFFF) ret++;
		}
	ret = TM_ADC_Read(ADC1, ADC_Channel_0);
	printf("soilhumd get : %d\n", ret);
	return ret;
}
uint16_t TM_ADC_ReadVbat(ADC_TypeDef* ADCx) {
	uint32_t result;
	
	/* Read battery voltage */
	result = TM_ADC_Read(ADCx, (TM_ADC_Channel_t) ADC_CHANNEL_VBAT);
	
	/* Convert to voltage */
	result = result * ADC_VBAT_MULTI * ADC_SUPPLY_VOLTAGE / 0xFFF;
	
	/* Return value in mV */
	return (uint16_t) result;
}
int light_get(void *data)
{
	static uint32_t initialized = 0;
	int ret;
	if (!initialized) {
		initialized = 1;
		/* Initialize ADC1 on channel 0, this is pin PA0 */
		TM_ADC_Init(ADC1, ADC_Channel_3);
		}
	ret = TM_ADC_Read(ADC1, ADC_Channel_3);
	printf("light val: %d\n", ret);
	return ret;
}
Beispiel #5
0
static uint16_t voltage_readADC1( TM_ADC_Channel_t ch ){
	//_D(("-- %d\n", TM_ADC_Read( ADC1, ch )));
	return TM_ADC_Read( ADC1, ch );
}
Beispiel #6
0
int main(void) {

    int accelData[3];
    int analogData[BUFFER];
    int i=0;
    for(i=0;i<BUFFER;i++){ analogData[i]=0;	}
    int a = 0;
    int analogIn = 0;
    int analogMin, analogMax;

    /* Initialize system */
    SystemInit();

    /* Initialize delay */
    //TM_DELAY_Init();

    /* Initialize PG13 (GREEN LED) and PG14 (RED LED) */
    TM_GPIO_Init(GPIOG, GPIO_PIN_13 | GPIO_PIN_14, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Fast);
    TM_GPIO_SetPinValue(GPIOG, GPIO_PIN_14, 1); // Red: ON

#ifdef ENABLE_USART
    /* Initialize USART1 at 115200 baud, TX: PA10, RX: PA9 */
    TM_USART_Init(USART1, TM_USART_PinsPack_1, 115200);
#endif

#ifdef ENABLE_VCP
    /* Initialize USB Virtual Comm Port */

    TM_USB_VCP_Result status = TM_USB_VCP_NOT_CONNECTED;
    while (TM_USB_VCP_GetStatus() != TM_USB_VCP_CONNECTED) {
    	TM_USB_VCP_Init();
    	TM_GPIO_TogglePinValue(GPIOG, GPIO_PIN_14);
    	Delay(500000);
    }
    SendString("USB VCP initialized and connected\n");
    TM_GPIO_TogglePinValue(GPIOG, GPIO_PIN_14 | GPIO_PIN_13); // Red: OFF, Gr: ON

#endif

#ifdef ENABLE_MMA

    /* Initialize MMA845X */
    uint8_t mma_status = MMA845X_Initialize(MMA_RANGE_4G);
    if (mma_status == MMA_OK) {
    	SendString("MMA initialized\n");
    } else {
    	SendString("MMA initialization failed, error code: ");
    	// Add 48 to the byte value to have character representation, (48 = '0')
    	SendChar('0'+mma_status);
    	SendChar('\n');
    }

#endif

    /* Initialize Display */
	TM_ILI9341_Init();
	TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_1);
	TM_ILI9341_SetLayer1();
	TM_ILI9341_Fill(ILI9341_COLOR_BLACK); /* Fill data on layer 1 */

	/* Initialize ADC1 */
	TM_ADC_Init(CURRENT_ADC, CURRENT_CH);

	/* Initialize PE2 and PE3 for digital output (Motor direction) */
    TM_GPIO_Init(GPIOE, GPIO_PIN_2 | GPIO_PIN_3, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Fast);
    // Set them to HIGH/LOW
    TM_GPIO_SetPinHigh(GPIOE, GPIO_PIN_3);
    TM_GPIO_SetPinLow(GPIOE, GPIO_PIN_2);

#ifdef ENABLE_PWM
    /* Set up PE5 (in front of PE4) for PWM (TIM9 CH1 PP2) (Motor speed control) */
    TM_PWM_TIM_t TIM9_Data;
    // Set PWM to 1kHz frequency on timer TIM4, 1 kHz = 1ms = 1000us
	TM_PWM_InitTimer(TIM9, &TIM9_Data, 1000);
	// Initialize PWM on TIM9, Channel 1 and PinsPack 2 = PE5
	TM_PWM_InitChannel(&TIM9_Data, TM_PWM_Channel_1, TM_PWM_PinsPack_2);
	// Set channel 1 value, 50% duty cycle
	TM_PWM_SetChannelPercent(&TIM9_Data, TM_PWM_Channel_1, 50);
#endif

	/* Initialize DAC channel 2, pin PA5 (Shaker control) */
	//TM_DAC_Init(TM_DAC2);
	/* Set 12bit analog value of 2047/4096 * 3.3V */
	//TM_DAC_SetValue(TM_DAC2, 4096);

#ifdef ENABLE_DAC
	// DAC PIN PA5
	/* Initialize DAC1, use TIM4 for signal generation */
	TM_DAC_SIGNAL_Init(TM_DAC2, TIM4);
	/* Output predefined triangle signal with frequency of 5kHz */
	TM_DAC_SIGNAL_SetSignal(TM_DAC2, TM_DAC_SIGNAL_Signal_Sinus, 50);
#endif

	/* MAIN LOOP */
    while (1) {

    	// Read acceleration data
#ifdef ENABLE_MMA
		MMA845X_ReadAcceleration(accelData);
#endif

		// Read analog input
		analogData[a] = TM_ADC_Read(CURRENT_ADC, CURRENT_CH);
		a++;
		if(a==BUFFER) {a=0;}

		// Analog average
		analogIn=0;
		analogMax=0;
		analogMin=4096;
		for(i=0;i<BUFFER;i++){
			if(analogData[i] > analogMax) { analogMax = analogData[i]; }
			if(analogData[i] < analogMin) { analogMin = analogData[i]; }
			analogIn+=analogData[i];
		}
		analogIn/=BUFFER;

		// Print graphs
		printGraphsLCD(accelData, analogData[a], analogIn, analogMin, analogMax);

		// Toggle Green led
		TM_GPIO_TogglePinValue(GPIOG, GPIO_PIN_13);

    }
}