init
This commit is contained in:
commit
917a4cc7cd
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*.hex
|
||||||
|
obj
|
69
Makefile
Normal file
69
Makefile
Normal 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
26
include/intrins.h
Normal 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
21
include/public.h
Normal 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
22
include/uart.h
Normal 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
33
reset_mcu.sh
Executable 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
51
src/main.c
Normal 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
53
src/public.c
Normal file
@ -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<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
41
src/uart.c
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include "uart.h"
|
||||||
|
|
||||||
|
__bit busy = 0;
|
||||||
|
/*******************************************************************************
|
||||||
|
* 函 数 名 : uart_init
|
||||||
|
* 函数功能 : 串口通信中断配置函数,通过设置TH和TL即可确定定时时间
|
||||||
|
* 输 入 : baud:波特率对应的TH、TL装载值
|
||||||
|
* 输 出 : 无
|
||||||
|
*******************************************************************************/
|
||||||
|
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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user