找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3503|回复: 0
收起左侧

stm32103rb6单片机串口通信控制源码

[复制链接]
ID:225640 发表于 2017-8-8 10:35 | 显示全部楼层 |阅读模式
stm32103rb6单片机接收串口数据控制完整原创源码供学习
原理图:

原理图

原理图

3D图

3D图

0.png


单片机源程序如下:
  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, 下载次数: 18)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表