使用电阻应变片贴合在形变梁的形变点上,把被测件上的因为重力变化而产生的应变变化转换成电信号,而后通过信号处理电路,进行数据传输。处理电路采用ADS1247芯片,进行信号调理与A/D转换模块和并使用STM32单片机进行控制和数据处理。通过串口通信,将重量数据传输给STC12C5A60S2单片机构建的外围功能模块,实现被测物品重量信息的显示及其他辅助功能。
附有完成的源代码,欢迎各位下载参考。
单片机源程序如下:
- #include "system.h"
- #include "sht11.h"
- #include "ads1118.h"
- #include "ads1247.h"
- #include "ms5803.h"
- #define RTD_DRDY (GPIOC->IDR & BIT(9))
- //测试时屏蔽该参数,从而屏蔽设备检测
- //#define NOTEST
- //#define TEST_MODE
- #define FLASH_ADDRESS 0x807f800
- //
- //#define NBLE
- #define BIT(n) (1<<(n))
- #define ADDR 0x2000c000
- #define BUF_SIZE 64 //滑动滤波队列大小
- #define SQU_SIZE 127 //中值滤波队列大小
- #define SHIFTERR_THR 20 //误差门限
- #define DESHIFT_tiMSLOT 100 //去除漂移间隔时间
- void USART2_Configuration(USART_TypeDef *);
- unsigned char RxBuffer1[40];
- unsigned char RxCounter1;
- unsigned char P2PUart_TestFlag = 0; //P2P命令串口测试接收完成标志
- unsigned char Uart3RecFinishFlag = 0; //串口3接收完成标志
- unsigned char Uart2RecFinishFlag = 0;
- unsigned char RxBuffer2[20];
- unsigned char RxCounter2;
- unsigned char RxBuffer3[20];
- unsigned char RxCounter3;
- signed int Zero_Point = 0;
- signed int Ten_Point = 0;
- signed int Fifty_Point = 0;
- signed int TwoHundred_Point = 0;
- //signed int M_Buf[BUF_SIZE];
- //signed int S_Buf[SQU_SIZE];
- signed int Out_AdcData = 0;
- float Slot = 0;
- unsigned char Uart2_NEmptyFlag = 0;
- unsigned int Uart2_Tim = 0;
- unsigned int Tim = 0;
- extern vu16 ADCConvertedValue[16];
- void Delay_ms(unsigned int Time)
- {
- unsigned int n;
- while(Time--)
- for(n=0;n<1000;n++);
- }
- void SysTick_IRQHandlar(void)
- {
- }
- unsigned char Str_Cmp(unsigned char *Str1,unsigned char *Str2)
- {
- while((*Str1) != '\0')
- {
- if(*(Str1++) != *(Str2++))
- {
- return 0;
- }
- }
- return 1;
- }
- unsigned char Str_CmpNum(unsigned char *Str1,unsigned char *Str2,unsigned char Num)
- {
- unsigned char n;
- for(n=0;n {
- if(*(Str1++) != *(Str2++))
- {
- return 0;
- }
- }
- return 1;
- }
- void Quick_Sort(signed int *start,signed char num)
- {
- signed char i=0,j;
- j=num;
- if(num <= 0)
- return;
- while(i != j)
- {
- while(*start <= *(start + j) && i {
- j--;
- }
- while(*start >= *(start + i) && i {
- i++;
- }
- if(i {
- *(start+i) ^= *(start+j);
- *(start+j) ^= *(start+i);
- *(start+i) ^= *(start+j);
- }
- }
- if(i)
- {
- *start ^= *(start+i);
- *(start+i) ^= *start;
- *start ^= *(start+i);
- }
- Quick_Sort(start,i-1);
- Quick_Sort(start+i+1,num-i-1);
- }
- signed int Shift_Revise = 0;
- unsigned char DeShift_Tim = 0;
- unsigned char DeShift_Counter = 0;
- signed int Shift_Err = 0;
- signed int Shift_SumErr = 0;
- signed int Shift_LastSum = 0;
- signed char Shift_Err2[10] = {0,0,0,0,0,0,0,0,0,0};
- //signed char Err2_Buf[10];
- signed int Last_Val = 0;
- void De_Shift(void)
- {
- unsigned char n;
- signed int temp = 0;
- Shift_Err = Out_AdcData - Last_Val;
- Last_Val = Out_AdcData;
- if((Shift_Err < SHIFTERR_THR) && (Shift_Err > -SHIFTERR_THR))
- {
- Shift_SumErr += Shift_Err;
- // DeShift_Tim++;
- // if(DeShift_Tim>10)
- // {
- // DeShift_Tim = 0;
- // }
- // Shift_Err2[DeShift_Tim] = Shift_SumErr-Shift_LastSum;
- // for(n=0;n<10;n++)
- // {
- // temp += Shift_Err2[n];
- // }
- // if(temp > 50 || temp < -50)
- // {
- // Shift_SumErr -= temp;
- // for(n=0;n<10;n++)
- // {
- // Shift_Err2[n] = 0;
- // }
- // }
- // Shift_LastSum = Shift_SumErr;
- }
- else
- {
- //DeShift_Tim = 0;
- Shift_Revise += Shift_SumErr;
- }
- // if(DeShift_Tim > 10)
- // {
- // DeShift_Tim = 0;
- //
- //
- // if(Shift_Err2 > 50 || Shift_Err2 < -50)
- // {
- // Shift_SumErr -= Shift_Err2;
- // }
- // Shift_LastSum = Shift_SumErr;
- // }
- // if(DeShift_Tim > DESHIFT_TIMSLOT)
- // {
- // DeShift_Tim = 0;
- // Shift_Revise = Shift_SumErr;
- // }
- #ifdef TEST_MODE
- printf("Err %d Error2 %d ",Shift_Err,temp);
- printf("Shift Sum Err is %d ",Shift_SumErr);
- #endif
- }
- void Delay_us(unsigned int nTime)
- {
- while(nTime--);
- }
- void Save_FlashData(void)
- {
- unsigned char n;
- signed int temp,temp1,temp2;
- temp = TwoHundred_Point-Zero_Point;
- FLASH_Unlock();
- FLASH_ErasePage(FLASH_ADDRESS);
- FLASH_ProgramHalfWord(FLASH_ADDRESS,0xAA55);
- temp1 = (unsigned int)temp & 0x0000FFFF;
- FLASH_ProgramHalfWord(FLASH_ADDRESS+2,temp1);
- temp2 = (((unsigned int)temp) >> 16) & 0x0000ffFF;
- FLASH_ProgramHalfWord(FLASH_ADDRESS+4,temp2);
- FLASH_ProgramHalfWord(FLASH_ADDRESS+6,0x33CC);
- FLASH_Lock();
- #ifdef TEST_MODE
- printf("Two Handred Data is %d ,Zero_Point is %d ,Temp is %d rn",TwoHundred_Point,Zero_Point,temp);
- printf("temp1 is %d ,temp2 is %d rn",temp1,temp2);
- printf("rn....Slot is %f...........rn",Slot);
- #endif
- }
- void Read_FlashData(void)
- {
- u16 temp[4];
- unsigned int Temp = 0;
- temp[0] = *(u16*)(FLASH_ADDRESS);
- temp[1] = *(u16*)(FLASH_ADDRESS + 2);
- temp[2] = *(u16*)(FLASH_ADDRESS + 4);
- temp[3] = *(u16*)(FLASH_ADDRESS + 6);
- if((temp[0] == 0xAA55) && (temp[3] == 0x33CC))
- {
- Temp = (unsigned int)temp[1];
- Temp |= (unsigned int)temp[2] << 16;
- Slot = (float)(signed int)Temp/(float)200;
- #ifdef TEST_MODE
- printf("Temp1 is %d,Temp2 is %d Temp Data is %d rn",temp[1],temp[2],Temp);
- printf("Flash Read Successfully....Slot is %f...........rn",Slot);
- #endif
- }
- else
- {
- #ifdef TEST_MODE
- printf("Flash Read Failed...............rn");
- #endif
- }
- }
- /**
- * @brief Main program
- * @param None
- * @retval None
- */
- int main (void)
- {
- unsigned int TIM = 0;
- unsigned int m,n=0,i;
- unsigned char off_p,off_n;
- signed int last_temp,temp,err;
- signed long OutAdc_Buf=0;
- unsigned short j=0;
- signed int *p;
- double calculate;
- signed long cal_buf = 0;
- signed int Sort_Buf[SQU_SIZE];
- unsigned char Sort_Tim=0;
- signed int Filtered_Data = 0;
- signed int Revised_Data = 0;
- unsigned char Stand_Flag = 0;
- unsigned char Stand_Tim = 0;
- signed int Stand_Buf = 0;
- float Ten_Slope = 0;
- float Fifty_Slope = 0;
- float TwoHundred_Slope = 0;
- unsigned char Init_Counter=60;
- // signed int disp;
- RCC_Configuration();
- NVIC_Configuration();
- GPIO_Configuration();
- Delay_ms(50);
- USART_Configuration(USART1);
- // USART2_Configuration(USART2); //配置串口2到控制台
- // USART_Configuration(USART3);
- SysTick_SetReload(9000);
- SysTick_ITConfig(ENABLE);
- SysTick_CounterCmd(SysTick_Counter_Enable);
- // adc_init();
- Delay_ms(50);
- #ifdef NBLE
- printf("$$$$$$$$44 My Test.... $$$$$$$ rn");
- printf("nr ############ www.csgsm.com ############ ("__DATE__ " - " __TIME__ ")nrnr");
- #endif
- // EXTI_Configuration();
- #ifdef NBLE
- printf("################scanning finish####################### rn");
- #endif
- #ifdef NBLE
- printf("################lwIP stack finish####################### rn");
- #endif
- // ADS1118_Init();
- LED2(1);
- watch_dog_config(); //看门狗测试
- RTD_Init();
- p = (signed int *)0x2000c000;
- //Ms_Reset();
- //MS5803_Init();
- //RTD_ContinueStart();
- cal_buf = 0;
- for(n=0;n {
- cal_buf += *(p+n);
- }
- Read_FlashData();
- while(1)
- {
- if(!(GPIOC->IDR & BIT(11)))
- {
- Stand_Flag = 1;
- }
- // if(!(GPIOC->IDR & BIT(12)))
- // {
- // Stand_Flag = 2;
- // }
- //
- // if(!(GPIOD->IDR & BIT(2)))
- // {
- // Stand_Flag = 3;
- // }
- if(!(GPIOB->IDR & BIT(5)))
- {
- Stand_Flag = 4;
- }
- n++;
- if(n<100)
- {
- LED1(1);
- }
- else if(n<200)
- {
- LED1(0);
- }
- else
- {
- n=0;
- }
- // temp = (signed int)RTD_TestOnce();
- TIM++;
- Sort_Buf[Sort_Tim] = (signed int)RTD_TestOnce();
- //test_buf[temp_tim] = disp;
- Sort_Tim++;
- if(Sort_Tim >= SQU_SIZE)
- {
- Quick_Sort(Sort_Buf,SQU_SIZE-1);
- Sort_Tim = 0;
- cal_buf -= *(p+j);
- *(p+j) = Sort_Buf[SQU_SIZE/2];
- cal_buf += *(p+j);
- j++;
- if(j>=BUF_SIZE)
- {
- j=0;
- }
- }
- Filtered_Data = cal_buf/BUF_SIZE;
- // for(k=0;k// {
- // cal_buf += M_Buf[k];
- // }
- OutAdc_Buf += Filtered_Data;
- // test_buf[TIM-1] = temp;
- // test = 0;
- if(TIM >= 500)
- {
- if(Init_Counter)
- {
- Init_Counter--;
- LED2(Init_Counter%2);
- if(!Init_Counter)
- {
- Stand_Flag = 1;
- LED2(1);
- }
- }
- De_Shift();
- Out_AdcData = OutAdc_Buf/500;
- OutAdc_Buf = 0;
- Revised_Data = Out_AdcData-Shift_SumErr;
- TIM = 0;
- // if(Revised_Data// {
- // calculate = ((double)Revised_Data-Zero_Point)/Ten_Slope;
- // }
- // else if(Revised_Data// {
- // calculate = ((double)Revised_Data-Ten_Point)/Fifty_Slope+10;
- // }
- // else if(Revised_Data// {
- // calculate = ((double)Revised_Data-Fifty_Point)/TwoHundred_Slope+50;
- // }
- // else
- // {
- // calculate = ((double)Revised_Data-TwoHundred_Point)/TwoHundred_Slope+200;
- // }
- calculate = ((double)Revised_Data-Zero_Point)/Slot;
- if(Stand_Flag)
- {
- LED2(0);
- Stand_Tim++;
- Stand_Buf += Revised_Data;
- if(Stand_Tim >= 20)
- {
- switch(Stand_Flag)
- {
- case 1: //零点校准
- {
- Zero_Point = Stand_Buf/20;
- Stand_Buf = 0;
- }break;
- // case 2: //10克斜率校准
- // {
- // Ten_Point = Stand_Buf/20;
- // Ten_Slope = (float)(Ten_Point-Zero_Point)/(float)10;
- ……………………
- …………限于本文篇幅 余下代码请从电子发烧友下载附件…………
所有资料51hei提供下载:
stm32的电子称程序.rar
|
0
|
|
|
|