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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3101|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

STM32在線水質(zhì)氨氮檢測儀變送器開發(fā)PCB和單片機源程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:571920 發(fā)表于 2019-6-25 17:06 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
開發(fā)的氨氮檢測儀,采用CAN總線輸出,源程序和PCB見附件:
技術(shù)參數(shù)如下:
技術(shù)參數(shù)
量程:0~100ppm(量程可調(diào))
精度:0.1ppm
輸出信號:CAN總線
傳輸協(xié)議:Modbus RTU
485波特率:9600(默認)
CAN頻率:500K(默認)
溫度范圍:0-80
溫度補償:自動補償
校準(zhǔn):命令方式(軟件校準(zhǔn))
工作電壓:5V/12V
電纜長度:標(biāo)配5

Altium Designer畫的原理圖和PCB圖如下:(51hei附件中可下載工程文件)


單片機源程序如下:
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "usart.h"
  5. #include "adc.h"
  6. #include "dma.h"
  7. #include "can.h"
  8. #include "rs485.h"
  9. #include "math.h"
  10. #include "stmflash.h"   

  11. //要寫入到STM32 FLASH的字符串?dāng)?shù)組
  12. u16 flash[10],read[10];//第一位判斷是否是初始設(shè)置
  13. #define SIZE sizeof(write)                 //數(shù)組長度
  14. #define FLASH_SAVE_ADDR  0X08040000         //設(shè)置FLASH 保存地址(必須為偶數(shù),且其值要大于本代碼所占用FLASH的大小+0X08000000)
  15. u16 get[20000];
  16. float n,m;
  17. int a,phase,crc,rsp;
  18. int array[]={115200,57600,38400,28800,19200,14400,9600,4800,2400,1200};
  19. u16 brq;

  20. int main(void)
  21. {
  22.          u8 rs485[8],send485[8],len;
  23.          u8 cnt=0;
  24.          u8 canbuf[8],canget[8];
  25.          int i;
  26.         float temp,nh;
  27.         delay_init();           
  28.         uart_init(9600);                 //串口初始化為9600
  29.         LED_Init();
  30.         if(flash[0]==10)RS485_Init(array[flash[4]]);
  31.         else                RS485_Init(9600);        //初始化RS485
  32.         MYDMA_Config(DMA1_Channel1,(u32)&ADC1->DR,(u32)get,1904);//DMA1通道1,外設(shè)為串口1,存儲器為SendBuff,長度應(yīng)為轉(zhuǎn)換通道數(shù)×轉(zhuǎn)換次數(shù)
  33.         //采樣952個點,50hz信號 20ms一周期,采樣952個點 約為21us采樣一次,即12Mhz頻率 239.5+12.5個周期252 252=12*21
  34.         Adc_Init();               
  35.   DMA_Cmd(DMA1_Channel1, ENABLE);  //使能ADC DMA1 所指示的通道         
  36.         ADC_SoftwareStartConvCmd(ADC1, ENABLE);                //使能指定的ADC1的軟件轉(zhuǎn)換啟動功能
  37.   STMFLASH_Read(FLASH_SAVE_ADDR,(u16*)flash,10);
  38.   if(flash[0]==10)CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_9tq,flash[3],CAN_Mode_Normal);
  39.   else         
  40.         CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_9tq,4,CAN_Mode_Normal);//36M/((8+9+1)*4)=500Kbps,brp可以設(shè)置從0~1024
  41.         while(1)
  42.         {
  43.                
  44. //                while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);作為保留 防止第一位數(shù)據(jù)丟失
  45.                 //MYDMA_Enable(DMA1_Channel1);
  46.     LED1=!LED1;        
  47.         for(a=0;a<100;a++) //電壓值求均值,疊加100次再除以100
  48.                 {
  49.         for(i=0;i<1902;i+=2)
  50.                 {
  51.                  n+=get[i]*get[i];
  52.                 }
  53.                 n=sqrt(n/952);
  54.                 nh+=n*(3.3/4096);
  55.                 n=0;
  56.                 }
  57.                 printf("nh4 is %f\r\n",nh/100);
  58.                 if(nh<51.3)
  59.           temp=1.01*nh-41.814;
  60.                 else temp=12*nh-614;
  61.                 printf("nh4 is %f\r\n",temp);
  62.                 nh=0;
  63. //                for(i=1;i<11;i+=2)
  64. //                {
  65. //                 printf("wendu is %f\r\n",(float)get[i]*(3.3/4096));
  66. //                 m+=get[i]*get[i];
  67. //                }
  68. //                        m=sqrt(m/952);
  69. //                printf("wendu is %f\r\n",(float)(m/100)*(3.3/4096));
  70. //                m=0;
  71.                  canbuf[2]=temp;        
  72.                  temp-=canbuf[2];
  73.                  temp*=100;
  74.                  canbuf[3]=temp;
  75.                  LED0=!LED0;
  76.                 RS485_Receive_Data(rs485,&len);
  77.                 if (rs485[0]==0xad)
  78.     {
  79.                         switch (rs485[1])
  80.                         {
  81.                                 
  82.                         case 0x02: //設(shè)置485頻率
  83.                                  send485[0]=0xad;
  84.                            send485[1]=0x02;
  85.                                  rsp=(int)rs485[3];
  86.                                  if(rsp<0&&rsp>9) {send485[2]=0x11;        //如頻率設(shè)置錯誤則返回錯誤代碼 0xad 0x11 0x11 0x11 0x65 0xa7
  87.                                  send485[3]=0x11;
  88.                                  crc=CRC16_2(send485,4);
  89.                                  send485[4]=(unsigned char)(crc&0xff);
  90.          send485[5]=(unsigned char)((crc&0xff00)>>8);
  91.                            RS485_Send_Data(send485,6); }
  92.                                  else{RS485_Init(array[rsp]);
  93.                                  flash[1]=10;
  94.                                  flash[4]=rsp;
  95.                      //設(shè)置成功 返回 0xad 0x02 0x00 0x00 0x57 0x91
  96.                                  send485[2]=0x00;
  97.                                  send485[3]=0x00;
  98.                                  crc=CRC16_2(send485,4);
  99.                                  send485[4]=(unsigned char)(crc&0xff);
  100.          send485[5]=(unsigned char)((crc&0xff00)>>8);
  101.                            RS485_Send_Data(send485,6);}
  102.                                  break;
  103.                         case 0x03:   //查詢NH4值
  104.                                  send485[0]=0xad;
  105.                            send485[1]=0x03;
  106.                            crc=CRC16_2(send485,4);
  107.                            send485[4]=(unsigned char)(crc&0xff);
  108.          send485[5]=(unsigned char)((crc&0xff00)>>8);
  109.                           RS485_Send_Data(send485,6);
  110.                            break;
  111.                         case 0x04:  //校準(zhǔn)斜率
  112.                                  switch(canget[2])
  113.                                  {
  114.                                  case 0x00:
  115.                                          
  116.          send485[0]=0xad;
  117.          send485[1]=0x04;
  118.          crc=CRC16_2(send485,4);
  119.                            send485[4]=(unsigned char)(crc&0xff);
  120.          send485[5]=(unsigned char)((crc&0xff00)>>8);
  121.                            RS485_Send_Data(send485,6);
  122.          }
  123.                          }
  124.                          }                                 
  125.     Can_Receive_Msg(canget);
  126.     if (canget[0]==0xad)
  127.     {
  128.                         switch (canget[1])
  129.                         {
  130.                                 
  131.                         case 0x02: //設(shè)置can頻率
  132.                                  canbuf[0]=0xad;
  133.                            canbuf[1]=0x02;
  134.                                  phase=(int)canget[3]+(int)(256*canget[2]);
  135.                                  if(2000%phase!=0) {canbuf[2]=0x11;        //如頻率設(shè)置錯誤則返回錯誤代碼 0xad 0x11 0x11 0x11 0x65 0xa7
  136.                                  canbuf[3]=0x11;
  137.                                  crc=CRC16_2(canbuf,4);
  138.                                  canbuf[4]=(unsigned char)(crc&0xff);
  139.          canbuf[5]=(unsigned char)((crc&0xff00)>>8);
  140.                            Can_Send_Msg(canbuf,6); }
  141.                                  else{brq=(u16)(2000/phase);
  142.                                  flash[0]=10;
  143.                                  flash[3]=brq;
  144.                                  STMFLASH_Write(FLASH_SAVE_ADDR,(u16*)flash,10);
  145.                      CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_9tq,brq,CAN_Mode_Normal);//設(shè)置成功 返回 0xad 0x02 0x00 0x00 0x57 0x91
  146.                                  canbuf[2]=0x00;
  147.                                  canbuf[3]=0x00;
  148.                                  crc=CRC16_2(canbuf,4);
  149.                                  canbuf[4]=(unsigned char)(crc&0xff);
  150.          canbuf[5]=(unsigned char)((crc&0xff00)>>8);
  151.                            Can_Send_Msg(canbuf,6);}
  152.                                  break;
  153.                         case 0x03:   //查詢NH4值
  154.                                  canbuf[0]=0xad;
  155.                            canbuf[1]=0x03;
  156.                            crc=CRC16_2(canbuf,4);
  157.                            canbuf[4]=(unsigned char)(crc&0xff);
  158.          canbuf[5]=(unsigned char)((crc&0xff00)>>8);
  159.                            Can_Send_Msg(canbuf,6);
  160.                            break;
  161.                         case 0x04:  //校準(zhǔn)斜率
  162.                                  switch(canget[2])
  163.                                  {
  164.                                  case 0x00:
  165.                                          
  166.          canbuf[0]=0xad;
  167.          canbuf[1]=0x04;
  168.          crc=CRC16_2(canbuf,4);
  169.                            canbuf[4]=(unsigned char)(crc&0xff);
  170.          canbuf[5]=(unsigned char)((crc&0xff00)>>8);
  171.                            Can_Send_Msg(canbuf,6);
  172.          }                                         
  173.                         }
  174.                 }        
  175.     delay_ms(250);
  176.      
  177.         }                                                                                            
  178. }        
復(fù)制代碼

所有資料51hei提供下載:
硬件.rar (626.83 KB, 下載次數(shù): 69)
軟件.7z (204.04 KB, 下載次數(shù): 49)

評分

參與人數(shù) 2黑幣 +62 收起 理由
6789364 + 12 贊一個!
admin + 50 共享資料的黑幣獎勵!

查看全部評分

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

使用道具 舉報

沙發(fā)
ID:453576 發(fā)表于 2019-9-5 09:32 | 只看該作者
好東西,感謝樓主,健康永遠是風(fēng)口
回復(fù)

使用道具 舉報

板凳
ID:453576 發(fā)表于 2019-9-5 09:32 | 只看該作者
感謝樓主!
回復(fù)

使用道具 舉報

地板
ID:728564 發(fā)表于 2020-4-13 21:30 | 只看該作者
真的很好。
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 97免费视频在线观看 | 亚洲精品久久久久久久久久久久久 | 久久久久欧美 | 久久视频免费观看 | 欧美伊人 | 国产乱码精品一区二区三区五月婷 | 91大神xh98xh系列全部 | 国产精品一区在线 | 欧美成人猛片aaaaaaa | 亚洲一区二区三区视频 | 午夜手机在线视频 | 日韩在线播放一区 | 国产亚洲精品久久yy50 | 精品婷婷 | 天天天插 | 国产精品久久久久久久久久妇女 | 久久综合久久综合久久 | 国产伦精品一区二区三区四区视频 | 夜夜操天天艹 | 欧美黄色绿像 | 亚洲日本欧美日韩高观看 | 伦理二区 | 久久99国产精一区二区三区 | 国产成人精品久久二区二区91 | 久久久久av | www.久| 成人精品一区二区三区中文字幕 | 国产精品免费一区二区三区四区 | 亚洲天堂久久 | 欧美一区免费在线观看 | 久草中文在线观看 | 中文字幕国产第一页 | 亚洲日本乱码在线观看 | 成人精品一区 | 久久精品视频免费看 | 国产精品久久久久久吹潮日韩动画 | 黄在线免费观看 | 国产伦精品一区二区三区高清 | 天堂色| 国产麻豆乱码精品一区二区三区 | av影音资源 |