Archive for ‘边走编程’ Category

April 29, 2009

功能描述

功能很简单,程序更简单:

简单的蜂鸣器实验程序:本程序通过在P1.1输出一个音频范围的方波,驱动实验板上的蜂鸣器发出蜂鸣声,其中包含有一个定时模块,作用是使输出的方波频率在人耳朵听觉能力之内的20KHZ以下,如果没有这个延时程序的话,输出的频率将大大超出人耳朵的听觉能力,我们将不能听到声音。更改定时常数,可以改变输出频率,也就可以调整蜂鸣器的音调。

Tags: ,. 396 views

功能描述

由p1口外接按键开关,p3.3接蜂鸣器组成。

1
2
3
4
5
6
7
8
9
 	org 	8000h		;定位
	ljmp 	start
	org 	8100h
start:
	mov 	sp, #60h
	mov	tmod, #10h	;置T1为方式1
;	mov	tl1, #010h
;	mov	th1, #010h	;设初值,定时为50ms
	setb	tr1		;启动定时器T1
Tags: ,. 235 views

功能描述

正常情况下(p3.2为高电平), p3.3的电平以一定的频率连续翻转,当p3.2为低电平时,触发int0外部中断,进入中断服务程序,另p3.3保持低电平,并将p1端口加1。p3.2外接一个按键开关时,中断服务程序有防抖动的功能,这时通过一定的延时(通常为10ms–20ms)来实现的。

程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 	org 	8000h		;定位
	ljmp 	start
	org 	8003h		;中断向量
	ljmp	int_0
	org 	8100h
start:
	mov 	sp, #60h
	setb	ex0		;开int0中断
	setb 	ea		;开总中断
	mov	tcon, #00h	;低电平触发
	mov	r3, #00h
	mov	a, r3
	cpl	a
loop3:				;死循环
	mov	p1, a
	cpl	p3.3		;翻转
	lcall	DELAY		;延时
	sjmp	loop3
 
int_0:				;中断服务子程序
	push	psw
	lcall	DELAY		;防抖动
	setb	p3.3
	inc	r3
	mov	a, r3
	cpl	a
	mov	p1, a
	jnb	p3.2, $		;查询,防止多次中断
	lcall	DELAY		;还是防抖动
	pop	psw
	reti
 
DELAY:  PUSH 06H
	PUSH 07H
	MOV R7, #70H
LABEL1:
	MOV R6, #70H
LABEL2:
	DJNZ R6, $
	DJNZ R7, LABEL1
	POP 07H
	POP 06H
	RET
	END
Tags: ,. 232 views

89C51单片机,每计时一秒将P1端口翻转一次,晶振频率12MHz,中断方式实现。这里另外一个查询方式的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 	org 	8000h		;定位
	ljmp 	start
	org 	801bh		;中断向量
	ljmp	int_t1
	org 	8100h
start:
	mov 	sp, #60h
	mov	tmod, #10h	;置T1为方式1
	mov	tl1, 0fch
	mov	th1, #4bh	;设初值,定时为50ms
	mov	r1, #20		;中断次数,以使定时为1ms
	setb	tr1		;启动定时器T1
	clr	a		;累加器清零
	setb	et1		;T1中断允许
	setb	ea		;开总外部中断标志
	sjmp	$		;等待中断
 
 
int_t1:				;中断服务子程序
	push	psw		;保存程序状态字
	mov 	tl1, #0fch	;重装计数器
	mov	th1, #4bh
	djnz	r1, exit	;1s时间未到,返回主程序,等待下一次中断
	mov	r1, #20		;1s时间到,重装r1
	cpl	a		;a取反
	mov	p1, a		;输出a
exit:	pop	psw		;程序状态字出栈
	reti
 
DELAY:  PUSH 06H
	PUSH 07H
	MOV R7, #70H
LABEL1:
	MOV R6, #70H
LABEL2:
	DJNZ R6, $
	DJNZ R7, LABEL1
	POP 07H
	POP 06H
	RET
	END
Tags: ,. 68 views

89C51单片机,每计时一秒将P1端口翻转一次,晶振频率12MHz,这里另外一个中断方式的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 	org 	8000h		;定位
	ljmp 	start
	org 	8100h
start:
	mov 	sp, #60h
	mov	r1, #20	
	clr	a
	mov	tmod, #10h	;置T1为方式1
loop:	mov	tl1, 0fch
	mov	th1, #4bh	;设初值,定时为50ms
	setb	tr1		;启动定时器T1
	jnb	tf1, $		;查询TF1是否溢出
 
	clr	tf1		;清除溢出标志
	djnz	r1, loop	;1s时间未到,重装计数器
	mov	r1, #20		;1s时间到,重装r1
	cpl	a		;a取反
	mov	p1, a		;输出a
	ljmp	loop
 
	END
Tags: ,. 11 views
April 28, 2009

POJ 的一道题,算法很简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <string>
#include <iostream>
using namespace std;
 
int main()
{
    int n;
    char b2h[17] = "0123456789ABCDEF";
    int flag[4] = {1,2,4,8};
    cin>>n;
    while (n--)
    {
        string s;cin>>s;
        int len = s.length();
        int mod = len%4;
        int num = 0;
        for (int i=mod;i>0;i--)
        {
            if (s[mod-i]=='1')
            {
                num += flag[i-1];
            }
        }
        if (mod)
            cout<<b2h[num];
        for (int i=mod;i<len;i+=4)
        {
            num = 0;
            for (int j=0;j<4;j++)
            {
                if (s[i+j]=='1')
                {
                    num += flag[3-j];
                }
            }
            cout<<b2h[num];
        }
        cout<<endl;
    }
    return 0;
}
Tags: . 34 views
April 27, 2009

运行期(runtime)判断
下面的程序可以在运行期判断 endianess:

int IsBigEndian (void)
{
        static const int v =1;
        return *(char*)&v?0:1;
}
Tags: ,. 19 views

Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.

Tags: . 26 views
April 25, 2009

Introduction

A good way to get into an argument with a computer programmer is to attempt to explain why the language they are using is not as good as the one you are using. Most of the programmers I know are positively religious over their Operating System (see my other article), their development language and finally their text editor.

Tags: . 10 views
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool fun(unsigned n)
{
    assert(n >0); //乘方不为正
    if (!(n & (n-1))) //n的二进制只有一个1,即n==2^m;
    {
        int offset = 0;//"1"位的偏移量
        while (n != 1)
        {
            n>>=1;
            offset++;
         }
         if (offset && !(offset&1))//偏移量为偶数,即 n == 2^(2*m),亦即n == 4^m;
        {
            return true;
         }
      }
      return false;
}
Tags: . 7 views