Loading...
Loading...
Embedded C Code Style Assistant, based on the code specifications of 51 MCU teaching projects. Uses snake_case as the default naming convention, with camelCase as an option. It is used to create project structures that comply with embedded development specifications, optimize code style, and provide hardware driver templates. Suitable for embedded C project development such as 51 MCU and STM32.
npx skill4agent add plugins-world/pw-skills pw-embedded-c-style# Basic example (default snake_case)
/pw-embedded-c-style Create an LED blinking project
# Project with peripherals
/pw-embedded-c-style Create a project with LCD1602 display and key input
# Specify chip
/pw-embedded-c-style Create a UART communication project for STM32F103
# Explicitly specify using camelCase
/pw-embedded-c-style Use camelCase to create a timer project# Optimize code style (default snake_case)
/pw-embedded-c-style Help me optimize this key scanning code
# Refactor module
/pw-embedded-c-style Refactor this code into an independent driver module
# Add comments
/pw-embedded-c-style Add standardized comments to this timer configuration code
# Optimize using camelCase
/pw-embedded-c-style Use camelCase to optimize this code# Generate specific peripheral driver
/pw-embedded-c-style Generate DS1302 real-time clock driver template
# Generate communication protocol
/pw-embedded-c-style Generate UART communication receive/transmit buffer implementation// Function: snake_case
void config_timer0(unsigned int ms);
void lcd_write_cmd(unsigned char cmd);
void init_ds1302(void);
// Variable: snake_case
unsigned char flag_500ms = 0;
unsigned char cnt_rxd = 0;
// Macro/Constant: All uppercase with underscores
#define SYS_MCLK (11059200/12)
#define LCD1602_RS P1^0
// sbit: All uppercase or snake_case
sbit LED = P0^0;
sbit EN_LED = P1^4;// Function: PascalCase
void ConfigTimer0(unsigned int ms);
void LcdWriteCmd(unsigned char cmd);
void InitDS1302(void);
// Variable: camelCase
unsigned char flag500ms = 0;
unsigned char cntRxd = 0;
// Macro/Constant: All uppercase with underscores (same as default)
#define SYS_MCLK (11059200/12)
#define LCD1602_RS P1^0
// sbit: All uppercase or PascalCase
sbit LED = P0^0;
sbit ENLED = P1^4;#include <reg52.h>
// Macro definitions
// sbit definitions
// Global variables
// Function declarations
void main() { }
// Function implementations
// Interrupt functionsconfig.h - Global configurations (type definitions, system parameters, IO definitions)
module.h - Module header file (structures, extern declarations, function declarations)
module.c - Module implementation (#define _MODULE_C)
main.c - Main programvoid config_timer0(unsigned int ms)
{
unsigned long tmp;
tmp = 11059200 / 12;
tmp = (tmp * ms) / 1000;
tmp = 65536 - tmp;
tmp = tmp + 12;
T0RH = (unsigned char)(tmp>>8);
T0RL = (unsigned char)tmp;
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1;
TR0 = 1;
}void interrupt_timer0() interrupt 1
{
static unsigned char tmr_500ms = 0;
TH0 = T0RH;
TL0 = T0RL;
if (++tmr_500ms >= 50)
{
tmr_500ms = 0;
flag_500ms = 1;
}
}bit flag_500ms = 0;
// Set in interrupt
flag_500ms = 1;
// Detect in main loop
while (1)
{
if (flag_500ms)
{
flag_500ms = 0;
// Execute task
}
}void key_scan(void)
{
static unsigned char keybuf[4] = {0xFF, 0xFF, 0xFF, 0xFF};
keybuf[i] = (keybuf[i] << 1) | KEY_IN[i];
if ((keybuf[i] & 0x0F) == 0x00)
key_sta[i] = 0; // Stable pressed
}unsigned char code led_char[] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
P0 = led_char[num];/* File Header Comment */
/*
*******************************************************************************
* File Name: main.c
* Description: Function description
* Version: v1.0.0
*******************************************************************************
*/
/* Function Comment */
void lcd_show_str(unsigned char x, unsigned char y,
unsigned char *str, unsigned char len)
// In-line Comment
EA = 1; // Enable global interrupt
config_timer0(10); // Configure T0 for 10ms timing// Error: Instruction cycle compensation not considered
tmp = 65536 - tmp;
// Correct: Add compensation value
tmp = 65536 - tmp;
tmp = tmp + 12; // Compensate for interrupt response delay// Error: Directly read key status
if (KEY == 0) { /* Process */ }
// Correct: Use shift register for debouncing
keybuf = (keybuf << 1) | KEY;
if ((keybuf & 0x0F) == 0x00) { /* Stable pressed */ }// Error: Multiple modules use the same variable name
unsigned char flag; // Defined in multiple .c files
// Correct: Use module prefix or static
static unsigned char uart_flag; // Visible only in this file
extern unsigned char g_system_flag; // Globally shared