|
zj57421017
新手上路
积分 5
发帖 2
注册 2010-5-7
状态 离线
|
『楼 主』:
跪求把这段汇编转换成C语言!!谢谢啦!!!
一,happy birthday 生日快乐歌用21个音阶设计的程序
Void happy birthday()要用C51格式写程序!
{
setb tr0(tr0=1;) ;启动定时器0
start0:
setb p3.3;
30h=0x00;
next:
mov a,30h
mov dptr,#table
inc dptr
movc a,@a+dptr
mov r5,a ;将节拍码存入r5
cjne a,#0ffh,next1
jmp ennd
next1:
clr c
mov a,dpl
subb a,#1
mov dpl,a
mov a,dph
subb a,#0
mov dph,a
mov a,30h
movc a,@a+dptr
jnz sing
clr tr0 ;休止符?是,则不发音
jmp d1 ;跳到d1
sing:
dec a ;取到的音符码减1
mov 22h,a ;存入(22h)
rl a ; 乘2
mov dptr,#table1 ;至table1取相对应的高字节
movc a,@a+dptr
mov th0,a ;取到的高字节存入TH0
mov 21h,a ;取到的高字节存入(21h)
mov a,22h ;乘2
rl a ;加1
inc a ;至table1取相对应的低位
movc a,@a+dptr
mov tl0,a ;取到低字节存入tl0
mov 20h,a ;取到低字节存入(20h)
setb tr0 ;启动timer0
d1:
call delay ;声音延时 基本单位时间 1/4拍187毫秒
inc 30h ;取简码简指针加1
inc 30h ;取简码简指针加1
jmp next ;取下一个码
ennd:
clr tr0 ;停止timer0
ret
table:
db 12,2,0,1,12,2,13,4,12,4,15,4,14,4,0,4
db 12,2,0,1,12,1,13,4,12,4,16,4,15,4,0,4
db 12,2,0,1,12,1,19,4,17,4,15,4,14,4,13,4
db 18,2,0,1,18,1,17,4,15,4,16,4,15,4,0,4
db 12,2,0,1,12,1,13,4,12,4,15,4,14,4,0,4
db 12,2,0,1,12,1,13,4,12,4,16,4,15,4,0,4
db 12,2,0,1,12,1,19,4,17,4,15,4,14,4,13,4
db 18,2,0,1,15,1,17,4,15,4,16,4,15,4,0,4,0ffh,0ffh
}
|
|
2010-5-8 19:19 |
|
|
070
高级用户
苏醒的沉睡者
积分 659
发帖 217
注册 2003-2-15 来自 福建
状态 离线
|
『第
2 楼』:
我乱写了一个,应该是有错误的,没测试,只供参考
应该适合turboc
#include <dos.h>
#define DELAY 168
#define SIZE 130
typedef unsigned char BYTE;
int main(int argc, char *argv[])
{
BYTE music[SIZE] = {
2,2,0,1,12,2,13,4,12,4,15,4,14,4,0,4,
12,2,0,1,12,1,13,4,12,4,16,4,15,4,0,4,
12,2,0,1,12,1,19,4,17,4,15,4,14,4,13,4,
18,2,0,1,18,1,17,4,15,4,16,4,15,4,0,4,
12,2,0,1,12,1,13,4,12,4,15,4,14,4,0,4,
12,2,0,1,12,1,13,4,12,4,16,4,15,4,0,4,
12,2,0,1,12,1,19,4,17,4,15,4,14,4,13,4,
18,2,0,1,15,1,17,4,15,4,16,4,15,4,0,4,
0ffh,0ffh
}
for ( i = 0 ; i < SIZE; i++ )
{
sound(music[i]);
delay(DELAY);
}
nosound();
return 0;
} 应该要把数组里的值再做一下运算,不然这么低的频率应该听不到。。。
|
好久没碰Dos,手都生了,赶紧回来练练.嘿嘿 |
|
2010-5-10 17:14 |
|
|
523066680
银牌会员
SuperCleaner
积分 2362
发帖 1133
注册 2008-2-2
状态 离线
|
|
2010-5-11 22:31 |
|
|
netwinxp
高级用户
积分 741
发帖 366
注册 2007-7-25
状态 离线
|
『第
4 楼』:
LZ是MCS51单片机的程序,当然不能用基于X86的Turbo C,2楼的程序当然不行。不知LZ所用的编译器可是Keil C?由于C51系列兼容单片机程序存储器容量通常很小(AT89C51为4KB),而C语言的编译效率并不高,在C51使用C语言开发通常意味着要增加不少硬件成本,个人强力推荐在低端单片机开发直接使用汇编。
另,单片机开发要附上电路图(起码要简图),否则很难理解LZ的用意。
|
|
2010-5-30 16:56 |
|
|
cnch
中级用户
积分 326
发帖 70
注册 2003-1-10
状态 离线
|
『第
5 楼』:
LZ的这段程序只需稍加改动,应该可以嵌入到Keil C51里的,就象TC一样,它也支持嵌入汇编;另外,你可以学习一下Keil C51,如:象启动定时器0只需写入TR0=1即可,等等....不一一例举.
|
|
2010-9-5 15:06 |
|
|
cobras
新手上路
积分 14
发帖 5
注册 2007-1-19
状态 离线
|
『第
6 楼』:
c51没写过,不过x86还可以.作者的代码差table1和delay.
下面代码已经将table1补全.
short table1[] = { // frequency table
// 1 2 3 4 5 6 7
// 82,
// 87, 98, 110, 124, 131, 147, 165,
175, 196, 220, 247, 262, 294, 330,
349, 392, 440, 494, 523, 587, 659,
698, 784, 880, 988, 1047,
};
char table[] = { // music map, pairs of (tone, beat)
12,2,0,1,12,2,13,4,12,4,15,4,14,4,0,4,
12,2,0,1,12,1,13,4,12,4,16,4,15,4,0,4,
12,2,0,1,12,1,19,4,17,4,15,4,14,4,13,4,
18,2,0,1,18,1,17,4,15,4,16,4,15,4,0,4,
12,2,0,1,12,1,13,4,12,4,15,4,14,4,0,4,
12,2,0,1,12,1,13,4,12,4,16,4,15,4,0,4,
12,2,0,1,12,1,19,4,17,4,15,4,14,4,13,4,
18,2,0,1,15,1,17,4,15,4,16,4,15,4,0,4,-1,-1
};
void c51_delay(char beat)
{
int delay;
delay = beat * 187;
// sleep of delay ms
}
void c51_start_timer0()
{
// TR0 = 1;
}
void c51_stop_timer0()
{
// TR0 = 0;
}
void c51_make_sound(char tone)
{
union {
short word;
char byte[2];
}u;
u.word = table1[tone - 1];
// TH0 = u.byte[0];
// TL0 = u.byte[1];
}
void c51_happy_birthday(void)
{
int i;
c51_start_timer0();
for (i = 0; table[i] != -1; i += 2) {
if (table[i] == 0) {
c51_stop_timer0();
}else {
c51_make_sound(table[i]);
c51_start_timer0();
}
c51_delay(table[i + 1]);
}
c51_stop_timer0();
}
#include <windows.h>
void happy_birthday(void)
{
int i;
int delay;
for (i = 0; table[i] != -1; i += 2) {
delay = table[i + 1] * 187;
if (table[i] == 0) {
Sleep(delay);
}else {
Beep(table1[table[i] - 1], delay);
}
}
}
int main(void)
{
happy_birthday();
return 0;
}
|
|
2010-9-10 17:43 |
|
|