stm32103rb6單片機接收串口數據控制完整原創源碼供學習
原理圖:
Protel Schematic.jpg (318.72 KB, 下載次數: 95)
下載附件
原理圖
2017-8-8 10:31 上傳
燈板3D圖2.png (128.99 KB, 下載次數: 47)
下載附件
3D圖
2017-8-8 10:31 上傳
0.png (47.88 KB, 下載次數: 68)
下載附件
2017-8-8 19:01 上傳
單片機源程序如下:
- #include "pbdata.h"
- extern u8 RxBuf[13];
- extern u8 RxLen;
- extern u8 RxOk;
- extern u8 SendDat;
- extern u8 k1;
- extern u8 k2;
- u16 Led_dt=1;
- //#define DBGMCU_CR (*((volatile unsigned long*)0xE0042004))
- void RCC_Configuration(void); //聲明時鐘函數
- void GPIO_Configuration(void); //聲明端口函數
- void NVIC_Configuration(void);
- void USART_Configuration(void);
- void TIM3_Configuration(void);
- void LED_All_OFF(void)
- {
- GPIO_Write(GPIOB,0xFFFF);
- GPIO_Write(GPIOC,0xFFFF);
- }
- void Control_LED(void)
- {
- if ((RxBuf[3]&0x31)==0x31){GPIO_ResetBits(GPIOB,GPIO_Pin_0);}
- else if ((RxBuf[3]|0x3e)==0x3e){GPIO_SetBits(GPIOB,GPIO_Pin_0);}
- if ((RxBuf[3]&0x32)==0x32){GPIO_ResetBits(GPIOB,GPIO_Pin_1);}
- else if ((RxBuf[3]|0x3d)==0x3d){GPIO_SetBits(GPIOB,GPIO_Pin_1);}
- if ((RxBuf[3]&0x34)==0x34){GPIO_ResetBits(GPIOB,GPIO_Pin_2);}
- else if ((RxBuf[3]|0x3b)==0x3b){GPIO_SetBits(GPIOB,GPIO_Pin_2);}
- if ((RxBuf[3]&0x38)==0x38){GPIO_ResetBits(GPIOB,GPIO_Pin_3);}
- else if ((RxBuf[3]|0x37)==0x37){GPIO_SetBits(GPIOB,GPIO_Pin_3);}
- if ((RxBuf[4]&0x31)==0x31){GPIO_ResetBits(GPIOB,GPIO_Pin_4);}
- else if ((RxBuf[4]|0x3e)==0x3e){GPIO_SetBits(GPIOB,GPIO_Pin_4);}
- if ((RxBuf[4]&0x32)==0x32){GPIO_ResetBits(GPIOB,GPIO_Pin_5);}
- else if ((RxBuf[4]|0x3d)==0x3d){GPIO_SetBits(GPIOB,GPIO_Pin_5);}
- if ((RxBuf[4]&0x34)==0x34){GPIO_ResetBits(GPIOB,GPIO_Pin_6);}
- else if ((RxBuf[4]|0x3b)==0x3b){GPIO_SetBits(GPIOB,GPIO_Pin_6);}
- if ((RxBuf[4]&0x38)==0x38){GPIO_ResetBits(GPIOB,GPIO_Pin_7);}
- else if ((RxBuf[4]|0x37)==0x37){GPIO_SetBits(GPIOB,GPIO_Pin_7);}
- if ((RxBuf[5]&0x31)==0x31){GPIO_ResetBits(GPIOB,GPIO_Pin_8);}
- else if ((RxBuf[5]|0x3e)==0x3e){GPIO_SetBits(GPIOB,GPIO_Pin_8);}
- if ((RxBuf[5]&0x32)==0x32){GPIO_ResetBits(GPIOB,GPIO_Pin_9);}
- else if ((RxBuf[5]|0x3d)==0x3d){GPIO_SetBits(GPIOB,GPIO_Pin_9);}
- if ((RxBuf[5]&0x34)==0x34){GPIO_ResetBits(GPIOB,GPIO_Pin_10);}
- else if ((RxBuf[5]|0x3b)==0x3b){GPIO_SetBits(GPIOB,GPIO_Pin_10);}
- if ((RxBuf[5]&0x38)==0x38){GPIO_ResetBits(GPIOB,GPIO_Pin_11);}
- else if ((RxBuf[5]|0x37)==0x37){GPIO_SetBits(GPIOB,GPIO_Pin_11);}
- if ((RxBuf[6]&0x31)==0x31){GPIO_ResetBits(GPIOB,GPIO_Pin_12);}
- else if ((RxBuf[6]|0x3e)==0x3e){GPIO_SetBits(GPIOB,GPIO_Pin_12);}
- if ((RxBuf[6]&0x32)==0x32){GPIO_ResetBits(GPIOB,GPIO_Pin_13);}
- else if ((RxBuf[6]|0x3d)==0x3d){GPIO_SetBits(GPIOB,GPIO_Pin_13);}
- if ((RxBuf[6]&0x34)==0x34){GPIO_ResetBits(GPIOB,GPIO_Pin_14);}
- else if ((RxBuf[6]|0x3b)==0x3b){GPIO_SetBits(GPIOB,GPIO_Pin_14);}
- if ((RxBuf[6]&0x38)==0x38){GPIO_ResetBits(GPIOB,GPIO_Pin_15);}
- else if ((RxBuf[6]|0x37)==0x37){GPIO_SetBits(GPIOB,GPIO_Pin_15);}
- if ((RxBuf[7]&0x31)==0x31){GPIO_ResetBits(GPIOC,GPIO_Pin_0);}
- else if ((RxBuf[7]|0x3e)==0x3e){GPIO_SetBits(GPIOC,GPIO_Pin_0);}
- if ((RxBuf[7]&0x32)==0x32){GPIO_ResetBits(GPIOC,GPIO_Pin_1);}
- else if ((RxBuf[7]|0x3d)==0x3d){GPIO_SetBits(GPIOC,GPIO_Pin_1);}
- if ((RxBuf[7]&0x34)==0x34){GPIO_ResetBits(GPIOC,GPIO_Pin_2);}
- else if ((RxBuf[7]|0x3b)==0x3b){GPIO_SetBits(GPIOC,GPIO_Pin_2);}
- if ((RxBuf[7]&0x38)==0x38){GPIO_ResetBits(GPIOC,GPIO_Pin_3);}
- else if ((RxBuf[7]|0x37)==0x37){GPIO_SetBits(GPIOC,GPIO_Pin_3);}
- if ((RxBuf[8]&0x31)==0x31){GPIO_ResetBits(GPIOC,GPIO_Pin_4);}
- else if ((RxBuf[8]|0x3e)==0x3e){GPIO_SetBits(GPIOC,GPIO_Pin_4);}
- if ((RxBuf[8]&0x32)==0x32){GPIO_ResetBits(GPIOC,GPIO_Pin_5);}
- else if ((RxBuf[8]|0x3d)==0x3d){GPIO_SetBits(GPIOC,GPIO_Pin_5);}
- if ((RxBuf[8]&0x34)==0x34){GPIO_ResetBits(GPIOC,GPIO_Pin_6);}
- else if ((RxBuf[8]|0x3b)==0x3b){GPIO_SetBits(GPIOC,GPIO_Pin_6);}
- if ((RxBuf[8]&0x38)==0x38){GPIO_ResetBits(GPIOC,GPIO_Pin_7);}
- else if ((RxBuf[8]|0x37)==0x37){GPIO_SetBits(GPIOC,GPIO_Pin_7);}
- if ((RxBuf[9]&0x31)==0x31){GPIO_ResetBits(GPIOC,GPIO_Pin_8);}
- else if ((RxBuf[9]|0x3e)==0x3e){GPIO_SetBits(GPIOC,GPIO_Pin_8);}
- if ((RxBuf[9]&0x32)==0x32){GPIO_ResetBits(GPIOC,GPIO_Pin_9);}
- else if ((RxBuf[9]|0x3d)==0x3d){GPIO_SetBits(GPIOC,GPIO_Pin_9);}
- if ((RxBuf[9]&0x34)==0x34){GPIO_ResetBits(GPIOC,GPIO_Pin_10);}
- else if ((RxBuf[9]|0x3b)==0x3b){GPIO_SetBits(GPIOC,GPIO_Pin_10);}
- if ((RxBuf[9]&0x38)==0x38){GPIO_ResetBits(GPIOC,GPIO_Pin_11);}
- else if ((RxBuf[9]|0x37)==0x37){GPIO_SetBits(GPIOC,GPIO_Pin_11);}
- if ((RxBuf[10]&0x31)==0x31){GPIO_ResetBits(GPIOC,GPIO_Pin_12);}
- else if ((RxBuf[10]|0x3e)==0x3e){GPIO_SetBits(GPIOC,GPIO_Pin_12);}
- if ((RxBuf[10]&0x32)==0x32){GPIO_ResetBits(GPIOC,GPIO_Pin_13);}
- else if ((RxBuf[10]|0x3d)==0x3d){GPIO_SetBits(GPIOC,GPIO_Pin_13);}
- if ((RxBuf[10]&0x34)==0x34){GPIO_ResetBits(GPIOC,GPIO_Pin_14);}
- else if ((RxBuf[10]|0x3b)==0x3b){GPIO_SetBits(GPIOC,GPIO_Pin_14);}
- if ((RxBuf[10]&0x38)==0x38){GPIO_ResetBits(GPIOC,GPIO_Pin_15);}
- else if ((RxBuf[10]|0x37)==0x37){GPIO_SetBits(GPIOC,GPIO_Pin_15);}
- }
- int main(void)
- {
- u16 i;
- RCC_Configuration(); //系統時鐘初始化
- RCC_HSE_Configuration(); //系統時鐘初始化(自定義)
- GPIO_Configuration();//端口初始化
- USART_Configuration();
- NVIC_Configuration();
- TIM3_Configuration();
- Control_LED();
- LED_All_OFF();
- GPIO_ResetBits(GPIOA, GPIO_Pin_8);
- while(1)
- {
- if(RxOk==1) //串口1接收中斷
- {
- Led_dt=1;
- RxOk=0;
- GPIO_SetBits(GPIOA,GPIO_Pin_8);
- GPIO_ResetBits(GPIOA,GPIO_Pin_15);
- Control_LED();
- delay_ms(1);
- for(i=0;i<13;i++)
- {
- USART_ClearFlag(USART1,USART_FLAG_TC);
- USART_SendData(USART1,RxBuf[i]);//發送數據
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET){;}
- }
- // USART_SendData(USART1,SendDat);//串口1發送數據
- // while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET)//等待發送完成
- // {;}
- delay_ms(1);
- GPIO_ResetBits(GPIOA,GPIO_Pin_8); //拉低進入485接收模式
- RxBuf[0]= USART_ReceiveData(USART1); //讀一次
- for(i=0;i<13;i++){ RxBuf[i]=0; } //清空
- RxLen=0; k2=0;k1=0;
- USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清空接收標志
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //打開接收使能
- USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清空接收標志
- }
- else
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_15);
- delay_ms(10);
- Led_dt++;
- if (Led_dt>800)
- {
- Led_dt=1;
- LED_All_OFF();
- }
- TIM_SetCompare2(TIM3,Led_dt);
- }
- }
- }
- void RCC_Configuration(void)
- {
- SystemInit();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //RCC_APB2Periph_AFIO時鐘總線
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- // RCC_LSEConfig(RCC_LSE_OFF); //關閉外部低速外部時鐘信號功能 后,PC13 PC14 PC15 才可以當普通IO用。
- // BKP_TamperPinCmd(DISABLE);//關閉入侵檢測功能,也就是 PC13,也可以當普通IO 使用
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure; ///*函數結構體變量*/定義兩個變量 申明一個結構變量,用來初始化GPIO
- DBGMCU->CR = DBGMCU->CR & ~((uint32_t)1<<5); // 不分配跟蹤引腳,釋放PB3
- // AFIO->MAPR &= ~(0X7<<24);
- // AFIO->MAPR |= (0X2<<24);
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); //改變指定管腳的映射 GPIO_Remap_SWJ_Disable SWJ
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8; //CS_485使能腳
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽模式
- GPIO_Init(GPIOA,&GPIO_InitStructure); //指針函數,把上面函數傳進來
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //復用推挽輸出模式
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空輸入模式
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15; //
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽模式
- GPIO_Init(GPIOA,&GPIO_InitStructure); //指針函數,把上面函數傳進來
- GPIOB->ODR = 0xFFFFFFFF;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All; //GPIO_Pin=GPIO_Pin_X
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽模式
- GPIO_Init(GPIOB,&GPIO_InitStructure); //指針函數,把上面函數傳進來
-
- GPIOC->ODR = 0xFFFFFFFF;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All; //GPIO_Pin=GPIO_Pin_X
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽模式
- GPIO_Init(GPIOC,&GPIO_InitStructure); //指針函數,把上面函數傳進來
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //中斷優先級
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void USART_Configuration(void)
- {
- // u32 bps;
- USART_InitTypeDef USART_InitStructure;
- // USART_ClockInitTypeDef USART_ClockInitStructure;
- USART_InitStructure.USART_BaudRate=9600;
- USART_InitStructure.USART_WordLength=USART_WordLength_8b; //8位
- USART_InitStructure.USART_StopBits=USART_StopBits_1; //停止位
- USART_InitStructure.USART_Parity=USART_Parity_No; //無數據校驗
- USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //無硬件握手
- USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
- USART_Init(USART1,&USART_InitStructure); //初始化
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
- USART_Cmd(USART1,ENABLE);
- USART_ClearFlag(USART1,USART_FLAG_TC);
- }
- void TIM3_Configuration(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
- GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
- TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
- TIM_TimeBaseStruct.TIM_Period=899;//初值
- TIM_TimeBaseStruct.TIM_Prescaler=0;//預分頻
- TIM_TimeBaseStruct.TIM_ClockDivision=0;
- TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
- TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
- TIM_Cmd(TIM3,ENABLE);
- }
復制代碼
所有資料51hei提供下載:
stm32程序(20170717已完成).7z
(184.4 KB, 下載次數: 19)
2022-11-22 03:02 上傳
點擊文件名下載附件
stm32F103RB6 下載積分: 黑幣 -5
|