#include #define RS_CTRL 0x08000000 #define EN_CTRL 0x10000000 #define DT_CTRL 0x07800000 unsigned long int temp1 = 0, temp2 = 0, i, j; unsigned char flag1 = 0, flag2 = 0; unsigned long result, y; // ADC variables unsigned char adc_string[10]; // Buffer for ADC result string void lcd_write(void); void port_write(void); void delay_lcd(unsigned long); void int_to_string(unsigned long num, unsigned char* str); unsigned long int init_command[] = {0x30, 0x30, 0x30, 0x20, 0x28, 0x0C, 0x01, 0x80}; int main() { // LCD GPIO setup LPC_GPIO0->FIODIR = DT_CTRL | RS_CTRL | EN_CTRL; // ADC setup LPC_PINCON->PINSEL3 = 3 << 28; // P1.30 function 3 LPC_SC->PCONP = 1 << 12; // power control LPC_ADC->ADCR = 1 << 4 | 1 << 16 | 1 << 21; // ADC4, burst mode, enable LPC_ADC->ADINTEN = (1 << 4); // Interrupt enable NVIC_EnableIRQ(ADC_IRQn); // LCD initialization flag1 = 0; for (i = 0; i < 8; i++) { temp1 = init_command[i]; lcd_write(); } flag1 = 1; while(1) { // Convert ADC result to string and display int_to_string(result, adc_string); // Clear display and reset cursor flag1 = 0; temp1 = 0x01; // Clear display lcd_write(); temp1 = 0x80; // Set cursor to home lcd_write(); flag1 = 1; // Display the ADC result i = 0; while(adc_string[i] != '\0') { temp1 = adc_string[i]; i++; lcd_write(); } delay_lcd(5000000); // Update every ~1 second } } void ADC_IRQHandler(void) { result = (LPC_ADC->ADGDR & (0xFFF << 4)) >> 4; // Read 12-bit ADC result y = (LPC_ADC->ADDR4 & (0xFFF << 4)) >> 4; // Done bit reset } void int_to_string(unsigned long num, unsigned char* str) { int i = 0, j; unsigned char temp; // Handle zero case if (num == 0) { str[0] = '0'; str[1] = '\0'; return; } // Convert number to string (reverse order) while (num > 0) { str[i++] = (num % 10) + '0'; num /= 10; } str[i] = '\0'; // Reverse the string for (j = 0; j < i/2; j++) { temp = str[j]; str[j] = str[i-1-j]; str[i-1-j] = temp; } } void lcd_write(void) { flag2 = (flag1 == 1) ? 0 : (((temp1 == 0x30) || (temp1 == 0x20)) ? 1 : 0); temp2 = temp1 & 0xf0; temp2 = temp2 << 19; port_write(); if (!flag2) { temp2 = temp1 & 0x0f; temp2 = temp2 << 23; port_write(); } } void port_write(void) { LPC_GPIO0->FIOPIN = temp2; if (flag1 == 0) { LPC_GPIO0->FIOCLR = RS_CTRL; } else { LPC_GPIO0->FIOSET = RS_CTRL; } LPC_GPIO0->FIOSET = EN_CTRL; delay_lcd(100); LPC_GPIO0->FIOCLR = EN_CTRL; delay_lcd(500000); } void delay_lcd(unsigned long r1) { unsigned long r; for (r = 0; r < r1; r++); return; }