This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| pt:examples:sensor:thermistor [2015/12/11 20:17] – Criação deste novo documento. artica | pt:examples:sensor:thermistor [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Térmistor ====== | ||
| + | // | ||
| + | [HW] [[en: | ||
| + | [ELC] [[en: | ||
| + | [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| + | |||
| + | ===== Teoria ===== | ||
| + | |||
| + | [{{ :: | ||
| + | |||
| + | Um térmistor é um tipo de resistência em que a resistância varia com a temperatura. Existem dois tipos de térmistores: | ||
| + | |||
| + | A dependência da temperatura não é linear e isto complica o uso do mesmo. Para medições precisas de temperatura em fluctuações mais amplas é usada a equação exponencial de terceira ordem de Steinhart-Hart, | ||
| + | |||
| + | {{: | ||
| + | |||
| + | onde:\\ | ||
| + | * T< | ||
| + | * R< | ||
| + | * B - parâmetro B. | ||
| + | |||
| + | Parâmetro B é um coeficiente habitualmente fornecido na datasheet do térmistor. É somente suficiente constante estável entre certas gamas de temperatura, | ||
| + | |||
| + | Normalmente, | ||
| + | |||
| + | Com recursos limitados e com menos exigências de precisão, gráficos e tabelas préviamente calculados para temperaturas são usados. Geralmente as mesas têm gamas de temperaturas e respectivos valores de resistência, | ||
| + | |||
| + | ===== Prática ===== | ||
| + | |||
| + | O módulo de sensor do HomeLab está equipado com um térmistor NTC que tem 10 kΩ resistância nominal. Em temperaturas de 25-50 °C o parâmetro B do térmistor é de 3900. Um pino do térmitor está conectado à alimentação e o outro está ligado ao conversor analógico-digital (HomeLab II canal 2 e HomeLab III canal 14). Uma resistência típica de 10 kQ, também está ligada com o mesmo pino do microcontrolador e à terra, e em conjunto com o térmistor formam um divisor de tensão. Uma vez que estamos lidando com um térmistor NTC, cuja resistância diminui quando a temperatura aumenta; a tensão de saída do divisor de tensão está a aumentar com o crescente da temperatura. | ||
| + | |||
| + | |||
| + | Embora usando o AVR seja prático utilizar uma tabela de conversão de valores de temperatura e conversor analógico-digital para encontrar a temperatura correcta. É boa prática encontrar o valor correspondente do conversor analógico-digital para cada grau de temperatura do intervalo desejado de temperatura porque a tabela inversa será demasiado grande devido à quantidade de valores de ADC 10 bits. Recomenda-se usar qualquer tipo de programa de folha de cálculo (MS Excel, LibreOffice Calc, etc.) para fazer a tabela. A fórmula // | ||
| + | |||
| + | <code c> | ||
| + | // Table for converting temperature values to ADC values | ||
| + | // Every element of the array marks one Celsius degree | ||
| + | // Elements begin from -20 degree and end at 100 degree | ||
| + | // There are 121 elements in the array | ||
| + | const signed short min_temp = -20; | ||
| + | const signed short max_temp = 100; | ||
| + | |||
| + | const unsigned short conversion_table[] = | ||
| + | { | ||
| + | 91, | ||
| + | 160, | ||
| + | 257, | ||
| + | 375, | ||
| + | 501, | ||
| + | 619, | ||
| + | 720, | ||
| + | 799, | ||
| + | 859, | ||
| + | 903, | ||
| + | 934, | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | Seguindo algoritmo pode ser utilizado para encontrar a temperatura que corresponde aos parâmetros do ADC: | ||
| + | |||
| + | <code c> | ||
| + | // Converting the ADC values to Celsius degrees: | ||
| + | signed short thermistor_calculate_celsius(unsigned short adc_value) | ||
| + | { | ||
| + | signed short celsius; | ||
| + | |||
| + | // Covering the table backwards: | ||
| + | for (celsius = max_temp - min_temp; celsius >= 0; celsius--) | ||
| + | { | ||
| + | // If the value in the table is the same or higher than measured | ||
| + | // value, then the temperature is at least as high as the | ||
| + | // temperature corresponding to the element | ||
| + | if (adc_value >= conversion_table[celsius])) | ||
| + | { | ||
| + | // Since the table begins with 0 but values of the elements | ||
| + | // from -20, the value must be shifted | ||
| + | return celsius + min_temp; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // If the value was not found the minimal temperature is returned | ||
| + | return min_temp; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | As pesquisas de algoritmos vão desde a mesa onde o valor ADC se encontra, e adquire o escalão mais inferior deste intervalo. O número de classificação marca graus, adicionando a temperatura primária a esta temperatura com uma precisão de 1 grau. | ||
| + | |||
| + | Este quadro de conversão e função já estão na biblioteca do HomeLab, portanto, não há necessidade de escrevê-los para este exercício. Na biblioteca a função de conversão é chamada // | ||
| + | |||
| + | <code c> | ||
| + | // Example program of the thermistor of Sensors module | ||
| + | // The temperature is displayed on the LCD | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Robotic Homelab II | ||
| + | //#define ADC_CHANNEL 2 | ||
| + | |||
| + | // Robotic Homelab III | ||
| + | #define ADC_CHANNEL 14 | ||
| + | |||
| + | // Main program | ||
| + | int main(void) | ||
| + | { | ||
| + | unsigned short value; | ||
| + | signed short temperature; | ||
| + | char text[16]; | ||
| + | |||
| + | // Initialization of LCD | ||
| + | lcd_gfx_init(); | ||
| + | |||
| + | // Clearing the LCD and setting backlight | ||
| + | lcd_gfx_clear(); | ||
| + | lcd_gfx_backlight(true); | ||
| + | |||
| + | // Name of the program | ||
| + | lcd_gfx_goto_char_xy(1, | ||
| + | lcd_gfx_write_string(" | ||
| + | |||
| + | // Setting the ADC | ||
| + | adc_init(ADC_REF_AVCC, | ||
| + | |||
| + | // Endless loop | ||
| + | while (true) | ||
| + | { | ||
| + | // Reading the 4 times rounded values of the voltage of the | ||
| + | // thermistor | ||
| + | value = adc_get_average_value(ADC_CHANNEL, | ||
| + | |||
| + | // Converting the values of ADC into celsius scale | ||
| + | temperature = thermistor_calculate_celsius(value); | ||
| + | |||
| + | // Converting the temperature in to text | ||
| + | // To display the degree sign, the octal variable is 56 | ||
| + | sprintf(text, | ||
| + | |||
| + | // Displaying the text in the beginning of the third row of the LCD | ||
| + | lcd_gfx_goto_char_xy(5, | ||
| + | lcd_gfx_write_string(text); | ||
| + | |||
| + | hw_delay_ms(1000); | ||
| + | } | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||