hello, i am new here and asking for help to do this:
i use an LPC2132 to generate video chars, and need to read the output of an analog sensor that gives 0,3v-3,2v and on other AD port read the voltage of an 12v battery, the only thing that i know is that i have to make an voltage divider for the bat.
can anyone help me with some example to result in a function like “volts=read_ad(port_x)”
thanks in advance for your help
Here’s some code for an LPC2148:
//-------------------- initialisation stuff --------------------//
void Initialise(void)
{
volatile unsigned int x, i, j, c;
unsigned char temp[10];
// initialise PLL
PLL0CFG=0x24; // Cclk = 60Mhz
PLL0CON=0x01;
PLL0FEED=0xAA;
PLL0FEED=0x55;
while(!(PLL0STAT & 0x0400))
;
PLL0CON=0x3;
PLL0FEED=0xAA;
PLL0FEED=0x55;
// initialise MAM and VPB
MAMTIM=0x3; // 3 cycles to read from FLASH
MAMCR=0x2; // MAM functions fully enabled
APBDIV=0x02; // Pclk = 30MHz
//initialise pclk (3 MHz)
unsigned long pclk = liblpc2000_get_pclk(liblpc2000_get_cclk(OSCILLATOR_CLOCK_FREQUENCY));
// initialise TIMER0
T0TCR = 0; /* Reset timer 0 */
T0PR = 0; /* Set the timer 0 prescale counter */
T0MR0 = pclk/50 - 1; /* Set time 0 match register to generate an interrupt every 20 ms */
T0MCR = 3; /* Generate interrupt and reset counter on match */
T0TCR = 1; /* Start timer 0 */
// setup CTL stuff for Timer 0
ctl_set_isr(4, 0, CTL_ISR_TRIGGER_FIXED, timer0ISR, 0);
ctl_unmask_isr(4);
x = ctl_get_ticks_per_second();
// initialise ADC
PINSEL0 = PINSEL0 | 0x00003C00; // P0.5, P0.6 setup for AD0.7, AD1.0 inputs
// bits 11:10 13:12
// 11 11
PINSEL1 = 0x15000000; // P0.28, P0.29, P0.30 set for AD0.1, AD0.2, AD0.3 inputs
// bits 25:24 27:26 30:29
// 01 01 01
AD0CR = 0x00200604; // enable ADC, 11 clocks/10 bits, ADC clock = 4.286 MHz
AD1CR = 0x00208001;
// initialise GPIOs
IO1DIR |= 0x000F0000; // P1.16, P1.17, P1.18, P1.19 outputs
IO0DIR |= 0x00800100; // P0.23, P0.8 outputs
IO0SET |= 0x00800100; // set them high
//initialise timer ticks
ticks = 0;
// clear status flags
status = 0;
panic_state = 0;
// initialise Rx circular buffer
uart1_rx_extract_idx = uart1_rx_insert_idx = 0;
// initialise watchdog timer
// timer period = (Tpclk x 4) x WDTC value
WDTC = 0xFFFFFFFF; // set constant reload value
WDMOD = 0x00000003; // watchdog reset mode
WDFEED = 0xAA; // start watchdog
WDFEED = 0x55;
/*
//test UART1 comms with motor controller
while(1)
{
_puts1("EN"); //enable responses ("OK") to commands
if (uart1_rx_buffer[0] == 'O')
break;
delay(100);
}
*/
// initialise UART0 (RxD0 P0.1, TXD0 P0.0)
U0LCR = 0x83; // set DLAB. 8N1
U0DLL = 0xC3; // set for 9600 Baud
U0DLM = 0x00; // set for 9600 Baud
U0LCR &= ~0x80; // disable DLAB
U0FCR = 0x00000001; // enable FIFO
PINSEL0 = PINSEL0 & (~0xF)| 0x5; // enable UART0 pins
// initialise UART1 (RxD1 P0.9, TxD1 P0.8
U1LCR = 0x83; // 8 bit, 1 stop bit, no parity, enable DLAB
U1DLL = 0xC3; // set for 9600 baud
U1DLM = 0x00;
U1LCR &= ~0x80; // disable DLAB
U1FCR = 1; // enable FIFO
PINSEL0 = PINSEL0 & ~(0xFFFF << 16) | (0x5555 << 16);
// Setup UART0 RX interrupt
/*
ctl_set_isr(7, 1, CTL_ISR_TRIGGER_FIXED, uart0ISR, 0);
ctl_unmask_isr(7);
U0IER = 1;
*/
//send "ANSW0" to motor and disable it
_puts1("ANSW0");
_puts1("DI");
// Setup UART1 RX interrupt
ctl_set_isr(7, 1, CTL_ISR_TRIGGER_FIXED, uart1ISR, 0);
ctl_unmask_isr(7);
U1IER = 1;
// enable global interrupts
ctl_global_interrupts_enable();
}
//-------------------------- getAnalogueInput_AD0 function ---------------//
unsigned short int getAnalogueInput_AD0(unsigned char channel)
{
unsigned short int val;
//start conversion (for selected channel)
AD0CR = (AD0CR & 0xFFFFFF00) | (1 << channel) | (1 << 24);
//wait until done
while((AD0GDR & 0x80000000) == 0)
;
// get ADC data
val = ((AD0GDR & 0x0000FFC0)) >> 6;
return(val);
}
Delete the stuff you don’t need.
Leon
first of all, thanks
can i use some of that code in LPC2132, the addresses are the same?
can you simplify the code to only use to read the AD ports?
i am newbie in microcontrollers :oops:
thanks
You need to check the data sheet for the addresses. Delete the unwanted code yourself.
Leon