Hi everyone!
I have a slight problem here. I am basically trying to sample 200 analog values using the ADC inside dspic30f2020 and then sending them via UART to a matlab program.

The thing is, I am sending these 200 values to matlab only once (as far as my understanding goes, in my code for dsPIC30F2020 given below, once we come out of the while loop after sending the 200 values..the uc does nothing until and unless the reset button is pressed) but, every time i run the matlab code, i get 500 values (500 because i have set the input buffer size=500) even if i do not press the reset button, which i feel shouldn't be the case.

dsPIC30F2020 c30 Compiler CODE:
Code:
#include
#include
#include "delay.h"

_FOSC(CSW_FSCM_OFF & OSC2_IO & HS );
_FOSCSEL(PRIOSC_PLL);
_FWDT(FWDTEN_OFF);
_FGS(CODE_PROT_OFF);
_FPOR(PWRT_OFF);

void initUart(void);
void putHex(int x); 

void _ISR _U1TXInterrupt(void);
void _ISR _U1RXInterrupt(void);

int x,rx_flag=0;
int adc_index,AN[200];

void initUart(void){
    
    U1MODEbits.UARTEN=  0;    //UART1 Disabled
    U1MODEbits.ALTIO =  1;   // Use alternate IO
    U1MODEbits.BRGH =   0;    //Use standard Bit rate
    U1MODEbits.PDSEL =  0;;    //8 bit no parity
    U1MODEbits.STSEL =  0;    //One stop bit
    
    IPC2bits.U1RXIP =5;//2;//Set UART1 RX Interrupt Priority 0-7, 0=disabled 7=Highest
    IPC2bits.U1TXIP =4;//TX Priority 2
    IFS0bits.U1RXIF =0; //Set UART1 RX Interrupt Flag=0
    
    U1STAbits.UTXISEL0 = 0; 
    U1STAbits.UTXISEL1 = 0; 
    U1STAbits.UTXBRK  = 0; 
    U1STAbits.URXISEL = 0;
    
    //U1BRG = (FCY/16*Baud Rate)-1 WITH BRGL
    //FCY = FCY = Primary Osc X 2 WITH PLL 
    U1BRG = 16; // 115200 baud  16 MHz Crystal BRGL
    
    U1MODEbits.UARTEN=  1;    //UART1 Enabled
    IEC0bits.U1RXIE = 0;//Enable UART1 Receiver Interrupt
    IEC0bits.U1TXIE = 1;//Enable UART1 Tx Interrupt
    U1STAbits.UTXEN   = 1;   // Enable transmit    
} 
 
void initialiseADC (void)
{
//This function initializes the ADC for a dsPIC30F2020
    // ADCON: ADC Control Register 1				
     ADCONbits.ADCS        = 3;  	//011 = FADC/10									
     ADCONbits.SEQSAMP   = 0;       // Simultaneous Sampling 
     ADCONbits.ORDER     = 0;       // Even channel first 
     ADCONbits.FORM      = 0;       // Output in Integer Format   
     ADCONbits.GSWTRG    = 0;       // global software trigger bit
     ADCONbits.ADSIDL    = 0;       // Operate in Idle Mode 
 
     ADPCFG = 0b1111111111111110;    // AN0-AN5 all analog inputs
                                     //Port bits that are to be set as analog inputs should have their ADPCFG
                                     //bit cleared
 
     ADSTAT = 0;     // Clear the ADSTAT register 
          
     //Settings for AN1 AN0
     ADCPC0bits.TRGSRC0 = 1;   // source is individual software trigger for AN1 AN0
     ADCPC0bits.SWTRG0 = 0;    // When set to 1 will  start conversion of AN1 and AN0
     ADCPC0bits.PEND0 = 0;     // goes to 1 on conversion pending & 0 on  conversion complete        
     ADCPC0bits.IRQEN0 = 0;    // disable the interrupt
     ADCPC0bits.IRQEN0 = 0;    // interrupt is not generated at end of conversion
 
     ADCONbits.ADON = 1; //Start the ADC module
 	
}

void putHex(int x) { 
	while(U1STAbits.UTXBF==1) continue;  //Checks if Buffer is Full and waits for it to have one    
                                                                 location empty 
		U1TXREG=x;
        
    return;     //Loops till Transmission is over 
}   

void __attribute__((__interrupt__, __auto_psv__)) _U1TXInterrupt(void){
	//TXflag=1;
	_U1TXIF=0; //clear RX Interrupt Flag

}

void convert_ch1(void)
{   
    while(adc_index<200)
    {
        ADCPC0bits.SWTRG0 = 1;     //start conversion of AN0 and store 200 samples
        while(ADCPC0bits.PEND0){} //conv pending becomes 0 when conv complete
        AN[adc_index]= ADCBUF0 >> 2;
        adc_index++;
	}		
    adc_index=0;
}

int main()
{
  __delay32(3200);
  //Delay();
 _TRISB0 = 1;
 adc_index=0; 
 
 initUart();
 initialiseADC();  
 __delay32(3200);
 
 convert_ch1();
  
 while(adc_index<200)
  {
    putHex(AN[adc_index]);
    adc_index++;
  }
 
 return(0);
}
Also, though i am sending only 200 values, I get 500 proper values (I am actually giving a square wave as input). According to me, it should give me measured values only for the first 200 numbers and then should give 0's because my uc hasn't actually sampled those extra 300 values.

Matlab code:
Code:
 
clc;
clear all;

s=serial('COM3');
set(s,'BaudRate',115200,'InputBufferSize',400);
fopen(s);
s;

disp(fread(s));
k=get(s,'ValuesReceived');
disp(k);
disp(' bytes received:');

fclose(s);
delete(s);
clear s;
- - - Updated - - -

i figured it out..the uc dosent actually stop after the while loop..it keeps re-running the code...so i added a while(1); loop..that helped :D