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); } }
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; }
/******************************************************************************* * FUNCTION NAME: Get_Analog_Value * PURPOSE: Reads the analog voltage on an A/D port and returns the * 10-bit value read stored in an unsigned int. * CALLED FROM: user_routines.c, typically * ARGUMENTS: * Argument Type IO Description * ----------- ------------- -- ----------- * ADC_channel alias I alias found in ifi_aliases.h * RETURNS: unsigned int *******************************************************************************/ unsigned int Get_Analog_Value (unsigned char ADC_channel) { unsigned int result; OpenADC( ADC_FOSC_RC & ADC_RIGHT_JUST & ifi_analog_channels, ADC_channel & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS ); Delay10TCYx( 10 ); ConvertADC(); while( BusyADC() ); ReadADC(); CloseADC(); result = (int) ADRESH << 8 | ADRESL; return result; }
unsigned int MPX6115_uiGetPression(void) { int iResult; unsigned long ulPression; OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_12_TAD, ADC_CH0 & ADC_INT_OFF, 0); //open adc port for reading ADCON1 =0x00; //set VREF+ to VDD and VREF- to GND (VSS) SetChanADC(ADC_CH5); //Set ADC to Pin 5 Delay1KTCYx(5); ConvertADC(); while( BusyADC() ); iResult = ReadADC(); CloseADC(); ulPression=((unsigned long)iResult*5000/1024 + 475)*10 /45; #ifdef DEBUG printf("Pression %ld %x\n\r",ulPression,iResult); #endif ADCON1=0b00001111; // Digital Channel Allocation return (unsigned int)ulPression; }
unsigned int ADC_call(char c) { int adc; char ADC_CHAN; if (c == 0) ADC_CHAN = ADC_CH9; else if (c == 1) ADC_CHAN = ADC_CH8; else if (c == 2) ADC_CHAN = ADC_CH10; else ADC_CHAN = ADC_CH12; OpenADC(ADC_FOSC_32 & ADC_LEFT_JUST & ADC_2_TAD , ADC_CHAN & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS , 0); Delay10TCYx(5); // Delay for 5us ConvertADC(); // Start conversion while(BusyADC()); // wait for completion adc = ReadADC(); // Read result CloseADC(); // Disable A/D converter return adc; }
// This program // 1) Polls A/D conversions from 2 Proximity void main (void){ unsigned int result; uart_comm uc; uart_thread_struct uthread_data; // info for uart_lthread timer1_thread_struct t1thread_data; // info for timer1_lthread timer0_thread_struct t0thread_data; // info for timer0_lthread // set to run really, really fast... OSCCON = 0x6C; // 4 MHz OSCTUNEbits.PLLEN = 1; // 4x the clock speed in the previous line // initialize my uart recv handling code init_uart_recv(&uc); // init the timer1 lthread init_timer1_lthread(&t1thread_data); TRISAbits.TRISA0 = 1; //AN0 equals input TRISCbits.TRISC7 = 0;//1; //Rx equals input TRISCbits.TRISC6 = 0; //TX equals output TRISCbits.TRISC3 = 0; //cl equals output TRISCbits.TRISC4 = 0; //DA equals output //initialize all output pins to 0 LATCbits.LATC6 = 0; LATCbits.LATC3 = 0; LATCbits.LATC4 = 0; //Set PortB(leds) for output TRISB = 0; PORTB = 0; // set direction for PORTB to output LATB = 0; // initialize ADC OpenADC( /*config*/ ADC_FOSC_RC & ADC_RIGHT_JUST & ADC_20_TAD, /*config2*/ ADC_CH1 & ADC_REF_VDD_VSS & ADC_INT_OFF, /*portconfig*/ ADC_4ANA ); // set up PORTA for input /* PORTA = 0x0; // clear the port LATA = 0x0; // clear the output latch //ADCON1 = 0x0F; // turn off the A2D function on these pins // Only for 40-pin version of this chip CMCON = 0x07; // turn the comparator off //TRISA = 0x0F; // set RA3-RA0 to inputs */ // initialize Timers OpenTimer0( TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_128); OpenTimer1( TIMER_INT_ON & T1_PS_1_1 & T1_16BIT_RW & T1_SOURCE_INT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF); // Peripheral interrupts can have their priority set to high or low // enable high-priority interrupts and low-priority interrupts enable_interrupts(); // Decide on the priority of the enabled peripheral interrupts // 0 is low, 1 is high // Timer1 interrupt IPR1bits.TMR1IP = 0; // USART RX interrupt IPR1bits.RCIP = 0; // I2C interrupt IPR1bits.SSPIP = 1; // configure the hardware USART device OpenUSART( USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 0x19); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //My Stuff ///////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // result = factoryReset(); // initialize(); // result = joinNetwork(); // result = openConnection(); // sendGoalBlack(); while(1) { LATBbits.LATB0 = 1; Delay10KTCYx(0); Delay10KTCYx(0); LATBbits.LATB0 = 0; Delay10KTCYx(0); } CloseADC(); }