代码运行是正确的,但是烧录到STC32G8K64单片机上却无法实现预期目标,请帮我解决这些问题,这三个数码管是通过位码发送,并将所有.c和.h文件以及所有包含的代码都写出来,不要框架,不要省略,全部写出来,相信你可以的
时间: 2024-09-18 16:08:26 浏览: 34
根据您提供的信息及要求,以下是完整的代码实现,包括所有的`.c`和`.h`文件的内容,以便于您能够将其完整地用于您的项目中:
### main.h
```c
#ifndef __MAIN_H__
#define __MAIN_H__
#include <STC32G.H>
#define MAIN_Fosc 12000000UL
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 = P5^4;
extern unsigned char key;
void delayms(unsigned int ms);
void displayNumber(unsigned char number);
unsigned char scanKey(void);
void beepOn(void);
void beepOff(void);
#endif // __MAIN_H__
```
### config.h
```c
#ifndef CONFIG_H
#define CONFIG_H
#include "main.h"
// 定义按键值
#define KEY_0 0x3E
#define KEY_1 0x5E
#define KEY_2 0x6E
#define KEY_3 0x3D
#define KEY_4 0x5D
#define KEY_5 0x6D
#define KEY_6 0x3B
#define KEY_7 0x5B
#define KEY_8 0x6B
#define KEY_9 0x37
#define KEY_10 0x57
#define KEY_11 0x67
#endif
```
### delay.c
```c
#include "config.h"
void delayms(unsigned int ms) {
unsigned int i, j;
for (i = 0; i < ms; i++) {
for (j = 0; j < 120; j++);
}
}
```
### keypad.c
```c
#include "config.h"
unsigned char scanKey(void) {
unsigned char temp;
P2 = 0x7E;
temp = P2;
temp &= 0x70;
if (temp != 0x70) {
delayms(10);
if (temp == P2) {
temp = P2;
switch (temp) {
case KEY_0: return 0;
case KEY_1: return 1;
case KEY_2: return 2;
case KEY_3: return 3;
case KEY_4: return 4;
case KEY_5: return 5;
case KEY_6: return 6;
case KEY_7: return 7;
case KEY_8: return 8;
case KEY_9: return 9;
case KEY_10: return 10;
case KEY_11: return 11;
default: return 0;
}
}
}
return 0;
}
```
### deep.c
```c
#include "config.h"
void beepOn(void) {
BUZZER = 1;
}
void beepOff(void) {
BUZZER = 0;
}
```
### display.c
```c
#include "config.h"
unsigned char code SEG_Code[16] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x02, 0x5F, 0x7C, 0x7F};
void displayNumber(unsigned char num) {
if (num < 16) {
// 根据传入的参数选择不同的COM口来激活对应的数码管
switch(num / 10) {
case 0:
COM1 = 1;
COM2 = 0;
COM3 = 0;
break;
case 1:
COM1 = 0;
COM2 = 1;
COM3 = 0;
break;
case 2:
COM1 = 0;
COM2 = 0;
COM3 = 1;
break;
default:
COM1 = 0;
COM2 = 0;
COM3 = 0;
break;
}
P2 = SEG_Code[num % 10];
} else {
P2 = 0x00;
}
}
```
### main.c
```c
#include "config.h"
unsigned char key = 0;
void main(void) {
P2 = 0x7E;
while(1) {
key = scanKey();
if (key) {
beepOn();
delayms(100);
beepOff();
displayNumber(key);
}
delayms(10);
}
}
```
以上就是整个项目的代码,希望可以帮助您解决问题。如果您在烧录或调试过程中遇到任何困难,请检查是否正确设置了编译环境并选择了合适的开发板型号。同时确认硬件连接没有错误,特别是有关电源和接地的部分。
阅读全文