/* * INITIALIZATION AND MISC */ void setup_1(void) { uint8_t i; IFI_Initialization(); /* Initialize serial port communication. */ statusflag.b.NEW_SPI_DATA = 0; usart1_open(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, kBaud115); delay1ktcy(50); /* Make the master control all PWMs (for now) */ txdata.pwm_mask.a = 0xFF; for (i = IX_DIGITAL(1); i <= IX_DIGITAL(CT_DIGITAL); ++i) { digital_setup(i, false); } /* Init ADC */ /* Setup the number of analog sensors. The PIC defines a series of 15 * ADC constants in mcc18/h/adc.h that are all of the form 0b1111xxxx, * where x counts the number of DIGITAL ports. In total, there are * sixteen ports numbered from 0ANA to 15ANA. */ if (NUM_ANALOG_VALID(USER_CT_ANALOG) && USER_CT_ANALOG > 0) { /* ADC_FOSC: Based on a baud_115 value of 21, given the formula * FOSC/(16(X + 1)) in table 18-1 of the PIC18F8520 doc the * FOSC is 40Mhz. * Also according to the doc, section 19.2, the * ADC Freq needs to be at least 1.6us or 0.625MHz. 40/0.625=64 * (Also, see table 19-1 in the chip doc) */ #if defined(MCC18) OpenADC( ADC_FOSC_64 & ADC_RIGHT_JUST & ( 0xF0 | (16 - USER_CT_ANALOG) ) , ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS ); #elif defined(SDCC) adc_open( ADC_CHN_0, ADC_FOSC_64, ADC_CFG_16A, ADC_FRM_RJUST | ADC_INT_OFF | ADC_VCFG_VDD_VSS ); #else #error "Bad Comp" #endif } else { /* TODO: Handle the error. */ puts("ADC is disabled"); } }
/* * INITIALIZATION AND MISC */ void setup_1(void) { uint8_t i; IFI_Initialization(); /* Prevent the "slow loop" from executing until data has been received * from the master processor. */ statusflag.NEW_SPI_DATA = 0; /* Enable autonomous mode. FIXME: Magic Number (we need an enum of valid "user_cmd"s) */ /* txdata.user_cmd = 0x02; */ /* Make the master control all PWMs (for now) */ txdata.pwm_mask.a = 0xFF; /* Initialize all pins as inputs unless overridden. */ for (i = 0; i < 16; ++i) { pin_set_io( i, kInput); } /* Initialize Serial */ Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, kBaud115); Delay1KTCYx( 50 ); /* Settling time (5K Clock ticks) */ /* Init ADC */ /* Setup the number of analog sensors. The PIC defines a series of 15 * ADC constants in mcc18/h/adc.h that are all of the form 0b1111xxxx, * where x counts the number of DIGITAL ports. In total, there are * sixteen ports numbered from 0ANA to 15ANA. */ if ( NUM_ANALOG_VALID(kNumAnalogInputs) && kNumAnalogInputs > 0 ) { /* ADC_FOSC: Based on a baud_115 value of 21, given the formula * FOSC/(16(X + 1)) in table 18-1 of the PIC18F8520 doc the FOSC * is 40Mhz. * Also according to the doc, section 19.2, the * ADC Freq needs to be at least 1.6us or 0.625MHz. 40/0.625=64 * (Also, see table 19-1 in the chip doc) */ OpenADC( ADC_FOSC_64 & ADC_RIGHT_JUST & ( 0xF0 | (16 - kNumAnalogInputs) ) , ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS ); } }
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; } }
uint16_t analog_get(AnalogInIndex ain) { if ( ain > kAnalogSplit ) { /* get oi data */ /* we may not want to trust "ain" */ return rxdata.oi_analog[ ain - kAnalogSplit ]; } /* kNumAnalogInputs should be checked somewhere else... preferably at * compile time. */ else if ( ain < kNumAnalogInputs && NUM_ANALOG_VALID(kNumAnalogInputs) ) { /* read ADC */ SetChanADC(ain); Delay10TCYx( 5 ); /* Wait for capacitor to charge */ ConvertADC(); while( BusyADC() ); return ReadADC(); } else { return 0xFFFF; } }