This commit is contained in:
fengbh 2024-07-15 01:39:23 +08:00
commit 917a4cc7cd
9 changed files with 318 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.hex
obj

69
Makefile Normal file
View File

@ -0,0 +1,69 @@
# detect OS
ifeq ($(OS),Windows_NT)
RM := del /q
CC := sdcc
COM := COM3
else
RM := rm -rf
UNAME_S := $(shell cat /etc/*-release | grep 'DISTRIB_ID' | sed 's/DISTRIB_ID=\(\w*\)/\1/g')
ifeq ($(UNAME_S),Ubuntu)
CC := sdcc
COM := /dev/ttyUSB0
endif
ifeq ($(UNAME_S),Deepin)
CC := ablrun sdcc
COM := /dev/ttyUSB0
endif
endif
# set uart baudrate
BAUD := 9600
# set CC TOOL
PACKIHX := packihx
CFLAGS := -DUSE_FLOATS=1
LDFLAGS := --iram-size 0x100 --xram-size 0x400 --code-size 0xffff
# set DIR
INCDIR = include
SRCDIR = src
OBJDIR = obj
TARGET = obj/main.ihx
INC := ./include
SRC := $(wildcard $(SRCDIR)/*.c)
# OBJ := $(SRC:%.c=%.rel)
OBJ := $(patsubst src/%.c, obj/%.rel, $(SRC))
.PHONY: all clean
all: main.hex
clean:
-$(RM) obj/*
$(OBJ):obj/%.rel:src/%.c
-$(CC) $(CFLAGS) -I $(INC) -c $^ -o $@
$(TARGET): $(OBJ)
-$(CC) $(LDFLAGS) $^ -o $@
main.hex: $(TARGET)
-$(PACKIHX) $(TARGET) > main.hex
flash:
-sudo chmod 777 $(COM)
-./reset_mcu.sh $(COM) $(BAUD)
-stcgal -P stc89 -p $(COM) main.hex
com:
-minicom -D $(COM) -b $(BAUD)
#led.bin:led.hex
# objcopy -I ihex -O binary led.hex led.bin
#led.hex:main.ihx
# packihx main.ihx > led.hex
# led.bin:main.ihx
# makebin main.ihx led.bin

26
include/intrins.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef _INTRINS_H_
#define _INTRINS_H_
/* warning: __push __pop 使用堆栈临时保存 sfr 数据,必需成对使用!
__push(x);
... // 保护代码块
__pop(x) //缺少无该语句编译不会出错,但运行错误!
*/
#define __push(x) __asm push _##x __endasm /* void _push_ (unsigned char _sfr); */
#define __pop(x) __asm pop _##x __endasm /* void _pop_ (unsigned char _sfr); */
#define _push_ __push  /*兼容 keil c51*/
#define _pop_ __pop  /*兼容 keil c51*/
/* 安全使用保护宏:
pushSfr(x);
... // 受保护代码块
popSfr(x) // 缺少无该语句编译出错,确保生成正确代码。
*/
#define pushSfr(x) do{\
__push(x)
#define popSfr(x) __pop(x);\
}while(0)
#endif //_INTRINS_H_

21
include/public.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef _public_H
#define _public_H
// #include "reg52.h"
#include <8052.h>
typedef unsigned int u16;
typedef unsigned char u8;
// 用于自动重启的变量
__sfr __at (0xE7) ISP_CONTR;
#define RESET_PASSWORD_LENGTH 6
extern char __xdata reset_password[RESET_PASSWORD_LENGTH];
extern char __xdata receice_password[RESET_PASSWORD_LENGTH];
void delay_10us(u16 ten_us);
void delay_ms(u16 ms);
u8 auto_reset(u8 rec_data);
#endif

22
include/uart.h Normal file
View File

@ -0,0 +1,22 @@
/**************************************************************************************
PRECHIN
www.prechin.net
线
使线CH340旁的P5端子的UTX和P30短接URX和P31短接
***************************************************************************************/
#ifndef _uart_H
#define _uart_H
#include "public.h"
extern __bit busy;
void uart_init(u8 baud);
void send_string(char *s);
void send_data(u8 dat);
int putchar(char c);
#endif

33
reset_mcu.sh Executable file
View File

@ -0,0 +1,33 @@
#!/bin/bash
#===========================================================================
# Organization : Individual developer
# Filename : reset_mcu.sh
# Author : Feng Bohan
# Create Time : 02:51:28 2024-07-14
# Last Modified: 00:17:44 2024-07-15
# Abstract :
#--------------------------------------------------------------------------
# Description:
#
#--------------------------------------------------------------------------
# Modification History:
#--------------------------------------------------------------------------
# Rev Date Who Description
# --- ---- --- -----------
# 0.0.01 2024-07-14 Feng Bohan initial version
#===========================================================================
if [ "$#" -ne 2 ]; then
echo "错误此脚本需要且仅需要2个参数, 作为COM口号和波特率。"
exit 1 # 使用非零状态码退出表示异常
fi
stty -F $1 speed $2 cs8 -parenb -cstopb raw -echo -echoe -echok -echoctl -echoke
echo -e "RESET!" > $1
#说明:
#speed 串口波特率
#cs8 数据位8位
#parenb 无校验
#cstopb 停止位1位
#查看串口设置: stty -a -F /dev/ttyUSB0

51
src/main.c Normal file
View File

@ -0,0 +1,51 @@
/**************************************************************************************
PRECHIN
www.prechin.net
DS18B20温度传感器实验
线
DS18B20温度传感器
***************************************************************************************/
#include "public.h"
#include "uart.h"
#include "stdio.h"
/*******************************************************************************
* : main
* :
* :
* :
*******************************************************************************/
void main(void)
{
uart_init(0XFA);//波特率为9600
while(1)
{
delay_ms(1000);
printf("Hello,world!\r\n");
}
}
void uart(void) __interrupt (4) //串口通信中断函数
{
u8 rec_data;
if(RI)
{
RI = 0; //清除接收中断标志位
rec_data=SBUF; //存储接收到的数据
SBUF=rec_data; //将接收到的数据放入到发送寄存器
while(!TI); //等待发送数据完成
TI=0; //清除发送完成标志?
auto_reset(rec_data);
}
if(TI)
{
TI = 0;
busy = 0;
}
}

53
src/public.c Normal file
View File

@ -0,0 +1,53 @@
#include "public.h"
/*******************************************************************************
* : delay_10us
* : ten_us=110us
* : ten_us
* :
*******************************************************************************/
void delay_10us(u16 ten_us)
{
while(ten_us--);
}
/*******************************************************************************
* : delay_ms
* : ms延时函数ms=11ms
* : msms延时时间
* :
*******************************************************************************/
void delay_ms(u16 ms)
{
u16 i,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
/*******************************************************************************
* : auto_reset
* : RESET!MCU
* :
* : 0:1:
*******************************************************************************/
char __xdata reset_password[RESET_PASSWORD_LENGTH] = "RESET";
char __xdata receice_password[RESET_PASSWORD_LENGTH] = {0};
u8 auto_reset(u8 rec_data)
{
for(u8 i=0;i<RESET_PASSWORD_LENGTH-1;i++)
{
receice_password[i] = receice_password[i+1];
}
receice_password[RESET_PASSWORD_LENGTH-1] = rec_data;
for(u8 i=0;i<RESET_PASSWORD_LENGTH-1;i++)
{
if(receice_password[i]!= reset_password[i])
{
return 1;
}
}
ISP_CONTR = 0x60; //软件复位并切换到系统ISP监控程序区开始执行程序
return 0;
}

41
src/uart.c Normal file
View File

@ -0,0 +1,41 @@
#include "uart.h"
__bit busy = 0;
/*******************************************************************************
* : uart_init
* : TH和TL即可确定定时时间
* : baudTHTL装载值
* :
*******************************************************************************/
void uart_init(u8 baud)
{
TMOD|=0X20; //设置计数器工作方式2
SCON=0X50; //设置为工作方式1
PCON=0X80; //波特率加倍
TH1=baud; //计数器初始值设置
TL1=baud;
ES=1; //打开接收中断
EA=1; //打开总中断
TR1=1; //打开计数器
}
void send_string(char *s)
{
while(*s)
{
send_data(*s++);
}
}
void send_data(u8 dat)
{
while(busy);
busy = 1;
SBUF = dat;
}
int putchar(char c)//重定向
{
send_data(c);
return c;
}