<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > keil 中如何調(diào)用其他文件的函數(shù)

          keil 中如何調(diào)用其他文件的函數(shù)

          作者: 時間:2016-11-10 來源:網(wǎng)絡(luò) 收藏
          建立一個和這個C文件同名的H文件,把這個C文件中的函數(shù)都包含到這個H文件中就ok!

          例如:有這樣一個serial.C文件:

          本文引用地址:http://www.ex-cimer.com/article/201611/317300.htm

          /********************************************************************************
          *程序名稱:serial.c
          *程序描述:單片機串口通信,所用單片機為Philiph的P89C52*2
          *編 制:LZS
          *備 注:編譯器keil C51 V7.06;時間2008.2.11;版本V1.0
          * 定時計數(shù)器T1作為波特率發(fā)生器
          * 波特率為4800;接收采用中斷方式;發(fā)送采用查詢方式
          *********************************************************************************/
          #include "reg51.h"

          /**********************************************************************
          *函數(shù)名稱:void Init_SerialComm(void)
          *函數(shù)描述:串口初始化
          *入口參數(shù):無
          *出口參數(shù):無
          *備 注:
          ***********************************************************************/
          void Init_SerialComm(void)
          {
          TMOD=0x20; //定時計數(shù)器T1工作在方式2,8bit自動重裝
          TH1=0xEB;
          TL1=0xEB; //波特率為1200;晶振頻率為9.6MHZ
          SCON=0x50; //串口工作在方式1
          PCON=0x80;
          EA=1;
          ES=1;
          TR1=1;
          }


          /*****************************************************************************
          *函數(shù)名稱:void UartSendChar(unsigned char ch)
          *函數(shù)描述:向串口發(fā)送一個字節(jié)的數(shù)據(jù)
          *入口參數(shù): unsigned char ch
          *出口參數(shù):無
          *備 注:無
          ******************************************************************************/
          void UartSendChar(unsigned char ch)
          {
          SBUF=ch;
          while(TI==0);
          TI=0;
          }


          /*********************************************************************************
          *函數(shù)名稱:void UartSendString(unsigned int *p,unsigned int string)
          *函數(shù)描述:向串口發(fā)送strlong個字節(jié)的數(shù)據(jù)
          *入口參數(shù):unsigned int *p 指向發(fā)送數(shù)據(jù)的指針,unsigned int strlong發(fā)送數(shù)據(jù)的字節(jié)數(shù)
          *出口參數(shù):無
          *備 注:無
          **********************************************************************************/
          void UartSendString(unsigned char *p,unsigned int strlong)
          {
          unsigned char tmp=0;
          for(tmp=0;tmp{
          UartSendChar(*(p+tmp));
          }

          }

          /*************************************************************************
          4.bcc異或校驗法(block check character)
          實現(xiàn)方法:很多基于串口的通訊都用這種既簡單又相當(dāng)準(zhǔn)確的方法。
          它就是把所有數(shù)據(jù)都和一個指定的初始值(通常是0)異或一次,最
          后的結(jié)果就是校驗值,通常把她附在通訊數(shù)據(jù)的最后一起發(fā)送出去。
          接收方收到數(shù)據(jù)后自己也計算一次異或和校驗值,如果和收到的校驗
          值一致就說明收到的數(shù)據(jù)是完整的。校驗值計算的代碼類似于:
          unsigned uCRC=0;//校驗初始值
          for(int i=0;i

          適用范圍:適用于大多數(shù)要求不高的數(shù)據(jù)通訊。

          應(yīng)用例子:ic卡接口通訊、很多單片機系統(tǒng)的串口通訊都使用。
          ********************************************************************************/

          它對應(yīng)的頭文件如下:

          #ifndef __serial_H__
          #define __serial_H__

          void Init_SerialComm(void);
          void UartSendChar(unsigned char ch);
          void UartSendString(unsigned char *p,unsigned int strlong);
          //static void UartIntrruptService(void);

          #endif

          這樣就可以用#include來包含調(diào)用這個C文件中的函數(shù)了哦!呵呵!for example:

          #include "serial.h"
          #include "reg52.h"
          #define uchar unsigned char
          #define uint unsigned int
          #define InBufferLong 20
          uchar counter;
          uchar trdata[InBufferLong];
          uchar checksum;
          uint i;
          bit Read_Flag=0;

          /***************************************************************************************
          *函數(shù)名稱:int main(void)
          *函數(shù)描述:主函數(shù)
          *入口參數(shù):無
          *出口參數(shù):無
          *備 注:無
          *****************************************************************************************/

          int main(void)
          {


          Init_SerialComm();
          while(1)
          {

          if(Read_Flag==1)
          {
          UartSendString(trdata,InBufferLong);
          Read_Flag=0;
          }
          }
          }

          /****************************************************************************************
          *函數(shù)名稱:void UartIntrruptService(void)
          *函數(shù)描述:串口中斷服務(wù)函數(shù)
          *入口參數(shù):無
          *出口參數(shù):無
          *備 注:無
          *******************************************************************************************/

          static void UartIntrruptService(void) interrupt 4 using 3
          {

          if(RI)
          {
          RI=0;
          i=SBUF;
          if(i>127)//當(dāng)接收到的數(shù)值大于127時
          {
          counter=0;
          trdata[counter]=i;//把接收到的數(shù)值保存在trdata[0]
          checksum=i-128; //和校驗等于接收到的數(shù)值-128
          }
          else
          {
          counter++;
          trdata[counter]=i;
          checksum^=i;
          if((counter==(InBufferLong-1))&&(1)) //接收滿并且checksum=0時
          {
          Read_Flag=1;
          counter=0;
          }
          }
          }


          }
          在這應(yīng)注意:1.被調(diào)用的C文件中只包含頭文件即可,每個函數(shù)都使用局部變量

          2.所有的全局變量都在主C文件中定義

          3.中斷要放到主C文件中



          關(guān)鍵詞: keil調(diào)

          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();