Example #1
0
unsigned int io_read_analog(unsigned char port)

{
    unsigned int    result;
    unsigned char   channel;
#ifndef SDCC
    static unsigned char inputs[] = {
	ADC_CH0, ADC_CH1, ADC_CH2, ADC_CH3,
	ADC_CH4, ADC_CH5, ADC_CH6, ADC_CH7,
	ADC_CH8, ADC_CH9, ADC_CH10, ADC_CH11,
	ADC_CH12, ADC_CH13, ADC_CH14, ADC_CH15 };
#endif

    /* Make sure port does not exceed current Analog_ports */
    if ( ! VALID_ANALOG_PORT(port) )
	return OV_BAD_PARAM;

#ifdef SDCC
    result = 0;

    channel = port - 1;

    /*
     *  SDCC generic adc_open() doesn't work for the 8520.  Lots of
     *  stuff missing.
     */
    adc_open8520(channel);

    /* Allow settling time before starting a conversion */
    delay10tcy(10);
    
    adc_conv();
    while ( adc_busy() )
	;
    result = adc_read();
    adc_close();

#else   /* MCC18 */

    channel = inputs[port - 1];

    OpenADC(ADC_FOSC_RC & ADC_RIGHT_JUST & Analog_ports_const,
	  channel & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS);
    Delay10TCYx(10);
    ConvertADC();
    while (BusyADC())
	;
    ReadADC();
    CloseADC();
    result = (unsigned int)ADRESH << 8 | ADRESL;
#endif

    return result;
}
Example #2
0
File: hax.c Project: mkoval/hax
uint16_t analog_get(index_t index)
{
	if (IX_ANALOG(1) <= index && index <= IX_ANALOG(USER_CT_ANALOG) && NUM_ANALOG_VALID(USER_CT_ANALOG)) {
		/* Read ADC (0b10000111 = 0x87). */
		uint8_t chan = 0x87 | (IX_ANALOG_INV(index) << 3);
		adc_setchannel(chan);
		delay10tcy(5); /* Wait for capacitor to charge */
		adc_conv();
		while(adc_busy());
		return adc_read();
	} else {
		WARN_IX(index);
		return 0;
	}
}
Example #3
0
void init_user(){
  uint8_t i;

  init_display();

  TRISBbits.TRISB2 = 1;

  // SDCC is missing the TRISA7 bit in the define...
#if 0
  TRISAbits.TRISA0 = 1;
  TRISAbits.TRISA6 = 1;
  TRISAbits.TRISA7 = 1;
#else
  TRISA = TRISA | b(11010001);
#endif

  // Setup pwm and adc for the led brightness control
  ADCON0 = b(00000001);
  ADCON1 = b(01111110);
  ADCON2 = b(00000000);

  CCP1CON = b(00001100);
  PR2 = 0xFF;
  T2CON = b(00000100);
  CCPR1L = 0x00;

  debounce_init(hours_debounce,hours_switch_raw);
  debounce_init(mins_debounce,mins_switch_raw);
  debounce_init(secs_debounce,secs_switch_raw);

  if (!hours_switch_raw && !mins_switch_raw && secs_switch_raw){
    mode = fast_clock;
  } else if (hours_switch_raw && !mins_switch_raw && !secs_switch_raw){
    mode = slow_clock;
  } else if (!hours_switch_raw && mins_switch_raw && !secs_switch_raw){
    // Oooh! Metrics display mode! Don't see that very often...
    inc_metric_meta();

    // Basically just cycle through the bytes in the eeprom and display them on
    // screen forever.

    i = 0;
    while (1){
      display_digits(
                     i / 16,i % 16,
                     CHAR_BLANK,CHAR_BLANK,
                     (((uint8_t *)(&eeprom_data))[i] / 16),
                      ((uint8_t *)(&eeprom_data))[i] % 16);

      delay10tcy(1);

      if (debounce_just_pressed(hours_debounce,hours_switch_raw)){
        i++;
        if (i >= sizeof(eeprom_data))
          i = 0;
      }
      if (debounce_just_pressed(secs_debounce,secs_switch_raw)){
        i--;
        if (i >= sizeof(eeprom_data))
          i = sizeof(eeprom_data) - 1;
      }

      debounce_add_sample(hours_debounce,hours_switch_raw);
      debounce_add_sample(secs_debounce,secs_switch_raw);
    }

  } else {
    mode = none_chosen;
  }

  if (mode == none_chosen){
    mode = eeprom_read_uint32(EEPROM_ADDR_MODE);  
  } else {
    eeprom_write_uint32(mode,EEPROM_ADDR_MODE);
    trigger_save_eeprom();
  }

  silly_hour_display = eeprom_read_uint32(EEPROM_ADDR_SILLY_HOUR_DISPLAY);
}