语法: i32=make32( var1, var2, var3, var4);

MAKE32( )

语法:   i32=make32( var1, var2, var3, var4);

参数:   var1, var2, var3和var4是一个8位或16位整数; var2, var3和var4是可选项;

返回值:  返回值是一个32位整数;

功能:   该函数将任意结合的8位和16位数制造成32位的数据;注意:参数的个数可能是1~4中的任意值, var1为最高8位(即msb);若提供的总位数比32少,则0被添加到msb位;

有效性: 适合所有的CPU设备.

要求:   没有;

例子:   int32  x;

        int y;

        long z;

        x=make32(1, 2, 3, 4);  //x现在是0x01020304;

        y=0x12;

        z=0x4321;

        x=make32(y, z);      // x现在是0x00124321;

        x=make32(y, y, z);    // x现在是0x12124321;

例子文件: ex_freqc.c; ex_freqc.c如下:

#include <16F877.h>        //包含头文件

#fuses HS, NOWDT, NOLVP  //HS:高速晶振/谐振器, NOWDT:不使用WDT

                          // NOPROTECT:程序存储器代码不保护

#use delay(clock=20000000)   //one instruction=0.2us

                          //使能内置函数的功能:delay_ms()和delay_us()

                          //#USE DELAY()必须在#use rs232()使用之前出现.

#use rs232(baud=9600, xmit=PIN_c6, rcv=PIN_c7)

//使用波特率为9600,

//发送脚为PIN_ C6

//接收脚为PIN_ C7

//使能内置函数:GETC,PUTCPRINTF, kbhit();

#bit t1_overflow=0x0C.0              //使用#bit设置timer1溢出标志位;

// #bit t1_overflow=0xF9E.0  (PIC18, Reminder)

void main() {

   int cycles8, cycles;

   int32 freq;       //声明32位整型变量

   long freqc_high;  //声明长整型全局变量freqc_high

   long freqc_low;  //声明长整型全局变量freqc_low

   while (TRUE) {

      cycles8=0;

      cycles=0;

      freqc_high=0;

      t1_overflow=0;   //timer1溢出标志位清0;

      set_timer1(0);    //设置timer1的初始计数值为0;

setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);

//初始化定时器1,

                                        //时钟源为外部时钟

                                  //不用分频器,即每来时钟到来,T1计数一次

/* ___ wait one second ___ 1秒开始 */

      while (cycles!=0xFF) { //true=3, false=4

       cycles8=0; //1 cycle

       //start inner loop

       while (cycles8!=0xFF) {  //true=3, false=4

         if (t1_overflow)       //true=2,false=3             //----|

            {t1_overflow=0;freqc_high++;}     //6 cycles   //    |

         else                                         //    |-- 8 cycles

            {delay_cycles(5);}                           //----|

         delay_cycles(62); //x

         cycles8++; //1

 ///2 cycles to jump to top

 //math: end inner loop

 //math: total inner loop=((3+8+x+1+2)*255 + 4)*255

 //math: if x=62.87781 then inner loops takes 5mil instructions

 //math: if x=62 then inner loop takes 4942920, have to fill 57080 cycles

  }

 delay_cycles(216); //y

 cycles++;   ///1 cycle

 ///2 cylces to jump to top

 //math: outer=(3+1+y+1+2)*255+4=57080

 //math: y=(57080-4)/255)-(3+1+0+0+1+2)

 //math: if y=216.827450980392156862745098039216 then outer loop cylces is 57080

 //math: if y=216 then outer loop cycles is off by 211 cycles.  z=211

}

      delay_cycles(211); //z

/* ___ end waiting 1 second ___ 1秒结束*/

      setup_timer_1(T1_DISABLED); //不使能timer1

      if (t1_overflow)  //check one last time for overflow

          freqc_high++;

      freqc_low=get_timer1(); //get timer1 value as the least sign. 16bits of freq counter

      freq=make32(freqc_high,freqc_low); //use new make32 function to join lsb and msb

      printf("%LU Hz\r\n",freq); //and print frequency

   }

}

//上面的程序,采用定时器1计算频率,上面的方法太笨;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值