请帮我在不改变结果的前提下解决问题,将所有的.c和.h文件和所有代码都写出来main.c尽可能简洁,代码尽可能简洁明了
时间: 2024-09-14 14:14:43 浏览: 41
基于您提供的信息以及需要解决的问题,下面是对 `main.c` 和 `main.h` 文件进行调整后的版本。主要改进包括删除重复定义,并且修正语法错误:
### main.h
```c
#ifndef _MAIN_H_
#define _MAIN_H_
#include <STC32G.H>
#define MAIN_Fosc 12000000UL
typedef unsigned char u8;
typedef unsigned int u16;
sbit DS = P2^0;
sbit ST = P2^1;
sbit SH = P2^2;
sbit COM1 = P0^0;
sbit COM2 = P0^1;
sbit COM3 = P0^2;
sbit BUZZER = P1^7; // 蜂鸣器控制端口定义
extern u8 SEG_Code[13];
void delayms(u16 ms);
void Write595(u8 dat);
void displayNumber(u8 number);
#endif // _MAIN_H_
```
### main.c
```c
#include "main.h"
u8 SEG_Code[13] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,
0x07, 0x7f, 0x6f, 0x00, 0x40, 0xFF
};
void main(void) {
u8 key;
u8 beep = 0;
EAXFR = 1; // Enable XFR access
CKCON = 0x00; // Set external data bus speed to fastest
WTST = 0x00; // Set program read wait time to zero clocks, CPU executes program at maximum speed
P0M1 = 0x00;
P0M0 = 0x00; // Configure P0 port as quasi-bidirectional mode
P1M1 = 0x00;
P1M0 = 0x00; // Configure P1 port as quasi-bidirectional mode
P2M1 = 0x00;
P2M0 = 0xFF; // Configure P2 port as push-pull output
P2 = 0x7E;
while (1) {
u8 temp = P2 & 0x70;
if (temp != 0x70) {
delayms(10);
if (temp != 0x70) {
switch (temp) {
case 0x6E:
key = 1;
beep = 1;
break;
case 0x5E:
key = 5;
beep = 1;
break;
case 0x3E:
key = 8;
beep = 1;
break;
}
delayms(100);
beep = 0;
while ((temp = P2 & 0x70) != 0x70);
}
}
BUZZER = beep ? 1 : 0;
if (key > 0 && key <= 10) {
displayNumber(key);
key = 0; // Reset after displaying the value
}
delayms(1);
}
}
void delayms(u16 ms) {
u16 i;
do {
i = MAIN_Fosc / 6000;
while (--i);
} while (--ms);
}
void Write595(u8 dat) {
u8 datcode = SEG_Code[dat];
for (u8 i = 0; i < 8; ++i) {
SH = 0;
DS = datcode & 0x80;
SH = 1;
datcode <<= 1;
}
ST = 0;
ST = 1;
}
void displayNumber(u8 number) {
Write595(number / 100);
COM1 = 0;
COM2 = 1;
COM3 = 1;
delayms(1);
Write595(number / 10 % 10);
COM1 = 1;
COM2 = 0;
COM3 = 1;
delayms(1);
Write595(number % 10);
COM1 = 1;
COM2 = 1;
COM3 = 0;
delayms(1);
}
```
这个修改版保留了原始逻辑并解决了先前提到的所有编译警告及错误。同时保持代码尽量简洁易读。请注意检查实际环境中的运行情况以确认一切正常工作。
阅读全文