Exemplo n.º 1
0
u16 testPNP() {
    u8 i, C[2], B[2], E[2];
    u16 Vc[2], Vb[2];
    u32 hFE[2];
    B[0] = B[1] = tList[0][1];
    C[0] = E[1] = tList[1][0];
    C[1] = E[0] = tList[0][0];
    R_470K(B[0], LOW);
    for (i = 0; i < 2; i++) {
        R_680(C[i], LOW);
        R_0(E[i], HIGH);
        Delay_MS(10);
        Vc[i] = ReadADC(C[i]);
        Vb[i] = ReadADC(B[i]);
        hFE[i] = (u32) Vc[i]*691;
        hFE[i] = hFE[i] / Vb[i];
    }
    HiZ3(B[0], C[0], E[0]);
    if (hFE[0] > hFE[1])i = 0;
    else i = 1;
    pins[C[i]] = 'C';
    pins[B[i]] = 'B';
    pins[E[i]] = 'E';
    return hFE[i];
}
Exemplo n.º 2
0
/* Main function */
int main() {
    char tempVal; // 8 bit value of the temperature.
    char flowVal; // 8 bit value of the flow rate.
    int stat = 0; // Status of device.

    Init();

    while(1) {
        // Wait for command from base.
        SwtichAlpha(RX);

        while(!stat)
        {
            stat = (ID & ReceiveAlpha());
        }

        SwtichAlpha(ID);

        // Gather sensor values.
        tempVal = ReadADC(TEMP);
        flowVal = ReadADC(FLOW);

        // Transmit sensor values.
        SwtichAlpha(TX);

        SendAlpha(tempVal);
        SendAlpha(flowVal);
    }
}
Exemplo n.º 3
0
u16 testRES() {
    u8 R1, R2;
    u32 v0, v680, v470K, rt680, rt470K, rr;
    R1 = tList[0][0];
    R2 = tList[0][1];
    R_0(R2, LOW);
    R_680(R1, HIGH);
    Delay_MS(10);

    v0 = ReadADC(R2);
    v680 = ReadADC(R1);

    R_470K(R1, HIGH);
    Delay_MS(10);
    v470K = ReadADC(R1);
    HiZ(R1);
    HiZ(R2);

    //When Rx > sqrt(680*470000) the rt470K is more accurate
    //we calculate if this threshold has been passed below
    if (v680 > 512) rt680 = v680 - 512;
    else rt680 = 512 - v680;

    if (v470K > 512) rt470K = v470K - 512;
    else rt470K = 512 - v470K;

    if (rt470K > rt680){
        //We will use the value calculated with the 680R resistor
        if(v680==v0){ //Shortcircuit?
            rr = 1; //0 is considered error
        }
        else{
            //Use the common formula for a voltage divisor with some correction
            //The output is not 5V and 0V for HIGH and LOW, the difference increasses with
            //lower resistor values, empirically V_low=x, V_high=Vcc-2.80*V_low
            //R = 680*(v680-v0)/(1023-2.8v0-v680)
            rr = (5*R680_IDEAL*(v680-v0)) / (5*1023 - 14*v0 - 5*v680);
        }
    }
    else{
        //We will use the value calculated with the 470K resistor
        //Use the common formula for a voltage divisor
        rr = (R470K_IDEAL*v470K) / (1023 - v470K);
    }

    pins[R1] = 'R';
    pins[R2] = 'R';
    part_unit = 'R';

    if (rr > 0x03E7FC18) { //Values that overflow 16bits when divided by 1000
        rr /= 1000000;
        part_unit = 'M';
    }
    else if(rr > 0xFFFF){ //Values that overflow 16bits
            rr /= 1000;
            part_unit = 'K';
    }
    return (u16) rr;
}
void TaskSensores(void *pdata)
{    
#if OS_CRITICAL_METHOD == 3 
    OS_CPU_SR  cpu_sr;
#endif
    struct AdcMsg *m;
    INT16U value = 0;
    INT16U ValorTeclado = 0;
	INT8U err;
	for(;;)
	{
        
        m = (struct AdcMsg *) OSMemGet(dMemory,&err);
        if(err == OS_NO_ERR){
            OSSemPend(STeclado,0,&err);
            ValorTeclado = NumeroSensores;
            OSSemPost(STeclado);       
            m->adc0 = 0;
            m->adc1 = 0;
            m->adc2 = 0;
            switch(ValorTeclado){
                case 3:
                    Delay10TCYx(100);
                    SetChanADC(ADC_CH2);
                    ConvertADC();
                    while( BusyADC() );
                    value = ReadADC();
                    m->adc2 = Temp(value);
                case 2:
                    Delay10TCYx(100);
                    SetChanADC(ADC_CH1);
                    ConvertADC();
                    while( BusyADC() );
                    value = ReadADC();
                    m->adc1 = Temp(value);                    
                case 1:
                    Delay10TCYx(100);
                    SetChanADC(ADC_CH0);
                    ConvertADC();
                    while( BusyADC() );
                    value = ReadADC();
                    m->adc0 = Temp(value);
                default:
                    break;
            }

            err = OSQPost(QueueADC0,m);
            if(err == OS_Q_FULL){
                OSMemPut(dMemory,m);
                OSSemPost(STaskTxSerial);
            }
        }else{
            OSSemPost(STaskTxSerial);
        }
		//OSSemPost(STask2);
		OSTimeDly(1);
	}
}
Exemplo n.º 5
0
//	Read Pressure on board 2
static void ReadZ(int* z1, int* z2)
{
    DDRB = 0x10;        // XP = 0
    PORTB = 0x00;
    DDRD = 0x80;        // YM = 1
    PORTD = 0x80;
    *z1 = ReadADC(12);	// Measure z1 (Read YP ADC)
    *z2 = ReadADC(9);	// Measure z2 (Read XM ADC)
}
Exemplo n.º 6
0
/*
 * Read and publish voltage and current for power calculations
 */
void PublishMotorPowerData()
{
    //Generate the msg
    hardware_interface::RawMotorPowerData power_data;

    //Read the data
    power_data.current = ReadADC(ADC_CURRENT_SENSE, ADC_CH0);
    power_data.voltage = ReadADC(ADC_CURRENT_SENSE, ADC_CH1);

    //Publish the msg
    motor_power_pub.publish(power_data);
}
Exemplo n.º 7
0
uint8_t ShortedProbes(uint8_t Probe1, uint8_t Probe2)
{
  uint8_t           Flag1 = 0;      /* return value */
  unsigned int      U1;            /* voltage at probe #1 in mV */
  unsigned int      U2;            /* voltage at probe #2 in mV */
  unsigned int      URH;	   /* half of reference voltage */
  const uint8_t *addr;
  uint8_t pp;
  /*
   *  Set up a voltage divider between the two probes:
   *  - Probe1: Rl pull-up
   *  - Probe2: Rl pull-down
   */

  ADC_DDR =  TXD_MSK;		// all-Pins to Input
  ADC_PORT = TXD_VAL;		// all ADC-Ports to GND
  addr = &PinRLRHADCtab[Probe1];
  pp = pgm_read_byte(addr);
  R_PORT = pp;
  addr += (int8_t)(Probe2-Probe1);
  R_DDR =  pp | pgm_read_byte(addr);	// pgm_read_byte(PinRHtab[Probe1]) | pgm_read_byte(PinRLtab[Probe2]);

  /* read voltages */
  U1 = ReadADC(Probe1);
  U2 = ReadADC(Probe2);

  /*
   *  We expect both probe voltages to be about the same and
   *  to be half of Vcc (allowed difference +/- 20mV).
   */
 #ifndef MAX_UH_DIFF
  #define MAX_UH_DIFF 30
 #endif

  URH = ADCconfig.U_AVCC / 2;
  URH -= ((long)U_VCC * (long)(PIN_RP-PIN_RM)) / (4*(unsigned long)(R_L_VAL+PIN_RM));			// differenz of Pin resistance high (22) and low (20)
  if (((U1 + MAX_UH_DIFF) > URH ) && (U1 < (URH + MAX_UH_DIFF)))
  {
    if (((U2 + MAX_UH_DIFF) > URH) && (U2 < (URH + MAX_UH_DIFF)))
    {
      Flag1 = 1;
    }
  }

  /* reset port */
  R_DDR = 0;

  return Flag1;
}
Exemplo n.º 8
0
/*
 *  Setup ADC module to read in accelerometer and potentiometer values
 */   
void SEC_InitializeADC(void) {
    _mqx_int i;
    char dev_name[10];
    
    fd_adc = fopen(MY_ADC, (const char*)&adc_init);
    if (NULL == fd_adc) {    
        printf("ADC device open failed\n");
        _task_block();
    }
    
    for (i = 0; i < ADC_CH_COUNT; i++) {
        sprintf(dev_name, "%s%d", MY_ADC, i);
        fd_ch[i] = fopen(dev_name, (const char*)&adc_ch_param[i]);
        if (NULL == fd_ch[i]) {    
            printf("adc:%d channel open failed\n", i);
            _task_block();
        }
    }
    
    _time_delay (100);

#if ADC_CH_COUNT > 1
    for (i = 0; i < 3; i++) {
      SEC_Params.last[i] = ReadADC(ADC_ACCX + i);
    }
    
    SEC_Params.flat=SEC_Params.last[ACCY];
#endif

    _time_delay (200);
}
Exemplo n.º 9
0
void adc_int_handler()
{
    unsigned int result;
    result = ReadADC();
    result = result /4;
    Handle_i2c_data_save(1,&result);
}
Exemplo n.º 10
0
/*!
  \brief
  Tastsensor Abfrage im 'Polling-Betrieb'.

  \return
  Tastenwert bitorientiert, K1 = Bit5, K2 = Bit4, K3 = Bit3, K4 = Bit2,
  K5 = Bit1, K6 = Bit0

  \see
  Die globale Variable autoencode wird temporaer auf FALSE gesetzt und am Ende\n
  der Funktion mit dem alten Wert restauriert.

  \par  Hinweis:
  In dieser Funktion sind 2 Sleep() Aufrufe vorhanden. Sie werden benoetigt\n
  damit der Kondensator an der AD-Wandlereinheit genuegend Zeit hat geladen\n
  zu werden.

  \par  Beispiel:
  (Nur zur Demonstration der Parameter/Returnwerte)
  \code
  uint8_t t1, t2;
  unsigned char text [16];

  while (1)
  {
    t1 = PollSwitch ();
    t2 = PollSwitch ();
    // 2x PollSwitch aufrufen und beide Rueckgabewerte auf Gleichheit ueberpruefen
    if (t1 && t2 && t1 == t2)           // irgendeine Taste gedrueckt 
    {
      itoa (t1, text, 10);              // Tastenwert senden 
      SerPrint (text);
      SerPrint ("\r\n");                // Zeilenvorschub 
    }
    Msleep (500);                       // 0,5 sek warten
  }
  \endcode
*****************************************************************************/
unsigned char PollSwitch (void)
{
  unsigned int i;
  int ec_bak = autoencode;              // Sichert aktuellen Zustand

  /*
     Autoencode-Betrieb vom ADC-Wandler unterbinden.
  */
  autoencode = FALSE;

  DDRD |= SWITCHES;                     // Port-Bit SWITCHES als Output
  SWITCH_ON;                            // Port-Bit auf HIGH zur Messung
  i = ReadADC(SWITCH, 10);

  SWITCH_OFF;                           // Port-Bit auf LOW
  Sleep (5);

  /*
     Autoencode-Betrieb vom ADC-Wandler wiederherstellen.
  */
  autoencode = ec_bak;

  /*
    Die Original Umrechenfunktion von Jan Grewe - DLR wurder ersetzt durch
    eine Rechnung ohne FLOAT-Berechnungen.
  return  ((unsigned char) ((( 1024.0/(float)i - 1.0)) * 61.0 + 0.5));

    Wert 61L evtl. anpasssen, falls fuer K1 falsche Werte zurueckgegebn werden.
  */
  return ((10240000L / (long)i - 10000L) * MY_SWITCH_VALUE + 5000L) / 10000;
}
Exemplo n.º 11
0
void main(void)
{
    int16_t moyenne;

    BoardInit();

    /* Configuration de l'ADC
     *  horloge: oscillateur RC interne
     *  résultat justifié à droite
     *  temps d'acquisition: 2*Tad (délai minimum entre 2 conversions)
     *  canal 4
     *  interruption desactivée
     *  tensions référence par défaut (Vref+ = AVdd / Vref- = AVss)
     */
    OpenADC(ADC_FOSC_RC|ADC_RIGHT_JUST|ADC_2_TAD, ADC_CH4|ADC_INT_OFF, 0);

    moyenne = 0;
    for (uint8_t i = 0; i < 16; i++) {
        ConvertADC();               /* démarrage conversion */
        while (BusyADC());          /* attente fin conversion */
        moyenne += ReadADC();       /* lecture résultat */
    }
    moyenne /= 16;

    while (1);
}
Exemplo n.º 12
0
int main(void)
{
    uint16_t adc_result;
	int i;
	char value[10];
	sbi(DDRB, 4);
	sbi(DDRC, 5);
	cbi(PORTB,4);
	sbi(PORTC,5);	
    UART_init(250000);
	InitADC();
	sei();
	
	
	
	while(1)
	{
	adc_result=ReadADC(2);           // Read Analog value from channel-2
    // Voltage = adc_result*5/1024
	// Temperature = (V  -  1035 mV)/(-5.5 (mV/oC))
	itoa(adc_result,value,10);
	for(i=0;i<=2;i++){
	UART_TxChar(value[i]);}
	UART_TxStr("\n\r\0");
	PORTB ^= (1<<PB4);	
	_delay_ms(100);
	}
	
}
Exemplo n.º 13
0
void main(void) {
    int count;
    unsigned int adcValue = 0;
    unsigned int adcValueOld = 0;
    char buffer[16];

    ConfigureOscillator();
    InitApp();

    lcd_init();
    lcd_enable();
    lcd_cursor_off();
    lcd_test();
    lcd_pos(2, 1);
    while (1) {
        ConvertADC();
        while (BusyADC());
        adcValue = ReadADC();
        if (adcValue != adcValueOld) {
            lcd_clear();
            lcd_pos(1, 1);
            memset(&buffer[0], 0, sizeof(buffer));
            sprintf(&buffer[0], "Valor: %.4u %.3u%%", adcValue, (int)(((float)adcValue / 1024) * 100));
            lcd_write_buffer(buffer, strlen(buffer));
            adcValueOld = adcValue;
        }
        __delay_ms(20);
    }
}
Exemplo n.º 14
0
void ProcessIO(void)
{   
    BYTE numBytesRead;

    //Blink the LEDs according to the USB device status
    BlinkUSBStatus();
    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

    if(buttonPressed)
    {
        if(stringPrinted == FALSE)
        {
            if(mUSBUSARTIsTxTrfReady())
            {
                putrsUSBUSART("Button Pressed data-- \r\n");
                stringPrinted = TRUE;
            }
        }
    }
    else
    {
        stringPrinted = FALSE;
    }

    if(USBUSARTIsTxTrfReady())
    {
		numBytesRead = getsUSBUSART(USB_Out_Buffer,64);
		if(numBytesRead != 0)
		{
			BYTE i;
	        
			for(i=0;i<numBytesRead;i++)
			{
				switch(USB_Out_Buffer[i])
				{
					case 0x0A:
					case 0x0D:
						putUSBUSART(&USB_Out_Buffer[i],numBytesRead);
						break;
                                        case 0x53://letter S to start sampling
                                                ReadADC();
                                                putUSBUSART(ADC_sample,SAMPLE_SIZE);
                                                break;
                                        case 0x51: //letter Q to stop

                                                break;
					default:
						putUSBUSART(&USB_Out_Buffer[i],numBytesRead);
						break;
				}

			}

			//putUSBUSART(USB_In_Buffer,numBytesRead);
		}
	}

    CDCTxService();
}		//end ProcessIO
Exemplo n.º 15
0
//2conducts, 1 node. directon 2 (IN), muct be PNP/DD-CC
type getPartSS_PNP() {
    u16 test;

    if (tList[0][2]>(tList[1][2] + DARL_BJT)) {
        node = 0;
        return PNP_D;
    }
    if (tList[1][2]>(tList[0][2] + DARL_BJT)) {
        node = 1;
        return PNP_D;
    }

    R_680(tList[0][1], LOW); //B
    R_0(tList[0][0], HIGH);  //E
    R_680(tList[1][0], LOW); //C

    test = ReadADC(tList[1][0]);
    HiZ3(tList[0][0], tList[1][0], tList[0][1]);

    //if VCE>0V then PNP
    if (test > CP_LOW) return PNP;

    //else it's a double diode with CA
    return CC;
}
Exemplo n.º 16
0
//2conducts, 1 node, direction 1(OUT), must be NPN/DD-CA
type getPartSS_NPN() {
    u16 test;

    if (tList[0][2]>(tList[1][2] + DARL_BJT)) {
        node = 0;
        return NPN_D;
    }
    if (tList[1][2]>(tList[0][2] + DARL_BJT)) {
        node = 1;
        return NPN_D;
    }

    R_680(tList[0][0], HIGH); //B
    R_0(tList[1][1], LOW);    //E
    R_680(tList[0][1], HIGH); //C

    test = ReadADC(tList[0][1]);
    HiZ3(tList[0][0], tList[0][1], tList[1][1]);

    //if VCE<5V then NPN
    if (test < CP_HIGH) return NPN;

    //else it's a double diode with CA
    return CA;
}
Exemplo n.º 17
0
void adc_int_handler()
{
    unsigned int result;
    result = ReadADC();
    result = result /4;
    ToMainHigh_sendmsg(sizeof(result),MSGT_I2C_DATA_SAVE,(void *)&result);
}
Exemplo n.º 18
0
/*
 * Read in values through ADC of the accelerometer. Then determines if board is moving
 *  or tilted. Compares against accelerometer values that were recorded at bootup, and 
 *  returns 1 (MOVING) if values are outside of 'tolerance' bounds.
 */
_mqx_int SEC_DetectMotion(void) 
{
    _mqx_int i,axis[3] = {0,0,0};
    _mqx_int board_status;
    _mqx_int tolerance;
  
    if(SEC_GetMovementStatus() == MOVING) {
        /* If board has already been detected as moving, lower 
        tolerance to prevent false positives of the board being still*/
        tolerance = 0x20;
    }
    else {
        /* If board is not moving already, then make tolerance higher to prevent false positives 
        of the board moving */
        tolerance = 0x40;
    }
    
    /* Read in axis values */
   board_status = STOPPED;
   for (i = 0; i < 3; i++) {
      axis[i] = ReadADC(ADC_ACCX + i);  
      if (abs(SEC_Params.last[i] - axis[i]) > tolerance ) {
        board_status = MOVING;
      }
      SEC_Params.last[i] = axis[i];
   }
  
    return board_status;
}
int main(void)
{
	DDRB |= (1 << DDB0);
	
	adc_init();
	
	(PORTB |= (1 << PINB0));
    _delay_ms(1000);
    (PORTB &= ~(1 << PINB0));
    _delay_ms(2000);

	
    while(1)
    {
		sprayTime = cycleTime * ReadADC(0) * 2;
		(PORTB |= (1 << PINB0));
		delay_ms(sprayTime);
		(PORTB &= ~(1 << PINB0));
		delay_ms((cycleTime * 1000) - sprayTime);

		/*if (ReadADC(0) > 512)
		{
			(PORTB |= (1 << PINB0));
		} 
		else
		{
			(PORTB &= ~(1 << PINB0));
		}
		*/
    }
}
Exemplo n.º 20
0
/*--------------------------- main() ------------------------------------------------------
 Purpose     : Main insertion into program.
 Parameters  : N/A
 Output      : N/A
*/
void main()
{
    //setup dateTime
    //dateTime.seconds = 0;

    P18f45k20Init(); //Initialize the board and all necesary ports.

    //InitHD44780();  //Initialize the LCD Board
    //SetupTimeDS1307(&dateTime);// ****ONLY NEED TO DO THIS ONCE****
    //ReadTimeDS1307(&dateTime); //Send the date time construct.

    adcControl.high.allbits = ADC_COMPARE_VALUE; //Default to the ADC Compare value
    adcControl.low.allbits = 0;
    adcControl.outside = 0; //Between mode by default
    adcControl.enable = 0; //Disbaled by default
    adcControl.adcData.allbits = 0;

    while(1)
    {
        ReadADC(&adcControl);
        ProcessADC(&adcControl); //Send the value to turn LED on/off
        ReadTimeDS1307(&dateTime); //Send the date time construct.
        CheckParallel(&dateTime, &adcControl); //Check the Parallel Port for Communications.
        continue;
    }
}
Exemplo n.º 21
0
/* Read GPIO Analog Inputs */
void ReadADC(unsigned char value)
{
	int fd;
	unsigned char buff[5];
	unsigned char data[5];
	
	
	switch(value)
	{
	case 255:
	ReadADC(0);
	ReadADC(1);
	ReadADC(2);
	ReadADC(3);
	ReadADC(5);
	break;	
	case 0:	
	case 1:
	case 2:
	case 3:
	case 5:	
	
	buff[0]=0x10+value;
	data[0]=0x00;
	
	
	/* Open I2C-BUS */	
	I2C_Open(&fd, 0x21);
	
	/* Write register */
	I2C_Send(&fd, buff,1 );
	
	/* Read the ADC */
	
	I2C_Read(&fd, data, 2);
	/* Convert to Volts. Vref = 3.3V, ADC is 10 bits */
	float volts = data[0] * 0.003222656 + data[1]*0.825;
	printf("ADC%u: %1.3fV\n",value, volts);

	/* Close I2C-BUS */
	I2C_Close(&fd);
	break;
	default:
	
	printf("ADC%u not found\n",value);
}
}
int main() {
	uint8_t refTemp = 30; // reference
	uint8_t check, prev_check = 0;
	short up = 1, down = 1;
	uint16_t adc_in; // ADC value
	uint8_t temp = 1; // real temperature
	init_controller();
	init_PWM();
	lcd_init();
	sei(); // turn on interrupts
	lcd_clrscr();
	lcd_puts("Spinning");
	Delay_s(2); // 2 seconds delay
	init_ADC();
	refTemp = eeprom_read_byte((uint8_t*)0);
	if (!(refTemp >= TMIN && refTemp <= TMAX))
		refTemp = 30;
	while(1) {
		if (C_CHECKBIT(Minus_REF)) down = 0;
		if (C_CHECKBIT(Minus_REF) == 0 && down ==0)	{
			down = 1;
			if (refTemp > TMIN) refTemp--;
		}
		if (C_CHECKBIT(Plus_REF)) up = 0;
		if (C_CHECKBIT(Plus_REF) == 0 && up ==0) {
			up = 1;
			if (refTemp < TMAX) refTemp++;
		}
		eeprom_write_byte ((uint8_t *)0, refTemp); 
		adc_in = ReadADC(0);
		temp = adc_in/2;
		lcd_puts2("T%d-R%dC", temp, refTemp);
		if ((temp - refTemp) > TOL)
			check = 3;
		else if ((refTemp - temp) > TOL)
			check = 1;
		else
			check = 2;
		switch(check) {
			case 1:
				if (prev_check == 1) break;
				setDutyCycle1();
				prev_check = 1;
				break;
			case 2:
				if (prev_check == 2) break;
				setDutyCycle2();
				prev_check = 2;
				break;
			case 3:
				if (prev_check == 3) break;
				setDutyCycle3();
				prev_check = 3;
				break;
		}
	}

	return 1;
}
Exemplo n.º 23
0
static int ReadY()
{
	DDRB = 0x20;        // YP = 1
    PORTB = 0x20;
    DDRD = 0x80;        // YM = 0
    PORTD = 0x00;
    return ReadADC(11);    // Measure Y (Read XP ADC)
}
Exemplo n.º 24
0
static int ReadX()
{
	DDRB = 0x10;        // XP = 1
    PORTB = 0x10;
    DDRD = 0x40;        // XM = 0
    PORTD = 0x00;
    return ReadADC(12);	// Measure X (Read YP ADC)
}
Exemplo n.º 25
0
int Hardware_::GetBatteryMillivolts()
{
	DDRF &= 0x7F;
    PORTF &= 0x7F;
	long d = ReadADC(7);
	d *= 1652;
	return d >> 8;	//Battery divider scaled to millivolts from reference
}
Exemplo n.º 26
0
u16 testNMOS() {
    u32 rON;
    u16 vG, vD;
    u8 D, S, G;
    if (tList[2][0] != tList[0][0]) {
        D = tList[2][0];
        S = tList[2][1];
    } else {
        S = tList[2][0];
        D = tList[2][1];
    }
    G = 3 - (S + D);
    R_680(D, HIGH);
    R_0(S, LOW);
    R_680(G, LOW);
    Delay_MS(10);
    vG = ReadADC(G);
    if (vG > 10) {
        HiZ3(S, D, G);
        return 0; //if gate is lower the vdd it meas current is flowing into it which is not MOS
    }
    vD = ReadADC(D);
    if (vD < 1000) {
        HiZ3(S, D, G);
        return 0; //if the VD is higher then 0 when off means its not PMOS
    }
    R_680(G, HIGH);
    Delay_MS(10);
    vG = ReadADC(G);
    if (vG < 1000) {
        HiZ3(S, D, G);
        return 0; //if gate is higher then 0 it meas current is flowing into it which is not MOS
    }
    vD = ReadADC(D);
    if (vD > 123) {
        HiZ3(S, D, G);
        return 0; //if the VD is lower then 0.9*Vdd when ON means its not PMOS
    }
    HiZ3(S, D, G);
    pins[D] = 'D';
    pins[S] = 'S';
    pins[G] = 'G';
    rON = vD * R680_IDEAL;
    rON = rON / (1023 - vD);
    return (u16) rON;
}
Exemplo n.º 27
0
/*! 
	* Function is provided to conveniently measure the enabled ADC channels.
	* If continuous mode, write conversion flag to status register and read ADCs.
	* If triggered mode, send conversion trigger and read activated ADCs.
	* Values are stored in `adc_vals` member, retrieve with `GetADCReadings()`. 
	*/
uint8_t AMC7812Class::ReadADCs(){
	// if convert pin is connected, then use it for triggerring, 
	// otherwise use the internal trigger
	if ( !(amc_conf[0] & (1<<AMC7812_CMODE)) ){
#ifdef AMC7812_CNVT_PIN
	TriggerADCsExternal();
#else
	TriggerADCsInternal();
#endif
#ifdef AMC7812_DAV_PIN
	// wait for data available, polling
	// TODO: timeout cycles should have prefactor based on conversion speed 
	// setting and clock frequency
	uint8_t error = 1;
	for(uint16_t i=0; i<(uint16_t)AMC7812_TIMEOUT_CONV_CYCLS; i++){
		if ( !(AMC7812_DAV_PORT & (1<<AMC7812_DAV_PIN)) ){ 
		error = 0;
		break; 
		}
	}
	if (error){
		return AMC7812_TIMEOUT_ERR;
	}
#endif
	}

	int8_t last_valid = -1;
	for(uint8_t i=0; i<AMC7812_ADC_CNT; i++){
	if ( adc_status & (1<<i) ){
		uint16_t reading = ReadADC(i);
		if ( last_valid >= 0 ){
		adc_vals[last_valid] = reading;
		}
		last_valid = i;
	} else {
		adc_vals[i] = 0;
	}
	}
	// dummy read, if statement not necessary if at least one ADC is enabled
	if ( last_valid >= 0 ){
	adc_vals[last_valid] = ReadADC(0);
	}
	
	return 0;
}
Exemplo n.º 28
0
void __ISR(_TIMER_1_VECTOR, IPL5AUTO) Timer1INTHandler(void)
{
	int a;
	double perc;

	if(d1 == 25)
	{
		state = CheckButtons();

		if(state_init[3] == 1)
		{
			ReadADC(&adc1,&adc2);
			perc = (double)(adc1)/1023.00;
			SetPWMDutyCycle(3,perc);
		}
		else
		{
			perc = 1.00;
			SetPWMDutyCycle(3,perc);
		}

		d1 = 0;
	}
	else
		d1++;

	if(d2 == 1000)
	{
		if(state_init[1] == 0 && state_init[2] == 0)
		{
			LED1 = 1;
			LED2 = 1;
		}
		else if(state_init[1] == 1 && state_init[2] == 0)
		{
			LED1 = !LED1;
			LED2 = 1;
		}
		else if(state_init[2] == 1 && state_init[1] == 0)
		{
			LED2 = !LED2;
			LED1 = 1;
		}
		else if(state_init[2] == 1 && state_init[1] == 1)
		{
			a = LED1;
			LED1 = !LED1;
			LED2 = !a;
		}
		d2 = 0;
	}
	else
		d2++;
	// Clear the interrupt flag
	IFS0bits.T1IF = 0;
}
Exemplo n.º 29
0
void main (void)
{
  int count = 0;
  int analogTemp = 0;  	// A temporary place holder is needed
						// since ADC will screw with the number
						// you are assigning to, and we could
						// switch threads during that time.
  TRISB = 0;  // Output for relay
  TRISD = 0;  // Output for seven segment display
  TRISC = 0;  // More output for seven segment display
  TRISAbits.TRISA0 = 1; // Analog input
  writeNum(0);

  // Enable timer interrupt
  Flags.Byte = 0;
  INTCON = 0x20;                //disable global and enable TMR0 interrupt
  INTCON2 = 0x84;               //TMR0 high priority
  RCONbits.IPEN = 1;            //enable priority levels
  TMR0H = 0;                    //clear timer
  TMR0L = 0;                    //clear timer
  T0CON = 0x84;                 //set up timer0 - prescaler 1:8
  INTCONbits.GIEH = 1;          //enable interrupts
		
  while(1){
    OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_0_TAD,
       ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS,
	   0b1011);
    SetChanADC(ADC_CH0);
    ConvertADC(); // Start conversion
    while( BusyADC() ); // Wait for ADC conversion
    analogTemp = ReadADC(); // Read result and put in temp
    CloseADC();
    analogInput = analogTemp >> 6; // Get only the most significant bits

	// If we didn't change values since last time, we are in a holding state
	if( analogPrevious == analogInput && !hold) hold = 1;
    else if (analogPrevious != analogInput){
 	    acceptedNum = 0;
		hold = 0;
    }
	
	// Turn off potential for activating the relay if we aren't on 0
    if(analogInput) PORTB = 0;


	// If we are allowing the second relay activation and we are on 0
    if(secondRun && !analogInput) {
      PORTB = 0xFF;
      secondRun = 0;
	  passDigit = 0;
	}

    analogPrevious = analogInput;
    writeNum(analogInput);
  }
}
Exemplo n.º 30
0
void adc_int_handler()
{
    unsigned int result;
    result = ReadADC();
    result = result /4;
    //Handle_i2c_data_save(1,&result);
    //unsigned char buf = 'a';
    //ToMainLow_sendmsg(5, MSGT_UART_DATA, (void *) buf);
    //uart_write(1,&buf);
}