|
|
用STLINK來(lái)調(diào)試可以真實(shí)訪問(wèn)查看寄存器的變化,下面就以串口輸出內(nèi)容到上位機(jī)顯示為例,講述一下如何用STLINK來(lái)調(diào)試程序。
1.安裝STLINK驅(qū)動(dòng)程序。因本例程需要用到串口將內(nèi)容輸出到上位機(jī),我的板子采用芯片CH340將USB轉(zhuǎn)串口,所以要安裝CH340 驅(qū)動(dòng)。
2. 建立工程,將如圖文件包含到工程里。
3.Options for Target 選項(xiàng)卡在 Debug 欄選擇仿真工為具 Use:ST-Link Debugger,如圖依次 成4個(gè)步驟
然后點(diǎn)擊Settting進(jìn)行STLINK參數(shù)設(shè)置,我選用SW模式,JTAG占用比較多的I/O,容易影響其他外設(shè)的使用,速率不要選擇太大,否則會(huì)出現(xiàn)問(wèn)題。
接下來(lái)我們還需要在 Utilities 選項(xiàng)卡里面設(shè)置下載時(shí)的目標(biāo)編程器
我們直接勾選 Use Debug Driver,即和調(diào)試一樣,選擇 ST LINK 來(lái)給目標(biāo)器件的 FLASH 編程,然后點(diǎn)擊 Settings 按鈕,進(jìn)入 FLASH 燒錄設(shè)置
設(shè)置好后編寫程序,將硬件連接,下載后就點(diǎn)擊Debug后就可以實(shí)時(shí)跟蹤程序,很方便的找到程序的bug。
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_10MHz;
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- void USART_Configuration(void)
- {
- USART_InitTypeDef USART_InitStruct;
- USART_InitStruct.USART_BaudRate=9600;
- USART_InitStruct.USART_WordLength=USART_WordLength_8b ;
- USART_InitStruct.USART_StopBits=USART_StopBits_1;
- USART_InitStruct.USART_Parity=USART_Parity_No;
- USART_InitStruct.USART_Mode=USART_Mode_Tx |USART_Mode_Rx;
- USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
-
- USART_Init(USART1,&USART_InitStruct);
-
- USART_Cmd(USART1, ENABLE);
-
- }
- void RCC_Configuration(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
-
- }
- void Delay_Ms(u16 time)
- u16 i,j;
- for(i=0;i<time;i++)
- for(j=10000;j>0;j--);
- }
- int main(void)
- {
- u8 i,data;
- RCC_Configuration();
- GPIO_Configuration();
- USART_Configuration();
- data='A';
- for(i=0;i<30;i++)
- {
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
- USART_SendData(USART1,data);
- data++;
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
- }
-
- return(0);
- }
復(fù)制代碼
|
-
評(píng)分
-
查看全部評(píng)分
|