久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4187|回復: 0
打印 上一主題 下一主題
收起左側

stm32103rb6單片機串口通信控制源碼

[復制鏈接]
跳轉到指定樓層
樓主
stm32103rb6單片機接收串口數據控制完整原創源碼供學習
原理圖:





單片機源程序如下:
  1. #include "pbdata.h"

  2. extern u8 RxBuf[13];
  3. extern u8 RxLen;  
  4. extern u8 RxOk;        
  5. extern u8 SendDat;
  6. extern u8 k1;
  7. extern u8 k2;
  8. u16 Led_dt=1;
  9. //#define DBGMCU_CR (*((volatile unsigned long*)0xE0042004))
  10. void RCC_Configuration(void);                //聲明時鐘函數
  11. void GPIO_Configuration(void);                 //聲明端口函數
  12. void NVIC_Configuration(void);
  13. void USART_Configuration(void);
  14. void TIM3_Configuration(void);

  15. void LED_All_OFF(void)
  16. {
  17.   GPIO_Write(GPIOB,0xFFFF);
  18.   GPIO_Write(GPIOC,0xFFFF);
  19. }

  20. void Control_LED(void)
  21. {
  22.        if ((RxBuf[3]&0x31)==0x31){GPIO_ResetBits(GPIOB,GPIO_Pin_0);}
  23.   else if ((RxBuf[3]|0x3e)==0x3e){GPIO_SetBits(GPIOB,GPIO_Pin_0);}
  24.        if ((RxBuf[3]&0x32)==0x32){GPIO_ResetBits(GPIOB,GPIO_Pin_1);}
  25.   else if ((RxBuf[3]|0x3d)==0x3d){GPIO_SetBits(GPIOB,GPIO_Pin_1);}
  26.        if ((RxBuf[3]&0x34)==0x34){GPIO_ResetBits(GPIOB,GPIO_Pin_2);}
  27.   else if ((RxBuf[3]|0x3b)==0x3b){GPIO_SetBits(GPIOB,GPIO_Pin_2);}
  28.        if ((RxBuf[3]&0x38)==0x38){GPIO_ResetBits(GPIOB,GPIO_Pin_3);}
  29.   else if ((RxBuf[3]|0x37)==0x37){GPIO_SetBits(GPIOB,GPIO_Pin_3);}
  30.        if ((RxBuf[4]&0x31)==0x31){GPIO_ResetBits(GPIOB,GPIO_Pin_4);}
  31.   else if ((RxBuf[4]|0x3e)==0x3e){GPIO_SetBits(GPIOB,GPIO_Pin_4);}
  32.        if ((RxBuf[4]&0x32)==0x32){GPIO_ResetBits(GPIOB,GPIO_Pin_5);}
  33.   else if ((RxBuf[4]|0x3d)==0x3d){GPIO_SetBits(GPIOB,GPIO_Pin_5);}
  34.        if ((RxBuf[4]&0x34)==0x34){GPIO_ResetBits(GPIOB,GPIO_Pin_6);}
  35.   else if ((RxBuf[4]|0x3b)==0x3b){GPIO_SetBits(GPIOB,GPIO_Pin_6);}
  36.        if ((RxBuf[4]&0x38)==0x38){GPIO_ResetBits(GPIOB,GPIO_Pin_7);}
  37.   else if ((RxBuf[4]|0x37)==0x37){GPIO_SetBits(GPIOB,GPIO_Pin_7);}
  38.        if ((RxBuf[5]&0x31)==0x31){GPIO_ResetBits(GPIOB,GPIO_Pin_8);}
  39.   else if ((RxBuf[5]|0x3e)==0x3e){GPIO_SetBits(GPIOB,GPIO_Pin_8);}
  40.        if ((RxBuf[5]&0x32)==0x32){GPIO_ResetBits(GPIOB,GPIO_Pin_9);}
  41.   else if ((RxBuf[5]|0x3d)==0x3d){GPIO_SetBits(GPIOB,GPIO_Pin_9);}
  42.        if ((RxBuf[5]&0x34)==0x34){GPIO_ResetBits(GPIOB,GPIO_Pin_10);}
  43.   else if ((RxBuf[5]|0x3b)==0x3b){GPIO_SetBits(GPIOB,GPIO_Pin_10);}
  44.        if ((RxBuf[5]&0x38)==0x38){GPIO_ResetBits(GPIOB,GPIO_Pin_11);}
  45.   else if ((RxBuf[5]|0x37)==0x37){GPIO_SetBits(GPIOB,GPIO_Pin_11);}
  46.        if ((RxBuf[6]&0x31)==0x31){GPIO_ResetBits(GPIOB,GPIO_Pin_12);}
  47.   else if ((RxBuf[6]|0x3e)==0x3e){GPIO_SetBits(GPIOB,GPIO_Pin_12);}
  48.        if ((RxBuf[6]&0x32)==0x32){GPIO_ResetBits(GPIOB,GPIO_Pin_13);}
  49.   else if ((RxBuf[6]|0x3d)==0x3d){GPIO_SetBits(GPIOB,GPIO_Pin_13);}
  50.        if ((RxBuf[6]&0x34)==0x34){GPIO_ResetBits(GPIOB,GPIO_Pin_14);}
  51.   else if ((RxBuf[6]|0x3b)==0x3b){GPIO_SetBits(GPIOB,GPIO_Pin_14);}
  52.        if ((RxBuf[6]&0x38)==0x38){GPIO_ResetBits(GPIOB,GPIO_Pin_15);}
  53.   else if ((RxBuf[6]|0x37)==0x37){GPIO_SetBits(GPIOB,GPIO_Pin_15);}
  54.        if ((RxBuf[7]&0x31)==0x31){GPIO_ResetBits(GPIOC,GPIO_Pin_0);}
  55.   else if ((RxBuf[7]|0x3e)==0x3e){GPIO_SetBits(GPIOC,GPIO_Pin_0);}
  56.        if ((RxBuf[7]&0x32)==0x32){GPIO_ResetBits(GPIOC,GPIO_Pin_1);}
  57.   else if ((RxBuf[7]|0x3d)==0x3d){GPIO_SetBits(GPIOC,GPIO_Pin_1);}
  58.        if ((RxBuf[7]&0x34)==0x34){GPIO_ResetBits(GPIOC,GPIO_Pin_2);}
  59.   else if ((RxBuf[7]|0x3b)==0x3b){GPIO_SetBits(GPIOC,GPIO_Pin_2);}
  60.        if ((RxBuf[7]&0x38)==0x38){GPIO_ResetBits(GPIOC,GPIO_Pin_3);}
  61.   else if ((RxBuf[7]|0x37)==0x37){GPIO_SetBits(GPIOC,GPIO_Pin_3);}
  62.        if ((RxBuf[8]&0x31)==0x31){GPIO_ResetBits(GPIOC,GPIO_Pin_4);}
  63.   else if ((RxBuf[8]|0x3e)==0x3e){GPIO_SetBits(GPIOC,GPIO_Pin_4);}
  64.        if ((RxBuf[8]&0x32)==0x32){GPIO_ResetBits(GPIOC,GPIO_Pin_5);}
  65.   else if ((RxBuf[8]|0x3d)==0x3d){GPIO_SetBits(GPIOC,GPIO_Pin_5);}
  66.        if ((RxBuf[8]&0x34)==0x34){GPIO_ResetBits(GPIOC,GPIO_Pin_6);}
  67.   else if ((RxBuf[8]|0x3b)==0x3b){GPIO_SetBits(GPIOC,GPIO_Pin_6);}
  68.        if ((RxBuf[8]&0x38)==0x38){GPIO_ResetBits(GPIOC,GPIO_Pin_7);}
  69.   else if ((RxBuf[8]|0x37)==0x37){GPIO_SetBits(GPIOC,GPIO_Pin_7);}
  70.        if ((RxBuf[9]&0x31)==0x31){GPIO_ResetBits(GPIOC,GPIO_Pin_8);}
  71.   else if ((RxBuf[9]|0x3e)==0x3e){GPIO_SetBits(GPIOC,GPIO_Pin_8);}
  72.        if ((RxBuf[9]&0x32)==0x32){GPIO_ResetBits(GPIOC,GPIO_Pin_9);}
  73.   else if ((RxBuf[9]|0x3d)==0x3d){GPIO_SetBits(GPIOC,GPIO_Pin_9);}
  74.        if ((RxBuf[9]&0x34)==0x34){GPIO_ResetBits(GPIOC,GPIO_Pin_10);}
  75.   else if ((RxBuf[9]|0x3b)==0x3b){GPIO_SetBits(GPIOC,GPIO_Pin_10);}
  76.        if ((RxBuf[9]&0x38)==0x38){GPIO_ResetBits(GPIOC,GPIO_Pin_11);}
  77.   else if ((RxBuf[9]|0x37)==0x37){GPIO_SetBits(GPIOC,GPIO_Pin_11);}
  78.        if ((RxBuf[10]&0x31)==0x31){GPIO_ResetBits(GPIOC,GPIO_Pin_12);}
  79.   else if ((RxBuf[10]|0x3e)==0x3e){GPIO_SetBits(GPIOC,GPIO_Pin_12);}
  80.        if ((RxBuf[10]&0x32)==0x32){GPIO_ResetBits(GPIOC,GPIO_Pin_13);}
  81.   else if ((RxBuf[10]|0x3d)==0x3d){GPIO_SetBits(GPIOC,GPIO_Pin_13);}
  82.        if ((RxBuf[10]&0x34)==0x34){GPIO_ResetBits(GPIOC,GPIO_Pin_14);}
  83.   else if ((RxBuf[10]|0x3b)==0x3b){GPIO_SetBits(GPIOC,GPIO_Pin_14);}
  84.        if ((RxBuf[10]&0x38)==0x38){GPIO_ResetBits(GPIOC,GPIO_Pin_15);}
  85.   else if ((RxBuf[10]|0x37)==0x37){GPIO_SetBits(GPIOC,GPIO_Pin_15);}
  86. }

  87. int main(void)
  88. {
  89.    u16 i;
  90.    RCC_Configuration();        //系統時鐘初始化
  91.    RCC_HSE_Configuration();        //系統時鐘初始化(自定義)
  92.    GPIO_Configuration();//端口初始化
  93.    USART_Configuration();
  94.    NVIC_Configuration();
  95.    TIM3_Configuration();
  96.    Control_LED();
  97.    LED_All_OFF();
  98.    GPIO_ResetBits(GPIOA, GPIO_Pin_8);
  99.         while(1)
  100.         {
  101.      if(RxOk==1)  //串口1接收中斷
  102.          {
  103.          Led_dt=1;
  104.          RxOk=0;
  105.          GPIO_SetBits(GPIOA,GPIO_Pin_8);
  106.          GPIO_ResetBits(GPIOA,GPIO_Pin_15);
  107.      Control_LED();
  108.          delay_ms(1);
  109.           for(i=0;i<13;i++)               
  110.           {         
  111.           USART_ClearFlag(USART1,USART_FLAG_TC);
  112.           USART_SendData(USART1,RxBuf[i]);//發送數據
  113.           while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET){;}
  114.           }
  115. //          USART_SendData(USART1,SendDat);//串口1發送數據
  116. //          while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET)//等待發送完成
  117. //                {;}
  118.           delay_ms(1);
  119.           GPIO_ResetBits(GPIOA,GPIO_Pin_8);         //拉低進入485接收模式
  120.           RxBuf[0]= USART_ReceiveData(USART1);         //讀一次
  121.           for(i=0;i<13;i++){ RxBuf[i]=0; }             //清空
  122.           RxLen=0;        k2=0;k1=0;
  123.           USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清空接收標志
  124.           USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);          //打開接收使能
  125.           USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清空接收標志
  126.          }
  127.           else
  128.          {
  129.            GPIO_SetBits(GPIOA,GPIO_Pin_15);
  130.            delay_ms(10);
  131.            Led_dt++;
  132.            if (Led_dt>800)
  133.            {
  134.            Led_dt=1;
  135.            LED_All_OFF();
  136.            }
  137.            TIM_SetCompare2(TIM3,Led_dt);
  138.           }
  139.          }
  140.   }                                                         

  141. void RCC_Configuration(void)
  142. {
  143.     SystemInit();
  144.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);        //RCC_APB2Periph_AFIO時鐘總線
  145.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  146.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  147.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  148.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
  149.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);

  150. //        RCC_LSEConfig(RCC_LSE_OFF);        //關閉外部低速外部時鐘信號功能 后,PC13 PC14 PC15 才可以當普通IO用。
  151. //        BKP_TamperPinCmd(DISABLE);//關閉入侵檢測功能,也就是 PC13,也可以當普通IO 使用
  152. }
  153. void GPIO_Configuration(void)
  154. {
  155.         GPIO_InitTypeDef GPIO_InitStructure;         ///*函數結構體變量*/定義兩個變量         申明一個結構變量,用來初始化GPIO
  156.     DBGMCU->CR = DBGMCU->CR & ~((uint32_t)1<<5); // 不分配跟蹤引腳,釋放PB3
  157. //        AFIO->MAPR &= ~(0X7<<24);
  158. //        AFIO->MAPR |=  (0X2<<24);
  159.     GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);           //改變指定管腳的映射 GPIO_Remap_SWJ_Disable SWJ        

  160.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;                 //CS_485使能腳
  161.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  162.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;         //推挽模式
  163.         GPIO_Init(GPIOA,&GPIO_InitStructure);         //指針函數,把上面函數傳進來

  164.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
  165.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  166.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;           //復用推挽輸出模式
  167.         GPIO_Init(GPIOA,&GPIO_InitStructure);

  168.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
  169.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;   //浮空輸入模式
  170.         GPIO_Init(GPIOA,&GPIO_InitStructure);

  171.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;                 //
  172.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  173.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;         //推挽模式
  174.         GPIO_Init(GPIOA,&GPIO_InitStructure);         //指針函數,把上面函數傳進來

  175.         GPIOB->ODR =   0xFFFFFFFF;
  176.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;                 //GPIO_Pin=GPIO_Pin_X
  177.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  178.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;         //推挽模式
  179.         GPIO_Init(GPIOB,&GPIO_InitStructure);         //指針函數,把上面函數傳進來
  180.         
  181.         GPIOC->ODR =   0xFFFFFFFF;
  182.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;                 //GPIO_Pin=GPIO_Pin_X
  183.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  184.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;         //推挽模式
  185.         GPIO_Init(GPIOC,&GPIO_InitStructure);         //指針函數,把上面函數傳進來        
  186. }
  187. void NVIC_Configuration(void)
  188. {
  189.            NVIC_InitTypeDef NVIC_InitStructure;

  190.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);           //中斷優先級

  191.         NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  192.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  193.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  194.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  195.         NVIC_Init(&NVIC_InitStructure);
  196. }

  197. void USART_Configuration(void)
  198. {
  199. //         u32 bps;
  200.     USART_InitTypeDef USART_InitStructure;
  201. //        USART_ClockInitTypeDef  USART_ClockInitStructure;
  202.         USART_InitStructure.USART_BaudRate=9600;
  203.         USART_InitStructure.USART_WordLength=USART_WordLength_8b;  //8位
  204.         USART_InitStructure.USART_StopBits=USART_StopBits_1;     //停止位
  205.         USART_InitStructure.USART_Parity=USART_Parity_No;                 //無數據校驗
  206.         USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;        //無硬件握手
  207.         USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;

  208.         USART_Init(USART1,&USART_InitStructure);         //初始化
  209.         USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  210.         USART_Cmd(USART1,ENABLE);
  211.         USART_ClearFlag(USART1,USART_FLAG_TC);
  212. }
  213. void TIM3_Configuration(void)
  214. {
  215.         TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
  216.                 GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
  217.         TIM_ClearITPendingBit(TIM3,TIM_IT_Update);

  218.         TIM_TimeBaseStruct.TIM_Period=899;//初值
  219.         TIM_TimeBaseStruct.TIM_Prescaler=0;//預分頻
  220.         TIM_TimeBaseStruct.TIM_ClockDivision=0;
  221.         TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上

  222.         TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);

  223.         TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
  224.         TIM_Cmd(TIM3,ENABLE);         
  225. }
復制代碼

所有資料51hei提供下載:
stm32程序(20170717已完成).7z (184.4 KB, 下載次數: 19)

評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 久久一区二区精品 | 97精品超碰一区二区三区 | 日韩色图视频 | 成人性生交大片免费看r链接 | 日日操日日舔 | 91免费在线 | 亚洲日本欧美日韩高观看 | 91亚洲国产成人精品一区二三 | 91免费在线播放 | 午夜精品一区二区三区在线 | 精品国产欧美一区二区三区成人 | www.国产.com| 欧美日韩精品综合 | av中文字幕在线观看 | 欧美一区2区三区3区公司 | 毛片.com | 天天干天天操天天射 | 国产精品美女 | 色男人的天堂 | 免费激情 | 黄色大片免费看 | 日韩欧美国产精品 | 久久久久国产精品 | 黄片毛片免费观看 | 91亚洲国产成人久久精品网站 | 91精品综合久久久久久五月天 | 国产av毛片| 日韩 欧美 综合 | av毛片在线| 成人免费视频网站在线观看 | 日韩精品一区二区三区高清免费 | 精品无码久久久久久国产 | 久久久久国产 | 久久高清免费视频 | 国产精品免费看 | 欧美福利视频一区 | 国产激情视频在线观看 | 亚洲精品日韩欧美 | 亚洲三区在线播放 | 亚洲 欧美 激情 另类 校园 | 亚洲精品一区二区网址 |