系統通電后,要輸入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)
2019-1-10 20:23 上傳
點擊文件名下載附件
英文記事本 下載積分: 黑幣 -5
|