写一个用单片机C语言在并口液晶屏上画0-100进度条
时间: 2023-05-11 12:02:20 浏览: 188
以下是一个简单的用单片机C语言在并口液晶屏上画0-100进度条的代码:
#include <reg52.h>
#include <intrins.h>
#define LCD_DB P0
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_E = P2^2;
void delay(unsigned int t)
{
while(t--);
}
void write_cmd(unsigned char cmd)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_DB = cmd;
LCD_E = 1;
_nop_();
_nop_();
LCD_E = 0;
delay(10);
}
void write_data(unsigned char dat)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_DB = dat;
LCD_E = 1;
_nop_();
_nop_();
LCD_E = 0;
delay(10);
}
void init_lcd()
{
write_cmd(0x38);
write_cmd(0x0c);
write_cmd(0x06);
write_cmd(0x01);
}
void draw_progress_bar(unsigned char progress)
{
unsigned char i;
write_cmd(0x80);
write_data('0');
write_data('-');
write_data('1');
write_data('0');
write_data('0');
write_data('%');
write_cmd(0xc0);
for(i = 0; i < progress; i++)
{
write_data('*');
}
for(i = progress; i < 100; i++)
{
write_data(' ');
}
}
void main()
{
unsigned char i;
init_lcd();
for(i = 0; i <= 100; i++)
{
draw_progress_bar(i);
delay(1000);
}
}
关于lua closure factory的完整代码,可以参考以下示例:
function make_counter()
local count = 0
return function()
count = count + 1
return count
end
end
local counter1 = make_counter()
print(counter1()) -- 1
print(counter1()) -- 2
local counter2 = make_counter()
print(counter2()) -- 1
print(counter1()) -- 3
这个示例中,make_counter函数返回了一个闭包,闭包中包含了一个局部变量count,以及一个匿名函数。每次调用匿名函数时,count的值会加1,并返回新的count值。由于闭包中的count变量是局部变量,所以不会被外部的代码修改。因此,每个make_counter调用都会返回一个新的计数器。
阅读全文