// Prepare message for battery
void prepare_battery_status(void)
{
	adc_on(true);
	_delay_ms(10);
	uint16_t percentage = bat_percentage(read_battery(), 1100); // 1.1V * 2 cells = 2.2V = min. voltage for RFM12B
	adc_on(false);

	UART_PUTF("Sending battery: %u%%\r\n", percentage);

	// Set packet content
	pkg_header_init_generic_batterystatus_status();
	msg_generic_batterystatus_set_percentage(percentage);
}
예제 #2
0
파일: adc.c 프로젝트: 135u5/dynamiclinker
uint16_t adc_poll(uint8_t ch)
{
   uint16_t ret_val;

   if(mode == DEV_MODE_OFF)
      adc_on();

   if(ch != adc_channel)
      adc_set_channel(ch);
   
   ADCSRA &= ~(1 << ADIE); //disable the AD Interrupt
   ADCSRA |= (1 << ADIF); //clear any old conversions...
   ADCSRA |= (1 << ADEN) | (1 << ADSC); //turn on on and start conversion

   //poll ADSC (clears once conversion is complete)
   while (ADCSRA & (1 << ADSC))
      ;

   ret_val = ADCL;  
   ret_val |= (ADCH << 8);

   if(ch != adc_channel)
      adc_set_channel(adc_channel);
   
   if(mode == DEV_MODE_OFF)
      adc_off();
   
   return ret_val;
}
예제 #3
0
파일: adc.c 프로젝트: 135u5/dynamiclinker
static void adc_set_channel(uint8_t ch)
{
   adc_off();
   ADMUX = ch /*| (1 << REFS0) | (1 << REFS1)*/; //set the channel
   adc_channel = ch;
   adc_on();
}
예제 #4
0
파일: adc.c 프로젝트: 135u5/dynamiclinker
uint16_t adc_read_channel16(uint8_t ch)
{
   uint16_t retval;
   
   //turn on if necessary
   if(mode == DEV_MODE_OFF)
      adc_on();
   
   //set to appropriate channel
   if(ch != adc_channel)
      adc_set_channel(ch);
   
   //start the conversion, enable interrupt 
   ADCSRA |= (1 << ADIF);
   ADCSRA |= (1 << ADSC) | (1 << ADIE) | (1 << ADEN);
   
   // Wait for the conversion interrupt handler to post the semaphore.
   mos_sem_wait(&adc_sem);
   retval = adc_val;
   if(ch != adc_channel)
      adc_set_channel(adc_channel);
   
   if(mode == DEV_MODE_OFF)
      adc_off();
   
   return retval;
}
예제 #5
0
파일: adc.c 프로젝트: 3yc/libopencm3
void adc_setup(void)
{
	int i;

	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);

	/* Make sure the ADC doesn't run during config. */
	adc_off(ADC1);

	/* We configure everything for one single conversion. */
	adc_disable_scan_mode(ADC1);
	adc_set_single_conversion_mode(ADC1);
	adc_enable_discontinous_mode_regular(ADC1);
	adc_disable_external_trigger_regular(ADC1);
	adc_set_right_aligned(ADC1);
	/* We want to read the temperature sensor, so we have to enable it. */
	adc_enable_temperature_sensor(ADC1);
	adc_set_conversion_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);

	adc_on(ADC1);

	/* Wait for ADC starting up. */
	for (i = 0; i < 800000; i++)    /* Wait a bit. */
		__asm__("nop");

	adc_reset_calibration(ADC1);
	adc_calibration(ADC1);
}
예제 #6
0
int main(void) {
	system_init();
	uart_on(0, 115200, NULL);
	adc_on();

	while (1) {
		chenillard(500);
		/* ADC Test */
		adc_display(LPC_ADC_NUM(0), 0);
		TMP36_display(LPC_ADC_NUM(1), 0);
	}
	return 0;
}
예제 #7
0
파일: adc.c 프로젝트: 3yc/libopencm3
int main(void)
{
	u8 channel_array[16];
	u16 temperature;

	rcc_clock_setup_in_hse_16mhz_out_72mhz();
	gpio_setup();
	usart_setup();
	adc_setup();

	gpio_clear(GPIOB, GPIO7);	/* LED1 on */
	gpio_set(GPIOB, GPIO6);		/* LED2 off */

	/* Send a message on USART1. */
	usart_send(USART1, 's');
	usart_send(USART1, 't');
	usart_send(USART1, 'm');
	usart_send(USART1, '\r');
	usart_send(USART1, '\n');

	/* Select the channel we want to convert. 16=temperature_sensor. */
	channel_array[0] = 16;
	adc_set_regular_sequence(ADC1, 1, channel_array);

	/*
	 * If the ADC_CR2_ON bit is already set -> setting it another time
	 * starts the conversion.
	 */
	adc_on(ADC1);

	/* Wait for end of conversion. */
	while (!(ADC_SR(ADC1) & ADC_SR_EOC));

	temperature = ADC_DR(ADC1);

	/*
	 * That's actually not the real temperature - you have to compute it
	 * as described in the datasheet.
	 */
	my_usart_print_int(USART1, temperature);

	gpio_clear(GPIOB, GPIO6); /* LED2 on */

	while(1); /* Halt. */

	return 0;
}
예제 #8
0
int main(void) {
	system_init();
	uart_on(0, 115200, NULL);
	adc_on();

	uprintf(0, "System started\n");
	msleep(5);
	watchdog_config(&wdconf);
	uprintf(0, "Watchdog started\n");

	while (1) {
		watchdog_feed();
		chenillard(50);
		/* ADC Test */
		adc_display(LPC_ADC_NUM(0), 0);
	}
	return 0;
}
예제 #9
0
파일: adc.c 프로젝트: 135u5/dynamiclinker
//TODO: come up with a better way to handle ADC modes
uint8_t dev_mode_DEV_ADC(uint8_t new_mode)
{
   if(mode == new_mode)
      return new_mode;
   
   mode = new_mode;
   
   switch(new_mode) {
   case DEV_MODE_OFF:
      adc_off();
      break;
   case DEV_MODE_IDLE:
      break;
   case DEV_MODE_ON:
      adc_on();
      break;
   }

   return new_mode;
}
예제 #10
0
파일: adxl-335.c 프로젝트: ianhom/snowfort
int adxl_enable(void)
{
	// enable adc
	adc_on();

	ADC12CTL0 |= MSC;
	clock_delay(20000);

	ADC12CTL0 |= REFON; // 1.5V reference

	ADC12CTL1 |= CONSEQ_1; // sequence of channels
	clock_delay(20000);

	// configure adc channels
	ADC12MCTL0 = 0 + SREF_0;	//channel 0, x axis, A1
	ADC12MCTL1 = 1 + SREF_0;	//channel 1, y axis, A2
	ADC12MCTL2 = 2 + SREF_0; //channel 2, z axis, A3
	ADC12MCTL3 = INCH_11 + EOS + SREF_0; // channel 3, (AVcc-AVss)/2

	printf("%d,%d,%d,%d\n",ADC12MCTL0,ADC12MCTL1,ADC12MCTL2,ADC12MCTL3);
	return 1;
}
예제 #11
0
/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{
	unsigned char distance = 0;	// declare a variable to store distance 
	// ensure all the hardware port in zero initially
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;
	PORTD = 0;
	PORTE = 0;

	// Initialize the I/O port direction, this must be configured according to circuit
	// please refer to PTK40A schematic for details
	// TRISX control pin direction, output pin must be configure as '0'
	// while input must be configure as '1'
	TRISA = 0b00010001;
	TRISB = 0b00001111;
	TRISC = 0b10010011;
	TRISD = 0;
	TRISE = 0;

	// Initialize ADC.
	adc_initialize();	//Ensure pin share with analog is being configured to digital correctly
	
	// Initialize LCD
	lcd_initialize();	//Initialize LCD before use
	
	beep(2); 	//buzzer sound for twice
		
	// PTK40A come with an ADC input (RA0), 3 different source can be connected to ADC in
	// There are LM35 temperature sensor, Potentiometer and external sensor
	// This example will use external ADC to read information from SHARP ANALOG DISTANCE SENSOR
	// Connect GND of the sensor to GND of PTK40A (ADC external port)
	// Connect Vcc of sensor to 5V of PTK40A (ADC external port)
	// Connect Vo of senosr to IN of PTK40A (ADC external port)
	// Please refer to PTK40A schematic for the connection
	// Please move JP14 to EXT (under ADC)
	// This example is based on GP2Y0A21 (10 to 80 cm) 	
	adc_on();	//activate ADC module in PIC
		
	LCD_BACKLIGHT = 1;	//activate LCD Backlight
	
	lcd_putstr("Cytron PTK40A");	//display message on LCD
	lcd_2ndline();				//move cursor to 2nd line
	lcd_putstr("DISTANCE:");
	lcd_goto(0X4E);
	lcd_putstr("cm");	// display symbol of cm
	
	while(1) 	// create an infinite loop
	{

		//refer datasheet graph for the convertion calculation
		distance = ui_adc_read()>>2;	// read from Sharp distance sensor and convert to cm
		distance = 256 - distance;
		distance = ((distance*10)+100)/36; //calculation is calculater according to the data sheet graph
		lcd_goto(0x4B);		//move cursor to after DISTANCE: on LCD
		lcd_bcd(3,distance);	// display distance in cm	
		delay_ms(50);
	}	
	
		
	while(1) continue;	// infinite loop to prevent PIC from reset if there is no more program	
}
예제 #12
0
파일: xlayer_batt.c 프로젝트: turon/mantis
void appSend () {
	static uint8_t smpl_cnt = 10;  //number of samples we take to average on each channel
	uint8_t  count = 0;            //temp counter for samples taken
	uint8_t  i;
	uint8_t  nchannels=1;
	uint8_t  channels[1] = {INTERNAL_VOLTAGE};  //use only internal voltage for now  

	uint16_t adc_raw[8];
	uint8_t  ledcount = 0;
	uint8_t  ix;

	char     sendData[12];
	char     testStr[] = "test";
	char     iStr[3];
	uint16_t pause_time = 1000;

	// Avoid signal interference with default channel (26) and wi-fi 
	cc2420_set_channel(24);

	// Set transmit power to low-power mode 
	com_ioctl(IFACE_RADIO, CC2420_LOW_POWER_MODE);

    printf("Opening connection on port %d... \n", TRANSPORT_LISTENING_PORT);
    connect(TRANSPORT_LISTENING_PORT, 0);
    printf("Opened connection on port %d \n", TRANSPORT_LISTENING_PORT);
	
	//mos_thread_sleep(2000);
	while (true) {
		//--- MEASURING BATTERY VOLTAGE ---// 
		
		//mos_led_display( (ledcount++)%8 );  //see explanation in test_adc.c
		adc_on();  //turn on the ADC/Vref
		mos_thread_sleep(20);  //time to wait for the internal reference to settle in theory 

		// Measure each channel, possibly with multiple samples
		for(ix=0; ix<nchannels; ix++) {
			//take 10 samples and average the result
			//split this value out as the final channel reading
			count = 0;  //clear the samples counter
		 	adc_raw[ix]=0;  // clear adc reading accum

            //For this channel, do as many samples as are 'requested'
		    while(count < smpl_cnt) {
		    	adc_raw[ix] += adc_get_conversion16( channels[ix] );
	            count++;  //increment the no. of readings counter
	        }   //end of samples loop for this channel
		} //end channels loop 

		// Average the data for the samples taken on each channel
		for( ix=0; ix<nchannels; ix++ ) {
	    	adc_raw[ix] /= smpl_cnt;
        }

        // Report the results 
	   	//printf("Raw voltage reading = %x\n", adc_raw[0]); 
	    
		adc_off();
	    //mos_thread_sleep(4000);

		
		//--- SENDING DATA ---// 
		
		memset(sendData, 0, sizeof(sendData));
		strcpy(sendData, testStr);
		itoa(i, iStr, 10);
		strcat(sendData, iStr);

		printf("Calling sendPacket() for: %s \n", sendData);
		sendPacket(TRANSPORT_LISTENING_PORT, sendData, sizeof(sendData), 0);

		i++;
		// Reset counter
	    if (i >= 60) {
	    	i = 0;
	    }
		
		// Get raw voltage reading 
		printf("***** Raw voltage reading = %x *****\n", adc_raw[0]); 
		
		// Adjust pause time (send rate) for each specific voltage 
		if (adc_raw[0] > 0x950) {
			pause_time = 1000; 
		} else if (adc_raw[0] > 0x925) {
			pause_time = 1500;
		} else if (adc_raw[0] > 0x900) {
			pause_time = 2000;
		}
		// and so on... 
		// need to find the optimal values ... 
		
		mos_thread_sleep(pause_time); //adaptive send rate 
		
		// Close connection to destination (last) node; port 0
		closeConn(0, 3);
	    printf("Closed connection on port %d \n", TRANSPORT_LISTENING_PORT);
	}
}
예제 #13
0
/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{
	unsigned char step = 0;	// declare a variable to store stepper step sequence
	unsigned int delay = 0;	// declare a variable for stepper delay, need to be 16-bit for > 255
	// ensure all the hardware port in zero initially
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;
	PORTD = 0;
	PORTE = 0;

	// Initialize the I/O port direction, this must be configured according to circuit
	// please refer to PTK40A schematic for details
	// TRISX control pin direction, output pin must be configure as '0'
	// while input must be configure as '1'
	TRISA = 0b00010001;
	TRISB = 0b00001111;
	TRISC = 0b10010011;
	TRISD = 0;
	TRISE = 0;

	// Initialize ADC.
	adc_initialize();	//Ensure pin share with analog is being configured to digital correctly

	// PTK40A come with an ADC input (RA0), 3 different source can be connected to ADC in
	// There are LM35 temperature sensor, Potentiometer and external sensor
	// There is also a stepper motor
	// Please refer to PTK40A schematic for the connection
	// Turning the potentiometer clock wise will change the speed of stepper rotation
	// Please move JP10 to PWM, JP14 to POT, JP20&21 to STEPPER, JP23&24 to UNIPOLAR
	adc_on();	//activate ADC module in PIC
	step = 1;	//initial step is 1
	RC2 = 1;	//RC2 pin is the PWM pin, in stepper case, the PWM is always 1

	while(1) 	// create an infinite loop
	{
		if(step >= 5) step = 1;	// reset the step sequance to 1

		delay = ui_adc_read();
		if (delay > 10) // if delay is larger than 10, enter stepping
		{
			switch (step)
			{
				case 1:
				X = 1;			// step 1
				Y = 0;
				XN = 0;
				YN = 0;
				break;

				case 2:
				X = 0;			// step 2
				Y = 1;
				XN = 0;
				YN = 0;
				break;

				case 3:
				X = 0;			// step 3
				Y = 0;
				XN = 1;
				YN = 0;
				break;

				case 4:
				X = 0;			// step 4
				Y = 0;
				XN = 0;
				YN = 1;
				break;
			}//switch (step)
			delay_ms(delay);
			step ++;	// increase step
		}//if(delay > 10)
	}	// while (1)

	while(1) continue;	// infinite loop to prevent PIC from reset if there is no more program
}
예제 #14
0
/*---------------------------------------------------------------*/
PROCESS_THREAD(null_app_process, ev, data)
{

	static struct etimer rxtimer;

	PROCESS_BEGIN();


	printf("Hello world Started.\n");

#ifdef SF_FEATURE_SHELL_OPT
	serial_shell_init();
	remote_shell_init();
	shell_reboot_init();
	shell_blink_init();
	shell_sky_init();
#endif

	app_conn_open(&nullApp_callback);


#ifdef ADC_SENSOR
	static uint16_t samples[ADC_SAMPLES_PER_FRAME]={0};
	uint8_t i;
//	static uint8_t samples_sorted_bytes[2*ADC_SAMPLES_PER_FRAME];
	static uint8_t sample_num = 0; //increments from 0 to samples_per_frame-1


	if (node_id != 0){
		adc_on();
		//adc_configure(0); //to sample reference voltage (Vref/2), ~2048.
		etimer_set( &rxtimer, (unsigned long)(CLOCK_SECOND/(ADC_SAMPLING_FREQ)));
	}
	else
		etimer_set(&rxtimer,CLOCK_SECOND/20);

	if(node_id != 0)
	{

	  while(1)
	  {

	    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&rxtimer));
	    etimer_reset(&rxtimer);

	    samples[sample_num]=adc_sample();
	    sample_num++;
	    if(sample_num == ADC_SAMPLES_PER_FRAME){
	    	sample_num=0;
	    	/*
	    	 * Byte order needs to be reversed because of low-endian system.
	    	 * Can be done at AP level too, if needed.
	    	 */
	    	// for(i=0;i<ADC_SAMPLES_PER_FRAME;i++){
	    	// 	samples_sorted_bytes[2*i]=(samples[i]>>8);
	    	// 	samples_sorted_bytes[2*i+1]= (samples[i]& 0xff);
	    	// }


	    	//app_conn_send(samples_sorted_bytes,sizeof(uint8_t)*ADC_SAMPLES_PER_FRAME*2);
	    	 tdma_rdc_buf_ptr = 0;
	    	 tdma_rdc_buf_send_ptr = 0;
	    	 tdma_rdc_buf_full_flg = 0;
	    	 app_conn_send(samples,sizeof(uint16_t)*ADC_SAMPLES_PER_FRAME/sizeof(uint8_t));
	    }

	  }
	}
#endif


#ifdef I2C_SENSOR
	//static rtimer_clock_t rt, del;
	int i;
	static uint8_t MPU_status = 0;
	static uint8_t sample_count = 0;
/*
	static uint8_t samples_sorted_bytes[14*MPU_SAMPLES_PER_FRAME],comp_samples_sorted_bytes[14*MPU_SAMPLES_PER_FRAME];
	static uint8_t sample_num=0, uncomp_data_len=14*MPU_SAMPLES_PER_FRAME,comp_data_len;
	static uint8_t *st;
*/


	static mpu_data sampleArray[MPU_SAMPLES_PER_FRAME];

	if (node_id != 0){

		MPU_status = 0;
		for(i = 0; i < 100 & (~MPU_status);i++)
		{
			MPU_status = mpu_enable();
		}

		if (MPU_status == 0)
			printf("MPU could not be enabled.\n");


		MPU_status = 0;
		for(i = 0; i < 100 & (~MPU_status);i++)
		{
			MPU_status = mpu_wakeup();
		}

		if (MPU_status == 0)
			printf("MPU could not be awakened.\n");

		etimer_set(&rxtimer, (unsigned long)(CLOCK_SECOND/MPU_SAMPLING_FREQ));
		}
	else
		etimer_set(&rxtimer,CLOCK_SECOND/20);

	if(node_id != 0)
	{

		while(1){
			PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&rxtimer));
			etimer_reset(&rxtimer);

			mpu_data_union samples;
			int m=mpu_sample_all(&samples);
			app_conn_send((uint8_t *)(&samples),MPU_DATA_SIZE);
/*
			sampleArray[sample_count] = samples.data;

			sample_count = sample_count + 1;

			if(sample_count == MPU_SAMPLES_PER_FRAME)
			{
				sample_count = 0;
				tdma_rdc_buf_clear();

				app_conn_send(sampleArray,MPU_DATA_SIZE*MPU_SAMPLES_PER_FRAME);

			}
*/
/*
			st = &samples;
			for(i=0;i<7;i++){
				samples_sorted_bytes[2*i+14*sample_num]=*(st+2*i+1);
				samples_sorted_bytes[2*i+1+14*sample_num]= *(st+2*i);
			}
			sample_num++;

			if(sample_num==MPU_SAMPLES_PER_FRAME){
				sample_num=0;

				app_conn_send(samples_sorted_bytes,sizeof(uint8_t)*14*MPU_SAMPLES_PER_FRAME);
			}

			PRINTF("%d,%d,%d,%d,%d,%d,%d\n",samples.data.accel_x,samples.data.accel_y,samples.data.accel_z,samples.data.gyro_x,samples.data.gyro_y,
				samples.data.gyro_z,samples.data.temperature);
*/
//			app_conn_send(&samples,sizeof(mpu_data)/sizeof(uint8_t));
		}
	}

#endif

	PROCESS_END();

}
예제 #15
0
static void setup_stm32f1_peripherals(void)
{
	rcc_peripheral_enable_clock(&RCC_APB1ENR, 
			RCC_APB1ENR_I2C1EN);

	rcc_peripheral_enable_clock(&RCC_APB2ENR, 
			RCC_APB2ENR_ADC1EN);

	/* GPIO pin for I2C1 SCL, SDA */

	/* VESNA v1.0
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
			GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO6);
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
			GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO7);
	*/

	/* VESNA v1.1 */
	AFIO_MAPR |= AFIO_MAPR_I2C1_REMAP;
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
			GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, TDA_PIN_SCL);
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
			GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, TDA_PIN_SDA);

	/* GPIO pin for TDA18219 IRQ */
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
			GPIO_CNF_INPUT_FLOAT, TDA_PIN_IRQ);

	/* GPIO pin for TDA18219 IF AGC */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
			GPIO_CNF_OUTPUT_PUSHPULL, TDA_PIN_IF_AGC);
	/* Set to lowest gain for now */
	gpio_clear(GPIOA, TDA_PIN_IF_AGC);

	/* GPIO pin for AD8307 ENB */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
			GPIO_CNF_OUTPUT_PUSHPULL, TDA_PIN_ENB);

	/* ADC pin for AD8307 output */
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
			GPIO_CNF_INPUT_ANALOG, TDA_PIN_OUT);

	/* Setup I2C */
	i2c_peripheral_disable(I2C1);

	/* 400 kHz - I2C Fast Mode */
	i2c_set_clock_frequency(I2C1, I2C_CR2_FREQ_24MHZ);
	i2c_set_fast_mode(I2C1);
	/* 400 kHz */
	i2c_set_ccr(I2C1, 0x14);
	/* 300 ns rise time */
	i2c_set_trise(I2C1, 0x08);

	i2c_peripheral_enable(I2C1);


	/* Make sure the ADC doesn't run during config. */
	adc_off(ADC1);

	/* We configure everything for one single conversion. */
	adc_disable_scan_mode(ADC1);
	adc_set_single_conversion_mode(ADC1);
	adc_enable_discontinous_mode_regular(ADC1);
	adc_disable_external_trigger_regular(ADC1);
	adc_set_right_aligned(ADC1);
	adc_set_conversion_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);

	adc_on(ADC1);

	/* Wait for ADC starting up. */
	int i;
	for (i = 0; i < 800000; i++)    /* Wait a bit. */
		__asm__("nop");

	adc_reset_calibration(ADC1);
	adc_calibration(ADC1);

	uint8_t channel_array[16];
	/* Select the channel we want to convert. */
	if(TDA_PIN_OUT == GPIO0) {
		channel_array[0] = 0;
	} else if(TDA_PIN_OUT == GPIO2) {
		channel_array[0] = 2;
	}
	adc_set_regular_sequence(ADC1, 1, channel_array);
}