Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Microcontroller-Based Binary to Gray Code Converter with LED Display

View and Edit Project In Cirkit DesignerImage of Microcontroller-Based Binary to Gray Code Converter with LED Display

Circuit Documentation

Summary

This circuit is designed to read a 4-bit binary input from rocker switches and convert it to a 4-bit Gray code output, which is then displayed using LEDs. The circuit includes a microcontroller, multiple rocker switches, resistors, and LEDs. The microcontroller reads the binary input from the switches, performs the binary-to-Gray code conversion, and outputs the Gray code to the LEDs.

Component List

  1. Microcontroller: mini squadron

    • Pins: PC0, PC1, PC2, PC3, PC4, PC5, PC6, PD0, GND, 3.3V, PA2, PA1, PD7, PD6, PD5, PD4, PD3, PD2, PD1, PC7, 5V, SWCLK, SWDIO, RX, TX, RST
    • Description: Main processing unit for the circuit.
    • Purpose: Reads binary input, converts it to Gray code, and outputs the result.
  2. Rocker Switch

    • Pins: 1, 2
    • Description: Mechanical switch with two positions.
    • Purpose: Provides binary input to the microcontroller.
  3. LED: Two Pin (red)

    • Pins: Cathode, Anode
    • Description: Light-emitting diode.
    • Purpose: Displays the Gray code output.
  4. Resistor

    • Pins: Pin1, Pin2
    • Description: 220 Ohms resistor.
    • Purpose: Limits current to the LEDs.

Wiring Details

Microcontroller: mini squadron

Rocker Switch 1

Rocker Switch 2

Rocker Switch 3

Rocker Switch 4

LED 1

LED 2

LED 3

LED 4

Resistor 1

Resistor 2

Resistor 3

Resistor 4

Documented Code

// Included the required header files
#include <stdio.h>
#include <debug.h>
#include <ch32v00x.h>

// Function to calculate Binary to Gray Code
uint8_t BinaryToGray(uint8_t binary)
{
    return (binary >> 1) ^ binary;
}

// Configuring GPIO Pins
void GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0}; // Structure variable used for GPIO configuration
    
    // Enable clock for Port D (Input) and Port C (Output)
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

    // Input Pins Configuration (Switches: PD1 = LSB, PD4 = MSB)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // Input Pull-Up
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    // Output Pins Configuration (LEDs: PC0 = LSB, PC3 = MSB)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Output Push-Pull
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
}

// Main Function
int main()
{
    uint8_t binary_input = 0;  // Stores 4-bit binary input
    uint8_t gray_output = 0;   // Stores 4-bit Gray code output

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    SystemCoreClockUpdate();
    Delay_Init();
    GPIO_Config();

    while (1)
    {
        // Read Binary Input from switches (PD1 = LSB to PD4 = MSB)
        binary_input = 0;
        binary_input |= GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1) << 0; // Bit 0 (LSB)
        binary_input |= GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2) << 1; // Bit 1
        binary_input |= GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3) << 2; // Bit 2
        binary_input |= GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_4) << 3; // Bit 3 (MSB)

        // Convert Binary to Gray Code
        gray_output = BinaryToGray(binary_input);

        // Write Gray Code Output to LEDs (PC0 = LSB to PC3 = MSB)
        GPIO_WriteBit(GPIOC, GPIO_Pin_0, (BitAction)(gray_output & 0x01)); // Gray Bit 0 (LSB)
        GPIO_WriteBit(GPIOC, GPIO_Pin_1, (BitAction)((gray_output >> 1) & 0x01)); // Gray Bit 1
        GPIO_WriteBit(GPIOC, GPIO_Pin_2, (BitAction)((gray_output >> 2) & 0x01)); // Gray Bit 2
        GPIO_WriteBit(GPIOC, GPIO_Pin_3, (BitAction)((gray_output >> 3) & 0x01)); // Gray Bit 3 (MSB)
    }
}