💡 LED Blinking Program on Megawin MG82F6D17Now that we have finished setting up the IDE, let’s get into the coding part.
Below is the sample
LED Blink Code for the
MG82F6D17 microcontroller.
#include "MG82F6D17_CONFIG.h"
#define MCU_SYSCLK 12000000
#define MCU_CPUCLK (MCU_SYSCLK)
#define LED_Pin P33
/*************************************************
µS Delay Function
*************************************************/
void DelayXus(u8 xUs)
{
while(xUs!=0)
{
#if (MCU_CPUCLK>=11059200)
_nop_();
#endif
#if (MCU_CPUCLK>=14745600)
_nop_();
_nop_();
_nop_();
_nop_();
#endif
#if (MCU_CPUCLK>=16000000)
_nop_();
#endif
#if (MCU_CPUCLK>=22118400)
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
#endif
#if (MCU_CPUCLK>=24000000)
_nop_();
_nop_();
#endif
#if (MCU_CPUCLK>=29491200)
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
#endif
#if (MCU_CPUCLK>=32000000)
_nop_();
_nop_();
#endif
xUs--;
}
}
/*************************************************
mS Delay Function
*************************************************/
void DelayXms(u16 xMs)
{
while(xMs!=0)
{
DelayXus(200);
DelayXus(200);
DelayXus(200);
DelayXus(200);
DelayXus(200);
xMs--;
}
}
void main ()
{
System_Init();
while(1)
{
LED_Pin = !LED_Pin;
DelayXms(500);
}
}
🧩 Code ExplanationHeader and Definitions:#include "MG82F6D17_CONFIG.h"
#define MCU_SYSCLK 12000000
#define MCU_CPUCLK (MCU_SYSCLK)
#define LED_Pin P33
In the header section,
#include "MG82F6D17_CONFIG.h" adds all the necessary SPLs, header files, and definitions for the chip.
#define MCU_SYSCLK 12000000 sets the system oscillator frequency to 12 MHz.
The CPU frequency is later multiplied using PLL up to 32 MHz (defined as
MCU_CPUCLK).
---
⏱ Delay FunctionsThe delay functions are simple, software-based delays — no timers are used.
They generate microsecond and millisecond delays using the [_nop_()] instruction.
The number of [_nop_()] calls depends on the system clock frequency.
Microsecond Delay Function:void DelayXus(u8 xUs)
{
while(xUs!=0)
{
// Frequency-based delay using NOPs
...
xUs--;
}
}
Millisecond Delay Function: This calls the microsecond delay several times to approximate a 1 ms delay.
void DelayXms(u16 xMs)
{
while(xMs!=0)
{
DelayXus(200);
DelayXus(200);
DelayXus(200);
DelayXus(200);
DelayXus(200);
xMs--;
}
}
Note: These are
software delays, so they are not perfectly accurate — the precision depends on CPU frequency and compiler optimization.
---
🔁 Main FunctionThe
main() function is straightforward because most of the system configuration is handled by the Megawin SPL.
void main ()
{
System_Init(); // Initialize system registers and clock
while(1)
{
LED_Pin = !LED_Pin; // Toggle LED pin
DelayXms(500); // Wait for 500 ms
}
}
System_Init() is part of the Megawin driver and handles clock setup, I/O initialization, and system configuration automatically.
The loop simply toggles the LED connected to
P3.3 every 500 ms — making it blink continuously.
---
Summary- We used the MG82F6D17 Driver to simplify system setup.
- The LED blink is achieved by toggling P3.3 and using software delay functions.
- In future examples, we will use hardware timers for more accurate delays and better CPU performance.
Now let’s compile the program and see if there are any errors. For that click on Build Target from the Project menu or simply press the F7 key. If there are no errors the IDE will compile the code and generate the HEX file which can be found in the Object folder within the project folder.
That’s it! You’ve successfully written your first LED blinking program for the MG82F6D17 MCU!