⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 井下人员定位系统的单片机程序数据采集并发送到上位机
💻 C
字号:
/*==============================================================================
 *
 * Copyright (C) 2004 Nordic Semiconductor
 *
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * WARRANTY OF ANY KIND.
 *
 * Author(s): Ole Saether
 *
 * DESCRIPTION
 *
 *   EEPROM reading and writing test program. To use test this program connect
 *   a nRF24E1 or nRF9E5 evaluation board to the serial port of your computer
 *   and start up a terminal emulator in 19200 baud. Hit 'r' in the terminal
 *   window to read a byte from the EEPROM and 'w' to write a byte. Please also
 *   read the description in the header of "eeprom.c".
 *
 *   NOTE: This source code is compatible with both nRF9E5 and nRF24E1. Please
 *         select the device you are using by uncommenting/commenting the
 *         appropriate line at the top of "nrfexx.h".
 *
 * COMPILER:
 *
 *   This program has been tested with Keil C51 V7.09
 *
 * $Revision: 3 $
 *
 *==============================================================================*/
#include "nrf24e1.h"
#include <ABSACC.h>

#define addr 0x01
static unsigned int n;
static unsigned char k;
static void Delay100us(volatile unsigned char n);
static unsigned char SpiReadWrite(unsigned char b);
static unsigned char mespage(void);
static void sendpage(void);

xdata unsigned char buf[900][4]  _at_  0x02c0;

struct RFConfig
{
    unsigned char n;
    unsigned char buf[15];
};

typedef struct RFConfig RFConfig;

RFConfig rconf =
{
    15,
    0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x09, 0x43, 0x6f, 0x05
};

void Init(void)
{
    P0_ALT = 0x06;                  // Select alternate functions on pins P0.1 and P0.2,  TXD RXD
    P0_DIR = 0x06;                  // P0.0, P0.3 is input(S1, S2), the rest output
    //P0 = 0xF0;                    // P0.7-P0.4 = 1 for OFF LED1-LED4
    //P1_DIR = 0x03;		   	    // P1.0, P1.3 is input(S3, S4),
    PWR_UP = 1;                     // Turn on Radio
    Delay100us(30);                 // Wait > 3ms
    SPICLK = 0;                     // Max SPI clock (XTAL/8)
    SPI_CTRL = 0x02;                // Connect internal SPI controller to Radi
    TL1 = 0xf7;                     //  serial communication ini
    TH1 = 0xf7;                     // 9600@16MHz (when T1M=1 and SMOD=0)
    //CKCON |= 0x10;                // T1M=1 (/4 timer clock)
    PCON = 0x80;                    // SMOD=1 (double baud rate)
    SCON = 0xf0;                    // Serial mode1, enable receiver
    TMOD = 0x20;                    // Timer1 8bit auto reload
    TR1 = 1;
    IE=0x90;
}

unsigned char SpiReadWrite(unsigned char b)
{
    EXIF &= ~0x20;                  // Clear SPI interrupt
    SPI_DATA = b;                   // Move byte to send to SPI data register
    while((EXIF & 0x20) == 0x00)    // Wait until SPI hs finished transmitting
        ;
    return SPI_DATA;
}

void Delay100us(volatile unsigned char n)
{
    unsigned char i;
    while(n--)
    for(i=0;i<35;i++)
            ;
}

void Rido(void)
{
    unsigned char b; 
    CS = 1;
    for(b=0;b<rconf.n;b++)
        {
           SpiReadWrite(rconf.buf[b]);
        }
    CS = 0;
}

void PutChar(char c)
{
    SBUF=c;
    while(!TI) ;
    TI = 0;  
}

char GetChar(void)
{
    while(!RI)
        ;
    RI = 0;
    return SBUF;
}

void main(void)
{
    unsigned char a;
	n=1;
    Init();
    Rido();
    while(1)
    {
     CE = 1;
	 while(DR1 == 0);
     for(a=0;a<4;a++)
         {
	    buf[n][a] = SpiReadWrite(0);
         }
     k=0x55;
	 n=n+1;
     k=0x00;
     CE = 0;
    }
}

void serial_0(void) interrupt 4
{
    EA=0;                                      //关闭中断
    TL1 = 0xf7;                                //  serial communication ini
    TH1 = 0xf7;                                // 9600@16MHz (when T1M=1 and SMOD=0)
    if(RI)
    {
	   RI=0;
       if(SBUF == addr)
	   {
       SM2=0;
	   SBUF=addr;
	   while(!TI); TI=0;
	   while(!RI); RI=0;
       PutChar(n);
       if(n!=1)          
	      {
          sendpage();     //发送数据包
		  }
	   SM2=1;
	   }
    }
	EA=1;
}

/*******************************************************************************************************
函数功能:将打包数据送出
********************************************************************************************************/
void sendpage(void)
{  
     unsigned int a;
	 unsigned char b;
     if(k==0x00)
     {              
     GetChar();
	 for(a=1;a<n;a++)
	    {
	         for(b=0;b<4;b++)
	          {
	            PutChar(buf[a][b]);
		      }
		 GetChar();
         }
      n=1;
	  }
	  else if(k==0x55)
	  {
      GetChar();
	  for(a=1;a<n;a++)
	    {
	         for(b=0;b<4;b++)
	          {
	            PutChar(buf[a][b]);
		      }
         GetChar();
        } 
       n=1;
	   }
          
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -