活动介绍
file-type

深入解析DataStruct-Day2: Python编程基础

ZIP文件

下载需积分: 5 | 4KB | 更新于2025-04-12 | 44 浏览量 | 0 下载量 举报 收藏
download 立即下载
在给出的文件信息中,“标题”和“描述”均为“DataStruct-Day2”,“标签”为“Python”,而“压缩包子文件的文件名称列表”为“DataStruct-Day2-main”。虽然信息量有限,但从标题和标签来看,可以推断这个文件与数据结构学习中的“Day2”相关,并且是在使用Python语言进行讲解或实践。基于这些信息,以下是一些可能涉及的知识点。 ### Python中的数据结构基础 #### 列表(List) 在Python中,列表是一种可变的序列类型,用于存储元素的有序集合。列表是数据结构中的基础概念,它支持各种操作,如增加、删除、访问和修改元素等。 #### 元组(Tuple) 与列表类似,元组也是一种序列类型,不同之处在于元组是不可变的。元组在Python中的使用场景包括作为函数的返回值、多变量赋值等。 #### 字典(Dictionary) 字典是一种无序的键值对集合。它允许快速检索、插入和删除操作。在Python中,字典是通过键来存储值的,键必须是唯一的。 #### 集合(Set) 集合是一个无序的不重复元素集。它主要用于进行成员关系测试和消除重复元素。 ### 数据结构操作和算法 #### 排序算法 包括冒泡排序、选择排序、插入排序、快速排序、归并排序等。在Python中,通常会使用内置的排序方法,如`.sort()`或`sorted()`。 #### 查找算法 涉及线性查找、二分查找等。二分查找要求数据必须是有序的,可以在对数时间复杂度内找到目标元素。 #### 栈和队列 栈是一种后进先出(LIFO)的数据结构,支持push和pop操作。队列是一种先进先出(FIFO)的数据结构,常见的有普通队列和优先队列。 #### 树 树是一种分层数据抽象,包括二叉树、二叉搜索树、平衡树等。树的遍历有前序、中序、后序和层序遍历。 ### 实践知识点 #### Python编程技巧 - 列表推导式和生成器表达式 - 函数定义与高阶函数 - 装饰器的使用 - 迭代器和可迭代对象 #### 调试和测试 - 使用`print()`函数进行调试 - 使用`pdb`进行交互式调试 - 编写和运行单元测试,使用`unittest`模块 #### 文件操作 - 打开和读写文件 - 使用`with`语句进行文件操作 ### 具体到“DataStruct-Day2”可能涵盖的内容 由于“DataStruct-Day2”是标题,我们可以假设这个文件夹或文件是一个教学资料包,其中包含了当天的课程内容、实例代码、练习题以及可能的课程笔记。 - 第二天课程可能涵盖的数据结构和算法知识点会比第一天更为深入,比如二叉树的深度优先搜索(DFS)和广度优先搜索(BFS),堆(Heap)的使用和原理,图(Graph)的表示方法和图算法等。 - Python语言的特定实践,如利用Python标准库中的`heapq`模块来实现堆的操作,使用`collections`模块中的`namedtuple`和`Counter`等工具。 - 示例代码可能包括构建复杂数据结构的类和方法、在实际问题中应用数据结构解决问题的案例,以及优化算法性能的策略。 - 练习题可能要求学生实践编程技巧,例如要求学生实现一个特定的算法,并用Python编写相应的函数或类,然后进行测试。 - 课程笔记可能会包含对于复杂概念的图形化解释、重要概念的总结,以及在实际编程中常见的问题和解决方法。 由于文件名中包含“main”,可以推测这是一个包含当天主要教学内容的文件,例如可能是主讲义或主代码文件。而“压缩包子文件的文件名称列表”中的“DataStruct-Day2-main”暗示了这个文件夹中可能包含了其他补充材料或附加文件,如相关代码的示例文件、练习题文件、补充阅读材料等。在实际的IT教学环境中,这通常意味着学生可以解压这个文件来获取所有的学习资源。

相关推荐

filetype

#include <reg52.h> #include <stdio.h> // 用于 sprintf #include <intrins.h> // 包含 _nop_() // 定义数据类型别名 typedef unsigned char uchar; typedef unsigned int uint; typedef unsigned long ulong; // DS1302引脚定义(与硬件连接一致) sbit DS1302_SCLK = P3^5; sbit DS1302_IO = P3^4; sbit DS1302_RST = P3^2; // LCD1602引脚定义 sbit LCD_RS = P2^6; sbit LCD_RW = P2^5; sbit LCD_EN = P2^7; #define LCD_DATA P0 // 按键定义 sbit KEY_SET = P1^0; // 设置键 sbit KEY_UP = P1^1; // 加键 sbit KEY_DN = P1^2; // 减键 // 日期时间结构体 struct DateTime { uchar second; uchar minute; uchar hour; uchar day; uchar month; uint year; // 直接存储完整年份(如2023) uchar week; }; // DS1302写命令(时序优化) void DS1302_WriteByte(uchar dat) { uchar i; for(i=0; i<8; i++) { DS1302_IO = dat & 0x01; DS1302_SCLK = 1; _nop_(); DS1302_SCLK = 0; dat >>= 1; } } // DS1302读数据 uchar DS1302_ReadByte() { uchar i, dat = 0; for(i=0; i<8; i++) { dat >>= 1; if(DS1302_IO) dat |= 0x80; DS1302_SCLK = 1; _nop_(); DS1302_SCLK = 0; } return dat; } // 向DS1302写入数据(带写保护控制) void DS1302_WriteData(uchar addr, uchar dat) { DS1302_RST = 0; _nop_(); DS1302_SCLK = 0; _nop_(); DS1302_RST = 1; _nop_(); DS1302_WriteByte(addr); // 写入地址 DS1302_WriteByte(dat); // 写入数据 DS1302_SCLK = 1; _nop_(); DS1302_RST = 0; _nop_(); } // 从DS1302读取数据 uchar DS1302_ReadData(uchar addr) { uchar dat; DS1302_RST = 0; _nop_(); DS1302_SCLK = 0; _nop_(); DS1302_RST = 1; _nop_(); DS1302_WriteByte(addr | 0x01); // 读命令(最低位写0改读1) dat = DS1302_ReadByte(); // 读取数据 DS1302_SCLK = 1; _nop_(); DS1302_RST = 0; _nop_(); return dat; } // LCD写命令(优化延时) void LCD_WriteCmd(uchar cmd) { LCD_RS = 0; LCD_RW = 0; LCD_DATA = cmd; LCD_EN = 1; _nop_(); _nop_(); // 缩短延时(根据实际硬件调整) LCD_EN = 0; // Delay(5); // 若显示异常可恢复延时 } // LCD写数据 void LCD_WriteData(uchar dat) { LCD_RS = 1; LCD_RW = 0; LCD_DATA = dat; LCD_EN = 1; _nop_(); _nop_(); LCD_EN = 0; // Delay(5); } // LCD初始化 void LCD_Init() { LCD_WriteCmd(0x38); // 16x2显示,5x7点阵 LCD_WriteCmd(0x0C); // 显示开,无光标 LCD_WriteCmd(0x06); // 地址递增,不移屏 LCD_WriteCmd(0x01); // 清屏 } // 在指定位置显示字符串 void LCD_Display(uchar x, uchar y, uchar *str) { uchar addr; if(y == 0) addr = 0x80 + x; else addr = 0xC0 + x; LCD_WriteCmd(addr); while(*str) { LCD_WriteData(*str++); } } // BCD转十进制 uchar BCD2DEC(uchar bcd) { return (bcd >> 4) * 10 + (bcd & 0x0F); } // 十进制转BCD uchar DEC2BCD(uchar dec) { return ((dec / 10) << 4) + (dec % 10); } // 读取当前时间(直接存储完整年份) void GetDateTime(struct DateTime *dt) { dt->second = BCD2DEC(DS1302_ReadData(0x81)); dt->minute = BCD2DEC(DS1302_ReadData(0x83)); dt->hour = BCD2DEC(DS1302_ReadData(0x85)); dt->day = BCD2DEC(DS1302_ReadData(0x87)); dt->month = BCD2DEC(DS1302_ReadData(0x89)); dt->year = BCD2DEC(DS1302_ReadData(0x8D)) + 2000; // 年份修正 } // 蔡勒公式计算星期(优化为uint参数) uchar CalculateWeek(struct DateTime dt) { uint year = dt.year; uchar month = dt.month, day = dt.day; // 处理1、2月为前一年的13、14月 if(month <= 2) { month += 12; year--; } // 蔡勒公式计算(结果0-6对应星期日-星期六) return (day + 1 + 2*month + 3*(month+1)/5 + year + year/4 - year/100 + year/400) % 7; } // 显示日期时间(优化格式) void DisplayDateTime(struct DateTime dt) { uchar buffer[16]; static uchar* weekdays[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; // 显示时间:HH:MM:SS sprintf(buffer, "%02d:%02d:%02d", dt.hour, dt.minute, dt.second); LCD_Display(0, 0, buffer); // 显示日期:YYYY-MM-DD 星期 sprintf(buffer, "%04d-%02d-%02d %s", dt.year, dt.month, dt.day, weekdays[dt.week]); LCD_Display(0, 1, buffer); } // 软件延时(@12MHz) void Delay(uint ms) { uint i, j; for(i=ms; i>0; i--) for(j=110; j>0; j--); } // 时间设置模式 void TimeSetting(struct DateTime *dt) { uchar setStep = 0; // 0:退出,1-5:设置项(年/月/日/时/分) // 等待设置键释放 while(!KEY_SET) Delay(10); while(1) { DisplayDateTime(*dt); // 实时显示设置中的时间 // 检测设置键:切换设置项 if(!KEY_SET) { Delay(10); // 消抖 if(!KEY_SET) { while(!KEY_SET) Delay(10); // 等待释放 setStep = (setStep >=5) ? 0 : setStep + 1; // 0-5循环 } } // 检测加键 if(!KEY_UP && setStep != 0) { Delay(10); if(!KEY_UP) { while(!KEY_UP) Delay(10); switch(setStep) { case 1: dt->year++; break; // 年份递增 case 2: dt->month = (dt->month%12)+1; break; // 月份1-12 case 3: dt->day = (dt->day%31)+1; break; // 日期1-31(需后续完善大小月) case 4: dt->hour = (dt->hour+1)%24; break; case 5: dt->minute = (dt->minute+1)%60; break; } } } // 检测减键 if(!KEY_DN && setStep != 0) { Delay(10); if(!KEY_DN) { while(!KEY_DN) Delay(10); switch(setStep) { case 1: dt->year--; break; // 年份递减 case 2: dt->month = (dt->month==1)?12:dt->month-1; break; case 3: dt->day = (dt->day==1)?31:dt->day-1; break; // 简化处理,未考虑实际天数 case 4: dt->hour = (dt->hour==0)?23:dt->hour-1; break; case 5: dt->minute = (dt->minute==0)?59:dt->minute-1; break; } } } // 退出设置模式 if(setStep == 0) break; } } // DS1302初始化(含时间校准) void DS1302_Init() { uchar status; // 关闭写保护 DS1302_WriteData(0x8E, 0x00); // 检测时钟是否停止(秒寄存器最高位为1表示停止) status = DS1302_ReadData(0x81); if(status & 0x80) { // 启动时钟(秒寄存器写0) DS1302_WriteData(0x80, 0x00); } // 打开写保护 DS1302_WriteData(0x8E, 0x80); } void main() { struct DateTime currentTime = { .year=2023, .month=10, .day=1, .hour=12, .minute=0, .second=0, .week=0 }; LCD_Init(); // 初始化LCD DS1302_Init(); // 初始化DS1302 // 写入初始时间(需关闭写保护) DS1302_WriteData(0x8E, 0x00); // 关闭写保护 DS1302_WriteData(0x8C, DEC2BCD(currentTime.year - 2000)); // 存储年份后两位 DS1302_WriteData(0x88, DEC2BCD(currentTime.month)); DS1302_WriteData(0x86, DEC2BCD(currentTime.day)); DS1302_WriteData(0x84, DEC2BCD(currentTime.hour)); DS1302_WriteData(0x82, DEC2BCD(currentTime.minute)); DS1302_WriteData(0x80, DEC2BCD(currentTime.second)); DS1302_WriteData(0x8E, 0x80); // 打开写保护 while(1) { GetDateTime(¤tTime); // 读取时间 currentTime.week = CalculateWeek(currentTime); // 计算星期 DisplayDateTime(currentTime); // 刷新显示 // 检测设置按键 if(!KEY_SET) { Delay(10); // 消抖 if(!KEY_SET) { TimeSetting(¤tTime); // 进入设置模式 // 保存设置到DS1302 DS1302_WriteData(0x8E, 0x00); DS1302_WriteData(0x8C, DEC2BCD(currentTime.year - 2000)); DS1302_WriteData(0x88, DEC2BCD(currentTime.month)); DS1302_WriteData(0x86, DEC2BCD(currentTime.day)); DS1302_WriteData(0x84, DEC2BCD(currentTime.hour)); DS1302_WriteData(0x82, DEC2BCD(currentTime.minute)); DS1302_WriteData(0x8E, 0x80); } } Delay(200); // 主循环延时 } } (将其代码修改为用keil μVision 4 编译器和上图普中51实验板设置的万年历中运行的)

filetype

D:\C\code\day2\顺序表.c In function 'InitList': 20 3 D:\C\code\day2\顺序表.c [Error] conversion to non-scalar type requested D:\C\code\day2\顺序表.c In function 'CreatList': 29 9 D:\C\code\day2\顺序表.c [Error] invalid operands to binary & (have 'char (*)[20]' and 'double') D:\C\code\day2\顺序表.c In function 'pirntList': 36 13 D:\C\code\day2\顺序表.c [Error] 'n' undeclared (first use in this function) 36 13 D:\C\code\day2\顺序表.c [Note] each undeclared identifier is reported only once for each function it appears in 40 9 D:\C\code\day2\顺序表.c [Error] invalid operands to binary & (have 'char (*)[20]' and 'double') D:\C\code\day2\顺序表.c In function 'insertData': 53 3 D:\C\code\day2\顺序表.c [Warning] 'return' with a value, in function returning void 58 3 D:\C\code\day2\顺序表.c [Warning] 'return' with a value, in function returning void 64 3 D:\C\code\day2\顺序表.c [Warning] 'return' with a value, in function returning void 66 6 D:\C\code\day2\顺序表.c [Error] 'j' undeclared (first use in this function) 71 2 D:\C\code\day2\顺序表.c [Warning] 'return' with a value, in function returning void D:\C\code\day2\顺序表.c In function 'DelList': 76 3 D:\C\code\day2\顺序表.c [Error] stray '\243' in program 76 3 D:\C\code\day2\顺序表.c [Error] stray '\273' in program 77 2 D:\C\code\day2\顺序表.c [Error] expected ';' before 'scanf' 87 4 D:\C\code\day2\顺序表.c [Error] 'x' undeclared (first use in this function) 88 23 D:\C\code\day2\顺序表.c [Error] request for member 'data' in something not a structure or union 88 39 D:\C\code\day2\顺序表.c [Error] request for member 'name' in something not a structure or union 89 11 D:\C\code\day2\顺序表.c [Error] request for member 'price' in something not a structure or union D:\C\code\day2\顺序表.c In function 'GetDataBySite': 106 4 D:\C\code\day2\顺序表.c [Error] 'x' undeclared (first use in this function) D:\C\code\day2\顺序表.c In function 'GetDataByNumber': 118 6 D:\C\code\day2\顺序表.c [Error] incompatible type for argument 1 of 'strcmp' 4 0 D:\C\code\day2\顺序表.c In file included from D:\C\code\day2\顺序表.c 53 15 D:\代码\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note] expected 'const char *' but argument is of type 'double' 118 6 D:\C\code\day2\顺序表.c [Error] incompatible type for argument 2 of 'strcmp' 4 0 D:\C\code\day2\顺序表.c In file included from D:\C\code\day2\顺序表.c 53 15 D:\代码\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note] expected 'const char *' but argument is of type 'double' D:\C\code\day2\顺序表.c In function 'main': 142 20 D:\C\code\day2\顺序表.c [Error] 'x' undeclared (first use in this function) D:\C\code\day2\顺序表.c In function 'pirntList': 154 1 D:\C\code\day2\顺序表.c [Error] expected declaration or statement at end of input

filetype

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define CONTENT_LENGTH 100 typedef struct event{ int year; int month; int day; int hour; int minute; char content[CONTENT_LENGTH]; struct event *next; }Event; //初始化链表 Event* init(){ Event *eHead = (Event*)malloc(sizeof(Event)); eHead->next=NULL; if(eHead!=NULL){ return eHead; } else{ printf("空间分配失败!"); exit(1); } } //打印事件具体时间 void printInfo(Event *Ev){ printf("日期: %04d/%02d/%02d 时间: %02d:%02d 事件:%s\n",Ev->year,Ev->month,Ev->day,Ev->hour,Ev->minute,Ev->content); } //输入新的事件 总是从尾端插入 Event* insert(Event *eHead){ for(;eHead->next!=NULL;eHead=eHead->next); Event *test=(Event*)malloc(sizeof(Event)); test->next=NULL; printf("请输入日期:yyyy/mm/dd(例如:2025/04/23)\n"); scanf("%d %*c %d %*c %d",&test->year,&test->month,&test->day); printf("请输入具体时间:hh:MM(例如:04:23)\n"); scanf("%d %*c %d",&test->hour,&test->minute); fflush(stdin); printf("请输入事件内容(最长为50个字符):\n"); gets(test->content); printf("事件已成功保存!\n"); eHead->next=test; } //比较两事件的时间先后 int compare(Event *a,Event *b){ //将Int格式化为str,直接比较字符串的大小 char str_a[100]={0}; sprintf(str_a,"%d04%02d%02d%02d%02d",a->year,a->month,a->day,a->hour,a->minute); char str_b[100]={0}; sprintf(str_b,"%d04%02d%02d%02d%02d",b->year,b->month,b->day,b->hour,b->minute); int n=strcmp(str_a,str_b); if(n>0){ return 1; } else{ return 0; } } //排序 Event* sort(Event *L){ Event *p, *q, *next; int temp; for( p = L->next; p->next != NULL; p = p->next ) /*每次循环都找出一个最小值,将最小值交换到第一位,然后将指针向后移动一位*/ { next = p; for( q = p->next; q; q = q->next ) /*由前向后遍历,找出最小的节点*/ { if( compare(next,q)==1 ) next = q; } if( next != p ) { temp = p->year; p->year = next->year; next->year = temp; temp = p->month; p->month = next->month; next->month = temp; temp = p->day; p->day = next->day; next->day = temp; temp = p->hour; p->hour = next->hour; next->hour = temp; temp = p->minute; p->minute = next->minute; next->minute = temp; char tem[CONTENT_LENGTH]; memcpy(tem,p->content,100); memcpy(p->content,next->content,100); memcpy(next->content,tem,100); } } printf("排序成功!\n"); return L; } // 通过具体日期查找事件 void findByDate(Event *head) { int year, month, day; printf("请输入日期:yyyy/mm/dd(例如:2025/04/23)\n"); if (scanf("%d/%d/%d", &year, &month, &day) != 3) { printf("日期格式错误!\n"); while (getchar() != '\n'); // 清空缓冲区 return; } printf("查找中...\n"); Event *current = head; // 假设链表无头节点,否则改为 head->next int found = 0; for (; current != NULL; current = current->next) { if (current->year == year && current->month == month && current->day == day) { printf("已找到:\n"); printInfo(current); found = 1; break; } } if (!found) { printf("未找到该事件\n"); } } //通过事件内容查找事件 void findByEventContent(Event *head) { char temp[CONTENT_LENGTH]; printf("请输入要查找的事件内容: "); fgets(temp, sizeof(temp), stdin); temp[strcspn(temp, "\n")] = '\0'; // 去除换行符 printf("正在查找...n"); Event *current = head; int found = 0; while(current != NULL) { if(strcmp(temp, current->content) == 0) { printf("找到匹配事件:n"); printInfo(current); found = 1; break; } current = current->next; } if(!found) { printf("未找到匹配事件n"); } } //通过日期删除事件 void deleteByDate(Event *head){ Event *before,*cur; int year; int month; int day; int hour; int minute; printf("请输入日期:yyyy/mm/dd(例如:2025/04/23)\n"); scanf("%d %*c %d %*c %d",&year,&month,&day); printf("请输入具体时间:hh:MM(例如:04:23)\n"); scanf("%d %*c %d ",&hour,&minute); for(before=head,cur=head->next;cur!=NULL;cur=cur->next,before=before->next){ if(cur->day==day && cur->hour==hour && cur->minute==minute && cur->month==month && cur->year==year){ before->next=cur->next; printf("删除成功\n"); break; } } if(cur==NULL){ printf("未找到该事件\n"); } } //通过事件内容删除事件 void deleteByContent(Event *head){ Event *before,*cur; char temp[CONTENT_LENGTH]; printf("请输入事件内容:\n"); gets(temp); for(before=head,cur=head->next;cur!=NULL;before=before->next,cur=cur->next){ if(strcmp(temp,cur->content)==0){ before->next=cur->next; printf("删除成功\n"); break; } } if(cur==NULL){ printf("未找到该事件\n"); } } //修改事件时间 void modifyDate(Event *head) { Event *current = head; char temp[CONTENT_LENGTH]; printf("请输入要查找的事件内容:\n"); fgets(temp, sizeof(temp), stdin); temp[strcspn(temp, "\n")] = '\0'; // 去除换行符 if(!current) { printf("链表为空!\n"); return; } // 正确遍历逻辑 while(current) { if(strcmp(temp, current->content) == 0) { printf("找到事件:\n"); printInfo(current); break; } current = current->next; } if(!current) { printf("未找到事件!\n"); return; } // 安全输入处理 int year, month, day, hour, minute; printf("请输入新日期(yyyy/mm/dd): "); while(scanf("%d/%d/%d", &year, &month, &day) != 3) { printf("格式错误,请重新输入: "); while(getchar() != '\n'); // 清理缓冲区 } printf("请输入新时间(hh:mm): "); while(scanf("%d:%d", &hour, &minute) != 2) { printf("格式错误,请重新输入: "); while(getchar() != '\n'); } // 更新数据 current->year = year; current->month = month; current->day = day; current->hour = hour; current->minute = minute; printf("修改成功!\n"); printInfo(current); // 显示修改结果 } //遍历链表 void travel(Event *head){ head=head->next; while(head!=NULL){ printInfo(head); head=head->next; } } //查找的子菜单,分为 1 遍历 2时间查找 3 事件查找 void find(Event *head){ char decide = 'y'; //定义while变量,函数是否继续进行 int num; //定义switch变量,函数跳转到哪个子函数 while (decide != 'n') { printf(" ***************************************************\n"); printf(" **** 1 遍历 2 时间查找 3 事件查找 4 退出 ****\n"); printf(" ***************************************************\n"); scanf("%d", &num); fflush(stdin); switch (num) { case 1: travel(head); break; case 2: findByDate(head); break; case 3: findByEventContent(head); break; default: decide = 'n'; break; } } } //删除事件的子菜单,分为 1 时间查找删除 2事件查找删除 void eventDelete(Event *head){ char decide = 'y'; //定义while变量,函数是否继续进行 int num; //定义switch变量,函数跳转到哪个子函数 while (decide != 'n') { printf(" ***************************************************\n"); printf(" **** 1 时间查找 2 事件查找 3 退出 ****\n"); printf(" ***************************************************\n"); scanf("%d", &num); fflush(stdin); switch (num) { case 1: deleteByDate(head); break; case 2: deleteByContent(head); break; default: decide = 'n'; break; } } } //储存链表信息 void dataSave(Event *head) //系统的存储函数,由主函数调用 { FILE *fp; char filename[10]; printf("请输入存储文件名(包括物理地址)\n"); scanf("%s",filename); Event *p = (Event *)malloc(sizeof(Event)); p = head->next; if ( (fp=fopen(filename, "w"))== NULL) { printf("cannot open file"); return; } printf("input data:\n"); while (p != NULL) { fwrite(p, sizeof(Event), 1, fp); /* 成块写入文件*/ p = p->next; } fclose(fp); } // 提醒有无即将需要完成的事件 void alert(Event *head){ time_t t; struct tm * lt; time (&t);//获取Unix时间戳。 lt = localtime (&t);//转为时间结构。 printf("当前时间为:"); printf ( "%d/%d/%d %d:%d\n",lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday, lt->tm_hour, lt->tm_min);//输出结果 int flag=0; for(head=head->next;head!=NULL;head=head->next){ if(head->day==lt->tm_mday && head->hour==lt->tm_hour && head->month==lt->tm_mon+1 && head->year==lt->tm_year+1900){ printf("你现在该完成 %s \n",head->content); flag=1; } } if(flag==0){ printf("你现在没有要完成的事件\n"); } } //菜单列表 void menu(){ char decide = 'y'; //定义while变量,函数是否继续进行 int num = 1; //定义switch变量,函数跳转到哪个子函数 Event *head; //定义链表的头指针 head = init(); //给头指针开辟空间 head->next = NULL; //初始化头指针 while (decide != 'n') { printf(" ***************************************************\n"); printf(" ********** 日程管理系统 ********\n"); printf(" ***************************************************\n"); printf(" ********** 1 输入 2 查找 3 修改 4 提醒 ********\n"); printf(" ********** 5 删除 6 存储 7 排序 8 退出 ********\n"); printf(" ***************************************************\n"); scanf("%d", &num); fflush(stdin); switch (num) { case 1: insert(head); break; case 2: find(head); break; case 3: modifyDate(head); break; case 4: alert(head); break; case 5: eventDelete(head); break; case 6: dataSave(head); break; case 7: head=sort(head); break; default: decide = 'n'; break; } }; } //主程序画面函数 int main(){ menu(); return 0; } 根据这个代码画处事件管理模块的流程图,时间排序模块的流程图,提醒模块的流程图,文件存储模块的流程图.

filetype
filetype

程序1: #include "RTC.h" #define RTC_CLOCK_SOURCE_LXTAL // ÅäÖÃRTCʱÖÓÔ´ #define BKP_VALUE 0x32F0 rtc_parameter_struct rtc_initpara; rtc_alarm_struct rtc_alarm; __IO uint32_t prescaler_a = 0, prescaler_s = 0; uint32_t RTCSRC_FLAG = 0; /*! \brief main function \param[in] none \param[out] none \retval none */ void RTC_Init(void) { printf("\n\r ****************** RTC calendar demo ******************\n\r"); /* enable PMU clock */ rcu_periph_clock_enable(RCU_PMU); /* enable the access of the RTC registers */ pmu_backup_write_enable(); rtc_pre_config(); /* get RTC clock entry selection */ RTCSRC_FLAG = GET_BITS(RCU_BDCTL, 8, 9); /* check if RTC has aready been configured */ if((BKP_VALUE != RTC_BKP0) || (0x00 == RTCSRC_FLAG)){ /* backup data register value is not correct or not yet programmed or RTC clock source is not configured (when the first time the program is executed or data in RCU_BDCTL is lost due to Vbat feeding) */ rtc_setup(); }else{ /* detect the reset source */ if (RESET != rcu_flag_get(RCU_FLAG_PORRST)){ printf("power on reset occurred....\n\r"); }else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)){ printf("external reset occurred....\n\r"); } printf("no need to configure RTC....\n\r"); rtc_show_time(); } rcu_all_reset_flag_clear(); } /*! \brief RTC configuration function \param[in] none \param[out] none \retval none */ void rtc_pre_config(void) { #if defined (RTC_CLOCK_SOURCE_IRC32K) rcu_osci_on(RCU_IRC32K); rcu_osci_stab_wait(RCU_IRC32K); rcu_rtc_clock_config(RCU_RTCSRC_IRC32K); prescaler_s = 0x13F; prescaler_a = 0x63; #elif defined (RTC_CLOCK_SOURCE_LXTAL) rcu_osci_on(RCU_LXTAL); rcu_osci_stab_wait(RCU_LXTAL); rcu_rtc_clock_config(RCU_RTCSRC_LXTAL); prescaler_s = 0xFF; prescaler_a = 0x7F; #else #error RTC clock source should be defined. #endif /* RTC_CLOCK_SOURCE_IRC32K */ rcu_periph_clock_enable(RCU_RTC); rtc_register_sync_wait(); } /*! \brief use hyperterminal to setup RTC time and alarm \param[in] none \param[out] none \retval none */ void rtc_setup(void) { /* setup RTC time value */ uint32_t tmp_year = 0xFF, tmp_month = 0xFF, tmp_day = 0xFF; uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF; rtc_initpara.factor_asyn = prescaler_a; rtc_initpara.factor_syn = prescaler_s; rtc_initpara.year = 0x16; rtc_initpara.day_of_week = RTC_SATURDAY; rtc_initpara.month = RTC_APR; rtc_initpara.date = 0x30; rtc_initpara.display_format = RTC_24HOUR; rtc_initpara.am_pm = RTC_AM; /* current time input */ printf("=======Configure RTC Time========\n\r"); printf(" please set the last two digits of current year:\n\r"); while(tmp_year == 0xFF) { tmp_year = usart_input_threshold(99); rtc_initpara.year = tmp_year; } printf(" 20%0.2x\n\r", tmp_year); printf(" please input month:\n\r"); while(tmp_month == 0xFF) { tmp_month = usart_input_threshold(12); rtc_initpara.month = tmp_month; } printf(" %0.2x\n\r", tmp_month); printf(" please input day:\n\r"); while(tmp_day == 0xFF) { tmp_day = usart_input_threshold(31); rtc_initpara.date = tmp_day; } printf(" %0.2x\n\r", tmp_day); printf(" please input hour:\n\r"); while (0xFF == tmp_hh){ tmp_hh = usart_input_threshold(23); rtc_initpara.hour = tmp_hh; } printf(" %0.2x\n\r", tmp_hh); printf(" please input minute:\n\r"); while (0xFF == tmp_mm){ tmp_mm = usart_input_threshold(59); rtc_initpara.minute = tmp_mm; } printf(" %0.2x\n\r", tmp_mm); printf(" please input second:\n\r"); while (0xFF == tmp_ss){ tmp_ss = usart_input_threshold(59); rtc_initpara.second = tmp_ss; } printf(" %0.2x\n\r", tmp_ss); /* RTC current time configuration */ if(ERROR == rtc_init(&rtc_initpara)){ printf("\n\r** RTC time configuration failed! **\n\r"); }else{ printf("\n\r** RTC time configuration success! **\n\r"); rtc_show_time(); RTC_BKP0 = BKP_VALUE; } // /* setup RTC alarm */ // tmp_hh = 0xFF; // tmp_mm = 0xFF; // tmp_ss = 0xFF; // rtc_alarm_disable(RTC_ALARM0); // printf("=======Input Alarm Value=======\n\r"); // rtc_alarm.alarm_mask = RTC_ALARM_DATE_MASK|RTC_ALARM_HOUR_MASK|RTC_ALARM_MINUTE_MASK; // rtc_alarm.weekday_or_date = RTC_ALARM_DATE_SELECTED; // rtc_alarm.alarm_day = 0x31; // rtc_alarm.am_pm = RTC_AM; // /* RTC alarm input */ // printf(" please input Alarm Hour:\n\r"); // while (0xFF == tmp_hh){ // tmp_hh = usart_input_threshold(23); // rtc_alarm.alarm_hour = tmp_hh; // } // printf(" %0.2x\n\r", tmp_hh); // printf(" Please Input Alarm Minute:\n\r"); // while (0xFF == tmp_mm){ // tmp_mm = usart_input_threshold(59); // rtc_alarm.alarm_minute = tmp_mm; // } // printf(" %0.2x\n\r", tmp_mm); // printf(" Please Input Alarm Second:\n\r"); // while (0xFF == tmp_ss){ // tmp_ss = usart_input_threshold(59); // rtc_alarm.alarm_second = tmp_ss; // } // printf(" %0.2x", tmp_ss); // /* RTC alarm configuration */ // rtc_alarm_config(RTC_ALARM0,&rtc_alarm); // printf("\n\r** RTC Set Alarm Success! **\n\r"); // rtc_show_alarm(); // rtc_interrupt_enable(RTC_INT_ALARM0); // rtc_alarm_enable(RTC_ALARM0); } /*! \brief display the current time \param[in] none \param[out] none \retval none */ void rtc_show_time(void) { // uint32_t time_subsecond = 0; // uint8_t subsecond_ss = 0,subsecond_ts = 0,subsecond_hs = 0; rtc_current_time_get(&rtc_initpara); /* get the subsecond value of current time, and convert it into fractional format */ // time_subsecond = rtc_subsecond_get(); // subsecond_ss=(1000-(time_subsecond*1000+1000)/400)/100; // subsecond_ts=(1000-(time_subsecond*1000+1000)/400)%100/10; // subsecond_hs=(1000-(time_subsecond*1000+1000)/400)%10; printf("\r\nCurrent time: 20%0.2x-%0.2x-%0.2x", \ rtc_initpara.year, rtc_initpara.month, rtc_initpara.date); printf(" : %0.2x:%0.2x:%0.2x \r\n", \ rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); } /*! \brief display the alram value \param[in] none \param[out] none \retval none */ void rtc_show_alarm(void) { rtc_alarm_get(RTC_ALARM0,&rtc_alarm); printf("The alarm: %0.2x:%0.2x:%0.2x \n\r", rtc_alarm.alarm_hour, rtc_alarm.alarm_minute,\ rtc_alarm.alarm_second); } /*! \brief get the input character string and check if it is valid \param[in] none \param[out] none \retval input value in BCD mode */ uint8_t usart_input_threshold(uint32_t value) { uint32_t index = 0; uint32_t tmp[2] = {0, 0}; while (index < 2){ while (RESET == usart_flag_get(USART0, USART_FLAG_RBNE)); tmp[index++] = usart_data_receive(USART0); if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){ printf("\n\r please input a valid number between 0 and 9 \n\r"); index--; } } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10); if (index > value){ printf("\n\r please input a valid number between 0 and %d \n\r", value); return 0xFF; } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) <<4); return index; } 程序2: /************************************************************ * °æÈ¨£º2025CIMC Copyright¡£ * Îļþ£ºRTC.c * ×÷Õß: Qiao Qin @ GigaDevice * ƽ̨: 2025CIMC IHD-V04 * °æ±¾: Qiao Qin 2025/4/20 V0.01 original ************************************************************/ #include "RTC.h" #define RTC_CLOCK_SOURCE_LXTAL // ÅäÖÃRTCʱÖÓÔ´ #define BKP_VALUE 0x32F0 rtc_parameter_struct rtc_initpara; rtc_alarm_struct rtc_alarm; __IO uint32_t prescaler_a = 0, prescaler_s = 0; uint32_t RTCSRC_FLAG = 0; /*! \brief main function \param[in] none \param[out] none \retval none */ void RTC_Init(void) { printf("\n\r ****************** RTC calendar demo ******************\n\r"); /* enable PMU clock */ rcu_periph_clock_enable(RCU_PMU); /* enable the access of the RTC registers */ pmu_backup_write_enable(); rtc_pre_config(); printf("Configuring RTC on every initialization...\n\r"); char cmd_buffer[32] = "2000-01-01 00:02:51"; rtc_settime((const char*)cmd_buffer); // ????RTC?? if (RESET != rcu_flag_get(RCU_FLAG_PORRST)) { printf("power on reset occurred....\n\r"); } else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)) { printf("external reset occurred....\n\r"); } rtc_show_time(); // ?????? rcu_all_reset_flag_clear(); } /*! \brief RTC configuration function \param[in] none \param[out] none \retval none */ void rtc_pre_config(void) { #if defined (RTC_CLOCK_SOURCE_IRC32K) rcu_osci_on(RCU_IRC32K); rcu_osci_stab_wait(RCU_IRC32K); rcu_rtc_clock_config(RCU_RTCSRC_IRC32K); prescaler_s = 0x13F; prescaler_a = 0x63; #elif defined (RTC_CLOCK_SOURCE_LXTAL) rcu_osci_on(RCU_LXTAL); rcu_osci_stab_wait(RCU_LXTAL); rcu_rtc_clock_config(RCU_RTCSRC_LXTAL); prescaler_s = 0xFF; prescaler_a = 0x7F; #else #error RTC clock source should be defined. #endif /* RTC_CLOCK_SOURCE_IRC32K */ rcu_periph_clock_enable(RCU_RTC); rtc_register_sync_wait(); } /*! \brief use hyperterminal to setup RTC time and alarm \param[in] none \param[out] none \retval none */ void rtc_settime(const char *datetime) { /* setup RTC time value */ uint32_t tmp_year = 0, tmp_month = 0, tmp_day = 0; uint32_t tmp_hh = 0, tmp_mm = 0, tmp_ss = 0; if (sscanf(datetime, "%4u-%2u-%2u %2u:%2u:%2u", &tmp_year, &tmp_month, &tmp_day, &tmp_hh, &tmp_mm, &tmp_ss) != 6) { printf(" ERROR: Invalid time format!\n\r"); return; } rtc_initpara.factor_asyn = prescaler_a; rtc_initpara.factor_syn = prescaler_s; rtc_initpara.year = 0x16; rtc_initpara.day_of_week = RTC_SATURDAY; rtc_initpara.month = RTC_APR; rtc_initpara.date = 0x30; rtc_initpara.display_format = RTC_24HOUR; rtc_initpara.am_pm = RTC_AM; // Äê·Ý(2000-2099) if (tmp_year < 2000 || tmp_year > 2099) { printf(" ERROR: Year out of range [2000-2099]!\n\r"); return; } rtc_initpara.year = tmp_year % 100; // ???? // ÔÂ·Ý (1-12) if (tmp_month < 1 || tmp_month > 12) { printf(" ERROR: Month out of range [1-12]!\n\r"); return; } rtc_initpara.month = tmp_month; // ÈÕÆÚ (1-31) if (tmp_day < 1 || tmp_day > 31) { printf(" ERROR: Day out of range [1-31]!\n\r"); return; } rtc_initpara.date = tmp_day; // Сʱ(0-23) if (tmp_hh > 23) { printf(" ERROR: Hour out of range [0-23]!\n\r"); return; } rtc_initpara.hour = tmp_hh; // ·ÖÖÓ(0-59) if (tmp_mm > 59) { printf(" ERROR: Minute out of range [0-59]!\n\r"); return; } rtc_initpara.minute = tmp_mm; // mÃëÖÓ(0-59) if (tmp_ss > 59) { printf(" ERROR: Second out of range [0-59]!\n\r"); return; } rtc_initpara.second = tmp_ss; printf("\n\rRTC Config success\n\r"); printf("Time: 20%02u-%02u-%02u %02u:%02u:%02u\n\r", rtc_initpara.year, rtc_initpara.month, rtc_initpara.date, rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); /*RTC³õʼ»¯*/ if (ERROR == rtc_init(&rtc_initpara)) { // printf("\n\r** RTC configuration failed! **\n\r"); } else { // printf("\n\r** RTC configuration success! **\n\r"); RTC_BKP0 = BKP_VALUE; } } /*! \brief display the current time \param[in] none \param[out] none \retval none */ void rtc_show_time(void) { rtc_current_time_get(&rtc_initpara); printf("\r\nCurrent Time: 20%02u-%02u-%02u %02u:%02u:%02u\n\r", rtc_initpara.year, rtc_initpara.month, rtc_initpara.date, rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); } /*! \brief display the alram value \param[in] none \param[out] none \retval none */ void rtc_show_alarm(void) { rtc_alarm_get(RTC_ALARM0,&rtc_alarm); printf("The alarm: %0.2x:%0.2x:%0.2x \n\r", rtc_alarm.alarm_hour, rtc_alarm.alarm_minute,\ rtc_alarm.alarm_second); } /*! \brief get the input character string and check if it is valid \param[in] none \param[out] none \retval input value in BCD mode */ uint8_t usart_input_threshold(uint32_t value) { uint32_t index = 0; uint32_t tmp[2] = {0, 0}; while (index < 2){ while (RESET == usart_flag_get(USART0, USART_FLAG_RBNE)); tmp[index++] = usart_data_receive(USART0); if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){ printf("\n\r please input a valid number between 0 and 9 \n\r"); index--; } } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10); if (index > value){ printf("\n\r please input a valid number between 0 and %d \n\r", value); return 0xFF; } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) <<4); return index; } 程序1可以正常记时,程序二时间会有跳变,请改错 程序1: #include "RTC.h" #define RTC_CLOCK_SOURCE_LXTAL // ÅäÖÃRTCʱÖÓÔ´ #define BKP_VALUE 0x32F0 rtc_parameter_struct rtc_initpara; rtc_alarm_struct rtc_alarm; __IO uint32_t prescaler_a = 0, prescaler_s = 0; uint32_t RTCSRC_FLAG = 0; /*! \brief main function \param[in] none \param[out] none \retval none */ void RTC_Init(void) { printf("\n\r ****************** RTC calendar demo ******************\n\r"); /* enable PMU clock */ rcu_periph_clock_enable(RCU_PMU); /* enable the access of the RTC registers */ pmu_backup_write_enable(); rtc_pre_config(); /* get RTC clock entry selection */ RTCSRC_FLAG = GET_BITS(RCU_BDCTL, 8, 9); /* check if RTC has aready been configured */ if((BKP_VALUE != RTC_BKP0) || (0x00 == RTCSRC_FLAG)){ /* backup data register value is not correct or not yet programmed or RTC clock source is not configured (when the first time the program is executed or data in RCU_BDCTL is lost due to Vbat feeding) */ rtc_setup(); }else{ /* detect the reset source */ if (RESET != rcu_flag_get(RCU_FLAG_PORRST)){ printf("power on reset occurred....\n\r"); }else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)){ printf("external reset occurred....\n\r"); } printf("no need to configure RTC....\n\r"); rtc_show_time(); } rcu_all_reset_flag_clear(); } /*! \brief RTC configuration function \param[in] none \param[out] none \retval none */ void rtc_pre_config(void) { #if defined (RTC_CLOCK_SOURCE_IRC32K) rcu_osci_on(RCU_IRC32K); rcu_osci_stab_wait(RCU_IRC32K); rcu_rtc_clock_config(RCU_RTCSRC_IRC32K); prescaler_s = 0x13F; prescaler_a = 0x63; #elif defined (RTC_CLOCK_SOURCE_LXTAL) rcu_osci_on(RCU_LXTAL); rcu_osci_stab_wait(RCU_LXTAL); rcu_rtc_clock_config(RCU_RTCSRC_LXTAL); prescaler_s = 0xFF; prescaler_a = 0x7F; #else #error RTC clock source should be defined. #endif /* RTC_CLOCK_SOURCE_IRC32K */ rcu_periph_clock_enable(RCU_RTC); rtc_register_sync_wait(); } /*! \brief use hyperterminal to setup RTC time and alarm \param[in] none \param[out] none \retval none */ void rtc_setup(void) { /* setup RTC time value */ uint32_t tmp_year = 0xFF, tmp_month = 0xFF, tmp_day = 0xFF; uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF; rtc_initpara.factor_asyn = prescaler_a; rtc_initpara.factor_syn = prescaler_s; rtc_initpara.year = 0x16; rtc_initpara.day_of_week = RTC_SATURDAY; rtc_initpara.month = RTC_APR; rtc_initpara.date = 0x30; rtc_initpara.display_format = RTC_24HOUR; rtc_initpara.am_pm = RTC_AM; /* current time input */ printf("=======Configure RTC Time========\n\r"); printf(" please set the last two digits of current year:\n\r"); while(tmp_year == 0xFF) { tmp_year = usart_input_threshold(99); rtc_initpara.year = tmp_year; } printf(" 20%0.2x\n\r", tmp_year); printf(" please input month:\n\r"); while(tmp_month == 0xFF) { tmp_month = usart_input_threshold(12); rtc_initpara.month = tmp_month; } printf(" %0.2x\n\r", tmp_month); printf(" please input day:\n\r"); while(tmp_day == 0xFF) { tmp_day = usart_input_threshold(31); rtc_initpara.date = tmp_day; } printf(" %0.2x\n\r", tmp_day); printf(" please input hour:\n\r"); while (0xFF == tmp_hh){ tmp_hh = usart_input_threshold(23); rtc_initpara.hour = tmp_hh; } printf(" %0.2x\n\r", tmp_hh); printf(" please input minute:\n\r"); while (0xFF == tmp_mm){ tmp_mm = usart_input_threshold(59); rtc_initpara.minute = tmp_mm; } printf(" %0.2x\n\r", tmp_mm); printf(" please input second:\n\r"); while (0xFF == tmp_ss){ tmp_ss = usart_input_threshold(59); rtc_initpara.second = tmp_ss; } printf(" %0.2x\n\r", tmp_ss); /* RTC current time configuration */ if(ERROR == rtc_init(&rtc_initpara)){ printf("\n\r** RTC time configuration failed! **\n\r"); }else{ printf("\n\r** RTC time configuration success! **\n\r"); rtc_show_time(); RTC_BKP0 = BKP_VALUE; } // /* setup RTC alarm */ // tmp_hh = 0xFF; // tmp_mm = 0xFF; // tmp_ss = 0xFF; // rtc_alarm_disable(RTC_ALARM0); // printf("=======Input Alarm Value=======\n\r"); // rtc_alarm.alarm_mask = RTC_ALARM_DATE_MASK|RTC_ALARM_HOUR_MASK|RTC_ALARM_MINUTE_MASK; // rtc_alarm.weekday_or_date = RTC_ALARM_DATE_SELECTED; // rtc_alarm.alarm_day = 0x31; // rtc_alarm.am_pm = RTC_AM; // /* RTC alarm input */ // printf(" please input Alarm Hour:\n\r"); // while (0xFF == tmp_hh){ // tmp_hh = usart_input_threshold(23); // rtc_alarm.alarm_hour = tmp_hh; // } // printf(" %0.2x\n\r", tmp_hh); // printf(" Please Input Alarm Minute:\n\r"); // while (0xFF == tmp_mm){ // tmp_mm = usart_input_threshold(59); // rtc_alarm.alarm_minute = tmp_mm; // } // printf(" %0.2x\n\r", tmp_mm); // printf(" Please Input Alarm Second:\n\r"); // while (0xFF == tmp_ss){ // tmp_ss = usart_input_threshold(59); // rtc_alarm.alarm_second = tmp_ss; // } // printf(" %0.2x", tmp_ss); // /* RTC alarm configuration */ // rtc_alarm_config(RTC_ALARM0,&rtc_alarm); // printf("\n\r** RTC Set Alarm Success! **\n\r"); // rtc_show_alarm(); // rtc_interrupt_enable(RTC_INT_ALARM0); // rtc_alarm_enable(RTC_ALARM0); } /*! \brief display the current time \param[in] none \param[out] none \retval none */ void rtc_show_time(void) { // uint32_t time_subsecond = 0; // uint8_t subsecond_ss = 0,subsecond_ts = 0,subsecond_hs = 0; rtc_current_time_get(&rtc_initpara); /* get the subsecond value of current time, and convert it into fractional format */ // time_subsecond = rtc_subsecond_get(); // subsecond_ss=(1000-(time_subsecond*1000+1000)/400)/100; // subsecond_ts=(1000-(time_subsecond*1000+1000)/400)%100/10; // subsecond_hs=(1000-(time_subsecond*1000+1000)/400)%10; printf("\r\nCurrent time: 20%0.2x-%0.2x-%0.2x", \ rtc_initpara.year, rtc_initpara.month, rtc_initpara.date); printf(" : %0.2x:%0.2x:%0.2x \r\n", \ rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); } /*! \brief display the alram value \param[in] none \param[out] none \retval none */ void rtc_show_alarm(void) { rtc_alarm_get(RTC_ALARM0,&rtc_alarm); printf("The alarm: %0.2x:%0.2x:%0.2x \n\r", rtc_alarm.alarm_hour, rtc_alarm.alarm_minute,\ rtc_alarm.alarm_second); } /*! \brief get the input character string and check if it is valid \param[in] none \param[out] none \retval input value in BCD mode */ uint8_t usart_input_threshold(uint32_t value) { uint32_t index = 0; uint32_t tmp[2] = {0, 0}; while (index < 2){ while (RESET == usart_flag_get(USART0, USART_FLAG_RBNE)); tmp[index++] = usart_data_receive(USART0); if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){ printf("\n\r please input a valid number between 0 and 9 \n\r"); index--; } } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10); if (index > value){ printf("\n\r please input a valid number between 0 and %d \n\r", value); return 0xFF; } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) <<4); return index; } 程序2: /************************************************************ * °æÈ¨£º2025CIMC Copyright¡£ * Îļþ£ºRTC.c * ×÷Õß: Qiao Qin @ GigaDevice * ƽ̨: 2025CIMC IHD-V04 * °æ±¾: Qiao Qin 2025/4/20 V0.01 original ************************************************************/ #include "RTC.h" #define RTC_CLOCK_SOURCE_LXTAL // ÅäÖÃRTCʱÖÓÔ´ #define BKP_VALUE 0x32F0 rtc_parameter_struct rtc_initpara; rtc_alarm_struct rtc_alarm; __IO uint32_t prescaler_a = 0, prescaler_s = 0; uint32_t RTCSRC_FLAG = 0; /*! \brief main function \param[in] none \param[out] none \retval none */ void RTC_Init(void) { printf("\n\r ****************** RTC calendar demo ******************\n\r"); /* enable PMU clock */ rcu_periph_clock_enable(RCU_PMU); /* enable the access of the RTC registers */ pmu_backup_write_enable(); rtc_pre_config(); printf("Configuring RTC on every initialization...\n\r"); char cmd_buffer[32] = "2000-01-01 00:02:51"; rtc_settime((const char*)cmd_buffer); // ????RTC?? if (RESET != rcu_flag_get(RCU_FLAG_PORRST)) { printf("power on reset occurred....\n\r"); } else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)) { printf("external reset occurred....\n\r"); } rtc_show_time(); // ?????? rcu_all_reset_flag_clear(); } /*! \brief RTC configuration function \param[in] none \param[out] none \retval none */ void rtc_pre_config(void) { #if defined (RTC_CLOCK_SOURCE_IRC32K) rcu_osci_on(RCU_IRC32K); rcu_osci_stab_wait(RCU_IRC32K); rcu_rtc_clock_config(RCU_RTCSRC_IRC32K); prescaler_s = 0x13F; prescaler_a = 0x63; #elif defined (RTC_CLOCK_SOURCE_LXTAL) rcu_osci_on(RCU_LXTAL); rcu_osci_stab_wait(RCU_LXTAL); rcu_rtc_clock_config(RCU_RTCSRC_LXTAL); prescaler_s = 0xFF; prescaler_a = 0x7F; #else #error RTC clock source should be defined. #endif /* RTC_CLOCK_SOURCE_IRC32K */ rcu_periph_clock_enable(RCU_RTC); rtc_register_sync_wait(); } /*! \brief use hyperterminal to setup RTC time and alarm \param[in] none \param[out] none \retval none */ void rtc_settime(const char *datetime) { /* setup RTC time value */ uint32_t tmp_year = 0, tmp_month = 0, tmp_day = 0; uint32_t tmp_hh = 0, tmp_mm = 0, tmp_ss = 0; if (sscanf(datetime, "%4u-%2u-%2u %2u:%2u:%2u", &tmp_year, &tmp_month, &tmp_day, &tmp_hh, &tmp_mm, &tmp_ss) != 6) { printf(" ERROR: Invalid time format!\n\r"); return; } rtc_initpara.factor_asyn = prescaler_a; rtc_initpara.factor_syn = prescaler_s; rtc_initpara.year = 0x16; rtc_initpara.day_of_week = RTC_SATURDAY; rtc_initpara.month = RTC_APR; rtc_initpara.date = 0x30; rtc_initpara.display_format = RTC_24HOUR; rtc_initpara.am_pm = RTC_AM; // Äê·Ý(2000-2099) if (tmp_year < 2000 || tmp_year > 2099) { printf(" ERROR: Year out of range [2000-2099]!\n\r"); return; } rtc_initpara.year = tmp_year % 100; // ???? // ÔÂ·Ý (1-12) if (tmp_month < 1 || tmp_month > 12) { printf(" ERROR: Month out of range [1-12]!\n\r"); return; } rtc_initpara.month = tmp_month; // ÈÕÆÚ (1-31) if (tmp_day < 1 || tmp_day > 31) { printf(" ERROR: Day out of range [1-31]!\n\r"); return; } rtc_initpara.date = tmp_day; // Сʱ(0-23) if (tmp_hh > 23) { printf(" ERROR: Hour out of range [0-23]!\n\r"); return; } rtc_initpara.hour = tmp_hh; // ·ÖÖÓ(0-59) if (tmp_mm > 59) { printf(" ERROR: Minute out of range [0-59]!\n\r"); return; } rtc_initpara.minute = tmp_mm; // mÃëÖÓ(0-59) if (tmp_ss > 59) { printf(" ERROR: Second out of range [0-59]!\n\r"); return; } rtc_initpara.second = tmp_ss; printf("\n\rRTC Config success\n\r"); printf("Time: 20%02u-%02u-%02u %02u:%02u:%02u\n\r", rtc_initpara.year, rtc_initpara.month, rtc_initpara.date, rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); /*RTC³õʼ»¯*/ if (ERROR == rtc_init(&rtc_initpara)) { // printf("\n\r** RTC configuration failed! **\n\r"); } else { // printf("\n\r** RTC configuration success! **\n\r"); RTC_BKP0 = BKP_VALUE; } } /*! \brief display the current time \param[in] none \param[out] none \retval none */ void rtc_show_time(void) { rtc_current_time_get(&rtc_initpara); printf("\r\nCurrent Time: 20%02u-%02u-%02u %02u:%02u:%02u\n\r", rtc_initpara.year, rtc_initpara.month, rtc_initpara.date, rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); } /*! \brief display the alram value \param[in] none \param[out] none \retval none */ void rtc_show_alarm(void) { rtc_alarm_get(RTC_ALARM0,&rtc_alarm); printf("The alarm: %0.2x:%0.2x:%0.2x \n\r", rtc_alarm.alarm_hour, rtc_alarm.alarm_minute,\ rtc_alarm.alarm_second); } /*! \brief get the input character string and check if it is valid \param[in] none \param[out] none \retval input value in BCD mode */ uint8_t usart_input_threshold(uint32_t value) { uint32_t index = 0; uint32_t tmp[2] = {0, 0}; while (index < 2){ while (RESET == usart_flag_get(USART0, USART_FLAG_RBNE)); tmp[index++] = usart_data_receive(USART0); if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){ printf("\n\r please input a valid number between 0 and 9 \n\r"); index--; } } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10); if (index > value){ printf("\n\r please input a valid number between 0 and %d \n\r", value); return 0xFF; } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) <<4); return index; } 程序1可以正常记时,不会出现跳变,程序二时间会有跳变,请改错程序2,我需要在程序2上改错,保留大部分不错的内容,仅改变错误的地方

蜜蜜蜜蜜糖
  • 粉丝: 24
上传资源 快速赚钱