Skip to content

Instantly share code, notes, and snippets.

@RicoElectrico
Created June 2, 2018 19:09
Show Gist options
  • Save RicoElectrico/42735602afa151d1402d9b844fd616c5 to your computer and use it in GitHub Desktop.
Save RicoElectrico/42735602afa151d1402d9b844fd616c5 to your computer and use it in GitHub Desktop.
Atmega8 LCD thermometer
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "lcd.h" // HD44780U LCD library, Peter Fleury
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <stdbool.h>
#define F_CPU 1000000
void iprint(int n)
{
if( n > 9 )
{ int a = n / 10;
n -= 10 * a;
iprint(a);
}
lcd_putc('0'+n);
}
void lcd_putnum(int n, bool decimal_point){
if (n<0) {
lcd_putc('-');
n=-n;
}
if (decimal_point){
iprint(n/10);
lcd_putc('.');
iprint(n%10);
} else iprint(n);
}
const int16_t values[] PROGMEM = {4078,3122,2600,2241,1965,1741,1550,1382,1232,1095,969,850,737,629,
524,423,323,224,125,25,-76,-179,-287,-399,-519,-649,-793,-957,-1155,-1414,-1816};
int adc2temp(int x){
const int min=32;
const int step=32;
int i, y1, y0, x0;
if ( (x>=min) && (x<=min+step* (sizeof(values)/sizeof(int16_t) - 1) ) ){
i=(x-min)/step;
y1 = (int16_t)pgm_read_word( &values[i+1] );
y0 = (int16_t)pgm_read_word( &values[i] );
x0=min+step*i;
return ( y0 + ( (y1-y0) * (x-x0) ) / step ) / 4;
} else return -9999;
}
ISR (TIMER0_OVF_vect) // timer0 overflow interrupt
{
cli();
ADCSRA |= (1<<ADSC);
sei();
}
ISR (ADC_vect){
cli();
int temp;
temp=adc2temp(ADC);
lcd_gotoxy(0,0);
lcd_putnum(temp, true);
lcd_puts(" | ");
lcd_putnum(ADC, false);
lcd_puts(" "); // potrzebne na wypadek gdy wcześniejszy napis był krótszy niż obecny
sei();
}
int main(void)
{
/* initialize display, cursor off */
lcd_init(LCD_DISP_ON);
TCCR0 |= (1 << CS02) | (1 << CS00);
// set prescaler to 1024 and start the timer
TIMSK |= (1 << TOIE0);
ADMUX |= (1 << REFS0) | (1 << MUX2) | (1 << MUX0); //adc5 - pin 28, ref=avcc
ADCSRA=(1<<ADEN)|(1<<ADIE)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2);
sei();
while(1) {
//set_sleep_mode(SLEEP_MODE_IDLE);
//sleep_enable();
}
// zatrzymujemy CPU, cała reszta działa i wybudza przerwaniami
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment