|
本代码亲测无误。
1.在PC串口工具发送给stm32 20字节的数据.
2.stm32收到20字节后,回发给PC串口工具,并闪灯。
主程序文件:
- /* 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);
- }
- }
复制代码
|
评分
-
查看全部评分
|