void BSP_RTCC_Initialize (BSP_RTCC_DATETIME * value) { // Turn on the secondary oscillator __builtin_write_OSCCONL(0x02); // Set the RTCWREN bit __builtin_write_RTCWEN(); RCFGCALbits.RTCPTR0 = 1; RCFGCALbits.RTCPTR1 = 1; // Set it to the correct time if (value->bcdFormat) { RTCVAL = 0x0000 | value->year; RTCVAL = ((uint16_t)(value->month) << 8) | value->day; RTCVAL = ((uint16_t)(value->weekday) << 8) | value->hour; RTCVAL = ((uint16_t)(value->minute) << 8) | value->second; } else { // Set (Reserved : year) RTCVAL = BSP_RTCC_DecToBCD (value->year); // Set (month : day) RTCVAL = (BSP_RTCC_DecToBCD (value->month) << 8) | BSP_RTCC_DecToBCD(value->day); // Set (weekday : hour) RTCVAL = (BSP_RTCC_DecToBCD (value->weekday) << 8) | BSP_RTCC_DecToBCD(value->hour); // Set (minute : second) RTCVAL = (BSP_RTCC_DecToBCD (value->minute) << 8) | BSP_RTCC_DecToBCD(value->second); } // Enable RTCC, clear RTCWREN RCFGCAL = 0x8000; }
void RTCC_Init(void) { __builtin_disi(0x3FFF); //disable interrupts // Set the RTCWREN bit __builtin_write_RTCWEN(); RCFGCALbits.RTCEN = 0; //disable RTCC // set date Mon Jun 29 17:18:47 MST 2015 RCFGCALbits.RTCPTR = 3; // start the sequence RTCVAL = 0x15; // YEAR RTCVAL = 0x629; // MONTH-1/DAY-1 RTCVAL = 0x117; // WEEKDAY/HOURS RTCVAL = 0x1847; // MINUTES/SECONDS // set alarm Mon Jan 01 23:59:00 MST 2001 ALCFGRPTbits.ALRMPTR = 2; //sart write sequence ALRMVAL = 0x101; ALRMVAL = 0x123; ALRMVAL = 0x5900; ALCFGRPTbits.ALRMEN = 1; //enable alarm and chime for every second ALCFGRPTbits.ARPT = 0xFF; ALCFGRPTbits.CHIME = 1; ALCFGRPTbits.AMASK = 0x2; // PWCPOL disabled; PWCEN disabled; RTCLK LPRC; PWCPRE disabled; RTCOUT alarm pulse; PWSPRE disabled; RTCPWCbits.RTCLK = 0b01; //clear RTCWREN RCFGCALbits.RTCWREN = 0; __builtin_disi(0); //enable interrupts }
/** * Activates or deactivates the alarm. * \param run: <UL> <LI><B>FALSE</B> Deactivates alarm</LI> <LI><B>TRUE</B> Activates alarm</LI> </UL> * \return None */ void RTCCAlarmSet(BYTE run) { if (run != ALCFGRPTbits.ALRMEN ) { while (RCFGCALbits.RTCSYNC == 1); asm volatile("disi #5"); __builtin_write_RTCWEN(); ALCFGRPTbits.ALRMEN = run; RCFGCALbits.RTCWREN = 0; } }
void RTCC_Access(unsigned char Yes_No) { if(Yes_No == YES ) { __builtin_write_RTCWEN(); /* Set RTCWREN bit */ } else { asm("BCLR RCFGCAL,#13"); /* Clear RTCWREN bit */ } }
/** * Function to configure the alarm. It's possible to set the interval for the alarm and if must be repeated continously. * \param repeats - Specifies how many time the alarm must be repeated: <UL> <LI><B>REPEAT_NO</B> - the alarm must not repeated.</LI> <LI><B>an int comprised between 1-254</B> - the number of times alarm must be repeated.</LI> <LI><B>REPEAT_INFINITE</B> - the alarm must be repeated forever.</LI> </UL> * \param how often alarm should be raised <UL> <LI><B>EVERY_HALF_SEC</B> alarm is raised every half second</LI> <LI><B>EVERY_SEC</B> alarm is raised every second</LI> <LI><B>EVERY_TEN_SEC</B> alarm is raised every 10 seconds</LI> <LI><B>EVERY_MIN</B> alarm is raised every minute</LI> <LI><B>EVERY_TEN_MIN</B> alarm is raised every 10 minutes</LI> <LI><B>EVERY_HOUR</B> alarm is raised every hour</LI> <LI><B>EVERY_DAY</B> alarm is raised every day</LI> <LI><B>EVERY_WEEK</B> alarm is raised every week</LI> <LI><B>EVERY_MONTH</B> alarm is raised every month</LI> <LI><B>EVERY_YEAR</B> alarm is raised every year</LI> </UL> * \param custom user function to execute when alarm event is raised. <B>please use NO_ALRM_EVENT</B> to ignore. * \return None */ void RTCCAlarmConf(struct tm* rtcc, int repeats, BYTE whenToRaise, void (*fptr)()) { BYTE chime, crepeat; if (repeats == 0) { chime = 0; crepeat = 0; } else if (repeats > 255) { chime = 1; crepeat = 255; } else { chime = 0; crepeat = (BYTE)(repeats - 1); } IEC3bits.RTCIE = 0; // Setting function pointer to execute on alarm event alarmFP = fptr; // Synch operations and critical section to avoid context switch vTaskSuspendAll(); while (RCFGCALbits.RTCSYNC == 1); // Enabling writing on RTCC registers asm volatile("disi #5"); __builtin_write_RTCWEN(); ALCFGRPTbits.ALRMEN = 0; ALCFGRPTbits.ALRMPTR = 2; ALRMVAL = _BtoW(_DtoB(rtcc->tm_mon + 1),_DtoB(rtcc->tm_mday)); ALRMVAL = _BtoW(_DtoB(rtcc->tm_wday),_DtoB(rtcc->tm_hour)); ALRMVAL = _BtoW(_DtoB(rtcc->tm_min),_DtoB(rtcc->tm_sec)); ALCFGRPTbits.CHIME = chime; ALCFGRPTbits.ARPT = crepeat; ALCFGRPTbits.AMASK = whenToRaise; RCFGCALbits.RTCWREN = 0; IPC15bits.RTCIP = 5; IEC3bits.RTCIE = 1; IFS3bits.RTCIF = 0; xTaskResumeAll(); }
/** * Sets the date/time for the RTCC and enables the RTCC module. * \param rtcc - a pointer to a struct tm variable, containing the date and the time to set * \return None */ void RTCCSet(struct tm* rtcc) { __builtin_write_OSCCONL(OSCCON | 0x02); asm volatile("disi #5"); __builtin_write_RTCWEN(); ALCFGRPTbits.ALRMEN = 0; RCFGCALbits.RTCEN = 0; RCFGCALbits.RTCPTR = 3; RTCVAL = _DtoB(rtcc->tm_year - 100); RTCVAL = _BtoW(_DtoB(rtcc->tm_mon + 1),_DtoB(rtcc->tm_mday)); RTCVAL = _BtoW(_DtoB(rtcc->tm_wday),_DtoB(rtcc->tm_hour)); RTCVAL = _BtoW(_DtoB(rtcc->tm_min),_DtoB(rtcc->tm_sec)); RCFGCALbits.RTCEN = 1; RCFGCALbits.RTCWREN = 0; }
int main (void) { FSFILE * pointer; #if defined(SUPPORT_LFN) char count = 80; #endif char * pointer2; SearchRec rec; unsigned char attributes; unsigned char size = 0, i; // Initialize and configure Primary PLL, and enabled Secondary Oscillator PLLFBD = 58; /* M = 60 */ CLKDIVbits.PLLPOST = 0; /* N1 = 2 */ CLKDIVbits.PLLPRE = 0; /* N2 = 2 */ OSCTUN = 0; __builtin_write_OSCCONH(0x03); __builtin_write_OSCCONL(0x03); while (OSCCONbits.COSC != 0x3); while (_LOCK == 0); // Activate the RTCC module __builtin_write_RTCWEN(); Nop(); Nop(); RCFGCALbits.RTCPTR0 = 1; RCFGCALbits.RTCPTR1 = 1; // Set it to the correct time // These values won't be accurate RTCVAL = 0x0011; RTCVAL = 0x0815; RTCVAL = 0x0108; RTCVAL = 0x2137; RCFGCAL = 0x8000; while (!MDD_MediaDetect()); // Initialize the library while (!FSInit()); #ifdef ALLOW_WRITES // Create a file pointer = FSfopen ("FILE1.TXT", "w"); if (pointer == NULL) while(1); // Write 21 1-byte objects from sendBuffer into the file if (FSfwrite (sendBuffer, 1, 21, pointer) != 21) while(1); // FSftell returns the file's current position if (FSftell (pointer) != 21) while(1); // FSfseek sets the position one byte before the end // It can also set the position of a file forward from the // beginning or forward from the current position if (FSfseek(pointer, 1, SEEK_END)) while(1); // Write a 2 at the end of the string if (FSfwrite (send2, 1, 1, pointer) != 1) while(1); // Close the file if (FSfclose (pointer)) while(1); // Create a second file pointer = FSfopen ("Microchip File 1.TXT", "w"); if (pointer == NULL) while(1); // Write the string to it again if (FSfwrite ((void *)sendBuffer, 1, 21, pointer) != 21) while(1); // Close the file if (FSfclose (pointer)) while(1); #endif // Open file 1 in read mode pointer = FSfopen ("FILE1.TXT", "r"); if (pointer == NULL) while(1); if (FSrename ("Microchip File 2.TXT", pointer)) while(1); // Read one four-byte object if (FSfread (receiveBuffer, 4, 1, pointer) != 1) while(1); // Check if this is the end of the file- it shouldn't be if (FSfeof (pointer)) while(1); // Close the file if (FSfclose (pointer)) while(1); // Make sure we read correctly if ((receiveBuffer[0] != 'T') || (receiveBuffer[1] != 'h') || (receiveBuffer[2] != 'i') || (receiveBuffer[3] != 's')) { while(1); } #ifdef ALLOW_DIRS // Create a small directory tree // Beginning the path string with a '.' will create the tree in // the current directory. Beginning with a '..' would create the // tree in the previous directory. Beginning with just a '\' would // create the tree in the root directory. Beginning with a dir name // would also create the tree in the current directory if (FSmkdir (".\\Mchp Directory 1\\Dir2\\Directory 3")) while(1); // Change to 'Directory 3' in our new tree if (FSchdir ("Mchp Directory 1\\Dir2\\Directory 3")) while(1); // Create another tree in 'Directory 3' if (FSmkdir ("Directory 4\\Directory 5\\Directory 6")) while(1); // Create a third file in directory THREE pointer = FSfopen ("CWD.TXT", "w"); if (pointer == NULL) while(1); #if defined(SUPPORT_LFN) // Get the name of the current working directory /* it should be "\Mchp Directory 1\Dir2\Directory 3" */ pointer2 = wFSgetcwd ((unsigned short int *)path1, count); #endif if (pointer2 != path1) while(1); // Simple string length calculation i = 0; while(*((unsigned short int *)path1 + i) != 0x00) { size++; size++; i++; } // Write the name to CWD.TXT if (FSfwrite (path1, size, 1, pointer) != 1) while(1); // Close the file if (FSfclose (pointer)) while(1); // Create some more directories if (FSmkdir ("Directory 4\\Directory 5\\Directory 7\\..\\Directory 8\\..\\..\\DIRNINE\\Directory 10\\..\\Directory 11\\..\\Directory 12")) while(1); /******************************************************************* Now our tree looks like this \ -> Mchp Directory 1 -> Dir2 -> Directory 3 -> Directory 4 -> Directory 5 -> Directory 6 -> Directory 7 -> Directory 8 DIRNINE -> Directory 10 -> Directory 11 -> Directory 12 ********************************************************************/ // This will delete only Directory 8 // If we tried to delete Directory 5 with this call, the FSrmdir // function would return -1, since Directory 5 is non-empty if (FSrmdir ("\\Mchp Directory 1\\Dir2\\Directory 3\\Directory 4\\Directory 5\\Directory 8", FALSE)) while(1); // This will delete DIRNINE and all three of its sub-directories if (FSrmdir ("Directory 4\\DIRNINE", TRUE)) while(1); // Change directory to the root dir if (FSchdir ("\\")) while(1); #endif #ifdef ALLOW_FILESEARCH // Set attributes attributes = ATTR_ARCHIVE | ATTR_READ_ONLY | ATTR_HIDDEN; // Functions "FindFirst" & "FindNext" can be used to find files // and directories with required attributes in the current working directory. // Find the first TXT file with any (or none) of those attributes that // has a name beginning with the letters "Mic" // These functions are more useful for finding out which files are // in your current working directory if (FindFirst ("Mic*.TXT", attributes, &rec)) while(1); // Find file to get "Microchip File 2.TXT" if (FindNext (&rec)) while(1); #if defined(SUPPORT_LFN) // Delete file 2 // If the file name is long if(rec.utf16LFNfoundLength) { // NOTE : "wFSremove" function deletes specific file not directory. // To delete directories use "wFSrmdir" function if (wFSremove (rec.utf16LFNfound)) while(1); } else #endif { // NOTE : "FSremove" function deletes specific file not directory. // To delete directories use "FSrmdir" function if (FSremove (rec.filename)) while(1); } #endif /********************************************************************* The final contents of our card should look like this: \ -> Microchip File 1.TXT -> Mchp Directory 1 -> Dir2 -> Directory 3 -> CWD.TXT -> Directory 4 -> Directory 5 -> Directory 6 -> Directory 7 *********************************************************************/ while(1); }