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

📄 bt_arm.c

📁 ARM7的通过蓝牙发送处理过压力信号显示。上位机上通过串口显示
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************
* 文件名:DATARET.C
* 功能:使用串口UART0接收上位机发送的数据,当接收到8个连续数据后,将接收计数值加一后输
*      出数码管D1显示,并将数据原封不动地发送回上位机。
* 说明:将跳线器JP5、JP3短接。
*      通讯波特率115200,8位数据位,1位停止位,无奇偶校验。
****************************************************************************/
#include  "config.h" 
#include  "BTh.h"
#include  "head1463.h"
//#define   SPI_CS	(1<<29)			/* P0.29 */
//#define   SPI_DATA	(1<<6)			/* P0.6 */
//#define   SPI_CLK	(1<<4)			/* P0.4 */

//#define   SPI_IOCON	(SPI_CS|SPI_DATA|SPI_CLK)	/* 定义SPI接口的I/O设置字 */

#define   BT_Reset	(1<<2)
#define	  LEDCON	0x02000000		/* P0.25引脚控制LED4,低电平点亮 */
/***************************************************************************
*1463的定义
****************************************************************************/
#define   MAX1463_CS    (1<<8 )
//#define   LED1CON       (1<<22)              /* P0.22口为LED1控制端 */
//#define   LED4CON       (1<<25)

#define   READ           0x01
#define   WRITE          0x00
/***************************************************************************
*
****************************************************************************/
uint8 Rst_F;
uint8 EvtFil_F;
uint8 WPgScan_F;

uint8 WInqScan_F;
uint8 WScanEn_F;
uint8 ConReq_F;
uint8 ConCplt_F;

uint8 GetPacket_F;//读取到数据包标志
//uint16 regvalue;
uint16 OPC_Insp=0;
uint16 Acl_Packet_handle;
uint8  ACL_hl,ACL_hh;
uint8  Ln_OneTemp;
uint8  Tdata[2];
uint8  ReadT;

#define TLOOP                                   60 
#define DEVICE_NUM                              ((unsigned char)0x00)/**/

#define HCI_Inquiry_Complete_Event				((unsigned char)0x01)
#define HCI_Command_Complete_Event				((unsigned char)0x0e)
#define HCI_Disconnect_Complete_Event			((unsigned char)0x05)
#define HCI_Mode_Change_Event				    ((unsigned char)0x14)


#define HCI_ACL_Packet                          ((unsigned char)0x02)
#define HCI_Event_Packet                        ((unsigned char)0x04)

#define SAMPLE_DATA                             ((unsigned char)0x0f)
#define COMMAND_DATA                            ((unsigned char)0x03)//COMMAND_DATA
#define TEMP_DATA                               ((unsigned char)0x05)

#define SAMP_PAUSE                              ((unsigned char)0xaa)
#define SAMP_RESUME							    ((unsigned char)0xbb)

#define DISCONNECTED                            ((unsigned char)0x55)
#define CONNECTED                               ((unsigned char)0x66) //DISCONNECTED 

uint8 HostCommandState;
uint8 SysState;
uint8 ConnectionState;//ConnectionState=DISCONNECTED;
/************************************************************************
*1463读取用到的变量
*************************************************************************/
uint8 highbyte,lowbyte;
uint16 PValue;
uint8 C1Data[2],C2Data[2];
uint8 Adc_OK=0;
uint8 trig_cnt=0;
uint8 Pause1463=0;


uint8 Mode_sta;
uint8 Mode_handle[2];
uint8 Mode_current;
uint8 Mode_Interval[2];

/************************************************************************/
/* 定义串口模式设置数据结构                                             */
/************************************************************************/
typedef  struct  UartMode
{  uint8 datab;         // 字长度,5/6/7/8
   uint8 stopb;         // 停止位,1/2
   uint8 parity;    	// 奇偶校验位,0为无校验,1奇数校验,2为偶数校验
}  UARTMODE;

BTCOM_buf  BTRxBuf,ADCDataBuf;

uint8 rcv_new;          // 接收新数据标志
uint8 rcv_cnt=0; 


void ARM_idleMode(void);
void HCI_SniffMode(void);
void HCI_Exit_Sniff_Mode(void);
/****************************************************************************
* 名称:DelayNS()
* 功能:长软件延时
* 入口参数:dly		延时参数,值越大,延时越久
* 出口参数:无
****************************************************************************/
void  DelayNS(uint32  dly)
{  
	uint32  i;

   	for(; dly>0; dly--) 
      for(i=0; i<50000; i++);
}

/************************************************************************/
/* 将数据送到发送缓冲区                                             */
/************************************************************************/
void Transfer_data(void)
{
   	unsigned char i;
   	for(i=0;i<17;i++)
    	data_packet.data_buffer[i]= buffer[i];

}
/****************************************************************************
* 名称:IRQ_UART0()
* 功能:串口UART0接收中断。
* 入口参数:无
* 出口参数:无
****************************************************************************/
void   __irq IRQ_UART0(void)
{ 
     
   	if( 0x04==(U0IIR&0x0F) ) rcv_new = 1;// 设置接收到新的数据标志
  
			
	BTRxBuf.buf[BTRxBuf.in]=U0RBR;
			
    BTRxBuf.cnt++;
            
    if (BTRxBuf.in>=(BT_BufSize-1))
    {
        BTRxBuf.in=0;
    }
                
    else
    {
        BTRxBuf.in++;
    }
            
        
    rcv_cnt++; 

    VICVectAddr = 0x00;              	// 中断处理结束
}               

/**************************************************************************
*
***************************************************************************/
void   __irq IRQ_Eint3(void)
{  
   	
  
   	while((EXTINT&0x08)!=0)  
   	{
   		EXTINT = 0x08;	 // 清除EINT3中断标志
   	}  
   	
   	 
   	Adc_OK=1;
   	trig_cnt++;
   
  	VICVectAddr = 0x00;   
}
/**************************************************************************
*函数ReadADCtoBuf() 读取AD转换结果到发送缓冲区
***************************************************************************/
uint8 ReadADCtoBuf(void)
{
	rwcode(0,0x0010,READ);
	SPISend(0xD4);
 	SPISend(0x28);
 	
 	SPISend(0x19);
 	
 	ReadDHR();
 	
 	ADCDataBuf.buf[ADCDataBuf.in]=lowbyte;
 	ADCDataBuf.in++;
 	ADCDataBuf.cnt++;
 	
 	ADCDataBuf.buf[ADCDataBuf.in]=highbyte;
 	ADCDataBuf.in++;
 	ADCDataBuf.cnt++;
 	
 	
 	rwcode(0,0x0013,READ);
	SPISend(0xD4);
 	SPISend(0x28);
 	
 	SPISend(0x19);
 	
 	ReadDHR();
 	
 	ADCDataBuf.buf[ADCDataBuf.in]=lowbyte;
 	ADCDataBuf.in++;
 	ADCDataBuf.cnt++;
 	
 	ADCDataBuf.buf[ADCDataBuf.in]=highbyte;
 	ADCDataBuf.cnt++;
 	
 	if (ADCDataBuf.in>=(BT_BufSize-1))
    {
        ADCDataBuf.in=0;
    }
                
    else
    {
        ADCDataBuf.in++;
    }

 	return(0);
	
}

/******************************************************************
*
******************************************************************/
/**************************************************************************
*函数ReadRAWtoBuf() 读取AD转换结果到发送缓冲区
***************************************************************************/
uint8 ReadRAWtoBuf(void)
{
	rwcode(0,0x0001,READ);
	SPISend(0xD4);
 	SPISend(0x28);
 	
 	SPISend(0x19);
 	
 	ReadDHR();
 	
 	ADCDataBuf.buf[ADCDataBuf.in]=lowbyte;
 	ADCDataBuf.in++;
 	ADCDataBuf.cnt++;
 	
 	ADCDataBuf.buf[ADCDataBuf.in]=highbyte;
 	ADCDataBuf.in++;
 	ADCDataBuf.cnt++;
 	
 	
 	rwcode(0,0x0004,READ);
	SPISend(0xD4);
 	SPISend(0x28);
 	
 	SPISend(0x19);
 	
 	ReadDHR();
 	
 	ADCDataBuf.buf[ADCDataBuf.in]=lowbyte;
 	ADCDataBuf.in++;
 	ADCDataBuf.cnt++;
 	
 	ADCDataBuf.buf[ADCDataBuf.in]=highbyte;
 	ADCDataBuf.cnt++;
 	
 	if (ADCDataBuf.in>=(BT_BufSize-1))
    {
        ADCDataBuf.in=0;
    }
                
    else
    {
        ADCDataBuf.in++;
    }

 	return(0);
	
}
/******************************************************************
*
*******************************************************************/
void ReadTemp(void)
{
	//rwcode(0,0x0007,READ);
	//SPISend(0xD4);
 	//SPISend(0x28);
 	
 	//SPISend(0x19);
 	
 	//ReadDHR();//Tdata
 	readport(0x07);
 	
 	Tdata[0]=lowbyte;  
 	
 	Tdata[1]=highbyte;
 	
 	ReadT=1;
}
/****************************************************************************
* 名称:SendByte()
* 功能:向串口UART0发送字节数据。
* 入口参数:data                要发送的数据
* 出口参数:无
****************************************************************************/
void  SendByte(uint8 data)
{  
	U0THR = data;                      	// 发送数据
	while( (U0LSR&0x20)==0 ); 
}
/****************************************************************************
*蓝牙HCI命令BTCOM_PutByte
****************************************************************************/
int HCI_BTReset(void)
{


    SendByte(0x01);
    SendByte(0x03);
    SendByte(0x0c);
    SendByte(0x00);

    return(1);
}

int HCI_BTSetEventFilter(void)
{


    SendByte(0x01);
    SendByte(0x05);
    SendByte(0x0c);
    SendByte(0x03);

    SendByte(0x02);//Connection Setup
    SendByte(0x00);//Allow Connections from all devices
    SendByte(0x02);//Do Auto accept the connection with role switch disabled.


        return(1);
}
int HCI_BTWritePageScanActivity(void)
{


    SendByte(0x01);
    SendByte(0x1c);
    SendByte(0x0c);
    SendByte(0x04);

    SendByte(0x40);//interval=0.625*0x40ms
    SendByte(0x00);//window=0.625*0x20ms;占空比50%
    SendByte(0x20);
    SendByte(0x00);


    return(1);
}
int HCI_BTWriteInquiryScanActivity(void)
{


    SendByte(0x01);
    SendByte(0x1e);
    SendByte(0x0c);
    SendByte(0x04);

    SendByte(0x40);//interval=0.625*0x40ms
    SendByte(0x00);//window=0.625*0x20ms;占空比50%
    SendByte(0x20);
    SendByte(0x00);


    return(1);
}

int HCI_BTWriteScanEnable(void)
{

    SendByte(0x01);
    SendByte(0x1a);
    SendByte(0x0c);
    SendByte(0x01);

    SendByte(0x03);//inquire scan enable;page scan enable


    return(1);
}


int HCI_AcceptConnectionReq()
{
	unsigned char i;
		
	SendByte(0x01);
    SendByte(0x09);
    SendByte(0x04);
    SendByte(0x07);

	for(i=0;i<6;i++) SendByte(connection_req.bd_addr.BD_ADDR[i]);
	SendByte(0x01);//Remain the slave

	return(1);
}


int HCI_WaitConnection(void)
{

	do
  	{
  		
  		GetEvent();
  		
 		if((ConReq_F==1)&(connection_req.Linktype==0x01))
  		HCI_AcceptConnectionReq();
  		OPC_Insp++;
  		
  	}while(ConCplt_F!=1);//等待连接建立
  		
  	return(1);
  		
} 
void HCI_SniffMode(void)
{
	unsigned char *phandle;
	
	SendByte(0x01);
	SendByte(0x03);
	SendByte(0x08);
	
	SendByte(0x0a);//0x14?
	
	phandle=creat_connection_cfm.handle;
	SendByte(*phandle++);
	SendByte(*phandle);//第一个数据包
	
	//SendByte(0x08);
	
	SendByte(0x00);
	SendByte(0x10);
	
	SendByte(0x00);
	SendByte(0x04);
	
	SendByte(0x00);
	SendByte(0x02);
	
	SendByte(0x00);
	SendByte(0x01);
	
	return;
}

void HCI_Exit_Sniff_Mode(void) 
{
	unsigned char *phandle;
	
	SendByte(0x01);
	SendByte(0x04);
	SendByte(0x08);
	SendByte(0x02);
	
	phandle=creat_connection_cfm.handle;
	SendByte(*phandle++);
	SendByte(*phandle);//第一个数据包
	
	return;
}
int ProcessCommadEvent(void)//
{
  	uint8 n;
 	// unsigned char eventlength;
 	// unsigned char eventcode;
 	// unsigned char i; 
  	n=0; 
  	do
  	{
  		HCI_BTReset();
  		DelayNS(30);
  		GetEvent();
  		n++;
  	}while(Rst_F!=1&n<3);
	  
  	n=0;
  	do
  	{
  		HCI_BTSetEventFilter();
  		DelayNS(30);
  		GetEvent();
  		n++;
  	}while(EvtFil_F!=1&n<3);
	  
  	n=0;
  	do
  	{
  		HCI_BTWritePageScanActivity();
  		DelayNS(30);
  		GetEvent();
  		n++;
  	}while(WPgScan_F!=1&n<3);
  
  	n=0;
 	do
  	{
  		HCI_BTWriteInquiryScanActivity();
  		DelayNS(30);
  		GetEvent();
  		n++;

⌨️ 快捷键说明

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