赞
踩
国际绝对音名标准频率定义(32位无符号整型精度、十二等律体系、A4=440.01000Hz)
代码如下:
#ifndef __PITCH_H__
#define __PITCH_H__
/*利用Python来编写定义
代码:
f = open('./PITCH.h', 'w')
C=44001000/((2**(1/12))**9)
Db=44001000/((2**(1/12))**8)
D=44001000/((2**(1/12))**7)
Eb=44001000/((2**(1/12))**6)
E=44001000/((2**(1/12))**5)
F=44001000/((2**(1/12))**4)
Gb=44001000/((2**(1/12))**3)
G=44001000/((2**(1/12))**2)
Ab=44001000/(2**(1/12))
A=44001000
Bb=44001000*(2**(1/12))
B=44001000*((2**(1/12))**2)
f.write('#ifndef __PITCH_H__\n')
f.write('#define __PITCH_H__\n')
f.write('\n')
for i in range(0,10):
f.write('#define C'+str(i)+' '+str(int(C/(2**(4-i))))+'\n')
f.write('#define Db'+str(i)+' '+str(int(Db/2**(4-i)))+'\n')
f.write('#define D'+str(i)+' '+str(int(D/2**(4-i)))+'\n')
f.write('#define Eb'+str(i)+' '+str(int(Eb/2**(4-i)))+'\n')
f.write('#define E'+str(i)+' '+str(int(E/2**(4-i)))+'\n')
f.write('#define F'+str(i)+' '+str(int(F/2**(4-i)))+'\n')
f.write('#define Gb'+str(i)+' '+str(int(Gb/2**(4-i)))+'\n')
f.write('#define G'+str(i)+' '+str(int(G/2**(4-i)))+'\n')
f.write('#define Ab'+str(i)+' '+str(int(Ab/2**(4-i)))+'\n')
f.write('#define A'+str(i)+' '+str(int(A/2**(4-i)))+'\n')
f.write('#define Bb'+str(i)+' '+str(int(Bb/2**(4-i)))+'\n')
f.write('#define B'+str(i)+' '+str(int(B/2**(4-i)))+'\n')
f.write('\n')
f.write('#endif\n')
f.close()
*/
#define C0 1635196
#define Db0 1732430
#define D0 1835446
#define Eb0 1944587
#define E0 2060219
#define F0 2182726
#define Gb0 2312517
#define G0 2450027
#define Ab0 2595713
#define A0 2750062
#define Bb0 2913589
#define B0 3086840
#define C1 3270393
#define Db1 3464861
#define D1 3670893
#define Eb1 3889175
#define E1 4120438
#define F1 4365452
#define Gb1 4625035
#define G1 4900054
#define Ab1 5191426
#define A1 5500125
#define Bb1 5827179
#define B1 6173681
#define C2 6540787
#define Db2 6929723
#define D2 7341786
#define Eb2 7778351
#define E2 8240876
#define F2 8730904
#define Gb2 9250070
#define G2 9800108
#define Ab2 10382853
#define A2 11000250
#define Bb2 11654358
#define B2 12347363
#define C3 13081575
#define Db3 13859446
#define D3 14683572
#define Eb3 15556702
#define E3 16481752
#define F3 17461808
#define Gb3 18500141
#define G3 19600217
#define Ab3 20765706
#define A3 22000500
#define Bb3 23308717
#define B3 24694726
#define C4 26163151
#define Db4 27718893
#define D4 29367144
#define Eb4 31113405
#define E4 32963504
#define F4 34923616
#define Gb4 37000283
#define G4 39200434
#define Ab4 41531413
#define A4 44001000
#define Bb4 46617435
#define B4 49389452
#define C5 52326302
#define Db5 55437786
#define D5 58734288
#define Eb5 62226810
#define E5 65927009
#define F5 69847233
#define Gb5 74000566
#define G5 78400868
#define Ab5 83062827
#define A5 88002000
#define Bb5 93234871
#define B5 98778905
#define C6 104652604
#define Db6 110875572
#define D6 117468576
#define Eb6 124453621
#define E6 131854019
#define F6 139694467
#define Gb6 148001132
#define G6 156801737
#define Ab6 166125654
#define A6 176004000
#define Bb6 186469742
#define B6 197557810
#define C7 209305209
#define Db7 221751144
#define D7 234937153
#define Eb7 248907243
#define E7 263708038
#define F7 279388934
#define Gb7 296002265
#define G7 313603475
#define Ab7 332251309
#define A7 352008000
#define Bb7 372939484
#define B7 395115620
#define C8 418610418
#define Db8 443502288
#define D8 469874307
#define Eb8 497814487
#define E8 527416077
#define F8 558777869
#define Gb8 592004530
#define G8 627206951
#define Ab8 664502618
#define A8 704016000
#define Bb8 745878969
#define B8 790231241
#define C9 837220836
#define Db9 887004577
#define D9 939748614
#define Eb9 995628975
#define E9 1054832155
#define F9 1117555739
#define Gb9 1184009061
#define G9 1254413903
#define Ab9 1329005236
#define A9 1408032000
#define Bb9 1491757939
#define B9 1580462482
#endif
unsigned int C0 = 1635196;
unsigned int Db0 = 1732430;
unsigned int D0 = 1835446;
unsigned int Eb0 = 1944587;
unsigned int E0 = 2060219;
unsigned int F0 = 2182726;
unsigned int Gb0 = 2312517;
unsigned int G0 = 2450027;
unsigned int Ab0 = 2595713;
unsigned int A0 = 2750062;
unsigned int Bb0 = 2913589;
unsigned int B0 = 3086840;
unsigned int C1 = 3270393;
unsigned int Db1 = 3464861;
unsigned int D1 = 3670893;
unsigned int Eb1 = 3889175;
unsigned int E1 = 4120438;
unsigned int F1 = 4365452;
unsigned int Gb1 = 4625035;
unsigned int G1 = 4900054;
unsigned int Ab1 = 5191426;
unsigned int A1 = 5500125;
unsigned int Bb1 = 5827179;
unsigned int B1 = 6173681;
unsigned int C2 = 6540787;
unsigned int Db2 = 6929723;
unsigned int D2 = 7341786;
unsigned int Eb2 = 7778351;
unsigned int E2 = 8240876;
unsigned int F2 = 8730904;
unsigned int Gb2 = 9250070;
unsigned int G2 = 9800108;
unsigned int Ab2 = 10382853;
unsigned int A2 = 11000250;
unsigned int Bb2 = 11654358;
unsigned int B2 = 12347363;
unsigned int C3 = 13081575;
unsigned int Db3 = 13859446;
unsigned int D3 = 14683572;
unsigned int Eb3 = 15556702;
unsigned int E3 = 16481752;
unsigned int F3 = 17461808;
unsigned int Gb3 = 18500141;
unsigned int G3 = 19600217;
unsigned int Ab3 = 20765706;
unsigned int A3 = 22000500;
unsigned int Bb3 = 23308717;
unsigned int B3 = 24694726;
unsigned int C4 = 26163151;
unsigned int Db4 = 27718893;
unsigned int D4 = 29367144;
unsigned int Eb4 = 31113405;
unsigned int E4 = 32963504;
unsigned int F4 = 34923616;
unsigned int Gb4 = 37000283;
unsigned int G4 = 39200434;
unsigned int Ab4 = 41531413;
unsigned int A4 = 44001000;
unsigned int Bb4 = 46617435;
unsigned int B4 = 49389452;
unsigned int C5 = 52326302;
unsigned int Db5 = 55437786;
unsigned int D5 = 58734288;
unsigned int Eb5 = 62226810;
unsigned int E5 = 65927009;
unsigned int F5 = 69847233;
unsigned int Gb5 = 74000566;
unsigned int G5 = 78400868;
unsigned int Ab5 = 83062827;
unsigned int A5 = 88002000;
unsigned int Bb5 = 93234871;
unsigned int B5 = 98778905;
unsigned int C6 = 104652604;
unsigned int Db6 = 110875572;
unsigned int D6 = 117468576;
unsigned int Eb6 = 124453621;
unsigned int E6 = 131854019;
unsigned int F6 = 139694467;
unsigned int Gb6 = 148001132;
unsigned int G6 = 156801737;
unsigned int Ab6 = 166125654;
unsigned int A6 = 176004000;
unsigned int Bb6 = 186469742;
unsigned int B6 = 197557810;
unsigned int C7 = 209305209;
unsigned int Db7 = 221751144;
unsigned int D7 = 234937153;
unsigned int Eb7 = 248907243;
unsigned int E7 = 263708038;
unsigned int F7 = 279388934;
unsigned int Gb7 = 296002265;
unsigned int G7 = 313603475;
unsigned int Ab7 = 332251309;
unsigned int A7 = 352008000;
unsigned int Bb7 = 372939484;
unsigned int B7 = 395115620;
unsigned int C8 = 418610418;
unsigned int Db8 = 443502288;
unsigned int D8 = 469874307;
unsigned int Eb8 = 497814487;
unsigned int E8 = 527416077;
unsigned int F8 = 558777869;
unsigned int Gb8 = 592004530;
unsigned int G8 = 627206951;
unsigned int Ab8 = 664502618;
unsigned int A8 = 704016000;
unsigned int Bb8 = 745878969;
unsigned int B8 = 790231241;
unsigned int C9 = 837220836;
unsigned int Db9 = 887004577;
unsigned int D9 = 939748614;
unsigned int Eb9 = 995628975;
unsigned int E9 = 1054832155;
unsigned int F9 = 1117555739;
unsigned int Gb9 = 1184009061;
unsigned int G9 = 1254413903;
unsigned int Ab9 = 1329005236;
unsigned int A9 = 1408032000;
unsigned int Bb9 = 1491757939;
unsigned int B9 = 1580462482;
首先是最常用的阻塞延时
void delay_ms(unsigned int ms)
{
SysTick->LOAD = 50000000/1000-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
while(ms--)
{
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
}
SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{
SysTick->LOAD = 50000000/1000/1000-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
while(us--)
{
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
}
SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
50000000表示工作频率
分频后即可得到不同的延时时间
以此类推
那么 不用两个嵌套while循环 也可以写成:
void delay_ms(unsigned int ms)
{
SysTick->LOAD = 50000000/1000*ms-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{
SysTick->LOAD = 50000000/1000/1000*us-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
但是这种写法有个弊端
那就是输入ms后,最大定时不得超过计数值,也就是不能超过LOAD的最大值,否则溢出以后,则无法正常工作
而LOAD如果最大是32位 也就是4294967295
晶振为50M的话 50M的计数值为1s 4294967295计数值约为85s
固最大定时时间为85s
但用嵌套while的话 最大可以支持定时4294967295*85s
如果采用非阻塞的话 直接改写第二种方法就好了:
void delay_ms(unsigned int ms)
{
SysTick->LOAD = 50000000/1000*ms-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
//while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
//SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{
SysTick->LOAD = 50000000/1000/1000*us-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
//while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
//SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
将等待和关闭定时器语句去掉
在使用时加上判断即可变为阻塞:
delay_ms(500);
while ((SysTick->CTRL & 0x00010000)==0);
SysTick->CTRL = 0;
在非阻塞状态下 可以提交定时器后 去做别的事情 然后再来等待
不过这样又有一个弊端 那就是定时器会自动重载 可能做别的事情以后 定时器跑过了 然后就要等85s才能停下
故可以通过内部定时器来进行非阻塞延时函数的编写
基本上每个mcu的内部定时器都可以配置自动重载等功能 网上资料很多 这里就不再阐述了
闲来无事写了个小程序 代码如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021
@author: 16016
"""
a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
for j in range(16):
a_list[j]=str(a_list[j])+'_'+str(j)
print("序号:",j)
print('a_list:\n',a_list)
c_list[j]=a_list
print('c_list[0]:\n',c_list[0])
print('\n')
# b_list[j]=a_list[7],a_list[8]
# print(b_list[j])
# 写入到Excel:
#print(c_list,'\n')
我在程序中 做了一个16次的for循环 把列表a的每个值后面依次加上"_"和循环序号
比如循环第x次 就是把第x位加上_x 这一位变成x_x 我在输出测试中 列表a的每一次输出也是对的
循环16次后列表a应该变成[‘0_0’, ‘1_1’, ‘2_2’, ‘3_3’, ‘4_4’, ‘5_5’, ‘6_6’, ‘7_7’, ‘8_8’, ‘9_9’, ‘10_10’, ‘11_11’, ‘12_12’, ‘13_13’, ‘14_14’, ‘15_15’] 这也是对的
同时 我将每一次循环时列表a的值 写入到空列表c中 比如第x次循环 就是把更改以后的列表a的值 写入到列表c的第x位
第0次循环后 c[0]的值应该是[‘0_0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘10’, ‘11’, ‘12’, ‘13’, ‘14’, ‘15’] 这也是对的
但是在第1次循环以后 c[0]的值就一直在变 变成了c[x]的值
相当于把c_list[0]变成了c_list[1]…以此类推 最后得出的列表c的值也是每一项完全一样
我不明白这是怎么回事
我的c[0]只在第0次循环时被赋值了 但是后面它的值跟着在改变
如图:
第一次老出bug 赋值以后 每次循环都改变c[0]的值 搞了半天都没搞出来
无论是用appen函数添加 还是用二维数组定义 或者增加第三个空数组来过渡 都无法解决
后来在我华科同学的指导下 突然想到赋值可以赋的是个地址 地址里面的值一直变化 导致赋值也一直变化 于是用第二张图的循环套循环深度复制实现了
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021
@author: 16016
"""
a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
for j in range(16):
a_list[j]=str(a_list[j])+'_'+str(j)
print("序号:",j)
print('a_list:\n',a_list)
for i in range(16):
c_list[j].append(a_list[i])
print('c_list[0]:\n',c_list[0])
print('\n')
# b_list[j]=a_list[7],a_list[8]
# print(b_list[j])
# 写入到Excel:
print(c_list,'\n')
解决了问题
第三次是请教了老师 用copy函数来赋真值
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021
@author: 16016
"""
a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
for j in range(16):
a_list[j]=str(a_list[j])+'_'+str(j)
print("序号:",j)
print('a_list:\n',a_list)
c_list[j]=a_list.copy()
print('c_list[0]:\n',c_list[0])
print('\n')
# b_list[j]=a_list[7],a_list[8]
# print(b_list[j])
# 写入到Excel:
#print(c_list,'\n')
同样能解决问题
最后得出问题 就是指针惹的祸!
a_list指向的是个地址 而不是值 a_list[i]指向的才是单个的值 copy()函数也是复制值而不是地址
如果这个用C语言来写 就直观一些了 难怪C语言是基础 光学Python不学C 遇到这样的问题就解决不了
C语言yyds Python是什么垃圾弱智语言
由于Python无法单独定义一个值为指针或者独立的值 所以只能用列表来传送
只要赋值是指向一个列表整体的 那么就是指向的一个指针内存地址 解决方法只有一个 那就是将每个值深度复制赋值(子列表内的元素提取出来重新依次连接) 或者用copy函数单独赋值
如图测试:
部分代码:
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 20 16:45:48 2021
@author: 16016
"""
def text1():
A=[1,2,3]
B=[[],[],[]]
for i in range(len(A)):
A[i]=A[i]+i
B[i]=A
print(B)
def text2():
A=[1,2,3]
B=[[],[],[]]
A[0]=A[0]+0
B[0]=A
print(B)
A[1]=A[1]+1
B[1]=A
print(B)
A[2]=A[2]+2
B[2]=A
print(B)
if __name__ == '__main__':
text1()
print('\n')
text2()
Pyinstaller打包exe(包括打包资源文件 绝不出错版)
依赖包及其对应的版本号
PyQt5 5.10.1
PyQt5-Qt5 5.15.2
PyQt5-sip 12.9.0
pyinstaller 4.5.1
pyinstaller-hooks-contrib 2021.3
Pyinstaller -F setup.py 打包exe
Pyinstaller -F -w setup.py 不带控制台的打包
Pyinstaller -F -i xx.ico setup.py 打包指定exe图标打包
打包exe参数说明:
-F:打包后只生成单个exe格式文件;
-D:默认选项,创建一个目录,包含exe文件以及大量依赖文件;
-c:默认选项,使用控制台(就是类似cmd的黑框);
-w:不使用控制台;
-p:添加搜索路径,让其找到对应的库;
-i:改变生成程序的icon图标。
如果要打包资源文件
则需要对代码中的路径进行转换处理
另外要注意的是 如果要打包资源文件 则py程序里面的路径要从./xxx/yy换成xxx/yy 并且进行路径转换
但如果不打包资源文件的话 最好路径还是用作./xxx/yy 并且不进行路径转换
def get_resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
而后再spec文件中的datas部分加入目录
如:
a = Analysis(['cxk.py'],
pathex=['D:\\Python Test\\cxk'],
binaries=[],
datas=[('root','root')],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
而后直接Pyinstaller -F setup.spec即可
如果打包的文件过大则更改spec文件中的excludes 把不需要的库写进去(但是已经在环境中安装了的)就行
这些不要了的库在上一次编译时的shell里面输出
比如:
然后用pyinstaller --clean -F 某某.spec
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。