精品剧情v国产在线观看 - 亚洲人成自拍网站在线观看 - 99久久无码国产孕妇精品 - 欧美XXXX性欧美XX000 - 国产精品视频色尤物yw

固話:021-31007158 熱線:15221679567 客服 :
手機(jī)  |   地圖  |   RSS

單片機(jī)

STM8串口打印調(diào)試信息

分類:?jiǎn)纹瑱C(jī) 發(fā)布:2018-05-20 10:27:33 瀏覽:721次 Tag:

API函數(shù)


static void uart1SendMsg(char msg[], uint16_t length)

{

       uint16_t i;


    for (i =0 ;i < length;i++)

    {

        UART1_SendData8(msg[i]);

        while(UART1_GetFlagStatus(UART1_FLAG_TC) == RESET);

    }

}

"`

void myPrintfAscii(char *msg)

{

    while((*msg) != '\0')

    {

        uart1SendMsg(msg, 1);

        msg++;

    }

}


void myPrintfInt8(uint8_t msg[], int length)

{

    char int2Ascii[64];

    uint16_t i, m, intLength;

    int n;

    long temp;


    for (i = 0;i < length;i++)

    {

        m = 0;

        temp = msg[i];

        while(m <= 64)

        {

            int2Ascii[m++] = temp % 10;

            temp /= 10;

            if (0 == temp)

            {

                break;

            }

        }

        intLength = m;

        for (n = intLength - 1;n >= 0;n--)

        {

            int2Ascii[n] += '0';

            uart1SendMsg(int2Ascii + n, 1);

        }

        myPrintfAscii(" ");

    }

}


void myPrintfInt16(uint16_t msg[], int length)

{

    char int2Ascii[64];

    uint16_t i, m, intLength;

    int n;

    long temp;


    for (i = 0;i < length;i++)

    {

        m = 0;

        temp = msg[i];

        while(m <= 64)

        {

            int2Ascii[m++] = temp % 10;

            temp /= 10;

            if (0 == temp)

            {

                break;

            }

        }

        intLength = m;

        for (n = intLength - 1;n >= 0;n--)

        {

            int2Ascii[n] += '0';

            uart1SendMsg(int2Ascii + n, 1);

        }


        myPrintfAscii("\r\n");

    }

}


void myPrintfInt32(uint32_t msg[], int length)

{

    char int2Ascii[64];

    uint16_t i, m, intLength;

    int n;

    long temp;


    for (i = 0;i < length;i++)

    {

        m = 0;

        temp = msg[i];

        while(m <= 64)

        {

            int2Ascii[m++] = temp % 10;

            temp /= 10;

            if (0 == temp)

            {

                break;

            }

        }

        intLength = m;

        for (n = intLength - 1;n >= 0;n--)

        {

            int2Ascii[n] += '0';

            uart1SendMsg(int2Ascii + n, 1);

        }

        myPrintfAscii(" ");

    }

}


上述方法相對(duì)笨一點(diǎn),而且無法打印負(fù)數(shù),浮點(diǎn)數(shù)。下面直接使用printf標(biāo)準(zhǔn)輸入輸出函數(shù),將調(diào)試信息串口打印出來。


printf 函數(shù)重映射


使用printf函數(shù)將調(diào)試信息用串口打印出來,首先在有printf函數(shù)的C文件下包含 #include ,其次將printf函數(shù)重定向串口輸出1, 參考網(wǎng)址1。貼代碼:


重映射代碼,myPrintf.c


#include "stm8s.h"

#include

/*

 由于不同的編譯器 putcha 和 getchar 形參和返回值略有不同。

 因此此處采用宏定義的方式區(qū)別。

 _RAISONANCE_ 和 _COSMIC_ 這2個(gè)宏是由編譯器自動(dòng)添加的預(yù)編譯宏

*/

#ifdef _RAISONANCE_

 #define PUTCHAR_PROTOTYPE int putchar (char c)

 #define GETCHAR_PROTOTYPE int getchar (void)

#elif defined (_COSMIC_)

 #define PUTCHAR_PROTOTYPE char putchar (char c)

 #define GETCHAR_PROTOTYPE char getchar (void)

#else /* _IAR_ */

 #define PUTCHAR_PROTOTYPE int putchar (int c)

 #define GETCHAR_PROTOTYPE int getchar (void)

#endif /* _RAISONANCE_ */



PUTCHAR_PROTOTYPE

{

 /* 發(fā)送一個(gè)字符 c 到UART1 */

 UART1_SendData8(c);

 /* 等待發(fā)送完畢 */

 while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);

 return (c);

}



GETCHAR_PROTOTYPE

{

 #ifdef _COSMIC_

  char c = 0;

 #else

  int c = 0;

 #endif

 /* 等待新數(shù)據(jù)到達(dá)  */

 while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET);

 /* 讀取數(shù)據(jù)寄存器 */

 c = UART1_ReceiveData8();

 return (c);

}


main.c 主要輸出部分:


#include

void main(void)

{       

    myUart1Init (115200);


    printf ("STM8S Start.\r\n");

    while(1)

    {   

    }


}


相關(guān)文章
在線留言聯(lián)系我們
電話:021-31007158
傳真:021-31007158
聯(lián)系人:王錦
24H熱線:15221679567
聯(lián)系QQ: 1196661789
郵箱:1196661789@qq.com
網(wǎng)址:www.szchanghongsheng.com.cn
QQ在線咨詢
銷售電話:
15221679567
021-31007158
1196661789