//If you use win7 follow this to install Hyperterminal for win7 , see //http://h0w2.blogspot.hk/2011/09/how-to-enable-hyper-terminal-in-windows.html //run hypertrm.exe, set 57600 (may be com4 depends on machine) for using adc /********************************************************************/ /* */ /* Demo program for CUHK ARM board (2012) */ /* ADC demo (2013) */ /* */ /********************************************************************/ /* */ /* ARM CPU: Philips NXP LPC2131 */ /* Crystal clock: 11.0592 MHz */ /* Serial port baudrate: 57600 */ /* Programming tool: KEIL uVision3 RealView for ARM */ /* */ /********************************************************************/ #include #define NEWLINE sendchar(0x0a); sendchar(0x0d) //=============================== void Init_Serial_A(void) { U0LCR = 0x83; //8 bit length ,DLAB=1 U0DLL = 0x0F; //Pclk/(16*baudrate)=(11059200 x 5)/(4 x 16 x 57600); U0DLM = 0x00; U0LCR = 0x03; //DLAB=0 } char getchar(void) { volatile char ch = '0'; while ((U0LSR&&0x1)==0) // wait until receive a byte ; ch = U0RBR; // receive character return ch; } void sendchar(char ch) { while( (U0LSR&0x40)==0 ); U0THR = ch; // Transmit next character } int print(char *p) { while(*p!='\0') { sendchar(*p++); } return(0); } void putint(int count) { sendchar('0' + count/10000); sendchar('.'); sendchar('0' + (count/1000) % 10); sendchar('0' + (count/100) % 10); } int read_sensor(int channel) { int temp; ADCR=0x1<>=6; //ADDR p6-p15 = RESULT temp&=0x3ff;//10 bits result return (temp*33); //return 0-3V value precision is 1024 } int main(void) { PINSEL0 = 0x00000005; // set p0.0 to TXD0, p0.1 to RXD0 and the rest to GPIO PINSEL1 = 0x00400000; // set p0.27 to ad0.0 and the rest to GPIO //PINSEL1 |= 0x01000000; // set p0.28 to ad0.1 //PINSEL1 |= 0x04000000; // set p0.29 to ad0.2 //PINSEL1 |= 0x10000000; // set p0.30 to ad0.3 //PINSEL1 |= 0x00040000; // set p0.25 to ad0.4 Init_Serial_A(); // Init COM port NEWLINE; print("======================================================================="); NEWLINE; print("* *"); NEWLINE; print("* CUHK Computer Science and Engineering Department *"); NEWLINE; print("* LPC2131 ARM Board (2012) *"); NEWLINE; print("* ADC demo (2013) *"); NEWLINE; print("* *"); NEWLINE; print("======================================================================="); NEWLINE; NEWLINE; while(1) { print("Sensor value = "); putint(read_sensor(0)); print(" volts"); NEWLINE; NEWLINE; } }