?? m10_adc.txt
字號:
// File : m10_adc.c
#include <GC80C520_PL44I.H>
#define ADC_DIVIDE_OSC_2 0
#define ADC_DIVIDE_OSC_4 1
#define ADC_DIVIDE_OSC_8 2
#define ADC_DIVIDE_OSC_16 3
#define ADC_DIVIDE_OSC_32 4
#define MAX_ADC 4
unsigned char channel;
unsigned char adc_intr;
unsigned int adc_result;
unsigned char value;
void m10_adc_clock_divide(unsigned char divide)
{
value = ADCSEL;
value &= 0x1f;
value = value + (divide << 5);
ADCSEL = value;
}
void m10_adc_channel_selection(unsigned char adc_num)
{
value = ADCSEL;
value &= ~(1 << adc_num);
ADCSEL = value;
}
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 m10_adc_mux_selection(unsigned char channel)
{
value = ADCON;
value &= 0xf3;
value = value + (channel << 2);
ADCON = value;
}
unsigned int adc_9bit_result()
{
unsigned int temp,temp1;
temp = ADCON & 0x01;
temp1 = ADCR << 1;
temp1 += temp;
return temp1;
}
void adc_interrupt_enable()
{
EADC = 1;
}
void adc_run(unsigned char channel)
{
m10_adc_mux_selection(channel);
adc_request();
while (!adc_intr);
adc_intr = 0;
adc_result = adc_9bit_result();
}
void enable_all_interrupt()
{
EA = 1;
}
void port0_pullup_off()
{
P0SEL = 0xff;
}
void adc_init()
{
adc_intr = 0;
port0_pullup_off();
enable_all_interrupt();
adc_interrupt_enable();
adc_enable();
m10_adc_clock_divide(ADC_DIVIDE_OSC_2);
for(channel=0;channel<MAX_ADC;channel++) // MAX_ADC
{
m10_adc_channel_selection(channel);
}
for(channel=0;channel<MAX_ADC;channel++) //MAX_ADC
{
adc_run(channel);
}
}
void adc_int(void) interrupt ADC_VECTOR
{
adc_clear_interrupt_flag();
adc_intr = 1;
}
void main()
{
adc_init();
while(1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -