GAPHF

### POTENTIOMETER ###

` #include <avr/io.h>
` #include <util/delay.h>
`
` #define F_CPU  1000000
` #define SEG1_LATCH   PB0
` #define SEG2_LATCH   PB1
` #define POWER_LATCH  PB6
` #define LED_LATCH    PB7
`
` unsigned char font[] = {
`   0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
`   0x7f, 0x6f, 0x77, 0x7c, 0x58, 0x5e, 0x79, 0x71
` };
`
` // initialize adc
` void adc_init()
` {
`   // AREF = AVcc
`   ADMUX = (1<<REFS0);
`
`   // ADC Enable and prescaler of 128
`   // 16000000/128 = 125000
`   ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
` }
`
` // read adc value
` uint16_t adc_read(uint8_t ch)
` {
`   // select the corresponding channel 0~7
`   // ANDing with '7' will always keep the value
`   // of 'ch' between 0 and 7
`   ch &= 0b00000111;  // AND operation with 7
`   // clears the bottom 3 bits before ORing
`   ADMUX = (ADMUX & 0xF8)|ch;
`
`   // start single conversion
`   // write '1' to ADSC
`   ADCSRA |= (1<<ADSC);
`
`   // wait for conversion to complete
`   // ADSC becomes '0' again
`   // till then, run loop continuously
`   while(ADCSRA & (1<<ADSC));
`
`   return (ADC);
` }
`
`   void init_board() {
`     DDRD = 0xff;
`     PORTD = 0xff;
`     DDRB = 0xc3;
`
`     PORTB |= (1<<SEG1_LATCH);
`     PORTB &= ~(1<<SEG1_LATCH);
`     PORTB |= (1<<SEG2_LATCH);
`     PORTB &= ~(1<<SEG2_LATCH);
`     PORTB |= (1<<POWER_LATCH);
`     PORTB &= ~(1<<POWER_LATCH);
`     PORTB |= (1<<LED_LATCH);
`     PORTB &= ~(1<<LED_LATCH);
`   }
`
` int main()
` {
`   uint16_t adc_result3;
`     init_board();
`   adc_init();
`
`   _delay_ms(50);
`
`   while(1)
`   {
`     // read adc value at PC1
`     adc_result3 = adc_read(3)/10.3;
`     PORTD = ~font[adc_result3/10];
`     PORTB = 0x02;
`     PORTB = 0;
`     PORTD = ~font[adc_result3%10];
`     PORTB = 0x01;
`     PORTB = 0;
`     _delay_ms(50);
`   }
` }

### IR-RECEIVER ###

` #include <avr/io.h>
` #include <util/delay.h>
`
` #define F_CPU  1000000
` #define SEG1_LATCH   PB0
` #define SEG2_LATCH   PB1
` #define POWER_LATCH  PB6
` #define LED_LATCH    PB7
`
`  const char font[] = {
`    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
`    0x80, 0x90, 0x88, 0x83, 0xA7, 0xA1, 0x86, 0x8E,
`    0xC2, 0x8B, 0xE6, 0xF2, 0x89, 0xC7, 0xC9, 0xAB,
`    0xA3, 0x8C, 0x98, 0xAF, 0x93, 0x87, 0xC1, 0xE3,
`    0x81, 0xEB, 0x91, 0xB6, 0xFF, 0xF7, 0xBF, 0xB7
`  };
`
` // initialize adc
` void adc_init()
` {
`   // AREF = AVcc
`   ADMUX = (1<<REFS0);
`
`   // ADC Enable and prescaler of 128
`   // 16000000/128 = 125000
`   ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
` }
`
` // read adc value
` uint16_t adc_read(uint8_t ch)
` {
`   // select the corresponding channel 0~7
`   // ANDing with '7' will always keep the value
`   // of 'ch' between 0 and 7
`   ch &= 0b00000111;  // AND operation with 7
`   // clears the bottom 3 bits before ORing
`   ADMUX = (ADMUX & 0xF8)|ch;
`
`   // start single conversion
`   // write '1' to ADSC
`   ADCSRA |= (1<<ADSC);
`
`   // wait for conversion to complete
`   // ADSC becomes '0' again
`   // till then, run loop continuously
`   while(ADCSRA & (1<<ADSC));
`
`   return (ADC);
` }
`
` void init_board() {
`   DDRD = 0xff;
`   PORTD = 0xff;
`   DDRB = 0xc3;
`
`   PORTB |= (1<<SEG1_LATCH);
`   PORTB &= ~(1<<SEG1_LATCH);
`   PORTB |= (1<<SEG2_LATCH);
`   PORTB &= ~(1<<SEG2_LATCH);
`   PORTB |= (1<<POWER_LATCH);
`   PORTB &= ~(1<<POWER_LATCH);
`   PORTB |= (1<<LED_LATCH);
`   PORTB &= ~(1<<LED_LATCH);
` }
`
` int main()
` {
`   uint16_t a, aold;
`   int c = 0;
`   init_board();
`   adc_init();
`
`   _delay_ms(50);
`
`   aold = 0;
`   while(1)
`   {
`     // read adc value at PC1 0..1023
`     a = 255 - adc_read(0)/4;
`     if ((a <= aold) && (c > 0)) {
`       a = aold; c--;
`     }
`     else {
`       aold = a; c = 300;
`     }
`     PORTD = font[a/16];
`     PORTB = 0x02;
`     PORTB = 0;
`     PORTD = font[a%16];
`     PORTB = 0x01;
`     PORTB = 0;
`     _delay_ms(5);
`   }
` }