Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 5 months ago.
MBED LPC1768 ADC sampling?
Hi,
Here's the problem that I've already posted on the MPLAB forums but I thought I might post it here too in case anyone has any ideas. Take a look:
......the only thing I can't get to work is the adc. Does anyone have any experience with the MBED LPC1768? I'm doing testing with a small 40kHz transducer. Below is just the 40khz emission and sampling code for the PIC and that I've re-written for the MBED. Any obvious suggestions? I'll post this on the MBED website too.
This: [code]
- include <xc.h> include file for port definitions etc CONFIG1
- pragma config FOSC = INTOSC Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
- pragma config WDTE = OFF Watchdog Timer Enable (WDT disabled)
- pragma config PWRTE = OFF Power-up Timer Enable (PWRT disabled)
- pragma config MCLRE = ON MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
- pragma config CP = OFF Flash Program Memory Code Protection (Program memory code protection is disabled)
- pragma config CPD = OFF Data Memory Code Protection (Data memory code protection is disabled)
- pragma config BOREN = ON Brown-out Reset Enable (Brown-out Reset enabled)
- pragma config CLKOUTEN = OFF Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
- pragma config IESO = ON Internal/External Switchover (Internal/External Switchover mode is enabled)
- pragma config FCMEN = ON Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled) CONFIG2
- pragma config WRT = OFF Flash Memory Self-Write Protection (Write protection off)
- pragma config PLLEN = ON PLL Enable (4x PLL enabled)
- pragma config STVREN = ON Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
- pragma config BORV = HI Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.)
- pragma config LVP = ON Low-Voltage Programming Enable (Low-voltage programming enabled)
- define SCAN_LENGTH 300 number of samples to scan
- define THRESHOLD 600 ADC threshold for detector
- define MIN_RANGE 55 minimum range in samples ********** InitIO function (initialises ports etc) ********** void InitIO(void) { OSCCON = 0x72; set clock to internal 8MHz (2MHz instruction cycle) TRISAbits.TRISA4 = 0; set RA4 to output (all ports are inputs by default) PORTAbits.RA4 = 0; set RA4 to logic zero TRISAbits.TRISA5 = 0; set RA5 to output (all ports are inputs by default) PORTAbits.RA5 = 0; set RA5 to logic zero TRISAbits.TRISA1 = 0; set RA1 to output (all ports are inputs by default) PORTAbits.RA1 = 0; set RA1 to logic zero TRISAbits.TRISA0 = 0; set RA0 to output (all ports are inputs by default) PORTAbits.RA0 = 0; set RA0 to logic zero TRISAbits.TRISA2 = 1; Set RA2 to input (ADC input) ANSELA = 0x04; Select AN2 as analog input ADCON0 = 0x09; AN2 selected, ADC enabled ADCON1 = 0xC0; right justified, Fosc/4, Vref = Vdd } ********** main function ********** int i,k,result; void main(void) { InitIO(); initialise IO while(1) infinite loop { TRISAbits.TRISA4 = 0; set RB0 to output transmit pulse for(i=0; i<20; i++) 20 cycles at 40kHz (complementary output on RB4 and RB5) { PORTA = 0x10; _delay(22); delay PORTA = 0x00; _delay(6); delay } PORTA = 0; both ports cleared TRISAbits.TRISA4 = 1; set RA4 to input (high impedance so as not to load Rx) PORTAbits.RA0 = 1; indicate start of sampling period on RA0 for(i=0; i<SCAN_LENGTH; i++f) sample echoes { ADCON0 = 0x0B; Start conversion (set bit 2) while(ADCON0 & 0x02); Wait for conversion to complete result = ADRESL + (ADRESH << 8);
PORTAbits.RA1 = 0;
} PORTAbits.RA0 = 0; turn off when threshold crossed
} }
[/code]
To this:
[code]
include "mbed.h"
- include "C12832.h" DigitalOut pin21(p21); pins used for signal generation DigitalInOut pin22(p22); AnalogIn ain(p15); analog input pin
- define SCAN_LENGTH 300 number of samples to scan
- define THRESHOLD 600 ADC threshold for detector
- define MIN_RANGE 55 minimum range in samples
C12832 lcd(p5, p7, p6, p8, p11);
float result; int i,j,k;
int main() { while(1) {
pin3.output(); reset pin3 as output pin3 = 0;
for(i=0; i<20; i++) 20 cycles at 40kHz (complementary output on RB4 and RB5) { pin2 = 1; generate 40khz wave pin3 = 0; wait(0.0000125); pin2 = 0; pin3 = 1; wait(0.0000125); delay }
pin3.input(); set pin 3 to high impedance/input
for(i=0; i<SCAN_LENGTH; i++) sample echoes
{ result = ain.read();
} } }
[/code]
I've displayed the 'result' on the mbed application board screen and it's all over the place ranging from 0 up to 1? I've even created a total from 1000 samples with nothing in front of the transducer that I've then compared with when there's metal plate in front of the transducer and the nothing changes.
Thanks.