|
|
View previous topic :: View next topic |
Author |
Message |
arelone
Joined: 06 Dec 2015 Posts: 42
|
dspic30f6014a-ADC&UART |
Posted: Sun Dec 06, 2015 8:30 am |
|
|
Hi all,
I am not familiar with c programming and I have a project which requires me to use c programming . I have a project consists of 16 ultrasonic transducers which can be functions as transmitter (Tx) and receiver (Rx). I am using dspic30f6014a device with 20 MHz oscillator. In receiver mode the excitation signal is received by 16 receivers which connected to signal conditioning circuits. However in each excitation only 9 receivers are considered.
All circuits are connected to analog inputs (AN0-AN15). when all the transducers are excited 144 (16x9) measurements will be obtained. The 144 measurements (which is considered as 1 frame) will be send via UART1 at baud rate of 9600.
For simplicity of this discussion, I wrote a program for one excitation signal Tx1. When the excited signal is introduced at Tx1 the signal will be received by 9 receivers (Rx5 to Rx13) which connected to (AN4-AN12). The sample & hold (SNH) at SNH5-SNH13 will hold the maximum value at the predetermined time-of-flight (toF) which calculated from the beginning of the excitation signal and it is different for each receivers. When all 9 values are received then only it transmitted via UART1 for post processing.
I wrote a program which is very straightforward program and easy to understand and hope some advice from the expert members. Will the program work as required? For the time being my Pickit2 is malfunctioning and I am waiting for the new set to arrive.
Regards
Code: |
#include <ADC_Test#1.h>
#use rs232(baud=9600,UART1)
void main()
{
setup_adc_ports(sAN0 | sAN1 | sAN2 | sAN3 | sAN4 | sAN5 | sAN6 | sAN7 | sAN8 | sAN9 | sAN10 | sAN11 | sAN12 | sAN13 | sAN14 | sAN15, VSS_VDD);
setup_adc(ADC_CLOCK_DIV_64 | ADC_TAD_MUL_2);
//setup_adc( ADC_CLOCK_INTERNAL );
int result,ADCvalue4, ADCvalue5, ADCvalue6, ADCvalue7, ADCvalue8, ADCvalue9, ADCvalue10, ADCvalue11, ADCvalue12;
int i=4;
//I/O ports configurations(1:input, 0:output)
set_tris_a(0x0000); //set port_a as output
set_tris_b(0xFFFF); //set port_b as analog input/ADC
set_tris_c(0x0000); //set port_c as output
set_tris_d(0x0000); //set port_d as output
set_tris_f(0x0000); //set port_f as output
set_tris_g(0x0000); //set port_g as output
output_a(0x0000); //clear port_a to all 0s
output_c(0x0000); //clear port_c to all 0s
output_d(0x0000); //clear port_d to all 0s
output_f(0x0000); //clear port_f to all 0s
output_g(0x0000); //clear port_g o all 0s
while(TRUE)
{
//Total time taken (excitation-->ADC conversion) = 450us
//Tx1 as transmitter
output_low(pin_G15); //mCA1; Tx1 Select to the transmitter (Tx) mode
//2_pulses excitation signal:
output_high(pin_G14); //Excitation signal to generate 2_pulses (+5V/0) for transmitter signal at approx.328kHz
delay_us(1.5);
output_low(pin_G14);
delay_us(1.5);
output_high(pin_G14);
delay_us(1.5);
output_low(pin_G14);
//Rx5 to Rx13 functions as receiver
//output_high(pin_C4); //mCA5; Rx5 Select to the receiver (Rx) mode
output_c(0b0000000000010000);
//output_high(pin_G6); //mCA6; Rx6 Select to the receiver (Rx) mode
//output_high(pin_G7); //mCA7; Rx7 Select to the receiver (Rx) mode
//output_high(pin_G8); //mCA8; Rx8 Select to the receiver (Rx) mode
//output_high(pin_G13); //mCA9; Rx9 Select to the receiver (Rx) mode
output_g(0b0010000111000000);
// output_high(pin_D1); //mCA10; Rx10 Select to the receiver (Rx) mode
// output_high(pin_D14); //mCA11; Rx11 Select to the receiver (Rx) mode
// output_high(pin_D15); //mCA12; Rx12 Select to the receiver (Rx) mode
output_d(0b11000000000000010);
//output_high(pin_F4); //mCA13; Rx13 Select to the receiver (Rx) mode
output_f(0b0000000000010000);
// sample & hold signals at SNH5 to SNH13
//sample & hold for SNH5
delay_us(45); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_A10); //hold (LOW) for peak; RA10- SNH5
delay_us(20);
output_high(pin_A10); //sample (HIGH) at all times
//sample & hold for SNH6
delay_us(48); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_F6); //hold (LOW) for peak; RF6- SNH6
delay_us(20);
output_high(pin_F6); //sample (HIGH) at all times
//sample & hold for SNH7
delay_us(52); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_G2); //hold (LOW) for peak; RG2- SNH7
delay_us(20);
output_high(pin_G2); //sample (HIGH) at all times
//sample & hold for SNH8
delay_us(55); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_G3); //hold (LOW) for peak; RG3- SNH8
delay_us(20);
output_high(pin_G3); //sample (HIGH) at all times
//sample & hold for SNH9
delay_us(59); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_A14); //hold (LOW) for peak; RA14- SNH9
delay_us(20);
output_high(pin_A14); //SNH5; sample (HIGH) at all times
//sample & hold for SNH10
delay_us(55); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_A15); //hold (LOW) for peak; RA15- SNH10
delay_us(20);
output_high(pin_A15); //sample (HIGH) at all times
//sample & hold for SNH11
delay_us(52); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_D8); //SNH5; hold (LOW) for peak; RD8- SNH11
delay_us(20);
output_high(pin_D8); //SNH5; sample (HIGH) at all times
//sample & hold for SNH12
delay_us(48); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_D9); //SNH5; hold (LOW) for peak; RD9- SNH12
delay_us(20);
output_high(pin_D9); //SNH5; sample (HIGH) at all times
//sample & hold for SNH13
delay_us(45); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_D10); //SNH5; hold (LOW) for peak; RD10- SNH13
delay_us(20);
output_high(pin_D10); //SNH5; sample (HIGH) at all times
//ON ADC4 to ADC12
set_adc_channel(4); //read values in ADC04
delay_us(10); //delay 10us
ADCvalue4=read_adc(4); //starts conversion and read the results
set_adc_channel(5);
delay_us(10);
ADCvalue5=read_adc(5);
set_adc_channel(6);
delay_us(10);
ADCvalue6=read_adc(6);
set_adc_channel(7);
delay_us(10);
ADCvalue7=read_adc(7);
set_adc_channel(8);
delay_us(10);
ADCvalue8=read_adc(8);
set_adc_channel(9);
delay_us(10);
ADCvalue9=read_adc(9);
set_adc_channel(10);
delay_us(10);
ADCvalue10=read_adc(10);
set_adc_channel(11);
delay_us(10);
ADCvalue11=read_adc(11);
set_adc_channel(12);
delay_us(10);
ADCvalue12=read_adc(12);
while (i=4,i<13,i++);
{
result = read_adc();
printf("%Lx", result);
delay_us(10);
}
return;
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19569
|
|
Posted: Sun Dec 06, 2015 9:59 am |
|
|
There are so many programming errors I gave up.
A couple of hints though:
1) read_adc, does not require a channel number.....
2) A 'while' command executes the statement block immediately afterwards. This can be a single line terminated with ';' of a group of statements in {} brackets. You have both.....
3) The ADC can automatically generate the acquire time. This is the ADC_TAD_MUL value. You have both this programmed, and are then giving acquire time yourself. Set the TAD_MUL value to the time needed, and you can just select the channel and read it.
4) Use an array for ADC_value, and all the ADC code can then be a just two code lines in a loop:
Code: |
const int8 channel[]=4,5,6,7,8,9,10,11,12;
int8 value;
for (value=0;value<sizeof(channel);value++)
{
set_adc_channel(channel[value]);
ADC_value[value]=read_adc();
}
|
Having read the ADC's, you then try to read them again without changing channels. It's the results you have already read you want to read..... |
|
|
arelone
Joined: 06 Dec 2015 Posts: 42
|
|
Posted: Sun Dec 06, 2015 11:36 pm |
|
|
Hi Ttelmah,
Thank you for the advice. Your comments and hints were very helpful for me to understand the mistakes I made. It feels good to have someone like you giving some advice besides the errors I made looks silly. I accept it as I still new in c programming. I take your advice and I made some changes on the program which I sent earlier. Your valuable comments are much appreciated.
Regards
Code: |
#include <ADC_Test#1.h>
#use rs232(baud=9600,UART1)
void main()
{
setup_adc_ports(sAN0 | sAN1 | sAN2 | sAN3 | sAN4 | sAN5 | sAN6 | sAN7 | sAN8 | sAN9 | sAN10 | sAN11 | sAN12 | sAN13 | sAN14 | sAN15, VSS_VDD);
setup_adc(ADC_CLOCK_DIV_64 | ADC_TAD_MUL_8);
//setup_adc( ADC_CLOCK_INTERNAL );
const int8 channel[]=4,5,6,7,8,9,10,11,12;
const int8 ADC_value[]=4,5,6,7,8,9,10,11,12;
int8 value;
int32 x;
SETUP_ADC_PORTS(0xFFFFFFFF); //all analogue
//I/O ports configurations(1:input, 0:output)
set_tris_a(0x0000); //set port_a as output
//set_tris_b(0xFFFF); //set port_b as analog input/ADC
set_tris_c(0x0000); //set port_c as output
set_tris_d(0x0000); //set port_d as output
set_tris_f(0x0000); //set port_f as output
set_tris_g(0x0000); //set port_g as output
output_a(0x0000); //clear port_a to all 0s
output_c(0x0000); //clear port_c to all 0s
output_d(0x0000); //clear port_d to all 0s
output_f(0x0000); //clear port_f to all 0s
output_g(0x0000); //clear port_g o all 0s
while(TRUE)
{
//Total time taken (excitation-->ADC conversion) = 450us
//Tx1 as transmitter
output_low(pin_G15); //mCA1; Tx1 Select to the transmitter (Tx) mode
//2_pulses excitation signal:
output_high(pin_G14); //Excitation signal to generate 2_pulses (+5V/0) for transmitter signal at approx.328kHz
delay_us(1.5);
output_low(pin_G14);
delay_us(1.5);
output_high(pin_G14);
delay_us(1.5);
output_low(pin_G14);
//Rx5 to Rx13 functions as receiver
//output_high(pin_C4); //mCA5; Rx5 Select to the receiver (Rx) mode
output_c(0b0000000000010000);
//output_high(pin_G6); //mCA6; Rx6 Select to the receiver (Rx) mode
//output_high(pin_G7); //mCA7; Rx7 Select to the receiver (Rx) mode
//output_high(pin_G8); //mCA8; Rx8 Select to the receiver (Rx) mode
//output_high(pin_G13); //mCA9; Rx9 Select to the receiver (Rx) mode
output_g(0b0010000111000000);
// output_high(pin_D1); //mCA10; Rx10 Select to the receiver (Rx) mode
// output_high(pin_D14); //mCA11; Rx11 Select to the receiver (Rx) mode
// output_high(pin_D15); //mCA12; Rx12 Select to the receiver (Rx) mode
output_d(0b11000000000000010);
//output_high(pin_F4); //mCA13; Rx13 Select to the receiver (Rx) mode
output_f(0b0000000000010000);
// sample & hold signals at SNH5 to SNH13
//sample & hold for SNH5
delay_us(45); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_A10); //hold (LOW) for peak; RA10- SNH5
delay_us(20);
output_high(pin_A10); //sample (HIGH) at all times
//sample & hold for SNH6
delay_us(48); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_F6); //hold (LOW) for peak; RF6- SNH6
delay_us(20);
output_high(pin_F6); //sample (HIGH) at all times
//sample & hold for SNH7
delay_us(52); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_G2); //hold (LOW) for peak; RG2- SNH7
delay_us(20);
output_high(pin_G2); //sample (HIGH) at all times
//sample & hold for SNH8
delay_us(55); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_G3); //hold (LOW) for peak; RG3- SNH8
delay_us(20);
output_high(pin_G3); //sample (HIGH) at all times
//sample & hold for SNH9
delay_us(59); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_A14); //hold (LOW) for peak; RA14- SNH9
delay_us(20);
output_high(pin_A14); //SNH5; sample (HIGH) at all times
//sample & hold for SNH10
delay_us(55); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_A15); //hold (LOW) for peak; RA15- SNH10
delay_us(20);
output_high(pin_A15); //sample (HIGH) at all times
//sample & hold for SNH11
delay_us(52); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_D8); //SNH5; hold (LOW) for peak; RD8- SNH11
delay_us(20);
output_high(pin_D8); //SNH5; sample (HIGH) at all times
//sample & hold for SNH12
delay_us(48); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_D9); //SNH5; hold (LOW) for peak; RD9- SNH12
delay_us(20);
output_high(pin_D9); //SNH5; sample (HIGH) at all times
//sample & hold for SNH13
delay_us(45); //delay = toF (measured from the beginning of excitation signal)
output_low(pin_D10); //SNH5; hold (LOW) for peak; RD10- SNH13
delay_us(20);
output_high(pin_D10); //SNH5; sample (HIGH) at all times
//ON ADC4 to ADC12
//send 9 measurement values via UART
for (value=0;value<sizeof(channel);value++)
{
set_adc_channel(channel[value]);
ADC_value[value]=read_adc();
char str[20];
int32 x = read_adc();
sprintf(str, "%09lu", x);
printf(str);
delay_us(50);
}
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19569
|
|
Posted: Mon Dec 07, 2015 2:28 am |
|
|
Looks a lot cleaner. However you are still reading the ADC twice. Also having the print in the read loop, might introduce unwanted delays (prints take time). This may be OK, but consider separating the loops.
ADC_Value, does not want to be a const. It is where you are putting the results.....
Code: |
const int8 channel[]=4,5,6,7,8,9,10,11,12;
int16 ADC_value[sizeof(channel)];
for (value=0;value<sizeof(channel);value++)
{
set_adc_channel(channel[value]);
ADC_value[value]=read_adc(); //you now have the ADC readings
}
//separately print these - or include this above
for (value=0;value<sizeof(channel);value++)
printf("%04lu\n", ADC_value[value]);
|
There also needs to be something 'between' the output values to separate them when printed. I've added a line feed. Since an ADC reading can't be more than 12bits (depending on your ADC), why print 9 digits?. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|