commit 917a4cc7cd035dd00dda5755232e4cf6ec1a4653 Author: fengbh <1953356163@qq.com> Date: Mon Jul 15 01:39:23 2024 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58b8d6d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.hex +obj diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ba8d1c5 --- /dev/null +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/include/intrins.h b/include/intrins.h new file mode 100644 index 0000000..b8d7c0a --- /dev/null +++ b/include/intrins.h @@ -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_ \ No newline at end of file diff --git a/include/public.h b/include/public.h new file mode 100644 index 0000000..79ccbea --- /dev/null +++ b/include/public.h @@ -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 diff --git a/include/uart.h b/include/uart.h new file mode 100644 index 0000000..8aa7952 --- /dev/null +++ b/include/uart.h @@ -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 \ No newline at end of file diff --git a/reset_mcu.sh b/reset_mcu.sh new file mode 100755 index 0000000..05b8a4d --- /dev/null +++ b/reset_mcu.sh @@ -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 diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..bf396a9 --- /dev/null +++ b/src/main.c @@ -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; + } +} \ No newline at end of file diff --git a/src/public.c b/src/public.c new file mode 100644 index 0000000..1a0f08d --- /dev/null +++ b/src/public.c @@ -0,0 +1,53 @@ +#include "public.h" + +/******************************************************************************* +* 函 数 名 : delay_10us +* 函数功能 : 延时函数,ten_us=1时,大约延时10us +* 输 入 : ten_us +* 输 出 : 无 +*******************************************************************************/ +void delay_10us(u16 ten_us) +{ + while(ten_us--); +} + +/******************************************************************************* +* 函 数 名 : delay_ms +* 函数功能 : ms延时函数,ms=1时,大约延时1ms +* 输 入 : ms:ms延时时间 +* 输 出 : 无 +*******************************************************************************/ +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