標題: stm8的C語言編程-基本程序與啟動代碼分析 [打印本頁]
作者: 13373092669 時間: 2016-12-29 17:43
標題: stm8的C語言編程-基本程序與啟動代碼分析
stm8的C語言編程(1)--基本程序與啟動代碼分析
現在幾乎所有的單片機都能用C語言編程了,采用C語言編程確實能帶來很多好處,至少可讀性比匯編語言強多了。
在STM8的開發環境中,可以通過新建一個工程,自動地建立起一個C語言的框架,生成后開發環境會自動生成2個C語言的程序,一個是main.c,另一個是stm8_interrupt_vector.c。main.c中就是一個空的main()函數,如下所示:
/* MAIN.C file
*
* Copyright (c)2002-2005 STMicroelectronics
*/
main()
{
while (1);
}
而在stm8_interrupt_vector.c中,就是聲明了對應該芯片的中斷向量,如下所示:
/* BASICINTERRUPT VECTOR TABLE FOR STM8 devices
* Copyright (c) 2007 STMicroelectronics
*/
typedef void @far(*interrupt_handler_t)(void);
struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};
@far @interrupt voidNonHandledInterrupt (void)
{
/* in order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction
*/
return;
}
extern void_stext(); /* startup routine */
struct interrupt_vectorconst _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap */
{0x82, NonHandledInterrupt}, /* irq0 */
{0x82, NonHandledInterrupt}, /* irq1 */
{0x82, NonHandledInterrupt}, /* irq2 */
{0x82, NonHandledInterrupt}, /* irq3 */
{0x82, NonHandledInterrupt}, /* irq4 */
{0x82, NonHandledInterrupt}, /* irq5 */
{0x82, NonHandledInterrupt}, /* irq6 */
{0x82, NonHandledInterrupt}, /* irq7 */
{0x82, NonHandledInterrupt}, /* irq8 */
{0x82, NonHandledInterrupt}, /* irq9 */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, NonHandledInterrupt}, /* irq11 */
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, NonHandledInterrupt}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, NonHandledInterrupt}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
在stm8_interrupt_vector.c中,除了定義了中斷向量表外,還定義了空的中斷服務程序,用于那些不用的中斷。當然在自動建立時,所有的中斷服務都是空的,因此,除了第1個復位的向量外,其它都指向那個空的中斷服務函數。
生成框架后,就可以用Build菜單下的Rebuild All對項目進行編譯和連接,生成所需的目標文件,然后就可以加載到STM8的芯片中,這里由于main()函數是一個空函數,因此沒有任何實際的功能。不過我們可以把這個框架對應的匯編代碼反出來,看看C語言生成的代碼,這樣可以更深入地了解C語言編程的特點。
生成的代碼包括4個部分,如圖1、圖2、圖3、圖4所示。
0.jpg (337.92 KB, 下載次數: 110)
下載附件
2016-12-30 01:48 上傳
圖1
1.jpg (36.48 KB, 下載次數: 120)
下載附件
2016-12-30 01:48 上傳
圖2
2.jpg (250.22 KB, 下載次數: 118)
下載附件
2016-12-30 01:48 上傳
圖3
3.jpg (25.9 KB, 下載次數: 109)
下載附件
2016-12-30 01:48 上傳
圖4
圖1顯示的是從內存地址8000H開始的中斷向量表,中斷向量表中的第1行82008083H為復位后單片機運行的第1跳指令的地址。從表中可以看出,單片機復位后,將從8083H開始運行。其它行的中斷向量都指向同一個位置的中斷服務程序80D0H。
圖2顯示的是3個字節,前2個字節8083H為復位后的第1條指令的地址,第3個字節是一個常量0,后面的啟動代碼要用到。
圖3顯示的是啟動代碼,啟動代碼中除了初始化堆棧指針外,就是初始化RAM單元。由于目前是一個空的框架,因此在初始化完堆棧指針(設置成0FFFH)后,由于8082H單元的內容為0,因此程序就跳到了80B1H,此處是一個循環,將RAM單元從0到5初始化成0。然后由于寄存器X設置成0100H,就直接通過CALL main進入C的main()函數。
圖4顯示的是main()函數和中斷服務函數,main()函數對應的代碼就是一個無限的循環,而中斷服務函數就一條指令,即中斷返回指令。
通過分析,可以看出用C語言編程時,比匯編語言編程時,就是多出了一段啟動代碼。
作者: 黑lllsss 時間: 2016-12-31 23:15
6686666
歡迎光臨 (http://www.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |