The Subject is self explaining so I will paste my code here.
Main.c
#include <stdint.h>
#include <math.h>
#include "MK20D5.h"
#include "gpio.h"
#include "systick.h"
#include "uart.h"
#include "adc.h"
#include "spi.h"
#include "nrf24l01.h"
#include "crc.h"
#include "wdog.h"
//ADC Variables Store Counts
uint16_t adc_channel0 = 0;
uint16_t adc_channel0_last_copy = 0;
uint16_t result = 0;
unsigned char finalSendData [6] = {
0x01,
0x02,
0x00,
0x00,
0x00,
0x00
};
uint16_t crc_nrf = 0;
uint8_t FIFO_STATUS = 0;
/******************************************************************************
º¯ÊýÃû³Æ£ºmain
º¯Êý¹¦ÄÜ£ºÏµÍ³Ö÷º¯Êý
Èë¿Ú²ÎÊý£ºÎÞ
³ö¿Ú²ÎÊý£º-ÎÞ
×÷ÕߣºMcuzone-freescale
******************************************************************************/
int main(void)
{
Systick_Initial();
// Delay_ms(5000);
// Delay_ms(5000);
UART0_Configuration();
ADC_Configuration();
SPI0_Configuration();
nrfTxInit();
gpio_init(PORTD, PTD, 7, OUTPUT);
gpio_init(PORTD, PTD, 6, OUTPUT);
Delay_ms(100);
gpio_set_pin (PTD, 7);
Delay_ms(100);
gpio_clear_pin (PTD, 7);
Delay_ms(100);
gpio_set_pin (PTD, 7);
wdog_init();
// wdog_enable();
while (1)
{
nrf24l01_read_register(nrf24l01_FIFO_STATUS, &FIFO_STATUS, 1);
if (FIFO_STATUS == 0x12)
nrf24l01_clear_flush(); /* Clear the EXTI line 0 pending bit */
wdog_refresh();
adc_channel0 = ADC_AverageConvert(); //PA1
finalSendData[2] = ((adc_channel0 >> 8) & 0x00FF); //toSendData1[1]
finalSendData[3] = ((adc_channel0) & 0x00FF); //toSendData2[0]
crc_nrf = crcNrfAdd(finalSendData);
finalSendData[4] = crc_nrf%256;
finalSendData[5] = crc_nrf/256;
if (fabs(adc_channel0 - adc_channel0_last_copy) > 4) {
adc_channel0_last_copy = adc_channel0;
wdog_refresh();
nrfSend(finalSendData);
gpio_toggle_pin (PTD, 6);
}
// wdog_refresh();
}
}
wdog.h
#include "wdog.h"
void wdog_init(void){
wdog_unlock();
delay(1);
WDOG->STCTRLH = WDOG_STCTRLH_DISTESTWDOG_MASK //Disable WDOG Test Mode
| WDOG_STCTRLH_ALLOWUPDATE_MASK | WDOG_STCTRLH_WDOGEN_MASK | WDOG_STCTRLH_DBGEN_MASK; //Allow WDOG CTRL Ref Update
WDOG->TOVALH = 0x0000;
WDOG->WINL = WDOG->WINH = 0;
WDOG->TOVALL = 0x0FFF;
WDOG->STCTRLH &= ~(WDOG_STCTRLH_ALLOWUPDATE_MASK) ;
}
void wdog_refresh(void) {
delay(1);
WDOG->REFRESH = WDOG_REFRESH_WDOGREFRESH(WDOG_REFRESH_SEQUENCE_1);
WDOG->REFRESH = WDOG_REFRESH_WDOGREFRESH(WDOG_REFRESH_SEQUENCE_2);
}
void wdog_enable(void){
delay(600);
wdog_unlock();
delay(1);
WDOG->STCTRLH |= WDOG_STCTRLH_WDOGEN_MASK;
delay(20);
}
void wdog_unlock(void){
WDOG->UNLOCK = WDOG_UNLOCK_WDOGUNLOCK(WDOG_UNLOCK_SEQUENCE_1);
WDOG->UNLOCK = WDOG_UNLOCK_WDOGUNLOCK(WDOG_UNLOCK_SEQUENCE_2);
}
void wdog_set_timeout(uint32_t time) {
}
void delay(uint16_t nCount) {
uint16_t delay = 0;
for (delay=0;delay<nCount*10;delay++);
}
I have checked couple of post discussing the same problem on this forum but there was no conclusive information regarding it.
The above code has no interrupts so i haven't disabled interrupts while refreshing or unlocking the wdog.
Withing the while loop of main.c I added a delay
Delay_ms(10);
while has solved this problem.
so we need to think what problem it must be....does calling repeated Refresh on the watchdog very quickly create a problem?