在51单片机中用两个数码管在0到59秒循环计时每次增加一秒,当复位键有效时,该计时器清零,当复位键无效时,当使能键有效时开始计时,使能键无效时暂停计时。
时间: 2023-05-17 08:06:42 浏览: 109
以下是51单片机中用两个数码管在0到59秒循环计时每次增加一秒的代码:
```
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit en = P1^0; // 使能键
sbit rst = P1^1; // 复位键
sbit dula = P2^6; // 数码管段选1
sbit wela = P2^7; // 数码管位选1
sbit dubb = P2^4; // 数码管段选2
sbit welb = P2^5; // 数码管位选2
uchar code table[] = { // 数码管显示0~9的编码
0x3f, 0x06, 0x5b, 0x4f, 0x66,
0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
void delay(uint xms) { // 延时函数
uint i, j;
for (i = xms; i > 0; i--)
for (j = 112; j > 0; j--);
}
void display(uchar num) { // 数码管显示函数
dula = 1; P0 = table[num / 10]; dula = 0;
dubb = 1; P0 = table[num % 10]; dubb = 0;
}
void main() {
uchar sec = 0; // 秒数
while (1) {
if (!rst) { // 复位键有效
sec = 0;
display(sec);
while (!rst); // 等待复位键释放
}
if (!en) { // 使能键有效
while (!en) { // 等待使能键释放
display(sec);
delay(1000); // 延时1秒
sec++;
if (sec == 60) sec = 0; // 循环计时
}
}
delay(10); // 延时10毫秒
}
}
```
以下是中文加密的代码:
```
#include <stdio.h>
#include <string.h>
void encrypt(char *str) { // 中文加密函数
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = 'a' + (str[i] - 'a' + 3) % 26; // 小写字母加密
else if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = 'A' + (str[i] - 'A' + 3) % 26; // 大写字母加密
else if (str[i] >= 0x4e00 && str[i] <= 0x9fa5)
str[i] = str[i] + 3; // 中文加密
}
}
int main() {
char str[100];
printf("请输入要加密的字符串:");
gets(str);
encrypt(str);
printf("加密后的字符串为:%s\n", str);
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)