找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2131|回复: 0
打印 上一主题 下一主题
收起左侧

STM32F303vct6,多通道adc,dma源程序

[复制链接]
跳转到指定楼层
楼主
ID:469608 发表于 2019-1-17 12:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
F303vct6,多通道adc,dma
1.gpio.c文件内容需要改,按你们的实际接线
2.查询状态指令<255/STATUS_?中下划线为非法字符,会导致串口命令解析出问题;我现在去掉了下划线改为:<255/STATUS?

STM32单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "adc1.h"
  4. #include "adc2.h"
  5. #include "usart1.h"
  6. #include "ws_spi3.h"
  7. #include "spi1.h"
  8. #include "string.h"
  9. #include "gpio.h"
  10. #include "usart2.h"
  11. #include "usart3.h"
  12. #include <math.h>
  13. #include "w5500.h"
  14. #include "W5500_conf.h"
  15. #include "socket.h"
  16. #include "utility.h"
  17. #include <stdio.h>
  18. #include "tcp_demo.h"
  19. #include "instruct.h"
  20. #include "instructADC.h"
  21. #include "dac.h"
  22. int paInputMin[3]={10,10,10};
  23. int paInputMax[3]={550,550,550};
  24. int paOutputRatio[3]={150,150,150};
  25. int paOutputMin[3]={10,1500,100};
  26. int pa1inPutmax;
  27. int pa2inputmax;
  28. int pa3inputmin;
  29. int pa1OutPutmin;
  30. int pa2Outputmin;
  31. int pa3Outputmin;
  32. int dataRatio;
  33. int  MaxTempter=90;
  34. int remateLocalold=0;
  35. int Errornumber1,Errornumber2;
  36. u8 pa1ContrlState, pa2ContrlState, pa3ContrlState;

  37. /** @addtogroup STM32F30x_StdPeriph_Examples
  38.   * @{
  39.   */

  40. /** @addtogroup Basic_example
  41.   * @{
  42.   */
  43. #ifdef __GNUC__
  44. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  45.    set to 'Yes') calls __io_putchar() */
  46. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  47. #else
  48. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  49. #endif /* __GNUC__ */

  50. __IO uint32_t TimingDelay = 0;
  51. u32 i=0;
  52. u8 j;
  53. float m,temp;

  54. int main(void)
  55. {
  56.   /*!< At this stage the microcontroller clock setting is already configured,
  57.        this is done through SystemInit() function which is called from startup
  58.        file (startup_stm32f30x.s) before to branch to application main.
  59.        To reconfigure the default setting of SystemInit() function, refer to
  60.        system_stm32f30x.c file
  61.      */
  62.         //resent STm32
  63. //        __set_FAULTMASK(1);      
  64. //        NVIC_SystemReset();
  65.        
  66.           uart1_init(115200);
  67.           uart2_init(115200);
  68.           uart3_init(115200);
  69.        
  70.           adc1_init();
  71.           adc2_init();
  72. //        SPI3_Configuration();
  73. //        SPI1_Configuration();       
  74.            GPIO_Config();
  75.         DAC_Config();
  76.         DAC1_out(1800);
  77.         DAC2_out(1500);
  78.        
  79.     gpio_for_w5500_config();                                                /*初始化MCU相关引脚*/
  80.     reset_w5500();                                                                                        /*硬复位W5500*/
  81.     set_w5500_mac();                                                                                /*配置MAC地址*/
  82.     set_w5500_ip();                                                                                        /*配置IP地址*/
  83.     socket_buf_init(txsize, rxsize);                /*初始化8个Socket的发送接收缓存大小*/
  84.     printf(" 源地网络适配版作为TCP 服务器,建立侦听,等待PC作为TCP Client建立连接 \r\n");
  85.     printf(" W5500监听端口为: %d \r\n",local_port);
  86.     printf(" 连接成功后,TCP Client发送数据给W5500,W5500将返回对应数据 \r\n");
  87.     while (1)
  88.     {

  89.                         loopback_tcps(SOCK_TCPS, local_port);/*TCP_Server 数据回环测试程序*/
  90.                         getADCpara();
  91.                         //异常保护       
  92.                         if(ALLerror_open==0)
  93.                         {
  94.                              PAERROR();
  95.                         }
  96.                         Delay(5);       
  97.                         usart1_cmd();
  98.                         if(controlState())
  99.                         {
  100.                                 if(remateLocalold==0)
  101.                                         {
  102.                                                                 SetPA1RFONOFF(0);
  103.                                                                 SetPA2RFONOFF(0);
  104.                                                                 SetPA3RFONOFF(0);
  105.                                         for(i=0;i<3;i++)
  106.                                                                         {
  107.                                                                         Tempter_errorState[i]=0;
  108.                                                                         output_errorState[i]=0;
  109.                                                                         input_errorState[i]=0;
  110.                                                                         }
  111.                                          clearchan1();
  112.                                         }
  113.                
  114.                         remateLocalold=1;
  115.                         }
  116.                         else
  117.                         {
  118.                         localCTR1();
  119.                         remateLocalold=0;
  120.                         }
  121.                                        
  122.                         Delay(5);               
  123.     }
  124.         //  }
  125. }



  126. /**
  127.   * @brief  Inserts a delay time.
  128.   * @param  nTime: specifies the delay time length, in milliseconds.
  129.   * @retval None
  130.   */
  131. void Delay(__IO uint32_t nTime)
  132. {
  133.   TimingDelay = nTime;

  134.   while(TimingDelay != 0);
  135. }

  136. void localCTR1(void)
  137. {
  138.         u8 i,PA1statan,PA2statan,PA3statan;
  139.        
  140.         ///////////////// remate to local///////////////////////////////
  141.         if(remateLocalold==1)
  142.         {
  143.         SetPA1RFONOFF(0);PA1statan=0;
  144.         SetPA2RFONOFF(0);PA2statan=0;
  145.         SetPA3RFONOFF(0);PA3statan=0;
  146.                 for(i=0;i<3;i++)
  147.                 {
  148.     Tempter_errorState[i]=0;
  149.                 output_errorState[i]=0;
  150.                 input_errorState[i]=0;
  151.                 }
  152.                  clearchan1();
  153.         }

  154. //////////////////////pA1 local /////////////////////////////////
  155.   
  156.                  if(Tempter_errorState[0]==0&&output_errorState[0]==0&&input_errorState[0]==0)
  157.                  {               
  158.                          if( remotePA1RFONOFF())
  159.                          {
  160.                                 SetPA1RFONOFF(1);                               
  161.                          }
  162.                          else
  163.                          {
  164.                          SetPA1RFONOFF(0);               
  165.                          }
  166.      }                        
  167.                  else
  168.                  {
  169.                   SetPA1RFONOFF(0);                         
  170.                  }                 
  171. if(GetPA1RFONOFF()==0&&remotePA1RFONOFF()==0)
  172.                  {
  173.                    Tempter_errorState[0]=0;
  174.                    output_errorState[0]=0;
  175.                    input_errorState[0]=0;
  176.                          clearchan1();               
  177.                  }
  178. //////////////////////pA2 local /////////////////////////////////
  179.                  if(!Tempter_errorState[1]&&!output_errorState[1]&&!input_errorState[1])
  180.                  {
  181.        
  182.                          if( remotePA2RFONOFF())
  183.                          {
  184.                                 SetPA2RFONOFF(1);
  185.                                                           }
  186.                          else
  187.                          {
  188.                          SetPA2RFONOFF(0);                                  
  189.                          }
  190.      }                 
  191.                  else
  192.                  {
  193.                  SetPA2RFONOFF(0);
  194.                        
  195.                  }
  196.                  
  197.                  if(GetPA2RFONOFF()==0&&remotePA2RFONOFF()==0)
  198.                  {
  199.                    Tempter_errorState[1]=0;
  200.                    output_errorState[1]=0;
  201.                    input_errorState[1]=0;               
  202.                  }

  203. //////////////////////pA3 local /////////////////////////////////
  204.         if(!Tempter_errorState[2]&&!output_errorState[2]&&!input_errorState[2])
  205.                  {                                             
  206.                                 if( remotePA3RFONOFF())
  207.                          {
  208.                                 SetPA3RFONOFF(1);
  209.                                  PA3statan=1;
  210.                          }
  211.                          else
  212.                          {
  213.                          SetPA3RFONOFF(0);
  214.                          PA3statan=0;
  215.                          }
  216.          }       
  217.          else
  218.          {
  219.          SetPA3RFONOFF(0);
  220.                  PA3statan=0;
  221.          }
  222.           if(GetPA3RFONOFF()==0&&remotePA3RFONOFF()==0)
  223.                  {
  224.                    Tempter_errorState[2]=0;
  225.                    output_errorState[2]=0;
  226.                    input_errorState[2]=0;                       
  227.                  }
  228. }

  229. void TimingDelay_Decrement(void)
  230. {
  231.   if (TimingDelay != 0x00)
  232.   {
  233.     TimingDelay--;
  234.   }
  235. }



  236. void PAERROR(void)
  237. {
  238.         int panumber;
  239.         int pa2inputmax=2000;//将PA2、PA3两个输入输出门限提前
  240.         int pa3inputmax=2000;
  241.         int pa2oOutputmin=900;
  242.         int pa3oOutputmin=90;
  243.         int un=0;
  244.         double voldata=0;       
  245.         for(panumber=0;panumber<3;panumber++)
  246.         {
  247.          if(TestTempter[panumber]> MaxTempter ||Tempter_errorState[panumber]==1 )//wendu
  248.            {
  249.              Tempter_errorState[panumber] = 1;
  250.                    SetPARFONOFF(panumber+1,0);
  251.             }       
  252.                  else
  253.                  {
  254.                  Tempter_errorState[panumber] = 0;
  255.                  }
  256.          
  257.                 if(input_errorState[panumber]==1)
  258.                 {
  259.                
  260.                 }
  261. else if(panumber==0)
  262.                 {
  263.                         input_errorState[panumber]=0;
  264.                 }
  265. else if(panumber==1)
  266. {
  267.            if(PaPower[1]>pa2inputmax)
  268.                 {
  269.                  input_errorState[panumber]=1;               
  270.                         InputPA2Dispay(1);
  271.                 }
  272.                
  273.         }
  274. else if(panumber==2)
  275. {
  276.           if(PaPower[4]> pa3inputmax)
  277.                 {
  278.                  input_errorState[panumber]=1;               
  279.                         //InputPA3Dispay(1);
  280.                 }       
  281.         }
  282. //output power+
  283.                 if(output_errorState[panumber]==1)
  284.                 {
  285.                        
  286.                 }
  287.                 else if(panumber==0)
  288.                 {
  289.                         output_errorState[panumber]=0;
  290.                         if(PA1IsOrNotError()==1)
  291.                         {
  292.                                 output_errorState[panumber]=1;                                                  
  293.                         }
  294.                 }
  295.                 else if(panumber==1)//0:no,1:red,2 green
  296.                 {
  297.                 voldata=  PaPower[3]-PaPower[2];               
  298.                 if(PaPower[2]>pa2oOutputmin)
  299.                 {
  300.                  output_errorState[panumber]=0;               
  301.      OutputPA2Dispay(0);
  302.     if(PaPower[1]<pa2inputmax)
  303.     {       
  304.                         input_errorState[panumber]=0;                       
  305.       InputPA2Dispay(0);
  306.                        
  307.                         Errornumber1=0;
  308.     }
  309.                 }
  310.                 else if(PaPower[2]<pa2oOutputmin && voldata<50)//todo 50
  311.                 {
  312.                          Errornumber1++;                                       
  313.                         if(Errornumber1>6)
  314.                         {
  315.                                 output_errorState[panumber]=1;       
  316.                
  317.                         OutputPA2Dispay(1);
  318.                                 un=10;
  319.                         }                               
  320.                
  321.                 }
  322.                 else
  323.                 {
  324.                  output_errorState[panumber]=0;               
  325.                         OutputPA2Dispay(2);
  326.                         Errornumber1=0;
  327.                         if(PaPower[1]<pa2inputmax)
  328. {               
  329.                          input_errorState[panumber]=0;               
  330.                         InputPA2Dispay(2);
  331. }
  332.                 }               
  333.                 }
  334.                
  335.          else if(panumber==2)
  336.                  {
  337.                  if(PaPower[7]<1)       
  338.            {               
  339.                 PaPower[7]=1;
  340.                 }
  341.                 dataRatio=PaPower[6]*100/PaPower[7];
  342.                
  343.          if(PaPower[6]<pa3oOutputmin)
  344.                 {
  345.                  output_errorState[panumber]=0;
  346.                         if(PaPower[4]<pa3inputmax)                       
  347. {
  348.                    Errornumber2=0;
  349.                   input_errorState[panumber]=0;               
  350.         InputPA3Dispay(0);
  351. }
  352.                         OutputPA3Dispay(0);
  353.                 }
  354.                 else if(PaPower[6]>pa3oOutputmin && dataRatio<180)//pulse is off
  355.                 {
  356.                         //printf("%d\r\n",dataRatio);
  357.                         Errornumber2++;               
  358.                         if(Errornumber2>5)
  359.                         {
  360.                  output_errorState[panumber]=1;               
  361.                         //OutputPA3Dispay(1);
  362.                                
  363.                         }
  364.                 }
  365.                 else
  366.                 {               
  367.      Errornumber2=0;                       
  368.                  output_errorState[panumber]=0;
  369.                 OutputPA3Dispay(2);       
  370. if(PaPower[4]<pa3inputmax)
  371. {
  372.                  input_errorState[panumber]=0;                       
  373.                         InputPA3Dispay(2);               
  374. }       
  375.                 }
  376.        
  377.         }
  378.                         }
  379. }

  380. #ifdef  USE_FULL_ASSERT

  381. /**
  382.   * @brief  Reports the name of the source file and the source line number
  383.   *         where the assert_param error has occurred.
  384.   * @param  file: pointer to the source file name
  385.   * @param  line: assert_param error line source number
  386.   * @retval None
  387.   */
  388. void assert_failed(uint8_t* file, uint32_t line)
  389. {
  390.   /* User can add his own implementation to report the file name and line number,
  391.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  392.   /* Infinite loop */
  393.   while (1)
  394.   {
  395.   }
  396. }
  397. #endif

  398. PUTCHAR_PROTOTYPE
  399. {
  400.   /* Place your implementation of fputc here */
  401.   /* e.g. write a character to the USART */
  402.   USART_SendData(EVAL_COM1, (uint8_t) ch);

  403.   /* Loop until the end of transmission */
  404.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  405.   {
  406.        
  407.         }

  408.   return ch;
  409. }

  410. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
监控程序_10.29.7z (306.32 KB, 下载次数: 53)



评分

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

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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