Compare commits

..

10 Commits

Author SHA1 Message Date
msksbr 32de81b3f5 archieve: homework5 2025-04-24 18:44:28 +08:00
msksbr a0e6c0945a archieve: homework5 test3 2025-04-24 17:53:48 +08:00
msksbr cd667a286e archieve: homework5 test2 2025-04-24 17:31:02 +08:00
msksbr acd7bb9ec9 fix: homework5 test1 digital display error 2025-04-23 23:13:33 +08:00
msksbr 10051c6a18 archieve: homework5 test1 2025-04-23 14:15:34 +08:00
msksbr 0e7cdd870b archieve: homework5 header: basic functions 2025-04-23 10:45:36 +08:00
msksbr 55d4cdb6b2 reinit: homework5 2025-04-23 10:08:58 +08:00
msksbr 2922312726 remove: homework5 2025-04-23 10:00:43 +08:00
msksbr 6712f5bf30 init: homework5 2025-04-22 12:27:26 +08:00
msksbr e1acc51117 archive: homework4 2025-04-14 18:20:23 +08:00
15 changed files with 657 additions and 1 deletions
+2 -1
View File
@@ -21,7 +21,8 @@
"excludeList": [
"src/test1.c",
"src/test2.c",
"src/test3.c"
"src/test3.c",
"src/test4.c"
],
"toolchain": "Keil_C51",
"compileConfig": {
+38
View File
@@ -0,0 +1,38 @@
// test5.c
// 数码管静态显示秒表的倒计时;
#include "homework4.h"
unsigned char cnt = 0; // 定时器溢出计数
unsigned char second = 0; // 秒计数
void test5()
{
set_38_2SEG();
TMOD = 0x01;
TH0 = 0x3C;
TL0 = 0xB0;
TR0 = 1;
// 初始化时立即显示9
second = 9;
show_number(second);
while (1) {
if (TF0 == 1) {
TF0 = 0;
TH0 = 0x3C;
TL0 = 0xB0;
if (++cnt >= 20) {
cnt = 0;
second = (second > 0) ? --second : 9;
show_number(second);
}
}
}
}
void main()
{
test5();
}
+38
View File
@@ -0,0 +1,38 @@
---
BasedOnStyle: Microsoft
Language: Cpp
###################################
# indent conf
###################################
UseTab: Never
IndentWidth: 4
TabWidth: 4
ColumnLimit: 0
AccessModifierOffset: -4
NamespaceIndentation: All
FixNamespaceComments: false
BreakBeforeBraces: Linux
###################################
# other styles
###################################
#
# for more conf, you can ref: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
#
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
IndentCaseLabels: true
SortIncludes: false
AlignConsecutiveMacros: AcrossEmptyLines
AlignConsecutiveAssignments: Consecutive
+85
View File
@@ -0,0 +1,85 @@
{
"name": "homework5",
"type": "C51",
"dependenceList": [],
"srcDirs": [
"src"
],
"virtualFolder": {
"name": "<virtual_root>",
"files": [],
"folders": []
},
"outDir": "build",
"deviceName": null,
"packDir": null,
"miscInfo": {
"uid": "5e3bef3a1c523fd2f57bb1fdff63f27d"
},
"targets": {
"Debug": {
"excludeList": [
"src/test.c",
"src/test2.c",
"src/test3.c",
"src/test4.c"
],
"toolchain": "Keil_C51",
"compileConfig": {
"options": "null"
},
"uploader": "Custom",
"uploadConfig": {
"bin": "",
"commandLine": "",
"eraseChipCommand": ""
},
"uploadConfigMap": {},
"custom_dep": {
"name": "default",
"incList": [],
"libList": [],
"defineList": []
},
"builderOptions": {
"SDCC": {
"version": 3,
"beforeBuildTasks": [],
"afterBuildTasks": [],
"global": {
"device": "mcs51",
"optimize-type": "speed",
"use-non-free": false
},
"c/cpp-compiler": {
"language-c": "c99"
},
"asm-compiler": {},
"linker": {
"$mainFileName": "main",
"output-format": "hex"
}
},
"Keil_C51": {
"version": 2,
"beforeBuildTasks": [],
"afterBuildTasks": [],
"global": {
"ram-mode": "SMALL",
"rom-mode": "LARGE"
},
"c/cpp-compiler": {
"optimization-type": "SPEED",
"optimization-level": "level-8"
},
"asm-compiler": {},
"linker": {
"remove-unused": true,
"output-format": "elf"
}
}
}
}
},
"version": "3.5"
}
+20
View File
@@ -0,0 +1,20 @@
##########################################################################################
# Append Compiler Options For Source Files
##########################################################################################
# syntax:
# <your pattern>: <compiler options>
# For get pattern syntax, please refer to: https://www.npmjs.com/package/micromatch
#
# examples:
# 'main.cpp': --cpp11 -Og ...
# 'src/*.c': -gnu -O2 ...
# 'src/lib/**/*.cpp': --cpp11 -Os ...
# '!Application/*.c': -O0
# '**/*.c': -O2 -gnu ...
version: "2.0"
options:
Debug:
files: {}
virtualPathFiles: {}
+15
View File
@@ -0,0 +1,15 @@
# dot files
/.vscode/launch.json
/.settings
/.eide/log
/.eide.usr.ctx.json
# project out
/build
/bin
/obj
/out
# eide template
*.ept
*.eide-template
+1
View File
@@ -0,0 +1 @@
{}
+40
View File
@@ -0,0 +1,40 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "${command:eide.project.build}",
"group": "build",
"problemMatcher": []
},
{
"label": "flash",
"type": "shell",
"command": "${command:eide.project.uploadToDevice}",
"group": "build",
"problemMatcher": []
},
{
"label": "build and flash",
"type": "shell",
"command": "${command:eide.project.buildAndFlash}",
"group": "build",
"problemMatcher": []
},
{
"label": "rebuild",
"type": "shell",
"command": "${command:eide.project.rebuild}",
"group": "build",
"problemMatcher": []
},
{
"label": "clean",
"type": "shell",
"command": "${command:eide.project.clean}",
"group": "build",
"problemMatcher": []
}
]
}
+45
View File
@@ -0,0 +1,45 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.autoGuessEncoding": true,
"C_Cpp.default.configurationProvider": "cl.eide",
"C_Cpp.errorSquiggles": "disabled",
"files.associations": {
".eideignore": "ignore",
"*.a51": "a51",
"*.h": "c",
"*.c": "c",
"*.hxx": "cpp",
"*.hpp": "cpp",
"*.c++": "cpp",
"*.cpp": "cpp",
"*.cxx": "cpp",
"*.cc": "cpp",
"REG52.H": "cpp"
},
"[yaml]": {
"editor.insertSpaces": true,
"editor.tabSize": 4,
"editor.autoIndent": "advanced"
}
},
"extensions": {
"recommendations": [
"cl.eide",
"keroc.hex-fmt",
"xiaoyongdong.srecord",
"hars.cppsnippets",
"zixuanwang.linkerscript",
"redhat.vscode-yaml",
"IBM.output-colorizer",
"cschlosser.doxdocgen",
"ms-vscode.vscode-serial-monitor",
"alefragnani.project-manager",
"cl.stm8-debug"
]
}
}
Binary file not shown.
+86
View File
@@ -0,0 +1,86 @@
// homework5.h
/*
* this header defines:
* variables:
* 1. pin numbers to control the LED and the Seg.
* 2. seg chars to display the number on the Seg.
* functions:
* 1. control the LED and the Seg.
* 2. to display the number on the Seg.
* 3. to set the 38 transistors.
* 4. to display the number on the Seg.
* 5. timer.
*/
#include <REG52.H>
// variables:
// pin numbers:
sbit BUS1A = P2 ^ 0; // BUS1A: BUS for the LED1(RED) and the Seg A.
sbit BUS2B = P2 ^ 1; // BUS2B: BUS for the LED2(ORANGE) and the Seg B.
sbit BUS3C = P2 ^ 2; // BUS3C: BUS for the LED3(YELLOW) and the Seg C.
sbit BUS4D = P2 ^ 3; // BUS4D: BUS for the LED4(GREEN) and the Seg D.
sbit BUS5E = P2 ^ 4; // BUS5E: BUS for the LED5(AQUA) and the Seg E.
sbit BUS6F = P2 ^ 5; // BUS6F: BUS for the LED6(BLUE) and the Seg F.
sbit BUS7G = P2 ^ 6; // BUS7G: BUS for the LED7(PURPLE) and the Seg G.
sbit BUS8DP = P2 ^ 7; // BUS8DP: BUS for the LED8(WHITE) and the Seg DP.
// 38 transistors controller:
sbit ADDR0 = P1 ^ 0; // to A(A0)
sbit ADDR1 = P1 ^ 1; // to B(A1)
sbit ADDR2 = P1 ^ 2; // to C(A2)
sbit ADDR3 = P1 ^ 3; // to E1
sbit ENLED = P1 ^ 4; // to E2 & E3
// seg chars:
unsigned char code LedChar[] = {
0xC0,
0xF9,
0XA4,
0XB0,
0X99,
0X92,
0X82,
0XF8,
0X80,
0X90,
0X88,
0X83,
0XC6,
0XA1,
0X86,
0X8E,
};
// functions:
// 38 transistors controller:
void set38Transistors(unsigned char number) // set the 38 transistors.
{
// turn on the 38 transistors:
ENLED = 0;
ADDR3 = 1;
// split the number into 3 binary numbers:
ADDR2 = (number & 0x04) >> 2;
ADDR1 = (number & 0x02) >> 1;
ADDR0 = (number & 0x01);
}
// timer
int timer(unsigned int ms)
{
TMOD &= 0xF0; // set the timer mode to 16-bit timer mode.
TMOD |= 0x01; // set the timer mode to 16-bit timer mode.
// 1ms timer definition:
TH0 = 0XFC;
TL0 = 0X18;
// start the timer:
TR0 = 1;
while (ms--) {
while (!TF0) {} // wait for the timer to overflow.
TF0 = 0; // clear the timer flag.
TH0 = 0XFC; // reload the timer value.
TL0 = 0X18; // reload the timer value.
}
TR0 = 0; // stop the timer.
return 0;
}
+47
View File
@@ -0,0 +1,47 @@
// test1.c
// 数码管动态显示自己学号后6位
// display the last 6 digits of your student ID on the Seg.
#include "homework5.h"
unsigned char LedBuff[6] = {
0xFF, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF};
unsigned char i = 0;
unsigned char display_id[6] = {1, 3, 0, 0, 7, 1};
void test1()
{
// display the number on the Seg.
// 5 is high digit.
// 0 is low digit.
LedBuff[5] = LedChar[display_id[0]];
LedBuff[4] = LedChar[display_id[1]];
LedBuff[3] = LedChar[display_id[2]];
LedBuff[2] = LedChar[display_id[3]];
LedBuff[1] = LedChar[display_id[4]];
LedBuff[0] = LedChar[display_id[5]];
while (1) {
/*
* seg id on 38 transistors:
* Y0: LEDS0 the latest digit.
* Y1: LEDS1 the second digit.
* Y2: LEDS2 the third digit.
* Y3: LEDS3 the forth digit.
* Y4: LEDS4 the fifth digit.
* Y5: LEDS5 the highest digit.
* Y6: 2LED to the LED lights.
*/
set38Transistors(i);
P2 = LedBuff[i];
i++;
timer(1);
// reset the i to 0 when it reaches 6.
if (i >= 6) {
i = 0;
}
}
}
void main()
{
test1();
}
+86
View File
@@ -0,0 +1,86 @@
// test2.c
// 不使用中断,让数码管动态显示秒表
// display the stopwatch on the Seg. without using interrupt.
/*
* seg display format:
* 00.00.00
* HH.MM.SS
* HH: hour, MM: minute, SS: second
* dot . is dp
*/
/*
* how it work?
* 1. sleep 1s
* 2. count the seconds, minutes, hours.
* 3. reset the seconds, minutes when it reaches 60.
* 4. reset all when everything overflow.
* 5. log the number in the buffer.
* 6. display the number on the Seg.
*/
#include "homework5.h"
unsigned char LedBuff[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // buffer to store the seg chars.
void test2()
{
unsigned char second = 0; // to count the seconds.
unsigned char minute = 0; // to count the minutes.
unsigned char hour = 0; // to count the hours.
unsigned char i = 0; // to count the times.
unsigned long time_counter = 0;
while (1) {
for (i = 0; i < 6; i++) {
// display number
set38Transistors(i);
P2 = LedBuff[i];
// display dot
if (i == 2 || i == 4) {
// 2nd dot between the minutes and seconds.
// 4th dot between the hours and minutes.
BUS8DP = 0; // display the dot.
} else {
BUS8DP = 1; // hide the dot on other places.
}
timer(1); // delay 1ms.
time_counter++;
// update time when 1000ms passed.
if (time_counter >= 1000) {
time_counter = 0; // reset the counter.
second++; // count the seconds.
// solve unit overflow.
// second overflow
if (second >= 60) {
second = 0; // reset the second.
minute++; // count the minutes.
}
// minute overflow.
if (minute >= 60) {
minute = 0; // reset the minute.
hour++; // count the hours.
}
// everything overflow.
if (hour >= 99 && second >= 59 && minute >= 59) {
hour = 0; // reset the hour.
second = 0; // reset the second.
minute = 0; // reset the minute.
}
}
// log the number in the buffer.
// 5 is high digit.
// 0 is low digit.
LedBuff[5] = LedChar[hour / 10]; // log the hour tens.
LedBuff[4] = LedChar[hour % 10]; // log the hour units.
LedBuff[3] = LedChar[minute / 10]; // log the minute tens.
LedBuff[2] = LedChar[minute % 10]; // log the minute units.
LedBuff[1] = LedChar[second / 10]; // log the second tens.
LedBuff[0] = LedChar[second % 10]; // log the second units.
}
}
}
void main()
{
test2();
}
+84
View File
@@ -0,0 +1,84 @@
// test3.c
// 让数码管动态显示秒表,但只显示有效位,高位的0不显示
// display the stopwatch on the Seg. but only display the valid digits.
/*
* how it work?
* 1. based on test2.c.
* 2. add check to the buffer.
* 3. only this unit is not 0 OR the previous unit is not 0, log the number in the buffer.
* 4. so this is my niubility code.
* 5. I'm so proud of myself.
* 6. msksbr NB!!!
* 7. msksbr NB!!!
* 8. msksbr NB!!!
*/
#include "homework5.h"
unsigned char LedBuff[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // buffer to store the seg chars.
void test3()
{
unsigned char second = 0; // to count the seconds.
unsigned char minute = 0; // to count the minutes.
unsigned char hour = 0; // to count the hours.
unsigned char i = 0; // to count the times.
unsigned long time_counter = 0;
while (1) {
for (i = 0; i < 6; i++) {
// display number
set38Transistors(i);
P2 = LedBuff[i];
// display dot
if (i == 2 || i == 4) {
// 2nd dot between the minutes and seconds.
// 4th dot between the hours and minutes.
BUS8DP = 0; // display the dot.
} else {
BUS8DP = 1; // hide the dot on other places.
}
timer(1); // delay 1ms.
time_counter++;
// update time when 1000ms passed.
if (time_counter >= 1000) {
time_counter = 0; // reset the counter.
second++; // count the seconds.
// solve unit overflow.
// second overflow
if (second >= 60) {
second = 0; // reset the second.
minute++; // count the minutes.
}
// minute overflow.
if (minute >= 60) {
minute = 0; // reset the minute.
hour++; // count the hours.
}
// everything overflow.
if (hour >= 99 && second >= 59 && minute >= 59) {
hour = 0; // reset the hour.
second = 0; // reset the second.
minute = 0; // reset the minute.
}
}
// log the number in the buffer.
// only log the valid digits.
// 5 is high digit.
// 0 is low digit.
LedBuff[5] = hour >= 10 ? LedChar[hour / 10] : 0XFF; // log the hour tens.
LedBuff[4] = hour > 0 ? LedChar[hour % 10] : 0XFF; // log the hour units.
LedBuff[3] = (minute >= 10 || hour > 0) ? LedChar[minute / 10] : 0XFF; // log the minute tens.
LedBuff[2] = minute > 0 ? LedChar[minute % 10] : 0XFF; // log the minute units.
LedBuff[1] = (second >= 10 || minute > 0) ? LedChar[second / 10] : 0XFF; // log the second tens.
LedBuff[0] = LedChar[second % 10]; // log the second units.
}
}
}
void main()
{
test3();
}
+70
View File
@@ -0,0 +1,70 @@
// test4.c
// 写一个从999999开始倒计时的秒表。并且改用定时器T1的中断来完成
// make a stopwatch that counts down from 999999. and use interrupt to finish it.
#include "homework5.h"
unsigned char LedBuff[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // buffer to store the seg chars.
volatile unsigned char i = 0; // to current digit.
volatile unsigned long display_time = 999999; // to store the time to display.
unsigned int cnt = 0; // to count interrupt.
// init timer
void initTimer(void)
{
TMOD &= 0x0F; // clear TMOD.
TMOD |= 0x10; // set TMOD to mode 1.
TH1 = 0xFC; // 1ms timer.
TL1 = 0x18;
ET1 = 1; // enable timer 1 interrupt.
EA = 1; // enable global interrupt.
TR1 = 1; // start timer 1.
}
// timer1 interrupt service routine.
void timer1ISR(void) interrupt 3
{
TH1 = 0xFC; // reset timer.
TL1 = 0x18;
// display the number.
set38Transistors(i);
P2 = LedBuff[i];
i = (i + 1) % 6; // to switch to the next digit.
// 1s timer.
if (++cnt >= 1000) { // if reached 1s.
cnt = 0;
if (display_time > 0) {
display_time--; // decrease the time.
}
}
}
void test4()
{
initTimer();
while (1) {
// split the time into 6 digits.
unsigned char first_digit = display_time / 100000; // to get the first digit.
unsigned char second_digit = (display_time % 100000) / 10000; // to get the second digit.
unsigned char third_digit = (display_time % 10000) / 1000; // to get the third digit.
unsigned char fourth_digit = (display_time % 1000) / 100; // to get the fourth digit.
unsigned char fifth_digit = (display_time % 100) / 10; // to get the fifth digit.
unsigned char sixth_digit = display_time % 10; // to get the sixth digit.
// load the number in the buffer.
// 5 is high digit.
// 0 is low digit.
LedBuff[5] = LedChar[first_digit]; // to display the first digit.
LedBuff[4] = LedChar[second_digit]; // to display the second digit.
LedBuff[3] = LedChar[third_digit]; // to display the third digit.
LedBuff[2] = LedChar[fourth_digit]; // to display the fourth digit.
LedBuff[1] = LedChar[fifth_digit]; // to display the fifth digit.
LedBuff[0] = LedChar[sixth_digit]; // to display the sixth digit.
}
}
void main()
{
test4();
}