系统通电后,要输入6位的密码才能进入系统。进入系统后,可以输入英文文章。即系统按键具有数字、标点符号、大小写字母、空格,回车等字符输入功能。附加功能为进入系统后,密码可以用按键更改;输入的文章,液晶界面显示满之后,能够分页切换,并且具有保存、清楚功能。 
单片机源程序如下:
- /**
- * note.c
- *
- * Created At: 2018-12-17
- * Author: yowfung
- * Description: 英文记事本程序
- */
-
- #include <reg52.h>
- #include "../inc/public.h"
- #include "../inc/lcd12864.h"
- #include "../inc/keys.h"
- /**
- * 定义常量
- */
- const unsigned char PASSWORD_CONTENT[7] = "yowfung"; // 登录密码
- const unsigned char PASSWORD_LENGTH = 7; // 密码长度
- /**
- * 声明函数
- */
- bit ValidatePassword();
- void Editor(void);
- void insCharToStr(unsigned char *str, unsigned char ch, unsigned int index);
- void delCharFromStr(unsigned char *str, unsigned int index);
- void WriteContent(unsigned char *content, unsigned char length);
- void setLcdPos(unsigned char pos);
- /**
- * 英文记事本程序运行
- *
- * 需要连接的线路:
- * 1. LCD12864电路: RS -> P0.0
- * RW -> P0.1
- * E -> P0.2
- * D0~D7 -> P1.0~1.7
- * 2. 矩阵按键电路: Row1~9 -> P2.0~2.7,P0.7
- * Col1~6 -> P3.2~3.7
- * 3. Shift状态灯电路:LED -> P0.3
- *
- * 功能及流程描述:
- * 1. 运行后LCD上第一行显示“Hello YowFung”,第二行显示“NoteBook”;
- * 2. 5秒后出现密码输入界面,通过按键输入六位数密码,然后按Enter键,密码
- * 正确则进入编辑状态,否则显示“Password Error!”3秒并回到密码输入
- * 界面;
- * 3. 在编辑状态中,通过按键输入字符,每输入一个字符光标向右移动一位,当
- * 一行显示满了后,则在下一行显示,当一个屏幕都显示满了后,则可以通过
- * 按 Up 或 Down 键来滚动屏幕;
- * 4. 按 ← 或 → 键可以左右移动光标;
- * 5. 按 Clear 键可以清屏;
- * 6. 按 Del 键可以删除当前光标左边的一个字符;
- * 7. 按下 Shift 键时,Shift 状态 LED 灯被点亮,此时输入的字符为第二字
- * 符,再按一次 Shift 键,LED 灭,回到第一字符输入状态。
- */
- void note_run(void)
- {
- bit vali_res = 0;
-
- // 初始化 LCD12864
- Lcd12864_init();
-
- // 显示欢迎界面
- Lcd12864_wrLine(2, " Hello YowFung");
- Lcd12864_wrLine(3, " NoteBook");
- delay(3000);
-
-
- // 输入登录密码,直到密码正确
- do {
- // 清屏
- Lcd12864_cls();
- delay(500);
-
- // 输入密码并验证
- vali_res = ValidatePassword();
-
- // 如果密码错误则显示错误信息
- if (vali_res == 0) {
- Lcd12864_cls();
- Lcd12864_showCursor(0);
- Lcd12864_wrLine(2, "Password Error,");
- Lcd12864_wrLine(3, "Input Again!");
- delay(2000);
- }
- } while (vali_res == 0);
-
- // 密码输入正确,显示欢迎信息
- Lcd12864_cls();
- Lcd12864_showCursor(0);
- Lcd12864_wrLine(2, " Welcome Back!");
- delay(2000);
-
- // 进入编辑界面
- Editor();
- while(1);
- }
- /**
- * 检测登录密码输入并验证
- *
- */
- bit ValidatePassword()
- {
- unsigned char input, length = 0, ch, ct, i;
- unsigned char pass_in[7] = {0x00}; // 全部显示空格
- unsigned char pass_show[7] = {0x20}; // 密码默认为空
- bit result = 1;
- // 提示输入登录密码的界面
- Lcd12864_wrLine(1, " Login Password");
- Lcd12864_wrLine(2, " ^^^^^^^^^^^^^^ ");
- Lcd12864_wrLine(3, "> <");
- Lcd12864_wrLine(4, " -------------- ");
- Lcd12864_setPos(3, 2);
- Lcd12864_showCursor(1);
- delay(500);
- // 监测按键输入(同时监测字符按键和控制按键)
- while (1) {
- input = Keys_get9x6Num();
- delay(20);
- if (input == 0)
- // 如果没有按下任何键
- continue;
- else {
- // 取出按下的控制键码
- ct = Keys_get9x6Ctrl(input);
- if (ct != NULL_CHAR) {
- switch (ct) {
- case CTRL_SHIFT : { // 切换 Shift 输入状态
- Keys_setShift(!Keys_getShift());
- }; break;
- case CTRL_DEL : { // 栅格
- if (length) {
- length--;
- pass_show[length] = 0x20;
- pass_in[length] = 0x00;
- }
- }; break;
- case CTRL_CLS : { // 清屏
- length = 0;
- for (i = 0; i < 7; i++) {
- pass_show[i] = 0x20;
- pass_in[i] = 0x00;
- }
- }; break;
- case CTRL_ENT : { // 回车
- for (i = 0; i < 7; i++) {
- if (pass_in[i] != PASSWORD_CONTENT[i]) {
- result = 0;
- break;
- }
- }
- return result;
- }; break;
- }
- }
- // 如果已经输满了,则不再监测字符输入
- if (length != PASSWORD_LENGTH) {
- // 取出按下的字符
- ch = Keys_get9x6Char(input);
- if (ch != NULL_CHAR) {
- pass_in[length] = ch;
- pass_show[length] = '*';
- length++;
- }
- }
- }
- // 显示输入情况
- Lcd12864_wrLine(3, "| |");
- Lcd12864_setPos(3, 2);
- Lcd12864_wrStr(pass_show, 7);
- Lcd12864_setPos(3, 2 + length/2);
- }
- }
- /**
- * 记事本编辑器
- */
- void Editor(void)
- {
- unsigned char input, ch, ct, i, j;
- unsigned char length = 0; // 字符长度
- unsigned char content[64] = {0x20}; // 字符串内容
- unsigned char pos = 0; // 光标位置
- // 清屏,显示光标
- Lcd12864_cls();
- Lcd12864_showCursor(1);
- delay(500);
- while(1) {
- // 监测按键输入
- input = Keys_get9x6Num();
- delay(20);
- if (input == 0)
- // 如果没有按键输入则跳过
- continue;
- else {
- // 监测是否为控制键码
- ct = Keys_get9x6Ctrl(input);
- if (ct != NULL_CHAR) {
- switch (ct) {
- case CTRL_CLS : { // 清屏
- Lcd12864_cls();
- length = 0;
- pos = 0;
- for (i = 0; i < 64; i++)
- content[i] = 0x20;
- }; continue;
- case CTRL_DEL : { // 栅格
- if (pos != 0) {
- pos--;
- delCharFromStr(content, pos);
- length--;
- }
- }; break;
- case CTRL_SHIFT : { // 切换 Shift 输入状态
- Keys_setShift(!Keys_getShift());
- }; continue;
- case CTRL_UP : { // 向上移动光标
- if (pos >= 16) {
- pos -= 16;
- setLcdPos(pos);
- }
-
- }; continue;
- case CTRL_DOWN : { // 向下移动光标
- if (pos + 16 < length) {
- pos += 16;
- setLcdPos(pos);
- }
-
- }; continue;
- case CTRL_LEFT : { // 向左移动光标
- if (pos != 0) {
- pos--;
- setLcdPos(pos);
- }
-
- }; continue;
- case CTRL_RIGHT : { // 向右移动光标
- if (pos < length - 1) {
- pos++;
- setLcdPos(pos);
- }
-
- }; continue;
- }
- }
- // 监测是否为字符按键
- ch = Keys_get9x6Char(input);
- if (ch != NULL_CHAR) {
- insCharToStr(content, ch, pos);
- pos++;
- length++;
- if (pos > 63)
- pos = 63;
- if (length > 64)
- length = 64;
- }
- // 显示记事本文本内容
- WriteContent(content, length);
-
- // 设置光标位置
- setLcdPos(pos);
- }
- }
- }
- /**
- * 插入字符到字符串中
- * @param str 字符串
- * @param ch 欲插入的字符
- * @param index 插入位置
- */
- void insCharToStr(unsigned char str[], unsigned char ch, unsigned int index)
- {
- unsigned int i = 0;
- unsigned char tmp1, tmp2;
- while(i < 64) {
- if (i == index) {
- tmp1 = str[i];
- str[i] = ch;
- } else if (i > index) {
- tmp2 = str[i];
- str[i] = tmp1;
- tmp1 = tmp2;
- }
- i++;
- }
- }
- /**
- * 从字符串中删除指定位置处的字符
- * @param str 字符串
- * @param index 欲删除字符的位置
- */
- void delCharFromStr(unsigned char str[], unsigned int index)
- {
- unsigned int i = 0;
- while(i < 64) {
- if (i >= index) {
- if (i == 63)
- str[i] = 0x20;
- else {
- str[i] = str[i+1];
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
英文记事本.zip
(145.44 KB, 下载次数: 28)
|