附件是使用浊度与叶绿素传感器检测水中浊度与叶绿素值得程序
采用modbus rtu 通讯方式
所有资料51hei提供下载:
禹山叶绿素 -CRC.rar
(329.59 KB, 下载次数: 293)
单片机源程序如下:
- #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; /*定义相关结构体*/
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//① 使能相应的时钟
- 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);//② 初始化相应的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);//② 初始化相应的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;//校验
- USART_InitStructure.USART_StopBits=USART_StopBits_1;//停止位
- USART_InitStructure.USART_WordLength=USART_WordLength_8b;//数据长度
-
- USART_Init(USART1,&USART_InitStructure);//③ 初始化串口相关参数
-
- 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;//设置中断优先级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//子优先级
- 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){}; //等待发送完成
- }
- 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时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟
-
- 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; //复用推挽
- 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);//复位串口2
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止复位
-
- USART_InitStructure.USART_BaudRate = bound;//波特率设置
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位数据长度
- USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
- USART_InitStructure.USART_Parity = USART_Parity_No;///奇偶校验位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//收发模式
- USART_Init(USART2, &USART_InitStructure); ; //初始化串口
-
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //使能串口2中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //先占优先级2级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
-
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
-
- USART_Cmd(USART2, ENABLE); //使能串口
- }
- void USART2_IRQHandler(void)
- {
-
- if(USART_GetITStatus(USART2,USART_IT_RXNE))
- {
- /*构建字节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){}; //等待发送完成
- }
- 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);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- 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);
- }
- }
复制代码
|