#include /******************************************************** * * Program Title: ADC Using PICKit 1 RP1 and LED D0 through D7 * * Program File Name: Lab_4A.c * * Microprocessors A 17.383 * * xxxxxxxx - Put in Semester (i.e. Fall 2010) here * * xxxxxxxx - Put in your name here * * xx/xx/xx - Put date here * ********************************************************/ /******************************************************** * Function: main * * Description: D0 - D7 on PICkit 1 will Display the results of the ADC * * Notes: * * RA0 - Input from RP1 * * * * Returns: This routine contains an infinite loop * ********************************************************/ /* Configuration Word */ __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \ & UNPROTECT & BORDIS & IESODIS & FCMDIS); void PORTA_init(void); void ADC_Disp(void); void Delay_LED_On(void); int ADC_Value = 0; const char PORTA_Value[8] = { 0b010000, // D0 0b100000, // D1 0b010000, // D2 0b000100, // D3 0b100000, // D4 0b000100, // D5 0b000100, // D6 0b000010}; // D7 const char TRISA_Value[8] = { 0b001111, // D0 0b001111, // D1 0b101011, // D2 0b101011, // D3 0b011011, // D4 0b011011, // D5 0b111001, // D6 0b111001}; // D7 main() { PORTA_init(); ANSEL = 1; // Just RA0 is an Analog Input TRISA0 = 1; // Corresponding TRIS bit is set as input ADCON0 = 0b00000001; // Turn on the ADC // Bit 7 - Left Justified Sample // Bit 6 - Use VDD // Bit 4:2 - Channel 0 // Bit 1 - Do not Start // Bit 0 - Turn on ADC ADCON1 = 0b00010000; // Select the Clock as Fosc/8 ADC_Disp(); GODONE = 1; // Start A/D Conversion while(1 == 1) // Loop Forever { if (GODONE == 0) // Is A/D Conversion complete? { ADC_Disp(); // Display A/D Conversion Results ADC_Value = ADRESH; // Get new A/D value GODONE = 1; // Start the next A/D Conversion } else // A/D Conversion still in progress ADC_Disp(); } } /******** END OF main ROUTINE ***************************/ /******************************************************** * Function: PORT_init * * Description: Initializes PORTA to a known condition * * Notes: None * * Returns: None * ********************************************************/ void PORTA_init(void) { PORTA = 0; // All PORTA Pins are low CMCON0 = 7; // Turn off Comparators ANSEL = 0; // Turn off ADC return; } /******** END OF PORTA_init ****************************/ /******************************************************** * Function: ADC_Disp * * Description: Displays the value of A/D Conversion on D0 - D7 * * Notes: * * * * Returns: None * ********************************************************/ void ADC_Disp(void) { int i; for (i = 0; i < 8; i++ ) { // Loop through Each of the 8 LEDS Delay_LED_On(); // Allows time for individual LEDs to light if ((ADC_Value & (1 << i)) == 0) PORTA = 0; else PORTA = PORTA_Value[i]; TRISA = TRISA_Value[i]; } // return; } /******** END OF ADC_Disp *************************/ /******************************************************** * Function: delay_LED_On * * Description: Causes a delay in program execution * * Notes: * * Delay was determined through trial and error * * Returns: None * ********************************************************/ void Delay_LED_On(void) { int j; for (j = 0; j < 60; j++); // Display "On" Loop return; } /******** END OF Delay_LED_On *************************/