BASIC4MCU | 질문게시판 | 아두이노 millis ->atmega128에서 사용하고 싶습니다.
페이지 정보
작성자 akmong413 작성일2022-05-19 16:22 조회487회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=20034
본문
안녕하세요, avr 공부중인 학생입니다.
제가 취미로 만들어 보고 싶은게 있는데 코드를 뒤져보니 아두이노 코드만 나오더라구요.
(sen0244써서 tds 측정하려고 합니다)
atmega128에서 사용해보고 싶어서 변환하려는데 millis함수가 최대 관건입니다ㅜㅜ
아무리 뒤져봐도 어떻게 바꿔야 하는지 잘 모르겠더군요...
아래 코드가 아두이노 코드입니다. 아시는 분은 꼭 답변 부탁드립니다ㅠ
#define TdsSensorPin A1#define VREF 5.0 // analog reference voltage(Volt) of the ADC#define SCOUNT 30 // sum of sample pointint analogBuffer[SCOUNT]; // store the analog value in the array, read from ADCint analogBufferTemp[SCOUNT];int analogBufferIndex = 0,copyIndex = 0;float averageVoltage = 0,tdsValue = 0,temperature = 25;void setup(){Serial.begin(115200);pinMode(TdsSensorPin,INPUT);}void loop(){static unsigned long analogSampleTimepoint = millis();if(millis()-analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC{analogSampleTimepoint = millis();analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the bufferanalogBufferIndex++;if(analogBufferIndex == SCOUNT)analogBufferIndex = 0;}static unsigned long printTimepoint = millis();if(millis()-printTimepoint > 800U){printTimepoint = millis();for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)analogBufferTemp[copyIndex]= analogBuffer[copyIndex];averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage valuefloat compensationCoefficient=1.0+0.02*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));float compensationVolatge=averageVoltage/compensationCoefficient; //temperature compensationtdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5; //convert voltage value to tds value//Serial.print("voltage:");//Serial.print(averageVoltage,2);//Serial.print("V ");Serial.print("TDS Value:");Serial.print(tdsValue,0);Serial.println("ppm");}}int getMedianNum(int bArray[], int iFilterLen){int bTab[iFilterLen];for (byte i = 0; i<iFilterLen; i++)bTab[i] = bArray[i];int i, j, bTemp;for (j = 0; j < iFilterLen - 1; j++){for (i = 0; i < iFilterLen - j - 1; i++){if (bTab[i] > bTab[i + 1]){bTemp = bTab[i];bTab[i] = bTab[i + 1];bTab[i + 1] = bTemp;}}}if ((iFilterLen & 1) > 0)bTemp = bTab[(iFilterLen - 1) / 2];elsebTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;return bTemp;}
댓글 0
조회수 487등록된 댓글이 없습니다.