1.2 Variables and Arithmetic Expressions

The Fahrenheit to Celsius temperature convert is an epitome for beginner to know about variables and arithmetic expression
——————————————————-
/*Fahrenheit-Celsius convert #1 edition*/

# include <stdio.h>

main()
{
    int f,s;
    int low,high,step;
    low = 0;
    high = 200;
    step = 20;
    f = low;
    while(f <= high)
    {
        s = (5*(f -32))/9;
        f += step;
        printf("%d\t%d\n",f,s);
    }
    system("pause");
}
——————————————————-
for the system is lack of a pause command,I add a note in the end saying system("pause"); to pause the program when run,or it will be enclosed in a second.

 

Celsius temperature is not always in decimal but a floating number carrying with a dot and fractional part,thus this program need some modification to have a better display.
——————————————————-
/*Fahrenheit-Celsius convert #2 edition*/

# include <stdio.h>

main()
{
    int f;
    float s;
    int low,high,step;
    low = 0;
    high = 200;
    step = 20;
    f = low;
    while(f <= high)
    {
        s = (5.0/9.0)*(f -32);
        f += step;
        printf("%3d\t%6.2f\n",f,s);
    }
    system("pause");
}
——————————————————-
Bold part above consists of two float value 5.0 and 9.0,if modified as 5/9.0 or 5.0/9,it also run all the same,that because in an arithmetic expression,lower character type could be converted into higher one automatically,float is higher than int,both expression are converted into float one as 5.0/9.0.
But if modified as 5/9,it will crash.Because in this expression,both value are performed in int,and their arithmetic result should also be in an int.But 5/9 = 0.xxxxx is less than 1,and its fractional part is discarded,leaving 0 behind.

2009_0412 – 74LS90功能测试

74LS90功能比较多,不过我只选用了十进制的计数功能。
连线规则如下:
1.QA连INB,就可以构成十进制计数。这个是根据该TTL集成模块内部逻辑结构判断出来的,可以作为一种经验,不用多说。
2.两个置零端R0和两个置九端R9均接地,不让置零或置九。其实这四个引脚是用来测试整个集成电路是否正常的时候用的,方法很简单,R0(1) = R0(2) = 1同时R9(1) =  R9(2) = 0数码管显示0,接着把置九端和置零端的电平互换,数码管显示9,则集成电路完好。
3.QA QB QC QD分别接入数码管的D C B A端口,检测计数
4.双踪示波器A端接时钟源,B端分别接QA QB QC QD,用以对比分频和占空比的情况。

整个连线图如下:
 1

经过Multisim10的仿真,数码管从0向上计数到9,再跳回到0,不断反复。
示波器观察到各端口和时钟源的分频情况如下
QA对时钟源进行了2分频,占空比为50%
未命名 

QB对时钟源进行了四分频,但占空比有变换,至于为什么会这样,我也不知道,但是上星期在实验室搭线时却没发生这样的现象
 未命名2

QC QD都是十分频,占空比50%

在仿真和实际搭线的过程中发现,把示波器接上四个端口之后,整个计数频率会降低,数字跳变速度变慢。询问过实验室老师后发现,其实示波器内部有电阻,接到四个输出端后会改变整个电路的输出电阻,这样反馈再反馈作用下,整个计数频率会有变换

2009_0326 – 简单调用timer2

timer2是52单片机比51型多出来的一个计时器(计数器),不过timer2的功能远不止是计时和计数这两个,所以之前一直弄不出来就是因为没注意到这点,而只将timer2认为是一个简单的16位资源,错误认为其调用也和timer0及timer1相同,其实不然,查看过datasheet后发现光是timer2的control寄存器T2CON的八个位的安排就与前面两个计数器(计时器)不相同。所以,花了点时间研究datasheet,学习了一下这个寄存器的设置,今天就弄了出来。

// use timer2 in an easy way

#include <reg52.h>
sbit P1_0 = P1^0;
sfr T2MOD = 0xC9;

main()
{
    T2MOD = 0x00;
    T2CON = 0x08;
    TH2 = 0xff00; TL2 = 0xff00;
    RCAP2H = 0xff00; RCAP2L = 0xff00;
    EA = 1; ET2 = 1;
    TR2 = 1;

    while(1);
}

void change(void) interrupt 5
{
    TF2 = 0;
    P1_0 = !P1_0;
}

程序中要三处加重的地方我之前一直没弄出来,经过datasheet的学习才发现其中的奥妙:
1.sfr T2MOD = 0xC9;
T2MOD
timer2的模式控制寄存器T2MOD其实没有多大的设置意义,顶多就是对其中DCEN位进行设置。DCEN全称decreasement enable即“递减使能”,设置为1的时候允许在计时时递减计时,相反,设置为0的时候就是递增计时。而出了这一个位,T2MOD的其它位都没有什么利用的价值(至少datasheet上没有说明其他位的具体用法,只是说允许用户自己设定),这个DCEN位又可以独立设置电平,所以整个T2MOD在reg51.h文件中都没有独立设置其地址。所以,如果在程序中要设置整个T2MOD的话,要在主程序之前给T2MOD设置全局变量的入口地址,就是0xc9,否则在主程序中调用会被编译器提醒没有T2MOD这个变量。

2.RCAP2H = 0xff00; RCAP2L = 0xff00;
之前我一直认为timer2的自动重装也就是给TH2和TL2重装原来的数罢了,所以写了两行这两个特殊功能寄存器的赋值,当然编译出来是错的。datasheet中讲到,要给timer2的TH2及TL2重新装入数值,必须把待装的数值存入RCAP2H以及RCAP2L这两个特殊功能寄存器,其实从电路图上可以更清楚地看出这样设置的原因:(图中红圈内)
auto_reload_mode 

3.TF2 = 0;
这点也是timer2与同样具有自动重装功能的timer0 timer1工作方式2不同的地方。0和1timer的工作方式2在计时满时相应的TF位会溢出,但进入中断处理后硬件会自动将其清零(当然如果使用查询的方式就得软件清零),但timer2的这个TF2位即使进入中断,也要软件清零,否则无法进入下一个计数周期。

2009_0221 流水灯程序(c)语言

//以移位的方式让LED呈现流水形式
#include <reg52.h>
void delay()   //延时程序
{
    int n = 30000;
    while(–n);
}

void main()
{
    P1 = 0xfe;     //初始化P1口,实现P1.0亮其它灭
    while(1)
    {
        if(P1 == 0xfe)   //进入初始化状态
        {
            while(1)   
            {
                P1 <<= 1;   //左移内存中的数实现P1.1亮,在循环中逐渐点亮其他引脚的LED
                P1 |= 0x01;
                delay();
                if(P1 == 0x7f)break;  //左移最多能到到0x7f,此后要右移,才能实现“流水”的形式
            }
        }

        if(P1 == 0x7f) 
        {
            while(1)
            {
                P1 >>= 1;      //右移了
                P1 |= 0x80;
                delay();
                if(P1 == 0xfe)break;
            }
        }
    }
}

The C Programming Language Exercise1-8

Exercise1-8
Write a programto count blanks,tabs and newlines

1.1st try
———————————————————-
#include <stdio.h>
/*initialize the varibles*/
int c,b,t,n;

/*get start the counting*/
printf("Please enter a sentence for testing")
if ((c = getchar()) = ‘ ‘)
++b;
else if ((c = getchar()) = ‘t’)
++t;
else if ((c = getchar()) = ‘n’)
++n;

printf("blank(s) —> %d
tabs(s) —> %d
newline(s) —> %d",b,t,n);
———————————————————-
This time I’ve made a mistake.I don’t use the ‘while’ loop,instead I use if wondering it may run a loop in program,but the complier says there’s many errors in this program,I think it’s something to do with the ‘if’
So,now I should remember that:‘If’ can’t run a loop!
While,let’s go back to the loop itself,in order to get in into loop,we should use ‘while’,noting
while((c = getchar()) != EOF)
the key word ‘EOF’ make it possible to go over all the sentenc character as what the book teaches.
Hey!Poor guy!You forgot main() at the beginning of this program!!!!!!
What’s more,the printf("") shouldn’t be noted like this!

2. 2nd try
———————————————————-
#include <stdio.h>
/*initialize the varibles*/
main()
{
int c,b,t,n;

b = 0;
t = 0;
n = 0;

printf ("Please enter a sentence for testing");

/*start the counting*/
while ((c = getchar()) != EOF)
if (c = ‘ ‘)
++b;
else if (c = ‘t’)
++t;
else if (c = ‘n’)
++n;

/*output the result*/
printf("blank(s) %dntabs(s) %dnnewline(s) %d", b,t,n);
}
———————————————————-
This time I’ve get this program run,but,It really don’t run as what I expected,I hope to have this result that,after entering a sentence for test and then click on ‘ctrl + c’ and then an ‘Enter’,I expect it dispaly such words like:
blank(s) 3
tabs(s) 4
newline(s) 5
ect.
But the result is against it!It doesn’t display anything,and it really makes me very anoying