/* C52ADC.C 4-channel 8-bit resolution ADC and 6-bit input port for C52-EVB Copyright 2000 Wichit Sirichote start conversion by high to low signal at RB0, 40-bit data is then shifted out through PA4 with low-to-high trnasition at RB0 The format is started from ADC0,ADC1,ADC2,ADC3,port_B ----________---___---___---___---___---___---___---~ ^ ^ ~60uS LSB after high-to-low starting 711 chip to read analog input and port B, at least 60 microseconds required before low-to-high data shifted out to begin. */ #include #fuses RC,NOPROTECT,NOWDT,PUT,BROWNOUT byte buffer[5]; read_analog() { byte i; for(i = 0; i < 4; i++) { set_adc_channel(i); delay_us(5); buffer[i] = read_adc(); } } read_portB() { buffer[4] = port_B; } send_data_out() { byte i; for(i=0;i<40;++i){ // 5 bytes while (!input(PIN_B0)); output_bit(PIN_A4,shift_right(buffer,5,0)); while (input(PIN_B0));} // shifted out when RB0 change from LOW to HIGH } /*This shifts 40 bits out Pin_A4, LSB first.*//*This shifts 40 bits out Pin_A4, LSB first.*/ // ___---___---___---___---____ // LSB ......................... MSB main() { port_b_pullups(TRUE); setup_counters(RTCC_INTERNAL,RTCC_DIV_2); setup_port_a(ALL_ANALOG); setup_adc(ADC_CLOCK_INTERNAL); while(1){ while(input(PIN_B0)); // wait High to Low trigger read signal from C-52EVB read_analog(); read_portB(); send_data_out(); // send 40 bits in buffer array to PIN_A4 } }