基于CC2530 E18-MS1-PCB Zigbee DIY作品(三)

目录

写作前情

实现过程

引用出

效果如下


写作前情

由于没有买cc2530的仿真烧录器,所以在调试过程中,遇到bug或者程序运行情况无法确认,之前使用esp8266时,可以用串口来输出相关信息,所以想着如果cc2530可以用串口输出log,将更有利于分析问题,

实现过程

网上搜索相关例程,找到这篇比较简单易懂,故采用,大致思路是寄存器直接发送,比较可靠。不过因为只是用来打印log的,没有用来接收,所以只有发送,如果打印太多,比较占cpu。

原作者实现例程,需要用哪个串口,哪个引脚,直接加入宏就好,打印log推荐使用SendDebugString()函数,用法类似printf,可变参数打印,比较方便。波特率选择的是115200

serialprintf.c文件

serialprintf.c文件

#include "iocc2530.h"
#include "hal_types.h"
#include "string.h"
#include "uart_debug.h"
#include <stdarg.h>
#include "stdio.h"
#include "cfg.h"
 
 
 
/*choose which IO to use*/
void UART_Debug_Init(void)
{
#ifdef UARTDEBUG
#ifdef UART0_ALT0_DEBUG/*  Uart0 Alt0  TX:P0.3  RX:P0.2   */   
    PERCFG &= 0xFE ;
    P0SEL |= 0x0C;
    U0CSR = 0x80;
    U0GCR = 11;
    U0BAUD = 216;
    U0CSR |= 0x40;
#elif (defined UART0_ALT1_DEBUG)/*  Uart0 Alt1   TX:P1.5  RX:P1.4  */
    PERCFG |= 0x01;
    P1SEL |= 0x30;
    U0CSR = 0x80;
    U0GCR = 11;
    U0BAUD = 216;
    U0CSR |= 0x40;
    
#elif (defined UART1_ALT0_DEBUG) /*  Uart1 Alt0   TX:P0.4  RX:P0.5  */
    PERCFG &= 0xFD ;
    P0SEL |= 0x30;
    U1CSR = 0x80;
    U1GCR = 11;
    U1UCR = 0x80;
    U1UCR |= 0x02;
    U1BAUD = 216;
    U1CSR |= 0x40;  
#elif (defined UART1_ALT1_DEBUG) /*  Uart1 Alt1   TX:P1.6  RX:P1.7  */
    PERCFG |= 0x02 ;
    P1SEL |= 0xC0;
    U1CSR = 0x80;
    U1GCR = 11;
    U1UCR = 0x80;
    U1UCR |= 0x02;
    U1BAUD = 216;
    U1CSR |= 0x40;  
    
#endif
#endif
 
}
 
uint16  SendDebugWordData(uint8 worddata)
{
#ifdef UARTDEBUG
#if ((defined  UART0_ALT1_DEBUG) || (defined UART1_ALT0_DEBUG))
  while(U1CSR&0x01);    //等待UART空闲时发送数据
  U1DBUF = worddata;  
  
#elif ((defined UART0_ALT1_DEBUG) || (defined UART0_ALT0_DEBUG))
  while(U0CSR&0x01);    //等待UART空闲时发送数据
  U0DBUF = worddata;
#endif
  
#endif
  return 1;
}
 
 
 
 
uint16 SendDebugString(char* string,...)        //it's used like printf()
{
#ifdef UARTDEBUG
  va_list ap;
  char pstring[150];
  va_start(ap,string);
  vsprintf(pstring,string,ap);
  SendDebugData((uint8*)pstring,strlen(pstring));
  va_end(ap);
   
#endif
  return 1;
}
 
 
 
uint16 SendDebugIfo(char* string,uint16 data)
{
#ifdef UARTDEBUG
  uint8 stringbuf[100] = "";
  sprintf((char *)stringbuf,string,data);
  SendDebugData(stringbuf,strlen((char *)stringbuf)); 
 
#endif
  return 1;
}
 
 
 
uint16  SendDebugData(uint8 *buf,uint8 len)
{
 
#ifdef UARTDEBUG
#if ((defined UART1_ALT0_DEBUG) || (defined UART1_ALT1_DEBUG))
    while(len--)
    {
        while(U1CSR & 0x01 == 0x01);
        U1DBUF = *buf++;
    }   
#elif ((defined UART0_ALT1_DEBUG) || (defined UART0_ALT0_DEBUG))
    while(len--)
    {
        while(U0CSR & 0x01 == 0x01);
        U0DBUF = *buf++;
    }  
#endif
  
#endif
    return 1;
}
 

serialprintf.h文件

serialprintf.h文件

#ifndef SERIALPRINTF_H
#define SERIALPRINTF_H

extern void UART_Debug_Init(void);
extern uint16 SendDebugString(char* string,...);
extern uint16 SendDebugData(uint8 *buf,uint8 len);
extern uint16  SendDebugWordData(uint8 worddata);
extern uint16 SendDebugIfo(char* string,uint16 data);
//const float BH1750_CONV_FACTOR = 1.2;
#endif

在使用的地方加入如下,方便输出

#include "serialprintf.h"

#define APS_TAG                  "[CC2530]"
#define APS_LOG(fmt, args...)    SendDebugString(APS_TAG" %s %d: "fmt"\n", __FUNCTION__, __LINE__, ##args)

引用出

效果如下

猜你喜欢

转载自blog.csdn.net/yyandad/article/details/126178100
今日推荐