my code is giving me some errors....please somebody chek

General discussion on mikroPascal PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
edwardcullen
Posts: 93
Joined: 02 Sep 2010 20:27

my code is giving me some errors....please somebody chek

#1 Post by edwardcullen » 12 Mar 2011 10:11

this program send pulse which excite sonar module, then wait for echo for some time and do ADC onecho and display all data on pc...
below is my complete working program, but i want somebody to please checkthis for me, incase i can make this better .thanks for a positive reply..:)

Code: Select all


                                                                    { 
Micro controller: P30F4013 XTx8 + 15 Mhz crystal = 120 Mhz 
use string,conversion and uart lib 
} 
program ADC_Test; 

const 
  TCY = 30000000;         // 30MIPS.  Make sure you get your OSC correct! 
  NUM_OF_SAMPLES = 200; 
  SAMPLE_FREQ = 30000;// 125000; 
  PRESCALER_1_1 = TCY / SAMPLE_FREQ;   // Should be 10,000 

var 
  Samples: array[NUM_OF_SAMPLES] of word; // live data 
  freq: word; // Auxiliary variables 
  txt: string[10]; 
  Filled : boolean; 
  j : byte; 

  // Number or samples is a global number because ISRs are global 
  SamplesRead : byte; 

//-------------- Initialization of AD converter 
procedure InitAdc(); 
begin 
  ADPCFG := 0x00FF; // PORTB<8:15> is analog, PORTB<0:7> is digital 
  TRISB.8 := 1; // RB8 as input pin( echo pin) 
  TRISB.0:=0; 
  ADCHS := 8; // Connect RBxx/ANxx as CH8 input. RB8 is input pin 
  ADCSSL := 0; // 

  ADCON3 := 0x0127;  // sample time = 31 Tad. 

  ADCON2 := 0; 


  ADCON1 := 0x80E0; // Turn ADC ON 
end;//~ 

//-------------- Main Initialization 
procedure Init(); 
begin 
// PASCAL initializes all variables to 0 at start 
//  memset(@Samples, 0, 1024); // Clear samples 
//function pulse_out; 
while TRUE do 
begin 
LATB.0 := 1;              // Set PORTB to zero 
Delay_us(500); 
LATB.0 := not LATB.0;   // Invert PORTB value 
Delay_ms(5); 
    end; 
  InitAdc(); 
  uart1_init(115000); // init usart at max speed 
  TON_bit := 0; // start timer1 
  T1CON.B5 := 0; // Timer1 prescaler 1: 1 
  T1CON.B4 := 0; 
  PR1 := PRESCALER_1_1;// dword(Get_Fosc_kHz) * 1000 / (4 * SAMPLE_FREQ); //sampling timer period register value = Frequency of operation (Fosc/4) [Hz] / Sampling_Frequency [Hz] 
end; 

procedure StartAcquire(); 
begin 
  IFS0.T1IF := 0; // clear interrupt flag 
  Filled := false; 
  SamplesRead := 0;  // Start with no samples 
  TON_bit := 1; // start timer1 
  IEC0.T1IE := 1; // enable T1 interrupt 
end; 

//-------------- Takes current sample 
{ INLINE this in the ISr to save cycles 
function ReadAdc() : word; 
begin 
//  SAMP_bit := 0; // start conversion 
  SAMP_bit := 1; // start conversion 
  repeat until (DONE_bit); 
  result := ADCBUF0; // Get ADC value 
end; 
} 
procedure Timer1Int(); iv IVT_ADDR_T1INTERRUPT; 
begin 
//  Samples[SamplesRead] := ReadAdc(); 
  SAMP_bit := 1; // start conversion 
  repeat until (DONE_bit); 
  Samples[SamplesRead] := ADCBUF0; // Get ADC value 
  if SamplesRead<NUM_OF_SAMPLES-1 then 
    SamplesRead:=SamplesRead+1 
  else begin 
    TON_bit := 0; // stop timer1 
    IEC0.T1IE := 0; // diable T1 interrupt 
    Filled := true;  // Flag the main loop 
  end; 
  IFS0.T1IF := 0; // clear interrupt flag 
end; 

//-------------- Main program starts here 
begin 
  Init(); // Initialize all 
  StartAcquire(); 
  while (true) do begin 
    if Filled then begin 
      for j := 0 to NUM_OF_SAMPLES-2 do begin 
        wordtostr(Samples[j],txt); 
        uart1_write_text(txt + ','); 
      end; 
      wordtostr(Samples[NUM_OF_SAMPLES-1],txt); 
      uart1_write_text(txt + #10 + #13); 
      StartAcquire(); 
    end; 
  end; 
end. 

edwardcullen
Posts: 93
Joined: 02 Sep 2010 20:27

Re: my code is giving me some errors....please somebody chek

#2 Post by edwardcullen » 15 Mar 2011 12:27

urgent help needed
when i test my pulse on oscilloscope i see the pulse, but when i run entire program with sonar chip i see nothing!!!!i had tested the adc part of program with different signals and send to pc sucessfully!!!
but when i test sending pulse with entire program i see no signal plzz someone help me
please help needed

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

Re: my code is giving me some errors....please somebody chek

#3 Post by jpc » 15 Mar 2011 14:44

the code you published will do nothing , it will simply get stuck in the loop

Code: Select all

while TRUE do
begin
LATB.0 := 1;              // Set PORTB to zero
Delay_us(500);
LATB.0 := not LATB.0;   // Invert PORTB value
Delay_ms(5);
end; 
Au royaume des aveugles, les borgnes sont rois.

edwardcullen
Posts: 93
Joined: 02 Sep 2010 20:27

Re: my code is giving me some errors....please somebody chek

#4 Post by edwardcullen » 15 Mar 2011 15:07

tcan you please tell me what to do??

edwardcullen
Posts: 93
Joined: 02 Sep 2010 20:27

Re: my code is giving me some errors....please somebody chek

#5 Post by edwardcullen » 16 Mar 2011 14:12

i modified this proram to be as follows so that no loop ,but i cannt see anything with my sonarchip...:( my aim is send pulse, wait for echo, do ADC on echo then send all to pc.

Code: Select all


                                                                                {
Micro controller: P30F4013 XTx8 + 15 Mhz crystal = 120 Mhz
use string,conversion and uart lib
}
program ADC_Test;
  
 function pulse_out:
 begin
while TRUE do

LATB.0 := 1;              // Set PORTB to zero
Delay_us(500);
LATB.0 := not LATB.0;   // Invert PORTB value
Delay_ms(5);
end;



const
  TCY = 30000000;         // 30MIPS.  Make sure you get your OSC correct!
  NUM_OF_SAMPLES = 200;
  SAMPLE_FREQ = 30000;// 125000;
  PRESCALER_1_1 = TCY / SAMPLE_FREQ;   // Should be 10,000

var
  Samples: array[NUM_OF_SAMPLES] of word; // live data
  freq: word; // Auxiliary variables
  txt: string[10];
  Filled : boolean;
  j : byte;

  // Number or samples is a global number because ISRs are global
  SamplesRead : byte;

//-------------- Initialization of AD converter
procedure InitAdc();
begin
  ADPCFG := 0x00FF; // PORTB<8:15> is analog, PORTB<0:7> is digital
  TRISB.8 := 1; // RB8 as input pin( echo pin)
  TRISB.0:=0;
  ADCHS := 8; // Connect RBxx/ANxx as CH8 input. RB8 is input pin
  ADCSSL := 0; //

  ADCON3 := 0x0127;  // sample time = 31 Tad.

  ADCON2 := 0;


  ADCON1 := 0x80E0; // Turn ADC ON
end;//~

//-------------- Main Initialization
procedure Init();
begin
// PASCAL initializes all variables to 0 at start
//  memset(@Samples, 0, 1024); // Clear samples


  InitAdc();
  uart1_init(115000); // init usart at max speed
  TON_bit := 0; // start timer1
  T1CON.B5 := 0; // Timer1 prescaler 1: 1
  T1CON.B4 := 0;
  PR1 := PRESCALER_1_1;// dword(Get_Fosc_kHz) * 1000 / (4 * SAMPLE_FREQ); //sampling timer period register value = Frequency of operation (Fosc/4) [Hz] / Sampling_Frequency [Hz]
end;

procedure StartAcquire();
begin
  IFS0.T1IF := 0; // clear interrupt flag
  Filled := false;
  SamplesRead := 0;  // Start with no samples
  TON_bit := 1; // start timer1
  IEC0.T1IE := 1; // enable T1 interrupt
end;

//-------------- Takes current sample
{ INLINE this in the ISr to save cycles
function ReadAdc() : word;
begin
//  SAMP_bit := 0; // start conversion
  SAMP_bit := 1; // start conversion
  repeat until (DONE_bit);
  result := ADCBUF0; // Get ADC value
end;
}
procedure Timer1Int(); iv IVT_ADDR_T1INTERRUPT;
begin
//  Samples[SamplesRead] := ReadAdc();
  SAMP_bit := 1; // start conversion
  repeat until (DONE_bit);
  Samples[SamplesRead] := ADCBUF0; // Get ADC value
  if SamplesRead<NUM_OF_SAMPLES-1 then
    SamplesRead:=SamplesRead+1
  else begin
    TON_bit := 0; // stop timer1
    IEC0.T1IE := 0; // diable T1 interrupt
    Filled := true;  // Flag the main loop
  end;
  IFS0.T1IF := 0; // clear interrupt flag
end;

//-------------- Main program starts here
begin
  Init(); // Initialize all
  StartAcquire();
  while (true) do begin
    if Filled then begin
      for j := 0 to NUM_OF_SAMPLES-2 do begin
        wordtostr(Samples[j],txt);
        uart1_write_text(txt + ',');
      end;
      wordtostr(Samples[NUM_OF_SAMPLES-1],txt);
      uart1_write_text(txt + #10 + #13);
      StartAcquire();
    end;
  end;
end.


jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

Re: my code is giving me some errors....please somebody chek

#6 Post by jpc » 16 Mar 2011 15:25

the code you published this time does not even build correctly, the so called function pulse_out is missing a return-type and has no returnvalue, maybe it should better be a procedure.
then it would only do

Code: Select all

while true do LATB.0 := 1; 
as such this must be a reliable way to set this output but your code will never do anything else once you make the mistake of calling this .
fortunately your code does never call that whatever.

Please do yourself and this forum ( and others forums as well) a favor and work on something you can handle. So many people have offered you help but it seems a never-ending story without you learning something.
Last edited by jpc on 16 Mar 2011 20:08, edited 1 time in total.
Au royaume des aveugles, les borgnes sont rois.

edwardcullen
Posts: 93
Joined: 02 Sep 2010 20:27

Re: my code is giving me some errors....please somebody chek

#7 Post by edwardcullen » 16 Mar 2011 17:26

hi sir
yeah you are right, when i finish this project i am going to never again touch the dspic..:(

can you please advice me how to proceed on this final program?

thanks for all your wonderful help, it is just that i amnot able to assimilate and understand well the function of dspic,....:(

sorry for all inconvenience caused, am really sorry.,

edwardcullen
Posts: 93
Joined: 02 Sep 2010 20:27

Re: my code is giving me some errors....please somebody chek

#8 Post by edwardcullen » 17 Mar 2011 08:34

again errors.....:(

Code: Select all

                                                                                {
Micro controller: P30F4013 XTx8 + 15 Mhz crystal = 120 Mhz
use string,conversion and uart lib
}
program ADC_Test;

const
  TCY = 30000000;         // 30MIPS.  Make sure you get your OSC correct!
  NUM_OF_SAMPLES = 200;
  SAMPLE_FREQ = 30000;// 125000;
  PRESCALER_1_1 = TCY / SAMPLE_FREQ;   // Should be 10,000

var
  Samples: array[NUM_OF_SAMPLES] of word; // live data
  freq: word; // Auxiliary variables
  txt: string[10];
  Filled : boolean;
  j : byte;

  // Number or samples is a global number because ISRs are global
  SamplesRead : byte;


while true do   // main loop
begin
pulse_out ;   // call function pulse sending pulse to start the sonar chip
LATB.0 := 1;              // Set PORTB to zero
Delay_us(500);
LATB.0 := not LATB.0;   // Invert PORTB value
Delay_ms(5);
put_data_in_array;  // call a procedure to put all receve data in a array and set a variable " array_is_full"  to true when the array is full
If Array_is_full do  // so that dont happens until the array is full and data ready to be send , if that dont happens the if loop is skip and return to pulse_out...
  end;
//-------------- Initialization of AD converter
procedure InitAdc();
begin
  ADPCFG := 0x00FF; // PORTB<8:15> is analog, PORTB<0:7> is digital
  TRISB.8 := 1; // RB8 as input pin( echo pin)
  TRISB.0:=0;
  ADCHS := 8; // Connect RBxx/ANxx as CH8 input. RB8 is input pin
  ADCSSL := 0; //

  ADCON3 := 0x0127;  // sample time = 31 Tad.

  ADCON2 := 0;


  ADCON1 := 0x80E0; // Turn ADC ON
end;//~

//-------------- Main Initialization
procedure Init();
begin
// PASCAL initializes all variables to 0 at start
//  memset(@Samples, 0, 1024); // Clear samples


  InitAdc();
  uart1_init(115000); // init usart at max speed
  TON_bit := 0; // start timer1
  T1CON.B5 := 0; // Timer1 prescaler 1: 1
  T1CON.B4 := 0;
  PR1 := PRESCALER_1_1;// dword(Get_Fosc_kHz) * 1000 / (4 * SAMPLE_FREQ); //sampling timer period register value = Frequency of operation (Fosc/4) [Hz] / Sampling_Frequency [Hz]
end;

procedure StartAcquire();
begin
  IFS0.T1IF := 0; // clear interrupt flag
  Filled := false;
  SamplesRead := 0;  // Start with no samples
  TON_bit := 1; // start timer1
  IEC0.T1IE := 1; // enable T1 interrupt
end;

//-------------- Takes current sample
{ INLINE this in the ISr to save cycles
function ReadAdc() : word;
begin
//  SAMP_bit := 0; // start conversion
  SAMP_bit := 1; // start conversion
  repeat until (DONE_bit);
  result := ADCBUF0; // Get ADC value
end;
}
procedure Timer1Int(); iv IVT_ADDR_T1INTERRUPT;
begin
//  Samples[SamplesRead] := ReadAdc();
  SAMP_bit := 1; // start conversion
  repeat until (DONE_bit);
  Samples[SamplesRead] := ADCBUF0; // Get ADC value
  if SamplesRead<NUM_OF_SAMPLES-1 then
    SamplesRead:=SamplesRead+1
  else begin
    TON_bit := 0; // stop timer1
    IEC0.T1IE := 0; // diable T1 interrupt
    Filled := true;  // Flag the main loop
  end;
  IFS0.T1IF := 0; // clear interrupt flag
end;

//-------------- Main program starts here
begin
  Init(); // Initialize all
  StartAcquire();
  while (true) do begin
    if Filled then begin
      for j := 0 to NUM_OF_SAMPLES-2 do begin
        wordtostr(Samples[j],txt);
        uart1_write_text(txt + ',');
      end;
      wordtostr(Samples[NUM_OF_SAMPLES-1],txt);
      uart1_write_text(txt + #10 + #13);
      StartAcquire();
    end;
  end;
end.

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: my code is giving me some errors....please somebody chek

#9 Post by hexreader » 17 Mar 2011 17:37

You are missing the opening curly brace on the very first line
Start every day with a smile...... (get it over with) :)

edwardcullen
Posts: 93
Joined: 02 Sep 2010 20:27

Re: my code is giving me some errors....please somebody chek

#10 Post by edwardcullen » 18 Mar 2011 09:53

hi alll
when i use only sending pulse part, i can excite my sonar module and see the echo on oscilloscope.
when i use my entitre program without SEND pulse part, i can do ADC on any signal And send data on pc perfectly..
but when i use entirte program together i see nothing!!!
i mean my sonar module is not getting excited !!!

please somebody help is needed in a desperate way..:(

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

Re: my code is giving me some errors....please somebody chek

#11 Post by jpc » 18 Mar 2011 10:35

you have messed up your code probably with fragments of help from different sources, now it is difficult to repair, as a last attempt to help you out, hereby a code that at least compiles and might do what you want ( obviously not tested )
Explain to the person who gave you this project that it is beyond your ability's and if you want to learn programming start blinking a LED.

Code: Select all

{
Micro controller: P30F4013 XTx8 + 15 Mhz crystal = 120 Mhz
use string,conversion and uart lib
}
program ADC_Test;

const
  TCY = 30000000;         // 30MIPS.  Make sure you get your OSC correct!
  NUM_OF_SAMPLES = 200;
  SAMPLE_FREQ = 30000;// 125000;
  PRESCALER_1_1 = TCY / SAMPLE_FREQ;   // Should be 10,000

var
  Samples: array[NUM_OF_SAMPLES] of word; // live data
  freq: word; // Auxiliary variables
  txt: string[10];
  Filled : boolean;
  j : byte;

  // Number or samples is a global number because ISRs are global
  SamplesRead : byte;

procedure pulse_out;
begin
  LATB.0 := 1;            // Set PORTB to zero
  Delay_us(500);
  LATB.0 := not LATB.0;   // Invert PORTB value
  Delay_ms(5);
end;

//-------------- Initialization of AD converter
procedure InitAdc();
begin
  ADPCFG := 0x00FF; // PORTB<8:15> is analog, PORTB<0:7> is digital
  TRISB.8 := 1; // RB8 as input pin( echo pin)
  TRISB.0:=0;
  ADCHS := 8; // Connect RBxx/ANxx as CH8 input. RB8 is input pin
  ADCSSL := 0; //

  ADCON3 := 0x0127;  // sample time = 31 Tad.

  ADCON2 := 0;


  ADCON1 := 0x80E0; // Turn ADC ON
end;//~

//-------------- Main Initialization
procedure Init();
begin
// PASCAL initializes all variables to 0 at start
//  memset(@Samples, 0, 1024); // Clear samples


  InitAdc();
  uart1_init(115200); // init usart at max speed
  TON_bit := 0; // start timer1
  T1CON.B5 := 0; // Timer1 prescaler 1: 1
  T1CON.B4 := 0;
  PR1 := PRESCALER_1_1;// dword(Get_Fosc_kHz) * 1000 / (4 * SAMPLE_FREQ); //sampling timer period register value = Frequency of operation (Fosc/4) [Hz] / Sampling_Frequency [Hz]
end;

procedure StartAcquire();
begin
  pulse_out; // this is a guess, i suppose you want to take the adc-samples of the echo of one pulse   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  
  IFS0.T1IF := 0; // clear interrupt flag
  Filled := false;
  SamplesRead := 0;  // Start with no samples
  TON_bit := 1; // start timer1
  IEC0.T1IE := 1; // enable T1 interrupt
end;

//-------------- Takes current sample
{ INLINE this in the ISr to save cycles
function ReadAdc() : word;
begin
//  SAMP_bit := 0; // start conversion
  SAMP_bit := 1; // start conversion
  repeat until (DONE_bit);
  result := ADCBUF0; // Get ADC value
end;
}
procedure Timer1Int(); iv IVT_ADDR_T1INTERRUPT;
begin
//  Samples[SamplesRead] := ReadAdc();
  SAMP_bit := 1; // start conversion
  repeat until (DONE_bit);
  Samples[SamplesRead] := ADCBUF0; // Get ADC value
  if SamplesRead<NUM_OF_SAMPLES-1 then
    SamplesRead:=SamplesRead+1
  else begin
    TON_bit := 0; // stop timer1
    IEC0.T1IE := 0; // diable T1 interrupt
    Filled := true;  // Flag the main loop
  end;
  IFS0.T1IF := 0; // clear interrupt flag
end;

//-------------- Main program starts here
begin
  Init(); // Initialize all
  StartAcquire();
  while (true) do begin
    if Filled then begin
      for j := 0 to NUM_OF_SAMPLES-2 do begin
        wordtostr(Samples[j],txt);
        uart1_write_text(txt + ',');
      end;
      wordtostr(Samples[NUM_OF_SAMPLES-1],txt);
      uart1_write_text(txt + #10 + #13);
      StartAcquire();
    end;
  end;
end.
Au royaume des aveugles, les borgnes sont rois.

edwardcullen
Posts: 93
Joined: 02 Sep 2010 20:27

Re: my code is giving me some errors....please somebody chek

#12 Post by edwardcullen » 18 Mar 2011 10:47

TO jpc,
a big thanks to you sir, the code works and even do ADC corrrectly..thanksssss

Post Reply

Return to “mikroPascal PRO for dsPIC30/33 and PIC24 General”