printf()函數(shù)可以直接向窗口格式化輸出,給我們調(diào)試程序帶來很大的方便,然而在Keil MDK環(huán)境下直接使用printf()函數(shù)就會陷入軟件中斷,導(dǎo)致死循環(huán)的產(chǎn)生;因而不能進(jìn)入main()函數(shù)。
單步調(diào)試程序時會發(fā)現(xiàn)程序指針一直在 SWI_Handler B SWI_Handler 打轉(zhuǎn);
解決方法:
工程選擇NXP的LPC21XX器件。
1.首先在Keil安裝目錄下面ARM——Startup找到Retarget.c文件將其復(fù)制到你的工程文件夾下面;并將其加入到工程中;
2. 在Keil安裝目錄下面ARM——Examples——Hello找到Serial.c文件將其復(fù)制到你的工程文件夾下面;并將其加入到工程中;
3.Serial.c代碼如下:
/******************************************************************************/
/* SERIAL.C: Low Level Serial Routines */
/******************************************************************************/
/* This file is part of the uVision/ARM development tools. */
/* Copyright (c) 2005-2006 Keil Software. All rights reserved. */
/* This software may only be used under the terms of a valid, current, */
/* end user licence from KEIL for a compatible version of KEIL software */
/* development tools. Nothing else gives you the right to use this software. */
/******************************************************************************/
#include
#define CR 0x0D
void init_serial (void) { /* Initialize Serial Interface */
PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1 */
U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U1DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock */
U1LCR = 0x03; /* DLAB = 0 */
}
/* implementation of putchar (also used by printf function to output data) */
int sendchar (int ch) { /* Write character to Serial Port */
if (ch == '\n') {
while (!(U1LSR & 0x20));
U1THR = CR; /* output CR */
}
while (!(U1LSR & 0x20));
return (U1THR = ch);
}
int getkey (void) { /* Read character from Serial Port */
while (!(U1LSR & 0x01));
return (U1RBR);
}
4.在頭文件中加入#include
5.在使用printf函數(shù)之前對串口進(jìn)行初始化;可以直接使用Serial.c中的 init_serial ()函數(shù)對串口初始化。
上一篇:單片機(jī)空閑與掉電模式
下一篇:Keil MDK破解過程詳解
推薦閱讀
史海拾趣
設(shè)計資源 培訓(xùn) 開發(fā)板 精華推薦
- 張人杰:車路協(xié)同趨勢下車載終端的技術(shù)和產(chǎn)品形態(tài)演進(jìn)
- 意法半導(dǎo)體和艾睿電子聯(lián)合發(fā)布符合小型發(fā)動機(jī)排放新規(guī)的電子燃油噴射參考設(shè)計方案
- 安費(fèi)諾推新型HSD電纜組件 適用于車載信息娛樂系統(tǒng)應(yīng)用
- 智能語音助手:大陸集團(tuán)開發(fā)車用自適應(yīng)聲控數(shù)字伴侶
- 韓國研究人員研發(fā)全球首個三進(jìn)制半導(dǎo)體 或影響AI/自動駕駛汽車等應(yīng)用
- 養(yǎng)成使用示波器的習(xí)慣將會幫你這個大忙
- 使用數(shù)字熒光示波器檢測電路故障并進(jìn)行調(diào)試
- 示波器ENOB對實(shí)際信號測試的影響
- 如何采用Microchip16位單片機(jī)設(shè)計音頻信號分析儀?
- realme X50 5G或會曝光 性能跑分媲美麒麟810