Ejemplo n.º 1
0
void tsReadZ(uint32_t* z1, uint32_t* z2)
{
  if (!_tsInitialised) tsInit();

  // Make sure that X+/Y- are set to GPIO
  TS_XP_FUNC_GPIO;
  TS_YM_FUNC_GPIO;

  // Set X- and Y+ to inputs (necessary?)
  gpioSetDir (TS_XM_PORT, TS_XM_PIN, 0);
  gpioSetDir (TS_YP_PORT, TS_YP_PIN, 0);

  // Set X+ and Y- to output
  gpioSetDir (TS_XP_PORT, TS_XP_PIN, 1);
  gpioSetDir (TS_YM_PORT, TS_YM_PIN, 1);

  // X+ goes low, Y- goes high
  gpioSetValue(TS_XP_PORT, TS_XP_PIN, 0);   // GND
  gpioSetValue(TS_YM_PORT, TS_YM_PIN, 1);   // 3.3V

  // Set X- and Y+ to ADC
  TS_XM_FUNC_ADC;  
  TS_YP_FUNC_ADC;  

  // Get ADC results
  *z1 = adcRead(TS_YP_ADC_CHANNEL);     // Z1 (Read Y+)
  *z2 = adcRead(TS_XM_ADC_CHANNEL);     // Z2 (Read X-)
}
Ejemplo n.º 2
0
int checkIfBalanced(){

    int xValue, yValue, zValue;
    float angle;
    //Read the X axis
    adcInit(ACCELEROMETER_X);
    xValue = adcRead();

    //Read the Y axis
    adcInit(ACCELEROMETER_Y);
    yValue = adcRead();

    //Read the Z axis
    adcInit(ACCELEROMETER_Z);
    zValue = adcRead();

    angle = computePitch(xValue, yValue, zValue);

    //check what the angle is and if we need to go forward or backward
    if(angle > FORWARD_THRESHOLD){
        motorForward();
        return FALSE;
    }else if(angle < REVERSE_THRESHOLD){
        motorReverse();
        return FALSE;
    }else{
        motorStop();
        return TRUE;
    }
}
Ejemplo n.º 3
0
void readSensors(DataPacket_t *packet)
{
    PRINTF("reading sensors...\n");
    uint32_t start = getJiffies();

    ledOn();
    humidityOn();

    packet->timestamp = getUptime();
    if (!islRead(&packet->islLight, true)) {
        PRINT("islRead failed\n");
        packet->islLight = 0xffff;
    // } else {
    //     PRINT("islRead OK\n");
    }
    packet->internalVoltage = adcRead(ADC_INTERNAL_VOLTAGE);
    packet->internalTemperature = adcRead(ADC_INTERNAL_TEMPERATURE);
    packet->sht75Humidity = humidityRead();
    packet->sht75Temperature = temperatureRead();

    humidityOff();
    ledOff();

    uint32_t end = getJiffies();
    PRINTF("   time spent: %u ms\n", end - start);
}
Ejemplo n.º 4
0
void tsReadZ(uint32_t* z1, uint32_t* z2)
{
  if (!_tsInitialised) tsInit();

  // XP = ADC
  // XM = GPIO Output Low
  // YP = GPIO Output High
  // YM = GPIO Input

  TS_XM_FUNC_GPIO;
  TS_YP_FUNC_GPIO;
  TS_YM_FUNC_GPIO;

  gpioSetDir (TS_XM_PORT, TS_XM_PIN, 1);
  gpioSetDir (TS_YP_PORT, TS_YP_PIN, 1);
  gpioSetDir (TS_YM_PORT, TS_YM_PIN, 0);

  gpioSetValue(TS_XM_PORT, TS_XM_PIN, 0);   // GND
  gpioSetValue(TS_YP_PORT, TS_YP_PIN, 1);   // 3.3V

  TS_XP_FUNC_ADC;
  *z1 = adcRead(TS_XP_ADC_CHANNEL);

  // XP = GPIO Input
  // XM = GPIO Output Low
  // YP = GPIO Output High
  // YM = ADC

  TS_XP_FUNC_GPIO;
  gpioSetDir (TS_YM_PORT, TS_YM_PIN, 0);

  TS_YM_FUNC_ADC;
  *z2 = adcRead(TS_YM_ADC_CHANNEL);
}
void main()
{
unsigned int adc_xval,adc_yval,adc_zval;
ADCInit();
lcd_init();
while(1)
{
adc_xval=adcRead(0);
lcd_gotoxy1(1);
lcd_showvalue(adc_xval);

adc_yval=adcRead(1);
lcd_gotoxy1(4);
lcd_showvalue(adc_yval);

adc_zval=adcRead(2);
lcd_gotoxy1(7);
lcd_showvalue(adc_zval);



}


}
Ejemplo n.º 6
0
uint32_t tsReadY(void)
{
  if (!_tsInitialised) tsInit();

  lcdOrientation_t orientation;
  orientation = lcdGetOrientation();

  if (orientation == LCD_ORIENTATION_LANDSCAPE)
  {
    // Make sure Y+/Y- are set to GPIO
    TS_YP_FUNC_GPIO;
    TS_YM_FUNC_GPIO;
  
    // Set X- and X+ to inputs
    gpioSetDir (TS_XM_PORT, TS_XM_PIN, 0);
    gpioSetDir (TS_XP_PORT, TS_XP_PIN, 0);
    
    // Set Y- and Y+ to output
    gpioSetDir (TS_YM_PORT, TS_YM_PIN, 1);
    gpioSetDir (TS_YP_PORT, TS_YP_PIN, 1);
  
    // Y+ goes high, Y- goes low
    gpioSetValue(TS_YP_PORT, TS_YP_PIN, 1);   // 3.3V
    gpioSetValue(TS_YM_PORT, TS_YM_PIN, 0);   // GND
  
    // Set pin 1.0 (X+) to ADC1
    TS_XP_FUNC_ADC;  
  
    // Return the ADC results
    return adcRead(TS_XP_ADC_CHANNEL);
  }
  else
  {
    // Make sure X+/X- are set to GPIO
    TS_XP_FUNC_GPIO;
    TS_XM_FUNC_GPIO;
  
    // Set Y- and Y+ to inputs
    gpioSetDir (TS_YM_PORT, TS_YM_PIN, 0);
    gpioSetDir (TS_YP_PORT, TS_YP_PIN, 0);
    
    // Set X- and X+ to output
    gpioSetDir (TS_XM_PORT, TS_XM_PIN, 1);
    gpioSetDir (TS_XP_PORT, TS_XP_PIN, 1);
  
    // X+ goes high, X- goes low
    gpioSetValue(TS_XP_PORT, TS_XP_PIN, 1);   // 3.3V
    gpioSetValue(TS_XM_PORT, TS_XM_PIN, 0);   // GND
  
    // Set pin 0.11 (Y+) to ADC0
    TS_YP_FUNC_ADC;
  
    // Return the ADC results
    return adcRead(TS_YP_ADC_CHANNEL);
  }
}
Ejemplo n.º 7
0
Archivo: main.c Proyecto: atiselsts/osw
void readSensors(DataPacket_t *packet)
{
    DPRINTF("reading sensors...\n");

    ledOn();
    humidityOn();

    packet->timestamp = getJiffies();
    packet->sourceAddress = localAddress;
    packet->dataSeqnum = ++dataSeqnum;

    if (localAddress != 0x0796) {
        if (!islRead(&packet->islLight, true)) {
            PRINT("islRead failed\n");
            packet->islLight = 0xffff;
        }
        packet->sq100Light = 0xffff;
    } else {
        packet->islLight = 0xffff;
        if (!readAds(&packet->sq100Light)) {
            PRINT("readAdsRegister failed\n");
            packet->sq100Light = 0xffff;
        }
    }

    packet->internalVoltage = adcRead(ADC_INTERNAL_VOLTAGE);
    packet->internalTemperature = adcRead(ADC_INTERNAL_TEMPERATURE);

    DPRINT("read hum\n");
    packet->sht75Humidity = humidityRead();
    packet->sht75Temperature = temperatureRead();
    DPRINT("read done\n");

    packet->crc = crc16((uint8_t *) packet, sizeof(*packet) - 2);

#if WRITE_TO_FLASH
    if (extFlashAddress < EXT_FLASH_SIZE) {
        DPRINT("Writing to flash\n");
        extFlashWrite(extFlashAddress, packet, sizeof(*packet));
        DataPacket_t verifyRecord;
        memset(&verifyRecord, 0, sizeof(verifyRecord));
        extFlashRead(extFlashAddress, &verifyRecord, sizeof(verifyRecord));
        if (memcmp(packet, &verifyRecord, sizeof(verifyRecord))) {
            ASSERT("writing in flash failed!" && false);
        }
        extFlashAddress += sizeof(verifyRecord);
    }
#endif

    humidityOff();
    ledOff();
}
Ejemplo n.º 8
0
void analyze_cmd(char *c) {
	unsigned int len = strlen(c);
	unsigned int i = 0;

	if (c[0] == '!') {
		switch(c[1]) {
		case 'V':
			// a V found
			if (c[2] == '?') {
				// voltage query
				adcRead(0);
				uartTxInt(g_adcValue);
				uartTxChar("\n");				// REMOVE this when using only python app
			}
			else if (c[2] == '=') {
				// voltage output command
				volatile unsigned int dacVoltage = 0;
				volatile unsigned int digit;
				volatile unsigned int place = 1;		// one's place

				for(i = 6; i > 2; i--) {
					digit = (c[i] - 0x30) * place;
					dacVoltage = dacVoltage + digit;
					place = place*10;					// move up a tens place
				}
				dacSend(dacVoltage);
			}
			else
				uartTxChar("\nInvalid V-option\n");
			break;
		case 'C':
			if (c[2] == '?') {
				;								// current query, read from ADC channel 2
				adcRead(2);
				uartTxInt(g_adcValue);
				uartTxChar("\n");				// REMOVE this when using only python app
			}
			else
				uartTxChar("\nInvalid C-option\n");
			break;
		default:
			uartTxChar("Unknown Command");
			break;
		}
	}
	else
		uartTxChar("\nMissing !\n");

}
Ejemplo n.º 9
0
uint32_t tsReadY(void)
{
  if (!_tsInitialised) tsInit();

  // YP = GPIO Output High
  // YM = GPIO Output Low
  // XP = GPIO Input
  // XM = ADC

  TS_YP_FUNC_GPIO;
  TS_YM_FUNC_GPIO;
  TS_XP_FUNC_GPIO;

  gpioSetDir (TS_YP_PORT, TS_YP_PIN, 1);
  gpioSetDir (TS_YM_PORT, TS_YM_PIN, 1);
  gpioSetDir (TS_XP_PORT, TS_XP_PIN, 0);

  gpioSetValue(TS_YP_PORT, TS_YP_PIN, 1);   // 3.3V
  gpioSetValue(TS_YM_PORT, TS_YM_PIN, 0);   // GND

  TS_XM_FUNC_ADC;

  // Return the ADC results
  return adcRead(TS_XM_ADC_CHANNEL);
}
Ejemplo n.º 10
0
void main()
{   
	uint32 last_ms;
	systemInit();
	//configure the P1_2 and P1_3 IO pins
	makeAllOutputs(LOW);
	//initialise Anlogue Input 0
	P0INP = 0x1;
	//initialise the USB port
	usbInit();

	usbComRequestLineStateChangeNotification(LineStateChangeCallback);
	
	last_ms = getMs();
	while (1)
	{
		boardService();
		usbComService();
		if((getMs()-last_ms) >=5000){
			LED_YELLOW_TOGGLE();
			printf("batteryPercent: %i\r\n", batteryPercent(adcRead(0 | ADC_REFERENCE_INTERNAL)));
			last_ms=getMs();
		}
	}
}
Ejemplo n.º 11
0
uint8_t *joyGetUnscaledXY(uint8_t *joyPos)
{
	
	uint8_t buffer1=adcRead(ADC_CH1_JOY_Y_AXIS);
	uint8_t buffer2=adcRead(ADC_CH2_JOY_X_AXIS);
	uint8_t buffer3=buffer1;
	uint8_t buffer4=buffer2;
	//printf("\n\r%d",buffer3);
	joyPos[0]=buffer1;
	joyPos[1]=buffer2;
	joyPos[2]=buffer3;
	joyPos[3]=buffer4;
	
	return joyPos;
	
}
Ejemplo n.º 12
0
int main() {
    int n;
    short vref = *(short*)((void*)0x1FFFF7BA);
    REG_L(RCC_BASE, RCC_AHBENR) |= (1 << 17); // port A clock
    REG_L(RCC_BASE, RCC_AHB2ENR) |= (1 << 9); // ADC clock
    
    REG_L(GPIOA_BASE, GPIO_MODER) |= 1;
    
    uartEnable();
    adcEnable();
    REG_L(ADC_BASE, ADC_CHSELR) |= (1 << 5); // ADC from PA5
    REG_L(ADC_BASE, ADC_CHSELR) |= (1 << 17); // ADC from vrefint
    
    while(1) {
        REG_L(GPIOA_BASE, GPIO_BSRR) |= (1 << 0);
        sends("on\n");
        adcRead(2);
        sends("adc=0x");
        sendHex(adc[0], 3);
        n = (intDiv(3300 * (int) adc[0], adc[1]) * vref) >> 12;
        sends(", V=");
        sendDec(n);
        sends("mv, T=");
        sendDec(intDiv(((n - 2980) + 5), 10) + 25);
        sends("\n");
        n=250000; while(--n);
        REG_L(GPIOA_BASE, GPIO_BSRR) |= (1 << 16);
        sends("off\n");
        n=1000000; while(--n);
    }    
}
Ejemplo n.º 13
0
uint32_t tsReadX(void)
{
  // XP = GPIO Output High
  // XM = GPIO Output Low
  // YP = ADC
  // YM = GPIO Input

  GPIO_InitTypeDef  GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = ADC_XP_Pin | ADC_XM_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(ADC_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = ADC_YM_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(ADC_PORT, &GPIO_InitStructure);

  GPIO_WriteBit(ADC_PORT, ADC_XP_Pin, Bit_SET);  // 3.3V
  GPIO_WriteBit(ADC_PORT, ADC_XM_Pin, Bit_RESET);  // GND

  return adcRead(ADC_YP_CHANNEL);
}
Ejemplo n.º 14
0
int main() {
	int value;
	
	
	adcInit(); //!!!! ne pozabi !!!!
	
	LEDInit();
	
	
	while(1) {
		value = adcRead() / 219;
		
		switch(value) {
			case 0: 
				GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15); 
				break;
			case 1: 
				GPIO_ResetBits(GPIOD, GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
				GPIO_SetBits(GPIOD, GPIO_Pin_12);
				break;
			case 2: 
				GPIO_ResetBits(GPIOD, GPIO_Pin_14 | GPIO_Pin_15);
				GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13);
				break;
			case 3: 
				GPIO_ResetBits(GPIOD, GPIO_Pin_15);
				GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14);
				break;
			case 4: 
				GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
		}
		
	}
}
Ejemplo n.º 15
0
void tsReadZ(uint32_t* z1, uint32_t* z2)
{
  // XP = ADC
  // XM = GPIO Output Low
  // YP = GPIO Output High
  // YM = GPIO Input

  GPIO_InitTypeDef  GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = ADC_XM_Pin | ADC_YP_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(ADC_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = ADC_YM_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(ADC_PORT, &GPIO_InitStructure);

  GPIO_WriteBit(ADC_PORT, ADC_XM_Pin, Bit_RESET);  // GND
  GPIO_WriteBit(ADC_PORT, ADC_YP_Pin, Bit_SET);  // 3.3V

  *z1 = adcRead(ADC_XP_CHANNEL);

  nopDelay(CFG_TFTLCD_TS_DELAYBETWEENSAMPLES);

  // XP = GPIO Input
  // XM = GPIO Output Low
  // YP = GPIO Output High
  // YM = ADC

  GPIO_InitStructure.GPIO_Pin = ADC_XP_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(ADC_PORT, &GPIO_InitStructure);

  *z2 = adcRead(ADC_YM_CHANNEL);
}
Ejemplo n.º 16
0
void usbHIDGetInReport (uint8_t src[], uint32_t length)
{
  usbhid_out_t out;

  out.gpio1Dir = GPIO_GPIO1DIR;
  out.gpio1Data = GPIO_GPIO1DATA;
  out.gpio2Dir = GPIO_GPIO2DIR;
  out.gpio2Data = GPIO_GPIO2DATA;
  out.gpio3Dir = GPIO_GPIO3DIR;
  out.gpio3Data = GPIO_GPIO3DATA;
  out.adc0 = adcRead(0);
  out.adc1 = adcRead(1);
  out.adc2 = adcRead(2);
  out.adc3 = adcRead(3);
  out.systicks = systickGetTicks();
  out.rollovers = systickGetRollovers();

  size_t i = 0;
  memcpy(&src[i], &out.gpio1Dir, sizeof out.gpio1Dir);
  i += sizeof out.gpio1Dir;
  memcpy(&src[i], &out.gpio1Data, sizeof out.gpio1Data);
  i += sizeof out.gpio1Data;
  memcpy(&src[i], &out.gpio2Dir, sizeof out.gpio2Dir);
  i += sizeof out.gpio2Dir;
  memcpy(&src[i], &out.gpio2Data, sizeof out.gpio2Data);
  i += sizeof out.gpio2Data;
  memcpy(&src[i], &out.gpio3Dir, sizeof out.gpio3Dir);
  i += sizeof out.gpio3Dir;
  memcpy(&src[i], &out.gpio3Data, sizeof out.gpio3Data);
  i += sizeof out.gpio3Data;
  memcpy(&src[i], &out.adc0, sizeof out.adc0);
  i += sizeof out.adc0;
  memcpy(&src[i], &out.adc1, sizeof out.adc1);
  i += sizeof out.adc1;
  memcpy(&src[i], &out.adc2, sizeof out.adc2);
  i += sizeof out.adc2;
  memcpy(&src[i], &out.adc3, sizeof out.adc3);
  i += sizeof out.adc3;
  memcpy(&src[i], &out.systicks, sizeof out.systicks);
  i += sizeof out.systicks;
  memcpy(&src[i], &out.rollovers, sizeof out.rollovers);
  i += sizeof out.rollovers;
}
Ejemplo n.º 17
0
// Gets the value at the ADC requested channel
int getIRValue(int channel) {
	int indexes[] = {0, 1, 6, 7};
	
	if (channel < 0 || channel > 4) {
		printf("wrong IR channel%d", channel);
		while(1);
	}
	
	return (int) adcRead(indexes[channel]);
}
Ejemplo n.º 18
0
// A callback triggered when the i2c master attempts to read from a register.
uint8_t i2cReadFromRegister(uint8_t reg)
{
	switch (reg)
	{
		case 0: 
			return 10;
		case 1:
			return adcRead();
		default:
			return 0xff;
	}
}
Ejemplo n.º 19
0
void joyCalibrateXY()
{
	//some buffer variables/ptrs
	uint8_t byteOfData=0;
	globalStruct *ptr=&globalVar;
	
	printf("\n\rPut joystick in center position\n\r");
	while(((byteOfData=usartReadByte())!='y'));
	ptr->joyData[JOY_Y_AXIS_MID]=adcRead(ADC_CH1_JOY_Y_AXIS);
	ptr->joyData[JOY_X_AXIS_MID]=adcRead(ADC_CH2_JOY_X_AXIS);
	
	printf("\n\rGet X_min\n\r");
	while(((byteOfData=usartReadByte())!='y'));
	ptr->joyData[JOY_X_AXIS_MIN]=adcRead(ADC_CH2_JOY_X_AXIS);
	
	printf("\n\rGet X_max\n\r");
	while(((byteOfData=usartReadByte())!='y'));
	ptr->joyData[JOY_X_AXIS_MAX]=adcRead(ADC_CH2_JOY_X_AXIS);
	
	printf("\n\rGet Y_min\n\r");
	while(((byteOfData=usartReadByte())!='y'));
	ptr->joyData[JOY_Y_AXIS_MIN]=adcRead(ADC_CH1_JOY_Y_AXIS);
	
	printf("\n\rGet Y_max\n\r");
	while(((byteOfData=usartReadByte())!='y'));
	ptr->joyData[JOY_Y_AXIS_MAX]=adcRead(ADC_CH1_JOY_Y_AXIS);
}
Ejemplo n.º 20
0
void appMain(void)
{
    uint16_t i;
    for (i = 0; ; i++) {
        i %= 64;
        PRINTF("calibrate potentiometer to %u\n", i);
        if (!ad5258Write(i)) {
            PRINTF("write failed!\n");
        }
        mdelay(PERIOD);
        PRINTF("read adc: %u\n", adcRead(0));
        ledToggle();
    }
}
Ejemplo n.º 21
0
void VoltageCheck(void){

    chrg=gpioGetValue(RB_PWR_CHRG);

	results = adcRead(1);
	results *= 10560;
	results /= 1024;

	if( results < 3500 ){
        nrf_off();
	    gpioSetValue (RB_PWR_GOOD, 0); 
	    gpioSetValue (RB_LCD_BL, 0); 
	    SCB_SCR |= SCB_SCR_SLEEPDEEP;
	    PMU_PMUCTRL = PMU_PMUCTRL_DPDEN_DEEPPOWERDOWN;
	    __asm volatile ("WFI");
    };
Ejemplo n.º 22
0
Archivo: main.c Proyecto: atiselsts/osw
//-------------------------------------------
//      Entry point for the application
//-------------------------------------------
void appMain(void)
{
    SENSOR_POWER_ON();

    hplAdcUseSupplyRef(); // use Supply power as voltage reference
    beeperInit();

    uint16_t y; // y-axis accelerometer reading
    while(1)
    {
        y = adcRead(ACC_Y_PORT);
        if (y < Y_NOTE1) {
            beeperBeepEx(10, NOTE1_FREQ);
        } else if (y > Y_NOTE2) {
            beeperBeepEx(10, NOTE2_FREQ);
        }
//        PRINTF("X= %u  Y= %u  Z= %u\n", x, y, z);
    }
}
Ejemplo n.º 23
0
/// Main function for ADC test firmware
int main(void) {
	uint8_t debugAdc, i;
	FunctionalState led = DISABLE;
	defaultInit();
	debugAdc = debugNewSource("ADC");
	debugSourceEnable(debugAdc, ENABLE);
	debugPrintln(debugAdc, "test start");
	timerStartMs(100);
	for (;;) {
		adcLoop();
		if (timerEnd()) {
			timerStartMs(500);
			ledCmd(0, led);
			led = !led;
			debugPrintRaw(debugAdc, "ADC:");
			for (i = 0; i < ADC_NUMBER; i++) {
				debugPrintRaw(debugAdc, " %u", adcRead(i));
			}
			debugPrintRaw(debugAdc, "\r\n");
		}
	}
}
Ejemplo n.º 24
0
void LightCheck(void){
    int iocon;
    char iodir;

    iocon=IOCON_PIO1_11;
//    iodir=gpioGetDir(RB_LED3);
    iodir= (GPIO_GPIO1DIR & (1 << (RB_LED3) ))?1:0;

    gpioSetDir(RB_LED3, gpioDirection_Input);
    IOCON_PIO1_11 = IOCON_PIO1_11_FUNC_AD7|IOCON_PIO1_11_ADMODE_ANALOG;
    light-=light/SAMPCT;
	light += (adcRead(7)/2);
    
    gpioSetDir(RB_LED3, iodir);
    IOCON_PIO1_11=iocon;

    if(_isnight && light/SAMPCT>(threshold+RANGE))
        _isnight=0;

    if(!_isnight && light/SAMPCT<threshold)
        _isnight=1;
};
void cmd_sysinfo(uint8_t argc, char **argv)
{
  printf("%-25s : %d.%d MHz %s", "System Clock", CFG_CPU_CCLK / 1000000, CFG_CPU_CCLK % 1000000, CFG_PRINTF_NEWLINE);
  printf("%-25s : %d.%d.%d %s", "Firmware", CFG_FIRMWARE_VERSION_MAJOR, CFG_FIRMWARE_VERSION_MINOR, CFG_FIRMWARE_VERSION_REVISION, CFG_PRINTF_NEWLINE);

  // 128-bit MCU Serial Number
  IAP_return_t iap_return;
  iap_return = iapReadSerialNumber();
  if(iap_return.ReturnCode == 0)
  {
    printf("%-25s : %08X %08X %08X %08X %s", "Serial Number", iap_return.Result[0],iap_return.Result[1],iap_return.Result[2],iap_return.Result[3], CFG_PRINTF_NEWLINE);
  }

  // Check the battery voltage
  #ifdef CFG_BAT
    uint32_t c;
    gpioSetDir(CFG_BAT_ENPORT, CFG_BAT_ENPIN, gpioDirection_Output );   
    gpioSetValue(CFG_BAT_ENPORT, CFG_BAT_ENPIN, 1 );    // Enable the voltage divider
    systickDelay(5);
    c = adcRead(CFG_BAT_ADC);                           // Pre-read ADC to warm it up
    systickDelay(10);
    c = adcRead(CFG_BAT_ADC);
    c = (c * CFG_VREG_VCC_MAIN) / 1000;                 // Value in millivolts relative to supply voltage
    c = (c * CFG_BAT_MULTIPLIER) / 1000;                // Battery voltage in millivolts (depends on resistor values)
    gpioSetValue(CFG_BAT_ENPORT, CFG_BAT_ENPIN, 0 );    // Turn the voltage divider back off to save power
    printf("%-25s : %u.%u V %s", "Supply Voltage", (unsigned int)(c / 1000), (unsigned int)(c % 1000), CFG_PRINTF_NEWLINE);
  #endif

  // Wireless Settings (if CFG_CHIBI enabled)
  #ifdef CFG_CHIBI
    chb_pcb_t *pcb = chb_get_pcb();
    printf("%-25s : %s %s", "RF Transceiver", "AT86RF212", CFG_PRINTF_NEWLINE);
    #if CFG_CHIBI_PROMISCUOUS == 1
      printf("%-25s : %s %s", "RF Receive Mode", "Promiscuous", CFG_PRINTF_NEWLINE);
    #else
      printf("%-25s : %s %s", "RF Receive Mode", "Normal", CFG_PRINTF_NEWLINE);
    #endif
    printf("%-25s : 0x%04X (%d) %s", "802.15.4 PAN ID", CFG_CHIBI_PANID, CFG_CHIBI_PANID, CFG_PRINTF_NEWLINE);
    printf("%-25s : 0x%04X (%d) %s", "802.15.4 Node Address", pcb->src_addr, pcb->src_addr, CFG_PRINTF_NEWLINE);
    printf("%-25s : %d %s", "802.15.4 Channel", CFG_CHIBI_CHANNEL, CFG_PRINTF_NEWLINE);
  #endif

  // CLI and buffer Settings
  #ifdef CFG_INTERFACE
    printf("%-25s : %d bytes %s", "Max CLI Command", CFG_INTERFACE_MAXMSGSIZE, CFG_PRINTF_NEWLINE);
  #endif

  // System Uptime (based on systick timer)
  printf("%-25s : %u s %s", "System Uptime", (unsigned int)systickGetSecondsActive(), CFG_PRINTF_NEWLINE);

  // System Temperature (if LM75B Present)
  #ifdef CFG_LM75B
    int32_t temp = 0;
    lm75bGetTemperature(&temp);
    temp *= 125;
    printf("%-25s : %d.%d C %s", "Temperature", (int)(temp / 1000), (int)(temp % 1000), CFG_PRINTF_NEWLINE);
  #endif

  #ifdef CFG_SDCARD
    printf("%-25s : %s %s", "SD Card Present", gpioGetValue(CFG_SDCARD_CDPORT, CFG_SDCARD_CDPIN) ? "True" : "False", CFG_PRINTF_NEWLINE);
  #endif
}
Ejemplo n.º 26
0
void sendReportIfNeeded()
{
    static uint32 lastReport;
    uint8 i, bytesToSend;
    uint16 result[6];
    uint16 vddMillivolts;

    // Create reports.
    if (getMs() - lastReport >= param_report_period_ms && reportLength == 0)
    {
        lastReport = getMs();
        reportBytesSent = 0;

        vddMillivolts = adcReadVddMillivolts();
        adcSetMillivoltCalibration(vddMillivolts);

        for(i = 0; i < 6; i++)
        {
            result[i] = adcRead(i);
        }

        if (param_bar_graph)
        {
            printf("\x1B[0;0H");  // VT100 command for "go to 0,0"
            printBar("P0_0", result[0]);
            printBar("P0_1", result[1]);
            printBar("P0_2", result[2]);
            printBar("P0_3", result[3]);
            printBar("P0_4", result[4]);
            printBar("P0_5", result[5]);
            printf("VDD  %4d mV", vddMillivolts);
        }
        else
        {
            printf("A, %4d, %4d, %4d, %4d, %4d, %4d,\r\n",
                    adcConvertToMillivolts(result[0]),
                    adcConvertToMillivolts(result[1]),
                    adcConvertToMillivolts(result[2]),
                    adcConvertToMillivolts(result[3]),
                    adcConvertToMillivolts(result[4]),
                    adcConvertToMillivolts(result[5])
                    );
        }
    }

    // Send the report to USB in chunks.
    if (reportLength > 0)
    {
        bytesToSend = usbComTxAvailable();
        if (bytesToSend > reportLength - reportBytesSent)
        {
            // Send the last part of the report.
            usbComTxSend(report+reportBytesSent, reportLength - reportBytesSent);
            reportLength = 0;
        }
        else
        {
            usbComTxSend(report+reportBytesSent, bytesToSend);
            reportBytesSent += bytesToSend;
        }
    }

}
Ejemplo n.º 27
0
int main() {
	int G = 512;
	float rms, power = 0, setPower = 3.0;
	unsigned char keyBuf;
	bool adRefresh = true, pwrValueRefresh = true, gainValueRefresh = true;
	bool enAGC = true, atLimit = false, isAdj = false;

	lcdInit();
	ec11Init();
	adInit();
	lcdBacklit(true);

	for(;;) {
		if(pwrValueRefresh || gainValueRefresh) {
			/*
			 * Display
			 */
			lcdWrite(0x0c,0,1); // Turn curson off
			if(pwrValueRefresh) {
				pwrValueRefresh = false;

				lcdLocate(1, 0);
				lcdPrintStr("PWR: ");
				lcdPrintFloat(power, 3, 1);
				lcdPrintStr("W");
				if(enAGC) {
					lcdPrintStr("/");
					lcdPrintFloat(setPower, 3, 1);
					lcdPrintStr("W");
				}
			}
			if(gainValueRefresh) {
				gainValueRefresh = false;
				lcdLocate(0, 1);
				lcdPrintStr("Gain: ");
				lcdPrintInt(G, 4);
				if(G == 0 || G == 2046) {
					lcdLocate(15, 1);
					lcdPrintStr("*");
					atLimit = true;
				} else if(atLimit) {
					atLimit = false;
					lcdLocate(15, 1);
					lcdPrintStr(" ");
				}

			}
			if(enAGC)
				lcdLocate(15, 0);
			lcdWrite(0x0e,0,1); // Turn cursor on
		}

		rms = (float)adcRead(0) * 10.0 / 1024.0;
		power = rms * rms / 8.0;
		if(enAGC) {
			if((!isAdj && power < setPower) || (isAdj && power < setPower + 0.1)) {
				if(G < 2047 - 1)
					G++;
				adRefresh = true;
				gainValueRefresh = true;
				isAdj = true;
			} else if((!isAdj && power > setPower + 0.03) || (isAdj && power > setPower + 0.02)) {
				/* Ensure precision for power higher than 0.4watt and reduce oscillation
				 * got x1.03 retired */
				if(G > 0)
					G--;
				adRefresh = true;
				gainValueRefresh = true;
				isAdj = true;
			} else if(isAdj) {
				isAdj = false;
				pwrValueRefresh = true;
			}
		}
		if(adRefresh) {
			adRefresh = false;
			adSetGain(G);
		}

		if((keyBuf = ec11Check()) == 2) {
			if(enAGC) {
				setPower += 0.10;
				pwrValueRefresh = true;
			} else {
				G++;
				gainValueRefresh = true;
			}
		} else if(keyBuf == 1) {
			if(enAGC) {
				setPower -= 0.10;
				pwrValueRefresh = true;
			} else {
				G--;
				gainValueRefresh = true;
			}
		} else if(keyBuf & 0b100) {
			enAGC = !enAGC;
			lcdClear();
			pwrValueRefresh = gainValueRefresh = true;
		}
	}
}
Ejemplo n.º 28
0
int main() {
	int G = 0, keyInterval = 1;
	float rms, power = 0, setPower = 3.0;
	unsigned char keyBuf;
	bool adRefresh = true, pwrValueRefresh = true, gainValueRefresh = true;
	bool enAGC = true, atLimit = false;
//	isAdj = false;

	lcdInit();
	ec11Init();
	adInit();
	lcdBacklit(true);

	lcdPrintStr("Design: CX Wang");
	lcdLocate(2, 1);
	lcdPrintStr("Initializing..");
	idelay_s(5);
	lcdClear();

	for(;;) {
		if(pwrValueRefresh || gainValueRefresh) {
			/*
			 * Display
			 */
			lcdWrite(0x0c,0,1); // Turn cursor off
			if(pwrValueRefresh) {
				pwrValueRefresh = false;

				lcdLocate(1, 0);
				lcdPrintStr("PWR: ");
				if(power < 9.94)
					lcdPrintFloat(power + 0.05, 3, 1); //For rounding
				else
					lcdPrintStr(">10");
				lcdPrintStr("W");
				if(enAGC) {
					lcdPrintStr("/");
					lcdPrintFloat(setPower, 3, 1);
					lcdPrintStr("W");
				}
			}
			if(gainValueRefresh) {
				gainValueRefresh = false;
				lcdLocate(0, 1);
				lcdPrintStr("Gain: ");
				lcdPrintInt(G, 4);
				if(G == 0 || G == 1023 * ADNUM) {
					lcdLocate(15, 1);
					lcdPrintStr("*");
					atLimit = true;
				} else if(atLimit) {
					atLimit = false;
					lcdLocate(15, 1);
					lcdPrintStr(" ");
				}

			}
			if(enAGC)
				lcdLocate(15, 0);
			else
				lcdLocate(10, 1);
			lcdWrite(0x0e,0,1); // Turn cursor on
		}

		/*
		 * Adjustment
		 */
		rms = (float)adcRead(0) * 10.0 / 1024.0;
		power = rms * rms / 8.0;
		if(enAGC) {
			if(power < setPower) {
				G += fabs(power - setPower) * 10;//imax(1, fabs(power - setPower) * 10);
				if(G > 1023 * ADNUM)
					G = 1023 * ADNUM;
				adRefresh = true;
				gainValueRefresh = pwrValueRefresh = true;
//				isAdj = true;
			} else if(power > setPower + 0.02) {
				/* Ensure precision for power higher than 0.4watt and reduce oscillation
				 * got x1.03 retired */
				G -= fabs(power - setPower) * 10;//imax(1, fabs(power - setPower) * 10);
				if(G < 0)
					G = 0;
				adRefresh = true;
				gainValueRefresh = pwrValueRefresh = true;
//				isAdj = true;
			} else if(setPower < 0.05) {
				G = 0;
				adRefresh = true;
				gainValueRefresh = pwrValueRefresh = true;
			}
//			else if(isAdj) {
//				isAdj = false;
//				pwrValueRefresh = pwrValueRefresh = true;
//			}
		} else if(fabs(power - setPower) > 0.05){
			setPower = power;
			if(setPower > 7.50)
				setPower = 7.50;
			pwrValueRefresh = true;
		}
		if(adRefresh) {
			adRefresh = false;
			adSetGain(G);
		}

		if((keyBuf = ec11Check()) == 2) {
			if(enAGC) {
				setPower += 0.10;
				if(setPower > 7.50)
					setPower = 7.50;
				pwrValueRefresh = true;
			} else {
				G += keyLevel(keyInterval);
				if(G > 1023 * ADNUM)
					G = 1023 * ADNUM;
				gainValueRefresh = adRefresh = true;
				if(keyInterval < 1000)
					keyInterval += 50;
			}
		} else if(keyBuf == 1) {
			if(enAGC) {
				setPower -= 0.10;
				if(setPower < 0)
					setPower = 0;
				pwrValueRefresh = true;
			} else {
				G -= keyLevel(keyInterval);
				if(G < 0)
					G = 0;
				gainValueRefresh = adRefresh = true;
				if(keyInterval < 1000)
					keyInterval += 50;
			}
		} else if(keyBuf & 0b100) {
			enAGC = !enAGC;
			lcdClear();
			pwrValueRefresh = gainValueRefresh = true;
		}
		if(keyInterval > 1)
			keyInterval--;
	}
}
Ejemplo n.º 29
0
uint8_t RH_RF22::temperatureRead(uint8_t tsrange, uint8_t tvoffs)
{
    spiWrite(RH_RF22_REG_12_TEMPERATURE_SENSOR_CALIBRATION, tsrange | RH_RF22_ENTSOFFS);
    spiWrite(RH_RF22_REG_13_TEMPERATURE_VALUE_OFFSET, tvoffs);
    return adcRead(RH_RF22_ADCSEL_INTERNAL_TEMPERATURE_SENSOR | RH_RF22_ADCREF_BANDGAP_VOLTAGE); 
}
Ejemplo n.º 30
0
static
void checkBatteryStatus(void){
	GPIO2DATA ^= _BV(0); /* led0 on */
	if(getMonitorStatus() == 1) wakeBq29312a();
	writeBQ29312A(FUNCTION_CTL, VMEN); /* enable voltage monitoring */
	/* -------------------------------------------- */
	//writeBQ29312A(CELL_SEL, 0b00001100);
	//systickDelay(1);
	//battery.vref = (float)((float)adcRead(1) * (float)(3.3/1024));
	//writeBQ29312A(CELL_SEL, VC4_5);
	//systickDelay(1);
	//battery.vo4_5 = (float)((float)adcRead(1) * (float)(3.3/1024));
	//writeBQ29312A(CELL_SEL, 0b00001000);
	//systickDelay(1);
	//battery.voutr = (float)((float)adcRead(1) * (float)(3.3/1024));
	/* -------------------------------------------- */

	/* Measure Cell Voltage */
	writeBQ29312A(CELL_SEL, VC4_5);
	systickDelay(1);
	battery.adcf[0] = (float)adcRead(1) * (float)(3.3/1024);
	writeBQ29312A(CELL_SEL, VC4_3);
	systickDelay(1);
	battery.adcf[1] = (float)adcRead(1) * (float)(3.3/1024);
	writeBQ29312A(CELL_SEL, VC3_2);
	systickDelay(1);
	battery.adcf[2] = (float)adcRead(1) * (float)(3.3/1024);

	writeBQ29312A(FUNCTION_CTL, VMEN | PACKOUT);
	//uint8_t chgFet = getFetState(FET_CHG);
	//setChgFet(0);
	//setChgPin(0); // chg circuit on
	systickDelay(8);
	battery.adcf[3] = (float)adcRead(1) * (float)(3.3/1024);
	//setChgPin(1); // chg curcuit off
	//setChgFet(chgFet);

	/* Covert Cell Voltage from adc value *but this value is little inaccuracy */
	battery.vcellf[0] = (0.975 - battery.adcf[0]) / 0.15;
	battery.vcellf[1] = (0.975 - battery.adcf[1]) / 0.15;
	battery.vcellf[2] = (0.975 - battery.adcf[2]) / 0.15;
	battery.vcellf[3] = battery.adcf[3] * 25;

	//battery.vcellf[0] = (float)(-0.15 * battery.adcf[0] + 0.975);

	//battery.kact = (battery.vo4_5 - battery.voutr) / battery.vref;
	//battery.vosact = (battery.vo4_5 - battery.vref) / (1 + battery.kact);

	//battery.res = (battery.adcf[0] - battery.vcellf[0]) / battery.kact;
	//float res = (vref + ( 1 + kact ) * vosact - adcf[0] ) / kact - ( vo4_5 - adcf[0] ) / kact;
	//float res = (-adcf[0] + vref ) / kact;


	/* Check DC_in status */
	if(battery.vcellf[3] > 12.6){
		/* DC connected */
		dc.stat = 1;
		GPIO2DATA &= ~(_BV(9)); /* led9 on */
	}else{
		/* DC disconnected */
		dc.stat = 0;
		GPIO2DATA |= _BV(9); /* led9 off */
	}

	/* When DCin is disconnect. CHG FET set to ON for reduce the voltage drop */
	if(dc.stat == 0) setChgFet(1);
	if(dc.stat == 1) setChgFet(0);

	//if(dc.stat == 1){
	//	setDsgFet(0);
	//}

	/* Low voltage alert */
	if(
		(battery.vcellf[0] <= 3.6) ||
		(battery.vcellf[1] <= 3.6) ||
		(battery.vcellf[2] <= 3.6)
	)
	{
		battery.batlow = 1;
		GPIO2DATA |= _BV(0); // led0 off
	}
	else
	{
		battery.batlow = 0;
	}

	/* Low voltage shutdown */
	if(
		(battery.vcellf[0] <= 3.4) ||
		(battery.vcellf[1] <= 3.4) ||
		(battery.vcellf[2] <= 3.4)
	)
	{
		setDCDC(0);
		setDsgFet(0);
		sleepBq29312a();
		shipBq29312a(); /* BQ29312A enter ShipMode. The way to return to NormalMode is only HardwareReset */
		__disable_irq();
		/* Todo: lpc1114 will enter deepsleep mode */
	}
	else
	{
		setDCDC(1);
		setDsgFet(1);
	}

	battery.checkBattery = 0;
}