Created
July 28, 2025 05:32
-
-
Save minhhieuec/69651a964cba9921cd050116e60e5cde to your computer and use it in GitHub Desktop.
Creating the First Application on PIC32CM JH01 - PIC32CM JH01 Curiosity Nano+ Touch Evaluation Kit - PIC32CM5164JH01048 MCU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
Main Source File | |
Company: | |
Microchip Technology Inc. | |
File Name: | |
main.c | |
Summary: | |
This file contains the "main" function for a project. | |
Description: | |
This file contains the "main" function for a project. The | |
"main" function calls the "SYS_Initialize" function to initialize the state | |
machines of all modules in the system | |
*******************************************************************************/ | |
// ***************************************************************************** | |
// ***************************************************************************** | |
// Section: Included Files | |
// ***************************************************************************** | |
// ***************************************************************************** | |
#include <stddef.h> // Defines NULL | |
#include <stdbool.h> // Defines true | |
#include <stdlib.h> // Defines EXIT_FAILURE | |
#include "definitions.h" // SYS function prototypes | |
#include <stdio.h> | |
// SYS function prototypes | |
/* Timer Counter Time period match values for input clock of 4096 Hz */ | |
#define PERIOD_500MS 512 | |
#define PERIOD_1S 1024 | |
#define PERIOD_2S 2048 | |
#define PERIOD_4S 4096 | |
#define TX_BUFFER_SIZE 100 | |
static volatile bool isRTCExpired = false; | |
static volatile bool changeTempSamplingRate = false; | |
static volatile bool isUARTTxComplete = true; | |
static volatile bool isUARTRxComplete = false; | |
static uint8_t uartTxBuffer[TX_BUFFER_SIZE] = {0}; | |
typedef enum | |
{ | |
TEMP_SAMPLING_RATE_500MS = 0, | |
TEMP_SAMPLING_RATE_1S = 1, | |
TEMP_SAMPLING_RATE_2S = 2, | |
TEMP_SAMPLING_RATE_4S = 3, | |
} TEMP_SAMPLING_RATE; | |
static TEMP_SAMPLING_RATE tempSampleRate = TEMP_SAMPLING_RATE_500MS; | |
static const char timeouts[4][20] = {"500 milliSeconds", "1 Second", "2 Seconds", "4 Seconds"}; | |
// ***************************************************************************** | |
// ***************************************************************************** | |
// Section: Main Entry Point | |
// ***************************************************************************** | |
// ***************************************************************************** | |
static void SW0_userHandler(uintptr_t context) { | |
changeTempSamplingRate = true; | |
} | |
static void rtcEventHandler(RTC_TIMER32_INT_MASK intCause, uintptr_t context) { | |
if (intCause & RTC_MODE0_INTENSET_CMP0_Msk) { | |
isRTCExpired = true; | |
} | |
} | |
static void uartDmaChannelHandler_Tx(DMAC_TRANSFER_EVENT event, uintptr_t contextHandle) { | |
if (event == DMAC_TRANSFER_EVENT_COMPLETE) { | |
isUARTTxComplete = true; | |
} | |
} | |
int main ( void ) | |
{ | |
uint8_t uartLocalTxBuffer[100] = {0}; | |
/* Initialize all modules */ | |
SYS_Initialize ( NULL ); | |
/* Initialize all modules */ | |
DMAC_ChannelCallbackRegister(DMAC_CHANNEL_0, uartDmaChannelHandler_Tx, 0); | |
RTC_Timer32CallbackRegister(rtcEventHandler, 0); | |
EIC_CallbackRegister(EIC_PIN_15, SW0_userHandler, 0); | |
sprintf((char*) uartTxBuffer, "************* Printing Toggling LED rate * ************\r\n"); | |
DMAC_ChannelTransfer(DMAC_CHANNEL_0, uartTxBuffer, \ | |
(const void *) &(SERCOM1_REGS->USART_INT.SERCOM_DATA), \ | |
strlen((const char*) uartTxBuffer)); | |
/* Start the timer */ | |
RTC_Timer32Start(); | |
while ( true ) | |
{ | |
/* Maintain state machines of all polled MPLAB Harmony modules. */ | |
// SYS_Tasks ( ); | |
if ((isRTCExpired == true) && (true == isUARTTxComplete)) { | |
isRTCExpired = false; | |
isUARTTxComplete = false; | |
LED0_Toggle(); | |
sprintf((char*) (uartTxBuffer), "Toggling LED at %s rate \r\n", | |
&timeouts[(uint8_t) tempSampleRate][0]); | |
DMAC_ChannelTransfer(DMAC_CHANNEL_0, uartTxBuffer, \ | |
(const void *) &(SERCOM1_REGS->USART_INT.SERCOM_DATA), \ | |
strlen((const char*) uartTxBuffer)); | |
} | |
if(changeTempSamplingRate == true) { | |
changeTempSamplingRate = false; | |
if (tempSampleRate == TEMP_SAMPLING_RATE_500MS) { | |
tempSampleRate = TEMP_SAMPLING_RATE_1S; | |
RTC_Timer32CompareSet(PERIOD_1S); | |
} else if (tempSampleRate == TEMP_SAMPLING_RATE_1S) { | |
tempSampleRate = TEMP_SAMPLING_RATE_2S; | |
RTC_Timer32CompareSet(PERIOD_2S); | |
} else if (tempSampleRate == TEMP_SAMPLING_RATE_2S) { | |
tempSampleRate = TEMP_SAMPLING_RATE_4S; | |
RTC_Timer32CompareSet(PERIOD_4S); | |
} else if (tempSampleRate == TEMP_SAMPLING_RATE_4S) { | |
tempSampleRate = TEMP_SAMPLING_RATE_500MS; | |
RTC_Timer32CompareSet(PERIOD_500MS); | |
} else { | |
; | |
} | |
RTC_Timer32CounterSet(0); | |
sprintf((char*) uartLocalTxBuffer, "LED Toggling rate is changed to %s\r\n", &timeouts[(uint8_t)tempSampleRate][0]); | |
DMAC_ChannelTransfer(DMAC_CHANNEL_0, uartLocalTxBuffer, \ | |
(const void *) &(SERCOM1_REGS->USART_INT.SERCOM_DATA), \ | |
strlen((const char*) uartLocalTxBuffer)); | |
} | |
} | |
/* Execution should not come here during normal operation */ | |
return ( EXIT_FAILURE ); | |
} | |
/******************************************************************************* | |
End of File | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment