1602 lcd 滚动字幕

#include typedef unsigned char uint8; typedef unsigned int uint16; sbit dula=P2^6; sbit wela=P2^7; sbit LCDEN = P3^4; sbit RS = P3^5; sbit RW = P3^6; sbit BUSY = P0^7; //繁忙检测标志 void _delay_ms(uint16 delaytime); //延时函数 void LCD1602_Wait(); //繁忙检测 void LCD1602_Write_Cmd(uint8 w_cmd); //写命令 void LCD1602_Write_Byte(uint8 w_byte); //写数据 void LCD1602_Init(); //初始化1602 LCD void LCD1602_Roll_String(uint8 *str); //显示滚动字幕 void main() { uint8 str[] = "Fuck you"; LCD1602_Init(); while (1) { LCD1602_Roll_String(str); } } void _delay_ms(uint16 delaytime) { uint8 i = 0; while (delaytime--) { for (i=0; i < 112; i++) ; } } void LCD1602_Wait() { do { RS = 0; RW = 1; LCDEN = 0; LCDEN = 1; } while (BUSY); LCDEN = 0; } void LCD1602_Write_Cmd(uint8 w_cmd) { LCD1602_Wait(); RS = 0; RW = 0; P0 = w_cmd; LCDEN = 0; _delay_ms(5); LCDEN = 1; _delay_ms(5); LCDEN = 0; } void LCD1602_Write_Byte(uint8 w_byte) { LCD1602_Wait(); RS = 1; RW = 0; P0 = w_byte; LCDEN = 0; _delay_ms(5); LCDEN = 1; _delay_ms(5); LCDEN = 0; } void LCD1602_Init() { dula=0; wela=0; //关数码管 LCD1602_Write_Cmd(0x38); //16X2显示 5X7点阵显示 8位数据接口 LCD1602_Write_Cmd(0x08); //显示关闭 LCD1602_Write_Cmd(0x01); //清屏 LCD1602_Write_Cmd(0x06); //显示光标移动设置 LCD1602_Write_Cmd(0x0c); //显示开及光标设置 } void LCD1602_Roll_String(uint8 *str) { static uint8 start = 0x80; //字幕起始位置 static uint8 offset = 0; //光标相对起始位置的偏移 static uint8 cursor = 0x80; //光标位置 LCD1602_Write_Cmd(0x01); //清屏 while (str[offset] != '/0') //显示字幕 { cursor = 0x80+(start+offset)%0x10; //取 模以防越界 LCD1602_Write_Cmd(cursor); LCD1602_Write_Byte(str[offset++]); } offset = 0; start++; if (start == 0x90) //滚动出边界 { start = 0x80; //重新从头开始滚动 } _delay_ms(200); }

你可能感兴趣的:(1602 lcd 滚动字幕)