DRESULT disk_writep ( const BYTE* wr, /* Pointer to the data to be written, NULL:Initiate/Finalize write operation */ DWORD sc /* Sector number (LBA) or Number of bytes to send */ ) { DRESULT res; unsigned char buff; int i=0,j; Delay_s(1); dummy_clocks(8); Command(0X58, sc*512, 0X00); // CMD24 proceed(); do{ buff = response(); }while(buff!=0x00); /*SOFTWARE RESET RESPONSE BIT OBTAINED (0X01)*/ Delay_s(2); dummy_clocks(8); WriteSPI_(0XFE); //START TOKEN for(j=0;j<512;j++) //DATA BLOCK { if(*wr!='\0') { WriteSPI_(*wr); wr++; } else WriteSPI_(0x00); } WriteSPI_(0XFF); // CRC 2 BYTES WriteSPI_(0XFF); proceed(); Delay_s(1); dummy_clocks(8); Command(0X4D,0X00000000,0X00); //CMD13 proceed(); do{ buff = response(); }while(buff!=0x00); return RES_OK; }
int main() { uint8_t refTemp = 30; // reference uint8_t check, prev_check = 0; short up = 1, down = 1; uint16_t adc_in; // ADC value uint8_t temp = 1; // real temperature init_controller(); init_PWM(); lcd_init(); sei(); // turn on interrupts lcd_clrscr(); lcd_puts("Spinning"); Delay_s(2); // 2 seconds delay init_ADC(); refTemp = eeprom_read_byte((uint8_t*)0); if (!(refTemp >= TMIN && refTemp <= TMAX)) refTemp = 30; while(1) { if (C_CHECKBIT(Minus_REF)) down = 0; if (C_CHECKBIT(Minus_REF) == 0 && down ==0) { down = 1; if (refTemp > TMIN) refTemp--; } if (C_CHECKBIT(Plus_REF)) up = 0; if (C_CHECKBIT(Plus_REF) == 0 && up ==0) { up = 1; if (refTemp < TMAX) refTemp++; } eeprom_write_byte ((uint8_t *)0, refTemp); adc_in = ReadADC(0); temp = adc_in/2; lcd_puts2("T%d-R%dC", temp, refTemp); if ((temp - refTemp) > TOL) check = 3; else if ((refTemp - temp) > TOL) check = 1; else check = 2; switch(check) { case 1: if (prev_check == 1) break; setDutyCycle1(); prev_check = 1; break; case 2: if (prev_check == 2) break; setDutyCycle2(); prev_check = 2; break; case 3: if (prev_check == 3) break; setDutyCycle3(); prev_check = 3; break; } } return 1; }
void main (void) { CMCON |= 7; ADCON1=0x0F; GLCD_Initialize(); GLCD_ClearScreen(); while(1) { GLCD_WriteString("ETIQ TECHNOLOGIES"); Delay_s(2); GLCD_Circle(64, 32, 28); Delay_s(2); GLCD_ClearScreen(); GLCD_Rectangle(30, 12, 70,40); Delay_s(2); GLCD_ClearScreen(); } }
void reset() { int i = 0, count = 0; unsigned char buff; /*SOFTWARE RESET COMMAND*/ do{ dummy_clocks(8); Command(0X40, 0X00000000, 0X95); //CMD0 proceed(); do{ buff = response(); count++; }while((buff!=0X01) && (count<10) ); count = 0; }while(buff!=0X01); /*SOFTWARE RESET RESPONSE BIT OBTAINED (0X01)*/ putsUART("\n\rSD Card Inserted\n\r"); Delay_s(2); return; //CHECK FOR A STA_NODISK return }
DSTATUS disk_initialize (void) { DSTATUS stat; int i = 0, count1 = 0, count2 = 0; unsigned char buff; reset(); //RESET THE SD CARD delay_ms(500); dummy_clocks(8); Command(0X41, 0X00000000, 0XFF); //CMD1 proceed(); do{ buff = response(); }while(buff!=0x01); /*SOFTWARE RESET RESPONSE BIT OBTAINED (0X01)*/ Delay_s(1); proceed(); if (buff == 0x01) { //SEND CMD55 + ACMD41 delay_ms(1); count1 = 0; do{ count2 = 0; dummy_clocks(8); Command(0X77, 0X00000000, 0X95); //CMD55 buff = 0XFF; /*CHECK THE BUFFER FOR A 0X00 RESPONSE BIT*/ do{ buff = response(); count2++; }while((buff!=0X01)&&(count2<10)); delay_ms(1); count2 = 0; dummy_clocks(8); Command(0X69,0X40000000,0X95); //CMD41 buff = 0XFF; /*CHECK THE BUFFER FOR A 0X00 RESPONSE BIT*/ proceed(); do{ buff = response(); count2++; }while((buff!=0X00)&&(count2<10)); }while(buff != 0X00); count1 = 0; //Delay before sending command delay_ms(1); stat = 0X00; putsUART("\n\r"); putsUART("Card Accepted\n\r"); Delay_s(2); } else if(buff == 0x05) { stat = STA_NOINIT; putsUART("Error!!!\n\r"); Delay_s(3); } Delay_s(1); /*SETTING BLOCK LENGTH TO 512 Bytes*/ dummy_clocks(8); Command(0X50,0X00000200,0XFF); //CMD16 proceed(); do{ buff = response(); }while(buff!=0x00); /*SOFTWARE RESET RESPONSE BIT OBTAINED (0X01)*/ putsUART("\n\rSetting Block Size to 512 Bytes\n\r"); Delay_s(2); putsUART("\n\rCOMPLETING INITIALIZATION"); return stat; }
DRESULT disk_readp ( BYTE* rd, /* Pointer to the destination object */ DWORD sector, /* Sector number (LBA) */ UINT offset, /* Offset in the sector */ UINT count /* Byte count (bit15:destination) */ ) { DRESULT res; unsigned char ptr=0X00,buff; unsigned long int start_add; static unsigned char arr[512]; int length,i; /*Read Sector*/ if(offset == 0){ start_add = ((sector*512)); Delay_s(1); dummy_clocks(8); Command(0X52,start_add,0X00); //CMD18 proceed(); do{ buff = response(); }while(buff!=0x00); Delay_s(1); proceed(); do{ buff = response(); }while(buff!=0xFE); length = 0; while ( length < count ) { arr[length] = response(); length++; } Delay_s(1); dummy_clocks(8); Command(0X4C,0X00000000,0X00); //CMD12 proceed(); do{ buff = response(); }while(buff!=0xFF); length = 0; while(arr[length]!='\0') { //WriteUART(arr[length]); length++; } *rd = length; return RES_OK; } else { start_add = (sector*512); length = 0; while(length<512) //CLEAR ARRAY { arr[length] = 0; length++; } Delay_s(1); dummy_clocks(8); //SPI_cmd(sd_cmd17); Command(0X51,start_add,0X00); proceed(); do{ buff = response(); }while(buff!=0x00); do{ buff = response(); }while(buff!=0xFE); length = 0; while ( length < 512 ) { while(offset--) //RUN DOWN TILL THE OFFSET VALUE { arr[length] = response(); length++; } while(count--) //RUN DOWN TILL THE COUNT VALUE { *rd = response(); arr[length] = *rd; rd++; length++; } while(length<512) //FOR THE TRAILING BYTES { arr[length] = response(); length++; } } Delay_s(1); // dummy_clocks(8); //SPI_cmd(sd_cmd12); Command(0X4C,0X00000000,0X00); //COMMAND12 MANDATORY proceed(); do{ buff = response(); }while(buff!=0xFF); length = 0; if(flag == 1){ while(arr[length]!='\0') { WriteUART(arr[length]); length++; } } else putsUART(".."); Delay_s(2); return RES_OK; } }