Author Topic: How to Program an 8051 Microcontroller Using Keil  (Read 146 times)

Offline SiVa000000

How to Program an 8051 Microcontroller Using Keil
« on: October 31, 2025, 11:19:25 PM »
English: I’m starting this post as a beginner-friendly guide and will keep updating it over time. Since I’m currently occupied with other work, some details may be added gradually.

The main goal is to help FTC Forum users understand the basics of programming the 8051 microcontroller using the Keil IDE. This is my first attempt, so please excuse any small mistakes.

I’ve chosen a low-cost 8051 microcontroller for learning purposes, but Keil is still a professional-grade tool — perfect for both students and developers.

In this thread, I’ll be sharing:

An introduction to the 8051 and Keil

Step-by-step working procedure

Code examples (starting with LED blink)

Screenshots and visuals for better understanding



Anyone trying or testing the setup can share snapshots or feedback — even a small visual reply will help guide others.

Let’s begin with the classic “Blink an LED” example as our first step!


Tamil: இந்த பதிவை நான் தொடக்க நிலை நண்பர்களுக்கும் எளிதாக புரியக்கூடிய வழிகாட்டியாக தொடங்குகிறேன். காலத்தின் போக்கில் இதை தொடர்ந்து புதுப்பித்து கொண்டு இருப்பேன்.
இப்போது வேறு சில பணிகளில் இருப்பதால், சில விவரங்கள் பின்னர் சேர்க்கப்படலாம்.

இந்த பதிவின் முக்கிய நோக்கம் — FTC Forum பயனர்களுக்கு Keil IDE-ஐப் பயன்படுத்தி 8051 மைக்ரோகண்ட்ரோலரை நிரலாக்குவது எப்படி என்று அடிப்படையாகப் புரியச் செய்வது.
இது எனது முதல் முயற்சி, சிறிய பிழைகள் இருந்தால் மன்னிக்கவும்.

நான் தேர்ந்தெடுத்திருப்பது குறைந்த செலவில் கிடைக்கும் 8051 மைக்ரோகண்ட்ரோலர், ஆனால் Keil ஒரு தொழில்முறை (professional) கருவி, மாணவர்கள் மற்றும் டெவலப்பர்கள் இருவருக்கும் சிறந்தது.

இந்த தொடரில் நான் பகிரப் போவது:

8051 மற்றும் Keil பற்றிய அறிமுகம்

படி படியாக செயல்முறை விளக்கம்

உதாரணக் கோடுகள் (முதல் உதாரணம் – LED Blink)

ஸ்கிரீன்ஷாட்கள் மற்றும் காட்சிகள் — எளிதாகப் புரிய உதவும்



யாராவது இந்த அமைப்பை முயற்சித்தால் அல்லது சோதித்தால், snapshot அல்லது visual reply ஒன்றை பகிரலாம் — சிறிய காட்சி உதவி கூட மற்றவர்களுக்கு வழிகாட்டியாக இருக்கும்.

அப்படியே ஆரம்பிப்போம் — நம் முதல் படியாக, “LED ஒன்றை மின்னச் செய்வோம்”! 💡
« Last Edit: November 01, 2025, 09:44:32 AM by SiVa000000 »

Offline SiVa000000

Re: How to Program an 8051 Microcontroller Using Keil
« Reply #1 on: November 01, 2025, 07:26:45 AM »
English:
🧩 Introduction to the 8051 and Keil

The 8051 microcontroller is one of the most popular and widely used controllers for learning embedded systems. Originally developed by Intel, it has been adapted by many manufacturers because of its simple architecture, low cost, and ease of programming.

The 8051 typically includes:

An 8-bit CPU

4 KB ROM (program memory)

128 bytes RAM

32 I/O pins

Timers, serial communication port, and interrupts


Even though it’s an older design, it’s still the best starting point for students to understand how microcontrollers work — including memory organization, port handling, and instruction cycles.


---

⚙️ What is Keil?

Keil µVision (pronounced MicroVision) is a professional IDE (Integrated Development Environment) used to write, compile, and debug embedded programs. It supports the 8051, ARM, and Cortex families of microcontrollers.

With Keil, you can:

Write programs in C or Assembly

Compile them to a .HEX file

Simulate the program using the built-in debugger

Flash it to your microcontroller hardware using any 8051 programmer


Keil provides a complete platform for both learning and professional development.


---

🔗 Official Keil C51 Download Link

You can download the Keil µVision C51 Compiler (for 8051) from the official Keil website:
👉 https://www.keil.com/download/product/

STEP 1: Go to https://www.keil.com/download/product/ –>Download –> Product Downloads –>Hit on C51 Setup. Enter your contact information with valid address, phone number and email. Fill in all fields of form. Download is free for evaluation version.


STEP 2: Then click on C51V954A.EXE and Download it on your computer.


STEP 3: Next step is to run setup file C51V954A.EXE and then we’ll get pop-up box, hit on Next and Proceed Installation.


STEP 4: Read license agreement, check I agree to all the terms…., and click Next.


STEP 5: Select Destination folder where you want to install Keil or default destination is already there. And hit on Next.


STEP 6: Fill up required fields with all relevant information and click on Next.


STEP 7: Wait for installation completes and hit on Next.


STEP 8: Tick on show release notes, deselect remaining (as per your choice) and click on Finish.




Tamil:
🧩 8051 மற்றும் Keil – அறிமுகம்

8051 மைக்ரோகண்ட்ரோலர் என்பது எம்பெடெட் சிஸ்டம் (Embedded System) கற்றுக்கொள்வதில் மிகவும் பிரபலமான மற்றும் பரவலாகப் பயன்படுத்தப்படும் கண்ட்ரோலர்களில் ஒன்று.
இது முதலில் Intel நிறுவனத்தால் உருவாக்கப்பட்டது. அதன் எளிய வடிவமைப்பு (architecture), குறைந்த செலவு, மற்றும் எளிதான நிரலாக்க (programming) திறன் காரணமாக பல உற்பத்தியாளர்கள் இதை தங்களின் வடிவில் மாற்றி பயன்படுத்தினர்.

8051 மைக்ரோகண்ட்ரோலரில் பொதுவாக அடங்குவது:

8-bit CPU

4 KB ROM (Program Memory)

128 bytes RAM

32 I/O Pins

Timers, Serial Communication Port, மற்றும் Interrupts



இது ஒரு பழைய வடிவமைப்பு என்றாலும், மாணவர்கள் மைக்ரோகண்ட்ரோலர் செயல்பாடு — நினைவக அமைப்பு (Memory Organization), போர்ட் கையாளல் (Port Handling), மற்றும் Instruction Cycle போன்ற அடிப்படைகளை புரிந்து கொள்ள சிறந்த தொடக்கமாகும்.


---

⚙️ Keil என்றால் என்ன?

Keil µVision (மைக்ரோவிஷன் என உச்சரிக்கப்படுகிறது) என்பது ஒரு தொழில்முறை IDE (Integrated Development Environment) ஆகும். இது எம்பெடெட் நிரல்கள் (Embedded Programs) எழுத, Compile செய்ய, மற்றும் Debug செய்ய பயன்படுத்தப்படுகிறது.
Keil, 8051, ARM, மற்றும் Cortex குடும்ப மைக்ரோகண்ட்ரோலர்களை ஆதரிக்கிறது.

Keil மூலம் நீங்கள் செய்யக்கூடியவை:

C அல்லது Assembly மொழியில் நிரல் எழுதலாம்

அதை .HEX கோப்பாக Compile செய்யலாம்

Built-in Debugger மூலம் நிரலை சோதிக்கலாம் (Simulation)

தயாரான நிரலை 8051 Programmer மூலம் ஹார்ட்வேரில் Flash செய்யலாம்


Keil ஒரு முழுமையான தளம் (Complete Platform) — கற்றலுக்கும், தொழில்முறை அபிவிருத்திக்கும் (Professional Development) ஏற்றது.


---

🔗 அதிகாரப்பூர்வ Keil C51 பதிவிறக்க இணைப்பு

Keil µVision C51 Compiler (8051க்காக) பதிவிறக்க, அதிகாரப்பூர்வ Keil வலைத்தளத்தைப் பார்வையிடுங்கள்:

👉 https://www.keil.com/download/product/
« Last Edit: November 01, 2025, 09:56:40 AM by SiVa000000 »

Offline SiVa000000

Re: How to Program an 8051 Microcontroller Using Keil
« Reply #2 on: November 01, 2025, 11:20:52 AM »
🚀 Step-by-Step Working Procedure: Let's Get Started!

In this section, we’ll begin with our first hands-on project — programming the MG82F6D17 (Megawin 8051 family) microcontroller using Keil µVision and Megawin ICE Programmer (TH065)
 
This will be a simple "Blink an LED" example, ideal for beginners to understand the basic setup and workflow.


🧰 Required Components and Tools

ItemDescriptionLink
MicrocontrollerMegawin MG82F6D17AL20 (8051-based, 20-pin SSOP)Buy Here
DatasheetOfficial MG82F6D17 Datasheet
ProgrammerMegawin 8051 ICE Programmer (TH065)View Product
AdapterSOIC/SSOP to DIP-20 Pin AdapterBuy Here
Soldering Iron StationYihua 948DB-II T12 PrecisionBuy Here
Soldering Flux10 ml flux for clean jointsBuy Here
Solder Wire1.00 mm, 50 gm spoolBuy Here
Male Header Pins2.54 mm Right Angle Header StripBuy Here
BreadboardMB102 (830 points, high quality)Buy Here
TweezerStraight ESD-11 Precision TweezerBuy Here
« Last Edit: November 01, 2025, 11:39:52 AM by SiVa000000 »

Offline SiVa000000

Re: How to Program an 8051 Microcontroller Using Keil
« Reply #3 on: November 01, 2025, 11:41:55 AM »
🚀 Step-by-Step Working Procedure: Assembling the DIP Adapter and MCU

Before we start programming, we need to assemble the MG82F6D17 microcontroller on a DIP-20 adapter board
Since the MG82F6D17 comes in an SSOP-20 surface-mount package, it must be soldered to an adapter to use it with a breadboard or programmer.





🪛 Step 1: Identify the Pin 1 Mark

On the MG82F6D17 IC, locate the Pin 1 marking — usually a small dot or notch on one side of the package. 
Also, note the Pin 1 position on the DIP adapter board. The correct orientation ensures that your pin mapping matches the programmer’s socket.

📘 Refer to the datasheet for pin configuration:





🔥 Step 2: Apply Flux and Align the Chip

1. Apply a thin layer of soldering flux on the SSOP pads of the adapter board. 
2. Carefully place the MG82F6D17 chip on the pads, ensuring all pins are aligned with their respective tracks. 
3. Use tweezers for accurate positioning — once aligned, tack down one corner pin with solder to keep it fixed.

Tip: Good alignment before soldering is crucial; SSOP pitch is very fine!


🧲 Step 3: Solder the Pins

1. Heat the soldering iron (around 330–350°C). 
2. Lightly tin the iron tip with solder. 
3. Gently drag-solder across the pins using minimal solder and flux — the surface tension will bridge correctly to each pad. 
4. If any pins get shorted, clean with flux and use a solder wick to remove the excess.

✅ Ensure all pins are properly soldered and no bridges exist between adjacent legs.


🔌 Step 4: Attach Male Headers

1. Insert Male headers into the DIP adapter holes. 
2. Solder them on the underside to make the module breadboard-friendly. 
3. Make sure the headers are straight for easy insertion into breadboards or programmer sockets.


⚡ Step 5: Continuity Check

Before powering the circuit: 
- Use a multimeter in continuity mode. 
- Check all 20 pins for proper solder contact and absence of short circuits. 
- Verify that VCC and GND pins are correctly connected.
« Last Edit: November 01, 2025, 12:01:15 PM by SiVa000000 »

Offline SiVa000000

Re: How to Program an 8051 Microcontroller Using Keil
« Reply #4 on: November 01, 2025, 12:05:15 PM »
⚙️ Step-by-Step Working Procedure: Connecting to the Programmer





When selecting the right microcontroller for a project, you must consider cost, performance, features, power consumption, and overall size

When we consider the price-to-performance or price-to-features ratio, most mainstream MCUs can be quite expensive — especially during the recent global silicon shortage

That’s where manufacturers like Megawin, Nuvoton, and STMicroelectronics step in to provide excellent alternatives at affordable prices.



🏗️ Megawin – MG82 Series 
Megawin’s MG82 series MCUs are cost-effective 8051-based controllers offering modern features such as enhanced flash memory, high-speed operation, and a wide range of peripherals — all while remaining compatible with classic 8051 tools and programmers.


⚡ Nuvoton – N76 / MS51 Series 
The Nuvoton N76 and MS51 series are also 8051-based and provide good performance at a reasonable price, suitable for industrial and hobby applications.


🔋 STMicroelectronics – STM8 Series 
The STM8 series from STMicroelectronics is another strong option. These MCUs are widely supported, but during the pandemic their prices increased considerably.


🏁 Final Comparison 
When comparing features, Megawin Microcontrollers clearly offer the best balance between cost and capability, making them an excellent choice for embedded developers seeking reliable and affordable 8-bit performance.

📊 Difference Between Megawin (MG82F6D17), Nuvoton (N76E003), and ST (STM8S003) Low-Cost Microcontrollers

Now, arguably — which is the best low-cost microcontroller currently available in the market? 

To find that out, we compared three popular budget-friendly MCUs: 
- MG82F6D17 from Megawin Technology 
- N76E003 from Nuvoton Technology 
- STM8S003 from STMicroelectronics 

The comparison below highlights the key features and specifications of each device, helping you decide which fits best for your next embedded project.



📋 Features Comparison Table

FeatureMegawin MG82F6D17Nuvoton N76E003ST STM8S003
Core ArchitectureEnhanced 8051Enhanced 8051STM8 (Proprietary 8-bit)
Program Memory (Flash)16 KB18 KB8 KB
RAM1 KB1.2 KB1 KB
EEPROM / Data Flash512 Bytes (Emulated)1 KB (Emulated)640 Bytes (Integrated)
Operating Voltage2.4V – 5.5V2.4V – 5.5V2.95V – 5.5V
Clock FrequencyUp to 24 MHzUp to 16 MHzUp to 16 MHz
I/O Pins181816
ADC Channels8-bit × 8 channels12-bit × 8 channels10-bit × 5 channels
Timers3 × 16-bit3 × 16-bit2 × 16-bit
UART / SPI / I²CYes / Yes / YesYes / Yes / YesYes / Yes / Yes
Package TypeSSOP-20 / DIP-20 (via adapter)TSSOP-20 / DIP-20TSSOP-20 / SOIC-20
Programming InterfaceICP / ISPICP / ISPSWIM
Typical Price (INR)₹45 – ₹55₹60 – ₹75₹70 – ₹85


🏁 Conclusion

Based on the specifications and availability, the Megawin MG82F6D17 offers excellent value with a good blend of performance, peripheral support, and compatibility with standard 8051 tools — making it a strong contender for cost-sensitive embedded designs.

« Last Edit: November 01, 2025, 12:26:18 PM by SiVa000000 »

Offline SiVa000000

Re: How to Program an 8051 Microcontroller Using Keil
« Reply #5 on: November 01, 2025, 12:15:27 PM »
🚀 Step-by-Step Working Procedure: Installing Megawin Chip Database

To program Megawin devices, you need to install a chip database into Keil µVision IDE
This will add all the necessary device definitions required for programming Megawin chips.

Download Link: 
8051_OCD_ICE_For_Keil.zip

Installation Steps:

  • 1. Download the file from the above link.
  • 2. Extract the contents of the ZIP file.
  • 3. Open setup.exe from the extracted folder.
  • 4. Follow the on-screen installation procedure as shown in the provided pictures.

  • 5. Once installed, Keil µVision will include all Megawin device definitions automatically.

Note: After installation, restart the Keil IDE to make sure the new devices appear in the device selection list.




Installing Programming Tools

The programming tools can be run directly — there is no need to install them

Steps:
  • 1. Already extracted folder contains IcpProgrammer.exe
  • 2.Run the IcpProgrammer.exe file directly — that’s it!

No driver installation is required for OCD ICE or ISP programmers
However, for UART programming, make sure you install the correct driver for your USB to TTL converter (if not already installed).

Tip: Common converters such as CP2102, CH340, or FTDI require their respective drivers available from the manufacturer’s website.
« Last Edit: November 07, 2025, 09:29:35 AM by SiVa000000 »

Offline SiVa000000

Re: How to Program an 8051 Microcontroller Using Keil
« Reply #6 on: November 07, 2025, 09:55:24 AM »
🚀 Step-by-Step Working Procedure: First Project – Let’s Blink an LED

To get started, let’s learn how to create a new project for the MG82F6D17 microcontroller in Keil µVision.

Step 1: Create a new folder for your project with your desired name. 
Example: MyFirstProject

Step 2: Inside this folder, create a subfolder named Source
Example structure:
Code: [Select]
MyFirstProject\
 ├── Source\

Step 3: Open the MG82F6D17 Driver ZIP file and extract the following files from:
Code: [Select]
Code\Sample\Source
into your newly created Source folder.

Download MG82F6D17 Driver: 
Click here to download the MG82F6D17 Driver



Now open the Keil µVision IDE and select New µVision Project from the Project menu.



Now open the previously created folder, name the project, and click Save as shown below.



Select MG82F6D17 from the list and click OK
Note: If it is not shown in the list, check that the database is installed properly or reinstall the database file.



Click on No if prompted to copy the ‘STARTUP.A51’ file.



Now in the µVision window, under Target → Source Group 1, right-click on Source Group 1 and select 
Add New Item to Group 'Source Group 1'.



In the new window, select C File, name the file as Main, select the Source folder we created earlier, and click Add.



Again, in the project window, right-click on Source Group 1 and select 
Add Existing Files to Group 'Source Group 1'.



Select the following files from Source/Driver/Source folder and click Add.



Right-click on Target 1 and select Options for Target 'Target 1' or press ALT + F7 to open the options window.



In the options, window tick Use On-chip ROM and Use On-chip XRAM options



Go to the Output tab and enable Create Hex File option



In the C51 tab selects Included paths and add the following folder location


I you want to use the OCD ICE adapter for Debug and programming make the following changes too and press Ok to close the window.

« Last Edit: November 07, 2025, 10:11:21 AM by SiVa000000 »

Offline SiVa000000

Re: How to Program an 8051 Microcontroller Using Keil
« Reply #7 on: November 07, 2025, 10:18:57 AM »
🚀 Step-by-Step Working Procedure: Schematics for the Blink Example



Added a led and current limiting resistor to the Pin P3.3.


Here is the hardware setup. I used a SSOP to DIP adapter to use it with the breadboard. As shown in the schematics LED is connected to the pin P3.3 through a 2.2K resistor.


You can follow the circuit given above and make the connections, I have built everything on a breadboard and have also connected my programmer to the microcontroller. The set-up looks like this as shown above.

Offline SiVa000000

Re: How to Program an 8051 Microcontroller Using Keil
« Reply #8 on: November 07, 2025, 10:28:59 AM »
💡 LED Blinking Program on Megawin MG82F6D17

Now 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.

Code: [Select]
#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 Explanation

Header and Definitions:
Code: [Select]
#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 Functions

The 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:
Code: [Select]
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.
Code: [Select]
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 Function

The main() function is straightforward because most of the system configuration is handled by the Megawin SPL.

Code: [Select]
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!
« Last Edit: November 07, 2025, 10:33:07 AM by SiVa000000 »

Offline SiVa000000

Re: How to Program an 8051 Microcontroller Using Keil
« Reply #9 on: November 07, 2025, 11:21:41 AM »
Uploading Code to Megawin Microcontroller

As we have already established there are multiple ways to upload the code to the MG82F6D17. Through OCD ICE or ISP Programmer or through UART. We will be showing two methods that include using OCD ICE and UART.

Using OCD ICE Adapter

First to uploading using OCD ICE, connect the OCD pins of the microcontroller to the OCD ICE adapter as shown below-


For MG82F6D17, we will only be using five lines including the VDD and GND. The CLK pin is not used in this case. To open the programmer UI, you can run ICPProgremmer.exe directly from the Database Installer folder from (EN)8051_OCD_ICE_For_Keil_v3.19.0.4.zip file or from the Keil installation directory (for me it was C:\Keil_v5\C51\INC\Megawin\ ICPProgrammer.exe). Select the Correct MCU Part Number



Click “Load File” and choose loading AP(Code) or IAP(Data). “Load File” can be clicked repeatedly to load different files. While loading IAP(Data), users have to key in Address. HEX and BIN data formats are supported for file loading.



Click “Insert ISP-Code” may choose to insert Megawin-provided ISP code or User-defined ISP code. If the ISP function is not needed, this step can be omitted.



H/W Option Setting let you configure ISP memory, IAP memory and fuse bits. Click on Update target to upload the firmware.










Offline SiVa000000

Re: How to Program an 8051 Microcontroller Using Keil
« Reply #10 on: November 07, 2025, 11:26:03 AM »
Final Output  8)


Here is the result. The LED is blinking at a constant rate of 2Hz, i.e. with a delay of 500ms. A 2.2K resistor is used as a current limit resistor to protect the LED.



Final Code

Code: [Select]
#include "MG82F6D17_CONFIG.h"

#define MCU_SYSCLK        12000000
#define MCU_CPUCLK        (MCU_SYSCLK)
#define LED_Pin        P33

/*************************************************
µS Dealy 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);
    }

}

« Last Edit: November 07, 2025, 11:31:01 AM by SiVa000000 »