有没有能写51单片机汉诺塔游戏程序的,大学内容不需要太复杂,价格300左右

实物是普中HC6800-ES开发板,STC89C52芯片,能在LCD12864液晶显示屏实现汉诺塔游戏机,按键控制。



```c
#include <reg51.h>
#include <stdio.h>
#include <intrins.h>

#define LCD_DATA P2
sbit LCD_RS = P0^0;
sbit LCD_RW = P0^1;
sbit LCD_EN = P0^2;

void delay(unsigned int i)
{
    while (i--);
}

void writeCommand(unsigned char cmd)
{
    LCD_RS = 0;  // Set RS to command mode
    LCD_RW = 0;  // Set RW to write mode
    LCD_DATA = cmd;  // Load command to data port
    LCD_EN = 1;  // Enable LCD
    _nop_();  // Delay for a short period
    _nop_();
    _nop_();
    _nop_();
    LCD_EN = 0;  // Disable LCD
}

void writeData(unsigned char dat)
{
    LCD_RS = 1;  // Set RS to data mode
    LCD_RW = 0;  // Set RW to write mode
    LCD_DATA = dat;  // Load data to data port
    LCD_EN = 1;  // Enable LCD
    _nop_();  // Delay for a short period
    _nop_();
    _nop_();
    _nop_();
    LCD_EN = 0;  // Disable LCD
}

void initLCD()
{
    delay(100);
    writeCommand(0x30);  // Function set: 8-bit data, 2-line display, 5x8 font
    delay(50);
    writeCommand(0x0C);  // Display control: Display on, cursor off, blink off
    delay(50);
    writeCommand(0x01);  // Clear display
    delay(200);
    writeCommand(0x06);  // Entry mode set: Increment cursor, no shift
    delay(50);
}

void clearLCD()
{
    writeCommand(0x01);  // Clear display
    delay(200);
}

void setPosition(unsigned char row, unsigned char column)
{
    unsigned char addr;
    if (row == 0)
        addr = 0x80 + column;  // First row address
    else
        addr = 0xC0 + column;  // Second row address
    writeCommand(addr);  // Set DDRAM address
}

void printString(unsigned char row, unsigned char column, char *str)
{
    setPosition(row, column);  // Set cursor position
    while (*str)
    {
        writeData(*str++);  // Write character to display
    }
}

void displayTower(unsigned char *tower, unsigned char size)
{
    unsigned char i;
    clearLCD();
    for (i = 0; i < size; i++)
    {
        setPosition(i, 0);
        writeData(tower[i] + '0');
    }
}

void hanoi(unsigned char n, unsigned char *source, unsigned char *target, unsigned char *auxiliary)
{
    if (n > 0)
    {
        hanoi(n - 1, source, auxiliary, target);
        target[n - 1] = source[n - 1];
        displayTower(source, 5);
        displayTower(target, 5);
        hanoi(n - 1, auxiliary, target, source);
    }
}

void main()
{
    unsigned char source[5] = {5, 4, 3, 2, 1};
    unsigned char target[5] = {0};
    unsigned char auxiliary[5] = {0};
    
    initLCD();
    displayTower(source, 5);
    delay(2000);
    
    hanoi(5, source, target, auxiliary);
    
    while (1);
}


```

参考GPT和自己的思路:
我能够提供一份基础的51单片机汉诺塔游戏程序供您参考:

#include <reg52.h>
#include <intrins.h> 

#define DATA P1

sbit RS = P0 ^ 5;
sbit RW = P0 ^ 6;
sbit E = P0 ^ 7;

void DelayUs2x(unsigned char t)
{
    while (--t);
}

void DelayMs(unsigned char t)
{
    while (t--)
    {
        DelayUs2x(245);
        DelayUs2x(245);
    }
}

void InitLcd()
{
    DelayMs(15);
    WriteCmd(0x38); 
    DelayMs(5);
    WriteCmd(0x38);
    DelayMs(5);
    WriteCmd(0x38);
    DelayMs(5);
    WriteCmd(0x0c); 
    DelayMs(5);
    WriteCmd(0x06); 
    DelayMs(5);
    WriteCmd(0x01); 
    DelayMs(5);
}

void WriteCmd(unsigned char cmd)
{
    RS = 0;
    RW = 0;
    DATA = cmd;
    DelayUs2x(5);
    E = 1;
    _nop_();
    _nop_();
    E = 0;
}

void WriteData(unsigned char dat)
{
    RS = 1;
    RW = 0;
    DATA = dat;
    DelayUs2x(5);
    E = 1;
    _nop_();
    _nop_();
    E = 0;
}

void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
    unsigned char i = 0;
    if (y != 0)
    {
        x |= 0x40;
    }
    x |= 0x80;
    WriteCmd(x);
    while (str[i] != '\0')
    {
        WriteData(str[i]);
        i++;
    }
}

void Hanoi(unsigned char n, unsigned char x, unsigned char y, unsigned char z)
{
    if (n == 1)
    {
        LcdShowStr(0, z, "A -> C ");
    }
    else
    {
        Hanoi(n - 1, x, z, y);
        LcdShowStr(0, z, "A -> C ");
        Hanoi(n - 1, y, x, z);
    }
}

void main()
{
    unsigned char str[16];
    InitLcd();
    LcdShowStr(0, 0, "Hanoi Tower Game");
    LcdShowStr(0, 1, "Move n discs from A to C");
    while (1)
    {
        unsigned char n, i;
        for (i = 0; i < 16; i++)
        {
            str[i] = ' ';
        }
        LcdShowStr(0, 3, "Please input n: ");
        for (i = 0; i < 3; i++)
        {
            n = P2;
            n &= 0x0f;
            n |= 0x30;
            str[15 - i] = n;
            LcdShowStr(0, 3, "Please input n: ");
            LcdShowStr(0, 3, str);
            DelayMs(500); 
        }
        n = str[13] - '0';
        Hanoi(n, 0, 1, 2);
        LcdShowStr(0, 5, "Hanoi Done!       ");
        DelayMs(2000);
        LcdShowStr(0, 5, "                  ");
    }
}

此程序利用51单片机驱动LCD12864液晶显示屏,实现了汉诺塔游戏。具体实现方法为:用户输入需要搬迁的盘数N,程序递归计算并显示移动过程,最终在屏幕上显示完成的信息。

请根据实际情况进行修改和优化,建议先进行仿真测试,确认无误后再下载到实际硬件环境中运行。

#include <reg52.h>
#include <intrins.h> 
 
#define DATA P1
 
sbit RS = P0 ^ 5;
sbit RW = P0 ^ 6;
sbit E = P0 ^ 7;
 
void DelayUs2x(unsigned char t)
{
    while (--t);
}
 
void DelayMs(unsigned char t)
{
    while (t--)
    {
        DelayUs2x(245);
        DelayUs2x(245);
    }
}
 
void InitLcd()
{
    DelayMs(15);
    WriteCmd(0x38); 
    DelayMs(5);
    WriteCmd(0x38);
    DelayMs(5);
    WriteCmd(0x38);
    DelayMs(5);
    WriteCmd(0x0c); 
    DelayMs(5);
    WriteCmd(0x06); 
    DelayMs(5);
    WriteCmd(0x01); 
    DelayMs(5);
}
 
void WriteCmd(unsigned char cmd)
{
    RS = 0;
    RW = 0;
    DATA = cmd;
    DelayUs2x(5);
    E = 1;
    _nop_();
    _nop_();
    E = 0;
}
 
void WriteData(unsigned char dat)
{
    RS = 1;
    RW = 0;
    DATA = dat;
    DelayUs2x(5);
    E = 1;
    _nop_();
    _nop_();
    E = 0;
}
 
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
    unsigned char i = 0;
    if (y != 0)
    {
        x |= 0x40;
    }
    x |= 0x80;
    WriteCmd(x);
    while (str[i] != '\0')
    {
        WriteData(str[i]);
        i++;
    }
}
 
void Hanoi(unsigned char n, unsigned char x, unsigned char y, unsigned char z)
{
    if (n == 1)
    {
        LcdShowStr(0, z, "A -> C ");
    }
    else
    {
        Hanoi(n - 1, x, z, y);
        LcdShowStr(0, z, "A -> C ");
        Hanoi(n - 1, y, x, z);
    }
}
 
void main()
{
    unsigned char str[16];
    InitLcd();
    LcdShowStr(0, 0, "Hanoi Tower Game");
    LcdShowStr(0, 1, "Move n discs from A to C");
    while (1)
    {
        unsigned char n, i;
        for (i = 0; i < 16; i++)
        {
            str[i] = ' ';
        }
        LcdShowStr(0, 3, "Please input n: ");
        for (i = 0; i < 3; i++)
        {
            n = P2;
            n &= 0x0f;
            n |= 0x30;
            str[15 - i] = n;
            LcdShowStr(0, 3, "Please input n: ");
            LcdShowStr(0, 3, str);
            DelayMs(500); 
        }
        n = str[13] - '0';
        Hanoi(n, 0, 1, 2);
        LcdShowStr(0, 5, "Hanoi Done!       ");
        DelayMs(2000);
        LcdShowStr(0, 5, "                  ");
    }
}

```bash



```

可以借鉴下

#define _CRT_SECURE_NO_WARNINGS 1
 
#include<stdio.h>
 
void move(char a, char b)
{
    printf("%c -> %c\n", a,b);           //打印盘子需要移动的方向
}
 
void hanoti(unsigned int n,char a,char b,char c)
{
    if (n == 1)
        move(a, c);
    else
    {
        hanoti(n - 1, a, c, b);
        move(a, c);
        hanoti(n - 1, b, a, c);
    }
}
 
 
int main()
{
    unsigned int n = 0;
    scanf("%d", &n);                    //输入几层汉诺塔
    hanoti(n,'A','B','C');
    return 0;
}