CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

circular buffer and adc

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
alimary158



Joined: 26 Jan 2012
Posts: 16

View user's profile Send private message

circular buffer and adc
PostPosted: Sun Jan 29, 2012 2:49 am     Reply with quote

Good morning,
I have wrote this code to read adc values from a sensor and using a circular buffer i want to write them on sd! I have managed to make the code working but it seems I'm missing something since when i make this code run i manage to write on sd just for the length of the buffer and nothing more.
Instead i would like to keep on acquiring variables till there is power off
so what am i doing wrong?
Code:

int16 data=85;

#define RxADCQsize 64
volatile int16   RxBaseADC[RxADCQsize];

// Rx Ring Buffer control
volatile BYTE   RxHeadADC;
BYTE   RxTailADC;

#INT_timer2
void timer2_isr()
{
 //data=read_adc(); //This function will read the digital value from the ADC
   RxBaseADC[RxHeadADC++]=data;

   // test if ring buffer wrap is required
   if (RxHeadADC >= RxADCQsize)
      RxHeadADC = 0;
}

void main (){
    FIL fsrc;            // file structures
    FRESULT result;     // FatFs function common result code
    WORD btw, bw;       // File R/W count
    FRESULT   FS_Status;
    char pressure[16];
    int16 value;
   
    disable_interrupts(GLOBAL);
   
    restart_wdt();
   
   // Pic Inizialization
   init_pic();
   
  // setup_adc_ports(sAN1);
   //setup_adc(ADC_CLOCK_INTERNAL);
   //set_adc_channel(1);
   
   setup_timer_2 (T2_DIV_BY_16,255,6);
   
   RxHeadADC=0;
   RxTailADC=0;
   
   clear_interrupt(INT_RDA);
   enable_interrupts(INT_RDA);
   enable_interrupts(int_timer2);
   enable_interrupts(GLOBAL);

    do
   {
      FS_Status = f_mountdrv();
   }
   while (FS_Status);
   
   //Il file creato si chiamera pressure.txt
   strcpy(pressure,"adc.txt");
   
   //F_OPEN(): if the file already exist the datas will be appened. if the file doesnt exsist it is created
   
   result=f_open(&fsrc,&pressure,FA_OPEN_ALWAYS | FA_WRITE);
   result=f_lseek(&fsrc,fsrc.fsize);
   
   //write a string delimiter to each append data log
    strcpy(pressure,"hello");
    btw=strlen(pressure);
    result=f_write(&fsrc,pressure,btw,&bw);
   
   int  i=0;
 
   for(i=0;i<70;i++){
         disable_interrupts(int_timer2);
         
         //while
         if(RxHeadADC != RxTailADC)
         {
            enable_interrupts(int_timer2);   
           
            // here we have data waiting to be written
            sprintf(pressure,"Valore=%lu\r\n",RxBaseADC[RxTailADC++]);
           
            // test if ring buffer wrap is required
            if (RxTailADC >= RxADCQsize)
               RxTailADC=0; 
           
            result=f_write(&fsrc,pressure,strlen(pressure),&bw);
         }
         else
           enable_interrupts(int_timer2);
}
     
   f_close(&fsrc);
}

In particular in this code i would like to use the adc in the interrupt timer2 and read the values there and put them in the buffer. I know i should have a infinite loop somewhere and i tried now to make a loop with a for() to check if i can actually lets say write 70 samples on the file. But it seems I am not able and I'm just outputting 30 samples.

Anyone can help? Thanks
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jan 29, 2012 11:35 am     Reply with quote

This is not a complete program, unknown PIC type, compiler version,etc.

Odds are real good you're trying to use a 5volt PIC with an SD card...never going to work unless you have logic level convertor chips in the circuit( will need to see your schematic).

Have NO idea what 'driver' is being used for the SD card access...

You say you want to use interrupts ,yet you disable them in main()...

I suggest you delete everything and start over..

Create a simple program that loops for say 255 times to store data to the sdcard. If you save it in CSV format, any PC will be able to read the data and you can confirm that it is correct.This program might take 15-20 lines of code.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 30, 2012 9:43 pm     Reply with quote

Quote:
btw=strlen(pressure);
result=f_write(&fsrc,pressure,btw,&bw);

int i=0;

for(i=0;i<70;i++){
disable_interrupts(int_timer2);

Declaration of variables in the middle of code is not allowed in CCS.
Put all variable declarations at the beginning of each function.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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