標(biāo)題:
STM32單片機(jī)水質(zhì)檢測-濁度與葉綠素傳感器程序源碼(modbus rtu通訊方式)
[打印本頁]
作者:
loading234
時(shí)間:
2017-9-12 15:04
標(biāo)題:
STM32單片機(jī)水質(zhì)檢測-濁度與葉綠素傳感器程序源碼(modbus rtu通訊方式)
附件是使用濁度與葉綠素傳感器檢測水中濁度與葉綠素值得程序
采用modbus rtu 通訊方式
所有資料51hei提供下載:
禹山葉綠素 -CRC.rar
(329.59 KB, 下載次數(shù): 293)
2017-9-13 01:49 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
單片機(jī)源程序如下:
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "rs485.h"
#include "stdio.h"
#include "math.h"
/************************************************
************************************************/
uint32_t i1;
u8 num2500[8]={0x01,0x03,0x25,0x00,0x00,0x00,0x4e,0xc6};
u8 num2600[8]={0x01,0x03,0x26,0x00,0x00,0x04,0x4f,0x41};
u8 crc1_H,crc1_L,crc2_H,crc2_L,num1,num2;
u8 usart2_rx_buf[13],usart1_rx_buf[13];
float fdata,pfvalue;
u8 usart1len,usart2len;
char usart1_ch,usart2_ch;
int length;
u8 key;
u8 i=0,t=0;
u8 cnt=0;
union data
{
unsigned char A[4];
float Temp;
};
void My_USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; /*定義相關(guān)結(jié)構(gòu)體*/
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//① 使能相應(yīng)的時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);//② 初始化相應(yīng)的IO口模式
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);//② 初始化相應(yīng)的IO口模式
USART_InitStructure.USART_BaudRate=9600;//波特率
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//無硬件流控
USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;//模式
USART_InitStructure.USART_Parity=USART_Parity_No;//校驗(yàn)
USART_InitStructure.USART_StopBits=USART_StopBits_1;//停止位
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//數(shù)據(jù)長度
USART_Init(USART1,&USART_InitStructure);//③ 初始化串口相關(guān)參數(shù)
USART_Cmd(USART1,ENABLE);//使能串口1
USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);//開啟接收中斷
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;//通道
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//開啟中斷通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//設(shè)置中斷優(yōu)先級
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//子優(yōu)先級
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE))
{
usart1_ch=USART_ReceiveData(USART1);
usart1_rx_buf[usart1len++]=usart1_ch;
}
}
void Usart1Send(u8 *buf1)
{
int i;
for(i=0;i<8;i++)
{
USART_SendData(USART1, buf1[i]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){}; //等待發(fā)送完成
}
return;
}
void RS485_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD, ENABLE);//使能GPIOA,D時(shí)鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2時(shí)鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //PD7端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_7);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//復(fù)位串口2
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止復(fù)位
USART_InitStructure.USART_BaudRate = bound;//波特率設(shè)置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位數(shù)據(jù)長度
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個(gè)停止位
USART_InitStructure.USART_Parity = USART_Parity_No;///奇偶校驗(yàn)位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//收發(fā)模式
USART_Init(USART2, &USART_InitStructure); ; //初始化串口
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //使能串口2中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //先占優(yōu)先級2級
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //從優(yōu)先級2級
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中斷通道
NVIC_Init(&NVIC_InitStructure); //根據(jù)NVIC_InitStruct中指定的參數(shù)初始化外設(shè)NVIC寄存器
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//開啟中斷
USART_Cmd(USART2, ENABLE); //使能串口
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2,USART_IT_RXNE))
{
/*構(gòu)建字節(jié)FIFO*/
usart2_ch=USART_ReceiveData(USART2);
usart2_rx_buf[usart2len++]=usart2_ch;
}
}
void Usart2Send(u8 *buf2)
{
int i;
for(i=0;i<8;i++)
{
USART_SendData(USART2, buf2[i]);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){}; //等待發(fā)送完成
}
return;
}
void delay_ms1(u16 time)
{
u16 y=0;
while(time--)
{
y=12000; //????
while(y--);
}
}
void numinit()
{
int h;
for( h=0;h<12;h++)
{
usart1_rx_buf[h]=0;
}
}
u16 crc_check(u8 *ptr , int len)
{
u16 crc16=0xffff;
int i,j,tmp;
for(i=0;i<len;i++)
{
crc16=*ptr^crc16;
for(j=0;j<8;j++)
{
tmp=crc16&0x0001;
crc16=crc16>>1;
if(tmp)
crc16=crc16^0xa001;
}
*ptr++;
}
return crc16;
}
int main(void)
{
union data a;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置中斷優(yōu)先級分組為組2:2位搶占優(yōu)先級,2位響應(yīng)優(yōu)先級
LED_Init(); //初始化與LED連接的硬件接口
KEY_Init();
My_USART1_Init();
RS485_Init(9600);
Usart1Send(num2500);
delay_ms1(1000);
numinit();
while(1)
{
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
Usart1Send(num2600);
if(usart1len>=13)
{
a.A[0]=usart1_rx_buf[3];
a.A[1]=usart1_rx_buf[4];
a.A[2]=usart1_rx_buf[5];
a.A[3]=usart1_rx_buf[6];
fdata=a.Temp;
a.A[0]=usart1_rx_buf[7];
a.A[1]=usart1_rx_buf[8];
a.A[2]=usart1_rx_buf[9];
a.A[3]=usart1_rx_buf[10];
pfvalue=a.Temp;
usart1len=0;
}
delay_ms1(100);
}
}
復(fù)制代碼
作者:
haxvn
時(shí)間:
2017-12-25 20:45
thanks,好資料,51黑有你更精彩!!!
作者:
洋洋灑灑
時(shí)間:
2018-6-4 22:23
51黑有你更精彩!!!
作者:
xiancv123
時(shí)間:
2018-6-8 09:13
謝謝,收藏了
作者:
ROS
時(shí)間:
2018-6-27 13:22
謝謝,收藏了,
51黑有你更精彩!!!
作者:
wenky
時(shí)間:
2018-11-29 15:08
haxvn 發(fā)表于 2017-12-25 20:45
**** 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽 ****
真好真好真好
作者:
cq740873729
時(shí)間:
2018-12-13 16:21
謝謝,收藏了,
作者:
晨星123
時(shí)間:
2019-1-13 11:48
有沒有防真圖
作者:
樂獨(dú)
時(shí)間:
2019-2-15 10:37
參考參考
作者:
winkle
時(shí)間:
2019-6-29 21:02
要是有引腳接線說明就更好了
作者:
winkle
時(shí)間:
2019-6-29 21:03
很不錯(cuò)
作者:
QL5470
時(shí)間:
2021-11-10 15:57
你好,有沒有具體的引腳連接圖啊
作者:
860741907
時(shí)間:
2022-6-29 08:59
51黑有你更精彩!!!
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1