C51汇编典型代码&一些org-mode技巧

​文档存放​

具体内容可见存放的数据。

下面主要介绍关键代码。

ASM 部分

1;; LCD数据发送=============================================================
2OUT_DATA:
3 PUSH 01H
4 PUSH 02H
5 PUSH 03H
6 MOV R2, #32 ;32行,(双屏结构中上半屏)
7 MOV R3, #80H ;Y地址寄存器
8LCD_ADDR_UP:
9 MOV SONG, R3 ;设置绘图区的Y地址坐标
10 INC R3 ;Y地址加1
11 LCALL SEND_ML
12 MOV SONG, #80H ;设置绘图区的X地址坐标
13 LCALL SEND_ML
14 MOV R1, #16 ;16*8列
15LCD_DISP_UP:
16 CLR A
17 MOVC A, @A+DPTR
18 MOV SONG, A
19 LCALL SEND_SJ
20 INC DPTR
21 DJNZ R1, LCD_DISP_UP
22 DJNZ R2, LCD_ADDR_UP ;写满全屏的16*8字节X64
23 MOV R2, #32 ;32行,(双屏结构的下半屏)
24 MOV R3, #80H ;Y地址寄存器
25LCD_ADDR_DOWN:
26 MOV SONG, R3 ;设置绘图区的Y地址坐标
27 INC R3 ;Y地址加1
28 LCALL SEND_ML
29 MOV SONG, #88H ;设置绘图区的X地址坐标
30 LCALL SEND_ML
31 MOV R1, #16 ;16*8列
32LCD_DISP_DOWN:
33 CLR A
34 MOVC A, @A+DPTR
35 MOV SONG, A
36 LCALL SEND_SJ
37 INC DPTR
38 DJNZ R1, LCD_DISP_DOWN
39 DJNZ R2, LCD_ADDR_DOWN ;写满全屏的16*8字节X64
40 POP 03H
41 POP 02H
42 POP 01H
43 RET

这部分介绍了如何在 LCD12864 上进行绘图操作。

值得纪念的是,这部分代码是我自己摸索,对比着网上找来的资料,不断地调整,最后终于搞定的。

开心的是,分享给了其他人,表现顽强

​LCD12864 GDRAM存储结构​

目前还没学会如何在 org-mode 中直接插入网络图片,并且可以直接显示。。

python 部分

1import binascii
2import serial
3from time import sleep
4import matplotlib.pyplot as plt
5import matplotlib.animation as animation
6
7
8#初始数据绘图
9def update(frame):
10 #读入模拟
11 read_string = binascii.hexlify(ser.read()).decode('ascii')
12 a = (int(read_string[0], 16) * 16 + int(read_string[1], 16))
13 print(a)
14 sleep(0.1)
15 #绘图数据生成
16 del(data_read[0])
17 data_read.append(a)
18 #绘图
19 line.set_ydata(data_read)
20 #颜色设置
21 if abs(a) >= 40:
22 plt.setp(line, 'color', 'r', 'linewidth', 2.0)
23 else:
24 plt.setp(line, 'color', 'b', 'linewidth', 2.0)
25 return line
26
27if __name__ == '__main__':
28 data_read = list(range(100))
29 fig, ax = plt.subplots()
30 line, = ax.plot(data_read)
31 ax.set_ylim(0, 100)
32 plt.grid(True)
33 ax.set_ylabel("Temperature: ℃")
34 ax.set_xlabel("Relative Time: s")
35
36 ser = serial.Serial(port='COM3', baudrate=1200)
37 ani = animation.FuncAnimation(fig, update, frames=None, interval=100)
38 plt.show()
39ser.close()

这段代码中使用了 matplotlib.animation 这个特殊的子模块,用来绘制动态图。

最开始用的是 Tkinter 来配合 matplotlib 显示串口数据的状态,但是不适合大量数据连续的绘制。会出现卡死的状态。

直到最后,还是放弃了自定义界面的想法,干脆只是单纯的做图,于是有了这个版本。

但是还是要记录下之前的努力,下面的是原来的基于 Tkinter 和 matplotlib 的代码。

1#  coding:utf-8
2"""
3实现读取端口数据实现绘图
42018年5月30日23:39:32
5"""
6import binascii
7import time
8from tkinter import *
9
10import matplotlib
11import serial
12from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
13from matplotlib.backend_bases import key_press_handler
14from matplotlib.figure import Figure
15
16
17def drawPic():
18 """
19 获取GUI界面设置的参数,利用该参数绘制图片
20 """
21 # 清空图像,以使得前后两次绘制的图像不会重叠
22 screen_draw.clf()
23 add_plot = screen_draw.add_subplot(111)
24 add_plot.axis([0, 100, 00, 100])
25
26 x = list(range(100))
27 y = data_read
28 add_plot.plot(x, y, 'r', label='Temperature')
29 add_plot.set_title('DRAW')
30 canvas_draw.draw()
31
32
33def serial_data():
34 """处理串口数据"""
35 # 打开端口
36 try:
37 port = input_port.get()
38 baudrate = int(input_baudrate.get())
39 except:
40 port = 'COM3'
41 baudrate = 9600
42 input_port.delete(0)
43 input_baudrate.delete(0)
44 input_port.insert(0, 'COM3')
45 input_baudrate.insert(0, '9600')
46
47 ser = serial.Serial(port, baudrate)
48 list_index = list(range(50))
49 data_read[0: 50] = data_read[50: 100]
50 for x in list_index:
51 read_string = binascii.hexlify(ser.read()).decode('ascii')
52 data_read[x + 50] = int(read_string[0], 16) * \
53 16 + int(read_string[1], 16)
54 print(data_read[x])
55 print("Over")
56 ser.close()
57
58
59def now_draw():
60 """实时绘制图像"""
61 try:
62 port = input_port.get()
63 baudrate = int(input_baudrate.get())
64 delay = int(input_delay.get())
65 except:
66 port = 'COM3'
67 baudrate = 9600
68 delay = 10
69 input_port.delete(0)
70 input_baudrate.delete(0)
71 input_delay.delete(0)
72 input_port.insert(0, 'COM3')
73 input_baudrate.insert(0, '9600')
74 input_delay.insert(0, '10')
75
76 list_index_draw = list(range(100))
77 ser = serial.Serial(port, baudrate)
78
79 start = time.clock()
80
81 while True:
82 read_string = binascii.hexlify(ser.read()).decode('ascii')
83 data_read[99] = int(read_string[0], 16) * 16 + int(read_string[1], 16)
84 data_read[0: 99] = data_read[1: 100]
85
86 drawPic()
87
88 # 指定退出死循环方式
89 if(data_read[99] == 85): # 温度采集模块关闭后,串口传回来的值就是 55H = 85
90 break
91 elif(delay > 0):
92 if(time.clock() - start >= delay):
93 break
94
95 ser.close()
96
97
98def on_key_event(event):
99 print('推出程序 %s' % event.key)
100 root.quit()
101 root.destroy()
102
103
104if __name__ == '__main__':
105 matplotlib.use('TkAgg')
106
107 # 用于接受数据的存储
108 data_read = list(range(100))
109 root = Tk()
110
111 # 在Tk的GUI上放置一个画布,并用.grid()来调整布局
112 screen_draw = Figure(figsize=(5, 4), dpi=100)
113 canvas_draw = FigureCanvasTkAgg(screen_draw, master=root)
114 # 绑定任意键退出
115 canvas_draw.mpl_connect('key_press_event', on_key_event)
116
117 canvas_draw.get_tk_widget().grid(row=0, columnspan=3)
118 canvas_draw.draw()
119
120 # 放置标签、文本框和按钮等部件,并设置文本框的默认值和按钮的事件函数
121 Label(root, text='请输入监测时长(s),0则无限,关闭测温即可停止:').grid(row=1, column=0)
122 input_delay = Entry(root)
123 input_delay.grid(row=1, column=1)
124 input_delay.insert(0, '50')
125
126 Label(root, text='请输入端口号:').grid(row=2, column=0)
127 input_port = Entry(root)
128 input_port.grid(row=2, column=1)
129 input_port.insert(0, 'COM3')
130
131 Label(root, text='请输入波特率:').grid(row=3, column=0)
132 input_baudrate = Entry(root)
133 input_baudrate.grid(row=3, column=1)
134 input_baudrate.insert(0, '9600')
135
136 Button(
137 root, text='实时监控', command=now_draw).grid(
138 row=1, column=2, columnspan=1)
139 Button(
140 root, text='开始画图', command=drawPic).grid(
141 row=2, column=2, columnspan=1)
142 Button(
143 root, text='打开端口', command=serial_data).grid(
144 row=3, column=2, columnspan=1)
145
146 # 启动事件循环
147 root.mainloop()

这里倒是学习了下 Tkinter 的相关操作,还是不错的。自定义的能力更强。不过很可惜。

并不是很适合处理动态数据显示。

ORG-MODE 部分

​org-mode-babel​

1简单模板对应快捷输入的首字母
2
3输入 < 后面跟一个字母,然后按 TAB 键,就可以生成对应的模板。(eg: <e + TAB )
4
5s #+BEGIN_SRC ... #+END_SRC
6e #+BEGIN_EXAMPLE ... #+END_EXAMPLE
7q #+BEGIN_QUOTE ... #+END_QUOTE
8v #+BEGIN_VERSE ... #+END_VERSE
9c #+BEGIN_CENTER ... #+END_CENTER
10l #+BEGIN_LaTeX ... #+END_LaTeX
11L #+LaTeX:
12h #+BEGIN_HTML ... #+END_HTML
13H #+HTML:
14a #+BEGIN_ASCII ... #+END_ASCII
15A #+ASCII:
16i #+INDEX: line
17I #+INCLUDE: line
18#+END_EXAMPLE
19
20#+BEGIN_EXAMPLE
21(7) 文档元数据
22
23#+TITLE: the title to be shown (default is the buffer name)
24#+AUTHOR: the author (default taken from user-full-name)
25#+DATE: a date, an Org timestamp1, or a format string for format-time-string
26#+EMAIL: his/her email address (default from user-mail-address)
27#+DESCRIPTION: the page description, e.g. for the XHTML meta tag
28#+KEYWORDS: the page keywords, e.g. for the XHTML meta tag
29#+LANGUAGE: language for HTML, e.g. ‘en’ (org-export-default-language)
30#+TEXT: Some descriptive text to be inserted at the beginning.
31#+TEXT: Several lines may be given.
32#+OPTIONS: H:2 num:t toc:t \n:nil @:t ::t |:t ^:t f:t TeX:t ...
33#+BIND: lisp-var lisp-val, e.g.: org-export-latex-low-levels itemize
34 You need to confirm using these, or configure org-export-allow-BIND
35#+LINK_UP: the ``up'' link of an exported page
36#+LINK_HOME: the ``home'' link of an exported page
37#+LATEX_HEADER: extra line(s) for the LaTeX header, like \usepackage{xyz}
38#+EXPORT_SELECT_TAGS: Tags that select a tree for export
39#+EXPORT_EXCLUDE_TAGS: Tags that exclude a tree from export
40#+XSLT: the XSLT stylesheet used by DocBook exporter to generate FO file
41
42其中#+OPTIONS是复合的选项,包括:
43
44H: set the number of headline levels for export
45num: turn on/off section-numbers
46toc: turn on/off table of contents, or set level limit (integer)
47\n: turn on/off line-break-preservation (DOES NOT WORK)
48@: turn on/off quoted HTML tags
49:: turn on/off fixed-width sections
50|: turn on/off tables
51^: turn on/off TeX-like syntax for sub- and superscripts. If
52 you write "^:{}", a_{b} will be interpreted, but
53 the simple a_b will be left as it is.
54-: turn on/off conversion of special strings.
55f: turn on/off footnotes like this[1].
56todo: turn on/off inclusion of TODO keywords into exported text
57tasks: turn on/off inclusion of tasks (TODO items), can be nil to remove
58 all tasks, todo to remove DONE tasks, or list of kwds to keep
59pri: turn on/off priority cookies
60tags: turn on/off inclusion of tags, may also be not-in-toc
61<: turn on/off inclusion of any time/date stamps like DEADLINES
62*: turn on/off emphasized text (bold, italic, underlined)
63TeX: turn on/off simple TeX macros in plain text
64LaTeX: configure export of LaTeX fragments. Default auto
65skip: turn on/off skipping the text before the first heading
66author: turn on/off inclusion of author name/email into exported file
67email: turn on/off inclusion of author email into exported file
68creator: turn on/off inclusion of creator info into exported file
69timestamp: turn on/off inclusion creation time into exported file
70d: turn on/off inclusion of drawers