[蓝牙开发] CC2540新手学习之发送字符串

/***************************************/  
/*                CNPF                 */  
/*     CC2540 BlueTooth 4.0 学习例程   */  
/*例程名称:  串口通讯1                */  
/*建立时间:2018/05/25                 */  
/*描述:在串口调试助手上可以看到不停地 */
/*      收到CC2540发过来的:	   */
/*      Hello BlueTooth4.0             */
/***************************************/ 
#include<ioCC2540.h>
#include <string.h>

#define  uint  unsigned int
#define  uchar unsigned char

//定义LED的端口
#define LED1 P1_0
#define LED2 P1_1

//函数声明
void Delay_ms(uint);
void initUART(void);
void UartSend_String(char *Data,int len);

char Txdata[19]; //存放"Hello BlueTooth4.0\n"共19个字符串

/****************************************************************
    延时函数											
****************************************************************/
void Delay_ms(uint n)
{
  uint i,j;
  for(i=0;i<n;i++)
    for(j=0;j<1774;j++);
}

void IO_Init()
{
  P1DIR = 0x01; 	               //P1_0,P1_1 IO方向输出
  LED1 = 0;
}

/****************************************************************
   串口初始化函数				
****************************************************************/
void InitUART(void)
{ 
  PERCFG = 0x00;		      //位置1 P0口
  P0SEL = 0x0c;		              //P0_2,P0_3用作串口(外部设备功能)
  P2DIR &= ~0XC0;                     //P0优先作为UART0
  
  U0CSR |= 0x80;		      //设置为UART方式
  U0GCR |= 11;				       
  U0BAUD |= 216;		      //波特率设为115200
  UTX0IF = 0;                         //UART0 TX中断标志初始置位0
}
/****************************************************************
串口发送字符串函数			
****************************************************************/
void UartSend_String(char *Data,int len)
{
  int j;
  for(j=0;j<len;j++)
  {
    U0DBUF = *Data++;
    while(UTX0IF == 0);
    UTX0IF = 0;
  }
}
/****************************************************************
主函数						
****************************************************************/
void main(void)
{	
  CLKCONCMD &= ~0x40;               //设置系统时钟源为32MHZ晶振
  while(CLKCONSTA & 0x40);          //等待晶振稳定为32M
  CLKCONCMD &= ~0x47;               //设置系统主时钟频率为32MHZ   
  IO_Init();
  InitUART();
  strcpy(Txdata,"Hello BlueTooth4.0\n");     //将发送内容copy到Txdata;
  while(1)
   {
     UartSend_String(Txdata,sizeof("Hello BlueTooth4.0\n")); //串口发送数据
     Delay_ms(500);                    //延时
     LED1=!LED1;                       //标志发送状态
   }
}

猜你喜欢

转载自blog.csdn.net/weixin_41602920/article/details/80447584