//****************************************************************************************** // Project : LPC1114 Sample Program // Program Name : // Programer : // Create date : Version 1.0 H24.09.29 // History : // Laungage : // ****************************************************************************************** // (1)Description // // ****************************************************************************************** #include "LPC11xx.h" // //Define function // void InitUART(void); void SendUART(unsigned char); void RecUART(unsigned char *); void SendUARTStr(unsigned char *); void InitADC(void); void GetADC(unsigned int *); void Editdata(unsigned char *,unsigned int); #define PI_DATALINE_MAX 100 #define PI_LOOP_N 1000000 int main(void) { unsigned int lp; unsigned int adc_data; unsigned char TMP_dataline[PI_DATALINE_MAX]; // ******************* // 1.Initialize // ******************* // (1)Define I/O Port LPC_IOCON->PIO1_6 = 1; // PIO1_6/RXD/CT32B0_MAT0 LPC_IOCON->PIO1_7 = 1; // PIO1_7/TXD/CT32B0_MAT1 LPC_IOCON->R_PIO0_11 = 2; // TDI/PIO0_11/AD0/CT32B0_MAT3 // (2)Initialize UART InitUART(); // (3)Initialize A/D Converter InitADC(); // (4)Put Message SendUARTStr(" LPC1114 Connected OK> "); // ******************* // 2.Program Main // ******************* while(1){ // (1)Get A/D conveter data GetADC(&adc_data); // (2)Edit data format Editdata(TMP_dataline, adc_data); // (3)Display A/D conveter data SendUARTStr(TMP_dataline); // (4)Wait for(lp=0;lpSYSAHBCLKCTRL |= (1<<12); LPC_SYSCON->UARTCLKDIV = 0x01; //UART clock DL = (SystemCoreClock * LPC_SYSCON->SYSAHBCLKDIV) / (16 * baudrate * LPC_SYSCON->UARTCLKDIV); LPC_UART->LCR = 0b10000011; // 8-bit character length, DLAB enable ( DLAB = 1) LPC_UART->DLM = DL / 256; // Determines the baud rate of the UART (MSB Register) (Access DLAB = 1) LPC_UART->DLL = DL % 256; // Determines the baud rate of the UART (LSB Register) (Access DLAB = 1) LPC_UART->LCR = 0b00000011; // 8-bit character length , DLAB disable ( DLAB = 0) LPC_UART->FCR = 0b00000111; // FIFOEN:Active high enable for both UART Rx and TX FIFOs and U0FCR[7:1] access // RXFIFORES:Writing a logic 1 to U0FCR[1] will clear all bytes in UART Rx FIFO, reset the pointer logic. // TXFIFORES:Writing a logic 1 to U0FCR[2] will clear all bytes in UART TX FIFO, reset the pointer logic. } //****************************************************************************************** // Function Name : SendUARTStr // Title : Send UART Strings // Input : // Output : //****************************************************************************************** void SendUARTStr(unsigned char in_s[]) { unsigned char lp = 0; while(in_s[lp] != 0){ SendUART(in_s[lp]); lp++; } } //****************************************************************************************** // Function Name : SendUART // Title : Send UART // Input : // Output : //****************************************************************************************** void SendUART(unsigned char in_c) { while (!(LPC_UART->LSR & (1<<5))); //THRE: Transmitter Holding Register Empty. // 0: THR contains valid data, 1: THR is empty LPC_UART->THR = in_c; } //****************************************************************************************** // Function Name : RecUART // Title : Recive UART // Input : // Output : //****************************************************************************************** void RecUART(unsigned char *out_c) { while (!(LPC_UART->LSR & 0x01)); //RDR: Receiver Data Ready //0: RBR is empty, 1: U0RBR contains valid data *out_c = LPC_UART->RBR; } //****************************************************************************************** // Function Name : InitADC // Title : Initialize A/D Converter // Input : // Output : // Description : Used AD0 //****************************************************************************************** void InitADC(void) { LPC_SYSCON->PDRUNCFG &= ~(1 << 4); //Supply ADC (ADC_PD='0') LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 13); //ADC Clock (ADC='1') // **************************************** // AD0CR(A/D Control Register) // **************************************** // AD0CR[7:0]:SEL Selects which of the AD7:0 pins is (are) to be sampled and converted. // Bit 0 selects Pin AD0, bit 1 selects pin AD1,..., and bit 7 selects pin AD7. // AD0CR[15:8]:CLKDIV The APB clock (PCLK) is divided by CLKDIV +1 to produce the clock for the ADC, which // should be less than or equal to 4.5 MHz. Typically, software should program the smallest // value in this field that yields a clock of 4.5 MHz or slightly less, but in certain cases (such // as a high-impedance analog source) a slower clock may be desirable. // CLKDIV >= ( PCLK / 4.5MHz ) - 1 // AD0CR[16]:BURST Burst mode // Remark: If BURST is set to 1, the ADGINTEN bit in the AD0INTEN register must be set to 0. // 0 Software-controlled mode: Conversions are software-controlled and require 11 clocks. // 1 Hardware scan mode: The AD converter does repeated conversions at the rate selected by the CLKS field, // scanning (if necessary) through the pins selected by 1s in the SEL field. // AD0CR[19:17]:CLKS This field selects the number of clocks used for each conversion in Burst mode, and the // number of bits of accuracy of the result in the LS bits of ADDR, between 11 clocks // (10 bits) and 4 clocks (3 bits). // 0x0 11 clocks / 10 bits // 0x1 10 clocks / 9 bits // 0x2 9 clocks / 8 bits // 0x3 8 clocks / 7 bits // 0x4 7 clocks / 6 bits // 0x5 6 clocks / 5 bits // 0x6 5 clocks / 4 bits // 0x7 4 clocks / 3 bits // AD0CR[23:20]:- Reserved // AD0CR[26:24]:START When the BURST bit is 0, these bits control whether and when an A/D conversion is started: // 0x0 No start (this value should be used when clearing PDN to 0). // 0x1 Start conversion now. // 0x2 Start conversion when the edge selected by bit 27 occurs on PIO0_2/SSEL/CT16B0_CAP0. // 0x3 Start conversion when the edge selected by bit 27 occurs on PIO1_5/DIR/CT32B0_CAP0. // 0x4 Start conversion when the edge selected by bit 27 occurs on CT32B0_MAT0[1]. // 0x5 Start conversion when the edge selected by bit 27 occurs on CT32B0_MAT1[1]. // 0x6 Start conversion when the edge selected by bit 27 occurs on CT16B0_MAT0[1]. // 0x7 Start conversion when the edge selected by bit 27 occurs on CT16B0_MAT1[1]. // AD0CR[27]: EDGE This bit is significant only when the START field contains 010-111. In these cases: 0 // 0 Start conversion on a rising edge on the selected CAP/MAT signal. // 1 Start conversion on a falling edge on the selected CAP/MAT signal. // AD0CR[31:28] - Reserved // // LPC_ADC->CR = 0b00,0000000000,0000000000,0000000000 LPC_ADC->CR = 0b00000000000000010000101000000001 ; } //****************************************************************************************** // Function Name : GetADC // Title : Get A/D Converter data // Input : // Output : // Description : Used AD0 //****************************************************************************************** void GetADC(unsigned int *adc_data) { while (((*adc_data = LPC_ADC->DR[0]) & (1<<31)) == 0); *adc_data = (*adc_data >> 6) & 0x3ff; } //****************************************************************************************** // Function Name : Editdata // Title : Edit data format // Input : // Output : //****************************************************************************************** Editdata(unsigned char put_str[],unsigned int adc_data) { unsigned int volt_data; strcpy(&put_str[0]," "); volt_data = adc_data * 3300 / 1023; sprintf(&put_str[2],"%d",volt_data); strcat(&put_str[0],"[mV] "); }