|
本代碼親測無誤。
1.在PC串口工具發送給stm32 20字節的數據.
2.stm32收到20字節后,回發給PC串口工具,并閃燈。
0.png (33.61 KB, 下載次數: 68)
下載附件
2016-4-24 04:03 上傳
主程序文件:
- /* MAIN.C file
- *
- * stm32-project
- */
- /***************************************************************
- / 深圳EU電子出品-版權所有-翻版必究
- / EU-熱愛嵌入式開發
- ***************************************************************/
- //類型轉換
- typedef unsigned char bool;
- typedef unsigned char u8;
- typedef unsigned short u16;
- #define True 1
- #define False 0
- //SET BIT. Example: a |= SETBIT0
- enum
- {
- SETBIT0 = 0x0001, SETBIT1 = 0x0002, SETBIT2 = 0x0004, SETBIT3 = 0x0008,
- SETBIT4 = 0x0010, SETBIT5 = 0x0020, SETBIT6 = 0x0040, SETBIT7 = 0x0080,
- SETBIT8 = 0x0100, SETBIT9 = 0x0200, SETBIT10 = 0x0400, SETBIT11 = 0x0800,
- SETBIT12 = 0x1000, SETBIT13 = 0x2000, SETBIT14 = 0x4000, SETBIT15 = 0x8000
- };
- //CLR BIT. Example: a &= CLRBIT0
- enum
- {
- CLRBIT0 = 0xFFFE, CLRBIT1 = 0xFFFD, CLRBIT2 = 0xFFFB, CLRBIT3 = 0xFFF7,
- CLRBIT4 = 0xFFEF, CLRBIT5 = 0xFFDF, CLRBIT6 = 0xFFBF, CLRBIT7 = 0xFF7F,
- CLRBIT8 = 0xFEFF, CLRBIT9 = 0xFDFF, CLRBIT10 = 0xFBFF, CLRBIT11 = 0xF7FF,
- CLRBIT12 = 0xEFFF, CLRBIT13 = 0xDFFF, CLRBIT14 = 0xBFFF, CLRBIT15 = 0x7FFF
- };
- //CHOSE BIT. Example: a = b&CHSBIT0
- enum
- {
- CHSBIT0 = 0x0001, CHSBIT1 = 0x0002, CHSBIT2 = 0x0004, CHSBIT3 = 0x0008,
- CHSBIT4 = 0x0010, CHSBIT5 = 0x0020, CHSBIT6 = 0x0040, CHSBIT7 = 0x0080,
- CHSBIT8 = 0x0100, CHSBIT9 = 0x0200, CHSBIT10 = 0x0400, CHSBIT11 = 0x0800,
- CHSBIT12 = 0x1000, CHSBIT13 = 0x2000, CHSBIT14 = 0x4000, CHSBIT15 = 0x8000
- };
- /* INCLUDES */
- //MCU
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_tim.h"
- #include "util.h"
- #include "uart1.h"
- #include <stdio.h>
- #include <string.h>
- #if 0
- #ifdef __GNUC__
- /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
- set to 'Yes') calls __io_putchar() */
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif /* __GNUC__ */
- PUTCHAR_PROTOTYPE
- {
- /* Place your implementation of fputc here */
- /* e.g. write a character to the USART */
- USART_SendData(USART1, (uint8_t) ch);
- /* Loop until the end of transmission */
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
- {}
- return ch;
- }
- #endif
- //設置PB10狀態 STM32-MINI開發板的LED管腳
- void SetPB10(bool sta)
- {
- static bool StartFlag = True;
- if(StartFlag)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- StartFlag = False;
- }
- if(!sta)
- GPIO_SetBits(GPIOB, GPIO_Pin_10);
- else
- GPIO_ResetBits(GPIOB, GPIO_Pin_10);
- }
- //定時器配置并開啟 使用定時器3
- void TimeON(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- //開啟定時器外設時鐘
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- //配置定時器參數
- TIM_DeInit(TIM3);
- TIM_TimeBaseStructure.TIM_Period = 10000; //10ms定時
- TIM_TimeBaseStructure.TIM_Prescaler = (72000000/1000000 - 1);
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- //中斷配置
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //搶占優先級2 低優先級別中斷
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應優先級0 高級別的響應中斷
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- //開中斷
- TIM_ClearFlag(TIM3, TIM_FLAG_Update);
- TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
- //開啟定時器
- TIM_Cmd(TIM3, ENABLE);
- }
- u32 led_time = 240;
- //定時器中斷處理 從stm32f10x_it.c添加
- void Time_IntHandle(void)
- {
- //清中斷標識
- TIM_ClearFlag(TIM3, TIM_FLAG_Update);
- //---------------- 中斷處理 ---------------------
- {
- static u8 Num = 0;
- if(++Num == led_time) //zlz modify 30->240
- {
- //----------- 0.3s事務 閃爍LED -----------
- static bool Temp = False;
- Temp = !Temp;
- ledon = !ledon;
- //SetPB10(Temp);
- //--------------------------------------
- Num = 0;
- //USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
- }
- }
- }
- /*-------------------------------------------------------------------------------------------------------
- * 程序從這里執行
- * 本程序通過定時器 閃爍STM32-MINI開發板LED 請保持LS1跳線帽處于連接狀態
- -------------------------------------------------------------------------------------------------------*/
- int main(void)
- {
- u32 test;
- u8 RxCounter, TxCounter, i;
- u8 RxBuffer[32], TxBuffer[32];
- //--------------------------- CLK INIT, HSE PLL ----------------------------
- ErrorStatus HSEStartUpStatus;
- //RCC reset
- RCC_DeInit();
- //開啟外部時鐘 并執行初始化
- RCC_HSEConfig(RCC_HSE_ON);
- //等待外部時鐘準備好
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- //啟動失敗 在這里等待
- while(HSEStartUpStatus == ERROR);
- //設置內部總線時鐘
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- RCC_PCLK1Config(RCC_HCLK_Div1);
- RCC_PCLK2Config(RCC_HCLK_Div1);
- //外部時鐘為8M 這里倍頻到72M
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- RCC_PLLCmd(ENABLE);
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- while(RCC_GetSYSCLKSource() != 0x08);
- //----------------------------- CLOSE HSI ---------------------------
- //關閉內部時鐘HSI
- RCC_HSICmd(DISABLE);
-
- //開PB口時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
- //中斷配置 2-level interrupt
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- //開總中斷
- __enable_irq();
-
- //開定時器
- TimeON();
-
- for(i=0; i<UART1_N_TX; ++i)
- {
- //TxBuffer1[i] = 0x18 + i;
- }
- Uart1_Cfg();
-
- ledon = 1;
-
- //printf("line %d, %s\n", __LINE__, __func__);
- //等待中斷
- while(1)
- {
- if( rx4pc1 )
- {
- rx4pc1 = 0;
- memcpy(TxBuffer1, RxBuffer1, UART1_N_RX);
- USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
-
- if( (RxBuffer1[18] == 0x55) && (RxBuffer1[19] == 0x1a) )
- {
- SetPB10(1);
- }
- else if( (RxBuffer1[0] == 0x56) && (RxBuffer1[1] == 0x1a) )
- {
- SetPB10(0);
- }
- memset(RxBuffer1, 0, sizeof(RxBuffer1));
- }
-
- //SetPB10(ledon);
- }
- }
復制代碼
|
評分
-
查看全部評分
|