I am using a dsPIC33E MUC to interface an UART camera(VGA) at 115.2Kbps. The purpose is to save an image to a SD card.
The program I wrote works well except that it's inside a loop checking if the buffer(512 bytes buffer is for one sector) is full or not. If the buffer is full, it will be written to a SD card. I am wondering how I can adapt this code to a time critical data acquisition project. Since it's UART interrupt driven, I am not sure if the best way to save images to a SD card is to use a flag. Here is the code with the loop checking flag to see if the buffer is full.
unsigned char buff[512];//buffer to store bytes comming from the camera
unsigned int bufferPtr=0;//pointer for the buffer. It will be incremented for each byte
while(1){
if(bufferPtr==512){Write_Sector(SectorNumber,Buff);}//if the buffer is full, Write a it to SD card
bufferPtr=0;//reset buffer Pointer so next byte will be save to the beginning of the buffer
}
interrupt routine
void ISR(){
Uart.FlagBit=0;//clear flag bit first
buffer[bufferPtr++]=UART_Read();//read a byte and put it into the buffer
}
As you can see, the program is kinda waiting for bytes to arrive and save it to a buffer. It works very well but I have a question about timing. In terms of writing speed, it needs roughly 3.6sec for 2000 sectors(each sector 512 bytes). So writing one sector needs 3600/2000=1.8ms. The baudrate tells me that 115200/10=11520Byte/Sec, which means 11.52 Bytes/ms. How is it possible to write one sector to SD card(needs 1.8ms) without overflowing UART FIFO buffer? During that 1.8ms period, there will be 11.52*1.8=20.736 bytes comming in.