#include <18F2550.h>
#device adc=8
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=19200,parity=N,xmit=PIN_A1,rcv=PIN_A0,bits=8, force_sw)

#define RS232_DEBUG 1

#include <SDCard_hard.c>



void main(){
char Buffer[512];
char i, j;
long k;

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED|T1_DIV_BY_1);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   #if RS232_DEBUG
      printf("******* Iniciando Memoria SD Card... *******\r\n");
   #endif
   SDCard_init();
   #if RS232_DEBUG
      printf("\r\n******* Leemos registro CID: *******\r\n");
   #endif
   SDCard_read_CID();
   #if RS232_DEBUG
      printf("\r\n******* Leemos registro CSD: *******\r\n");
   #endif
   SDCard_read_CSD();
   // Cargamos datos en Buffer:
   for(k=0;k<512;k++) Buffer[k]=k;
   #if RS232_DEBUG
      printf("\r\n******* Escribimos sector 0x200 *******\r\n");
   #endif
   SDCard_write_block(0x200,Buffer);
   #if RS232_DEBUG
      printf("\r\n******* Leemos sector 0x200 *******\r\n");
   #endif
   SDCard_read_block(0x200,Buffer);
   #if RS232_DEBUG
      printf("   >> Datos leidos:\r\n\r\n");
      for(i=0;i<16;i++){
         for(j=0;j<32;j++){
            printf("%X,",Buffer[((long)i*32+j)]);
         }
         printf("\r\n");
      }
   #endif
   while(1){}
}
