// File : m11_adc.c #include #define ADC_DIVIDE_OSC_2 1 #define ADC_DIVIDE_OSC_4 2 #define ADC_DIVIDE_OSC_8 3 #define ADC_DIVIDE_OSC_16 4 #define ADC_DIVIDE_OSC_32 5 #define ADC_DIVIDE_OSC_64 6 #define ADC_DIVIDE_OSC_128 7 #define MAX_ADC 12 unsigned char channel; unsigned char adc_intr; unsigned int adc_result; unsigned char value; void m11_pwm_clock(unsigned char divide) { PWMCON &= 0x8F; PWMCON += (divide << 4); } void m11_adc_clock_divide(unsigned char divide) { ADCON |= 0x04; m11_pwm_clock(divide); } void m11_adc_channel_select_all() { ADCSELH = 0x00; ADCSEL &= 0x0F; } void adc_enable() { value = ADCON; value |= 0x80; ADCON = value; } void adc_disable() { value = ADCON; value &= 0x7f; ADCON = value; } void adc_request() { value = ADCON; value |= 0x40; ADCON = value; } void adc_clear_interrupt_flag() { value = ADCON; value &= 0xef; ADCON = value; } void m11_adc_mux_selection(unsigned char channel) { ADCSEL &= 0xF0; ADCSEL += channel; } unsigned int adc_10bit_result() { unsigned int temp,temp1; temp = ADCON & 0x03; temp1 = ADCR << 2; temp1 += temp; return temp1; } void adc_interrupt_enable() { EADC = 1; } void adc_run(unsigned char channel) { m11_adc_mux_selection(channel); adc_request(); while (!adc_intr); adc_intr = 0; adc_result = adc_10bit_result(); } void enable_all_interrupt() { EA = 1; } void adc_port_pullup_off() { // P0.1 ~ P0.7, P2.2 ~ P2.6 P0SEL |= 0x7F; P2SEL |= 0x7C; } void adc_init() { adc_intr = 0; adc_port_pullup_off(); enable_all_interrupt(); adc_interrupt_enable(); adc_enable(); m11_adc_clock_divide(ADC_DIVIDE_OSC_2); m11_adc_channel_select_all(); for(channel=0;channel