cancel
Showing results for 
Search instead for 
Did you mean: 

Spc560dL4 MCU clock configuration for Watchdog Init and Gpio Init using Register level

Mani_N
Associate

Hello All,

I am doing project that Controls Watchdog Adc to trigger interrupt for higher Threshold value and lower Threshold, When Threshold Reaches Maximum and Minimum ISR happens, in ISR function it blink onboard led.

Using Onboard POT for watchdog adc Tuning High Threshold and Low threshold

Onboard LED Blink for ISR -Threshold high and low Interrupt 

For this I am using SPC560d Dev board . We have Example Application Library for ADC it has all the function to work properly, but i want to do on my own by Register based using Xpc560d.h header fine, for that i cleared main.c file in Example ADC Application Library. Now I struct at Clock configuration for Watchdog Adc, 

Below code when debugging, program hangs on Clock configuration like api config

I have selected Sysclk-->FIRC  in RLA clock tree

Sysclk= 48mhz

Run mode : DRUN 

RUN0 mode 

pheripheral clock 24mhz 

 

PINOUT:

PB[10]=ADC_1 Extended channel 6

PC[2]=GPIO output LED 

PC[3]= GPIO output LED 

 

//function for clock initilization

void Clock_init(void) {
ME.IS.R= 0xFU;      //clear status register
ME.MCTL.B.TARGET_MODE =0x0100;    //RUN0 mode
 
   ME.MCTL.R = 0x40005AF0;          // Mode transition to RUN0 mode (key)
   ME.MCTL.R = 0x4000A50F;          // Mode transition to RUN0 mode (inverted key)
 
 
 
      ME.MER.R= 0x0000001D;     //eanble all modes
      ME.RUN[0].R=0x001F0074;     //sysclk_source,enable Interbnal Rc oscillator
 
ME.PCTL[33].R = 0x01;      //enable clock for adc1
ME.PCTL[64].R= 0x01;   //enable clock for gpio portc SIU
 
 
 
// //configure FMPLL for 48 mhz system clock
// CGM.FMPLL_CR.B.IDF= 4-1;  //Input divide factor
// CGM.FMPLL_CR.B.NDIV =48;   // Multiplier 48
// CGM.FMPLL_CR.B.ODF= 4-1;    //output divide factor
 
 //while(CGM.FMPLL_SR)
 
 
   ME.PCTL[92].R= 0x01;      //Enable watchdog clock
 
 
   while (ME.GS.B.S_MTRANS ==1) {}      // Wait for mode transition to complete
   while (ME.GS.B.S_CURRENTMODE != 4) {} // verify  RUN0 mode is active
}
 
// Function for GPIO_init

void Gpio_init(void) {


SIU.PCR[34].R = 0x0203; // Configure pin 34 as GPIO output
SIU.PCR[35].R = 0x0203; // Configure pin 35 as GPIO output

SIU.PCR[24].R = 0x2000; // Configure pin 24 for alternate function (ADC)
SIU.PSMI[2].R=0x04; //Pad select for adc_1

}

 
 
//Function for adc init
void ADC_1_Init(void) {
ADC_1.MCR.R = 0x20000000; // Enable ADC and use scan mode
ADC_1.NCMR1.R = 0x00000001; // Enable Channel 0 for normal conversion
ADC_1.CTR1.R = 0x00008606; // Set ADC conversion time and sample time
// Configure watchdog thresholds
ADC_1.THRHLR0.B.THRH = THRESHOLD_HIGH; // High threshold = 800
ADC_1.THRHLR0.B.THRL = THRESHOLD_LOW; // Low threshold = 200
// Enable watchdog interrupts
ADC_1.WTIMR.B.MSKWDG0H = 1; // Enable high threshold interrupt
ADC_1.WTIMR.B.MSKWDG0L = 1; // Enable low threshold interrupt
// Configure interrupt priorities and enable in INTC
INTC.PSR[84].R = 0x0A; // ADC_1 watchdog interrupt priority
INTC.CPR.B.PRI = 0; // Allow all interrupt priorities
ADC_1.MCR.B.NSTART = 1; // Start ADC conversions
}//
 
//Interupt for Watchdog adc To blink LED

void ADC_1_IRQHandler(void) {
// Clear ADC interrupt flags
ADC_1.ISR.R = 0x0000001F;
// Check for watchdog events
if (ADC_1.WTISR.B.WDG0H || ADC_1.WTISR.B.WDG0L) {
adc_raw_value = ADC_1.CDR[4].B.CDATA; // Read ADC raw value for Channel 0
ResultInMv = (5000 * adc_raw_value) / 4096; // Convert ADC value to mV

if (adc_raw_value > THRESHOLD_HIGH) {
SIU.GPDO[34].R ^= 1; // Toggle GPIO if value exceeds high threshold
} else if (adc_raw_value < THRESHOLD_LOW) {
SIU.GPDO[35].R ^= 1; // Toggle GPIO if value is below low threshold
}

// Clear watchdog interrupt flags
ADC_1.WTISR.R = 0x01; // Clear low threshold flag
ADC_1.WTISR.R = 0x10; // Clear high threshold flag
}
}

//Main function 

int main(void) {
asm volatile("wrteei 1" : : : "memory"); // Enable global interrupts
Clock_init(); // Initialize clocks
Gpio_init(); // Initialize GPIO
ADC_1_Init(); // Initialize ADC
ADC_1_IRQHandler();
while (1) {

}
return 0;
}

 

please let me know any correction needed for my project 

 

0 REPLIES 0