+ Post New Thread
Results 1 to 9 of 9
  1. #1
    Member level 4
    Points: 640, Level: 5

    Join Date
    Nov 2012
    Posts
    68
    Helped
    5 / 5
    Points
    640
    Level
    5

    Real time serial data acquisition

    Hi

    I wish to take the samples from pic16f877a,4Mhz HS( if it is given a sine wave input) and plot the same.I am giving 230vp sine wave 50hz as input and transformed it into 5v range and level shifted it into positive voltages.

    This is my code

    Code:
                                // Loop variable
    
    
    float val,val1;//Declare the adcvalue stored variables
    char uart_rd[50],uart_rd1[50];
    
    void rmsv();
    void adc_uart();//adc read and uart write
    void interrupt(){
      if (PIR1.ADIF) {
        PIR1.ADIF=0; // clear AD interrupt flag
    
            val= (ADRESH << 8) | ADRESL; // read dadta from channel 0
                {
    
    
           val1=(val*325.0)/1023.0;
            FloatToStr(val1,uart_rd1);
           strncpy(uart_rd,uart_rd1,3);
           UART1_Write_Text(uart_rd1);
    
           //UART1_Write(10);
          UART1_Write(13);
          delay_us(1);
        }
    
             Delay_Cyc(3); //wait acquisition time
        ADCON0.F2=1; //start conversion again
      }
           }
    void main()
    {
        TRISA=0XFF;//porta as input
    ADCON1 = 0x82; // AN0->AN4 selected as analog input
      ADCON0 = 0b11000001; // Configue analog mode
      INTCON.GIE = 1; //Enable global interrupt
      INTCON.PEIE = 1; //Enable peripheral interrupt
      PIE1.ADIE = 1; //Enable ADC interrupt
      Delay_us(20); //wait for acquisition time
      ADCON0.F2 = 1; //start conversion
        // ADCON1=0X81;
         UART1_Init(9600);               // Initialize UART module at 9600 bps
         //ADC_Init();
    
    
        while(1);
    
     }
    I have checked the values in hyperterminal and see that the controller not even sample the peak voltage.I wish to have the samples correctly so that I can able to plot the waveform correctly.

    •   AltAdvertisment

        
       

  2. #2
    Banned
    Points: 12,436, Level: 26

    Join Date
    Apr 2013
    Posts
    2,528
    Helped
    538 / 538
    Points
    12,436
    Level
    26

    Re: Real time serial data acquisition

    The code inside ISR takes a lot of time.



  3. #3
    Member level 4
    Points: 640, Level: 5

    Join Date
    Nov 2012
    Posts
    68
    Helped
    5 / 5
    Points
    640
    Level
    5

    Re: Real time serial data acquisition

    Quote Originally Posted by milan.rajik View Post
    The code inside ISR takes a lot of time.
    If I removed the interrupt and put the adc code inside the while loop,it gives the same result



  4. #4
    Advanced Member level 5
    Points: 12,735, Level: 27
    Achievements:
    7 years registered

    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    2,123
    Helped
    598 / 598
    Points
    12,735
    Level
    27

    Re: Real time serial data acquisition

    Quote Originally Posted by aswathymohan View Post
    If I removed the interrupt and put the adc code inside the while loop,it gives the same result
    I would think that if the UART is running at 9600 baud (approx 960 characters/sec maximum - start bit, 8 data bits and one stop bit) you are not transmitting sufficient samples/sec to get meaningful results.

    I would suggest increasing the baud rate (e.g. to 115200) and rather than transmitting the samples as text transmit binary data, i.e. if you are sampling using an 8 bit ADC each character transmitted would be one sample. Even at 9600baud you would get approx 1000samples/sec which would be 20 samples/cycle



    •   AltAdvertisment

        
       

  5. #5
    Member level 4
    Points: 640, Level: 5

    Join Date
    Nov 2012
    Posts
    68
    Helped
    5 / 5
    Points
    640
    Level
    5

    Re: Real time serial data acquisition

    Quote Originally Posted by horace1 View Post
    I would think that if the UART is running at 9600 baud (approx 960 characters/sec maximum - start bit, 8 data bits and one stop bit) you are not transmitting sufficient samples/sec to get meaningful results.

    I would suggest increasing the baud rate (e.g. to 115200) and rather than transmitting the samples as text transmit binary data, i.e. if you are sampling using an 8 bit ADC each character transmitted would be one sample. Even at 9600baud you would get approx 1000samples/sec which would be 20 samples/cycle
    If I used baud rate 115200,the uart values are not displayed properly,can you advise me how to modify my code



  6. #6
    Advanced Member level 5
    Points: 12,735, Level: 27
    Achievements:
    7 years registered

    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    2,123
    Helped
    598 / 598
    Points
    12,735
    Level
    27

    Re: Real time serial data acquisition

    possibly at 115200baud hyperterminal cannot keep up with rate of characters arriving and is loosing data

    write a program to receive the data and save into a file (not displaying on screen) for a few seconds - does it make sense then?



  7. #7
    Member level 4
    Points: 640, Level: 5

    Join Date
    Nov 2012
    Posts
    68
    Helped
    5 / 5
    Points
    640
    Level
    5

    Re: Real time serial data acquisition

    Quote Originally Posted by horace1 View Post
    possibly at 115200baud hyperterminal cannot keep up with rate of characters arriving and is loosing data

    write a program to receive the data and save into a file (not displaying on screen) for a few seconds - does it make sense then?
    I need to have all the samples from the signal ,then only I can process the information.Actually I want to store the data into a file and then processed it and find the results.



  8. #8
    Advanced Member level 5
    Points: 12,735, Level: 27
    Achievements:
    7 years registered

    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    2,123
    Helped
    598 / 598
    Points
    12,735
    Level
    27

    Re: Real time serial data acquisition

    Quote Originally Posted by aswathymohan View Post
    I need to have all the samples from the signal ,then only I can process the information.Actually I want to store the data into a file and then processed it and find the results.
    if you use visual studio it has a SerialPort component that will talk to a COM: port and a Chart component you can use to plot graphs
    you could receive the data save it to a file and then plot from the file - if you save it in a suitable form you could plot graphs by reading it into MS Excel or Openoffice Calc
    http://www.wikihow.com/Create-a-Grap...-a-Spreadsheet



    •   AltAdvertisment

        
       

  9. #9
    Member level 5
    Points: 677, Level: 5

    Join Date
    Jun 2013
    Posts
    93
    Helped
    4 / 4
    Points
    677
    Level
    5

    Re: Real time serial data acquisition

    There may be lot of timings consideration involved as you must have seen uptil now sampling rate, uart timings etc. Its better to dump the data into the file and then read it. Even MATLAB can read com port easily and ten do whatever you have to do with the data. there are many tools like docklight that allows datalogging facilty. dump text file and then read using matlab.
    Or you may try to dump the data in ram first as you sample it and then get it out using UART this will help you to isolate sampling rate and UART timings consideration
    Thanks
    Aashish



--[[ ]]--