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,PUTC和PRINTF, 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计算频率,上面的方法太笨;