32单片机lcd绘制梯形
时间: 2023-06-15 12:04:40 浏览: 70
要在32单片机上绘制梯形,需要使用液晶显示器(LCD)和相应的图形绘制函数。以下是一些简单的步骤:
1.初始化LCD屏幕
2.选择LCD显示的区域
3.使用直线绘制函数绘制四条边,构成梯形
4.使用填充函数填充梯形内部
下面是一个简单的代码示例:
```
#include <reg52.h>
#include <intrins.h>
//定义LCD数据端口
sbit LCD_D4 = P1^4;
sbit LCD_D5 = P1^5;
sbit LCD_D6 = P1^6;
sbit LCD_D7 = P1^7;
//定义LCD控制端口
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
//LCD显示延时函数
void LCD_Delay(unsigned int i)
{
while(i--);
}
//LCD写指令函数
void LCD_WriteCommand(unsigned char cmd)
{
LCD_RW = 0; //写入模式
LCD_RS = 0; //写入指令
LCD_EN = 1; //使能
LCD_D4 = cmd & 0x01; //写入数据
LCD_D5 = cmd & 0x02;
LCD_D6 = cmd & 0x04;
LCD_D7 = cmd & 0x08;
LCD_Delay(10); //延时
LCD_EN = 0; //关闭使能
LCD_Delay(10); //延时
}
//LCD写数据函数
void LCD_WriteData(unsigned char dat)
{
LCD_RW = 0; //写入模式
LCD_RS = 1; //写入数据
LCD_EN = 1; //使能
LCD_D4 = dat & 0x01; //写入数据
LCD_D5 = dat & 0x02;
LCD_D6 = dat & 0x04;
LCD_D7 = dat & 0x08;
LCD_Delay(10); //延时
LCD_EN = 0; //关闭使能
LCD_Delay(10); //延时
}
//LCD初始化函数
void LCD_Init()
{
LCD_WriteCommand(0x02); //初始化LCD屏幕
LCD_WriteCommand(0x28); //设置LCD为4位数据线、2行显示、5x8点阵字符
LCD_WriteCommand(0x0C); //显示开、无光标、不闪烁
LCD_WriteCommand(0x06); //文字不动,地址自动加1
LCD_WriteCommand(0x01); //清屏
}
//LCD显示字符函数
void LCD_ShowChar(unsigned char x, unsigned char y, unsigned char chr)
{
unsigned char addr;
if(y == 0) addr = 0x80 + x;
else addr = 0xC0 + x;
LCD_WriteCommand(addr);
LCD_WriteData(chr);
}
//LCD显示字符串函数
void LCD_ShowString(unsigned char x, unsigned char y, unsigned char *str)
{
while(*str != '\0')
{
LCD_ShowChar(x, y, *str);
x++;
str++;
}
}
//LCD清屏函数
void LCD_Clear()
{
LCD_WriteCommand(0x01);
}
//绘制直线函数
void Draw_Line(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)
{
unsigned char i, dx, dy;
float k, b;
if(x1 == x2) //竖线
{
for(i = y1; i <= y2; i++)
{
LCD_ShowChar(x1, i, '*');
}
}
else if(y1 == y2) //横线
{
for(i = x1; i <= x2; i++)
{
LCD_ShowChar(i, y1, '*');
}
}
else //斜线
{
dx = x2 - x1;
dy = y2 - y1;
k = (float)dy / dx;
b = y1 - k * x1;
for(i = x1; i <= x2; i++)
{
LCD_ShowChar(i, (unsigned char)(k * i + b), '*');
}
}
}
//绘制梯形函数
void Draw_Trapezium(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char h)
{
Draw_Line(x1, y1, x2, y1);
Draw_Line(x1, y1, x1 + h, y1 + h);
Draw_Line(x2, y1, x2 - h, y1 + h);
Draw_Line(x1 + h, y1 + h, x2 - h, y1 + h);
}
//填充梯形内部函数
void Fill_Trapezium(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char h)
{
unsigned char i, j;
for(i = x1 + 1; i < x1 + h; i++)
{
for(j = y1 + 1; j < y1 + h; j++)
{
LCD_ShowChar(i, j, '*');
}
}
for(i = x2 - h + 1; i < x2; i++)
{
for(j = y1 + 1; j < y1 + h; j++)
{
LCD_ShowChar(i, j, '*');
}
}
}
void main()
{
LCD_Init(); //初始化LCD屏幕
Draw_Trapezium(10, 10, 50, 30, 10); //绘制梯形
Fill_Trapezium(10, 10, 50, 30, 10); //填充梯形内部
while(1);
}
```
该代码使用了32单片机上的LCD显示器,并通过绘制直线和填充函数来绘制和填充梯形。你可以根据自己的需要进行修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)