|
|
本帖最后由 snaker 于 2023-2-15 08:36 编辑
试试最近挺火的ChatGPT
1、尝试让他写一个51单片机驱动8x8点阵显示心形的程序,要用到74hc595
程序如下:
- #include <reg51.h>
- sbit SHCP = P2^0; // 74HC595芯片的串行时钟输入
- sbit STCP = P2^1; // 74HC595芯片的存储时钟输入
- sbit DS = P2^2; // 74HC595芯片的串行数据输入
- unsigned char code heart[8] = {0x3C, 0x7E, 0xE7, 0xC3, 0xC3, 0xE7, 0x7E, 0x3C}; // 8x8点阵显示的心形图案
- void delay(int time) // 延时函数
- {
- int i, j;
- for (i = 0; i < time; i++)
- for (j = 0; j < 110; j++);
- }
- void shift_out(unsigned char val) // 将一个字节数据输出到74HC595芯片
- {
- unsigned char i;
- for (i = 0; i < 8; i++)
- {
- SHCP = 0;
- DS = val & 0x80;
- val <<= 1;
- SHCP = 1;
- }
- }
- void latch() // 存储输出到74HC595芯片的数据
- {
- STCP = 0;
- delay(1);
- STCP = 1;
- }
- void main()
- {
- unsigned char i, j;
- P2 = 0x00; // 初始化P2口
- while (1)
- {
- for (i = 0; i < 8; i++) // 遍历8列
- {
- shift_out(~heart[i]); // 输出图案的一列到74HC595芯片
- latch(); // 存储输出到74HC595芯片的数据
- for (j = 0; j < 8; j++) // 遍历8行
- {
- P0 = 0x01 << j; // 设置P0口对应列的输出状态
- delay(1); // 稍作延时,使显示效果更佳
- }
- }
- }
- }
复制代码 还有个解释
2、尝试让它写一个51单片机驱动LCD1602的程序
程序如下:
- #include <reg51.h>
- #include <intrins.h>
- #define LCD_DATA_PORT P0 // LCD1602数据口
- sbit RS = P2^5; // 控制命令/数据选择
- sbit RW = P2^6; // 控制读写选择
- sbit E = P2^7; // 使能端
- void delay_us(unsigned int us) // 微秒级延时函数
- {
- while (us--)
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- }
- void delay_ms(unsigned int ms) // 毫秒级延时函数
- {
- while (ms--)
- {
- delay_us(1000);
- }
- }
- void lcd_write_command(unsigned char command) // 写入命令
- {
- RS = 0; // 选择命令
- RW = 0; // 写入模式
- LCD_DATA_PORT = command; // 写入数据
- E = 1; // 使能
- delay_us(1);
- E = 0; // 禁能
- delay_ms(1); // 等待指令执行
- }
- void lcd_write_data(unsigned char data) // 写入数据
- {
- RS = 1; // 选择数据
- RW = 0; // 写入模式
- LCD_DATA_PORT = data; // 写入数据
- E = 1; // 使能
- delay_us(1);
- E = 0; // 禁能
- delay_us(50); // 等待数据写入
- }
- void lcd_init() // LCD1602初始化
- {
- lcd_write_command(0x38); // 16x2显示,5x8点阵,8位数据总线
- lcd_write_command(0x08); // 关闭显示
- lcd_write_command(0x01); // 清屏
- lcd_write_command(0x06); // 光标移动方向为右,不移动屏幕
- lcd_write_command(0x0c); // 打开显示,不显示光标,不闪烁
- }
- void lcd_display_string(unsigned char x, unsigned char y, unsigned char *str) // 在指定位置显示字符串
- {
- unsigned char addr;
- if (y == 0)
- {
- addr = 0x80 + x;
- }
- else
- {
- addr = 0xc0 + x;
- }
- lcd_write_command(addr); // 设置显示地址
- while (*str != '\0')
- {
- lcd_write_data(*str); // 写入数据
- str++;
- }
- }
- void main()
- {
- lcd_init(); // 初始化LCD1602
- lcd_display_string(0, 0, "Hello World!"); // 在第一行显示"Hello World!"
- while (1); // 程序进入死循环
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|