Subversion Repositories group.electronics

Rev

Rev 48 | Rev 51 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>

#include "petelib.h"
#include <util/delay.h>

#include <stdlib.h>
#include <string.h>

#include "wire.h"
#include "lcd.h"


volatile uint8_t pcIntCurr[3] = {0,0,0};
volatile uint8_t pcIntLast[3] = {0,0,0};
volatile uint8_t pcIntMask[3] = {0,0,0};

volatile uint8_t tmr0_ovf = 0;
volatile uint16_t systime = 0;

uint8_t analogRead(uint8_t);

uint8_t emblock[] = {   0B00011111,
                        0B00010001,
                        0B00010001,
                        0B00010001,
                        0B00010001,
                        0B00010001,
                        0B00010001,
                        0B00011111 };
                        

int main(void) {

  ACSR |= (1<<ACD); // Disable analog comparator

  /*
        Setup ADC
        ADMUX: 8 bit mode, Avcc ref
        ADCSRA: Enable, 128 prescale
  */
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;

  /*
  DDR : 1 = Output, 0 = Input
  PORT: 1 = Pullup for Input, otherwise set output
  PIN : Read input pin
  */

  /*
        PB0     - Output                - Keypad 2
        PB1     - Output                - Keypad 7
        PB2     - Output                - Keypad 6
        PB3     - Output                - Keypad 4
        PB4     - Input, Pullup         - Function select
        PB5     - Input, Pullup         - Function select
  */
  DDRB          = 0B00001111;
  PORTB         = 0B00111111;

  DDRC          = 0B00000000;
  PORTC         = 0B00110000;

  /*
        PD0     - Input, Pullup, PCINT16        - Rotary 1a
        PD1     - Input, Pullup, PCINT17        - Rotary 1b


        PD4     - Output                - Keypad select status led
        PD5     - Input, Pullup         - Keypad 3
        PD6     - Input, Pullup         - Keypad 1
        PD7     - Input, Pullup         - Keypad 5
  */
  DDRD          = 0B00010000;
  PORTD         = 0B11100011;

  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts

  // Setup timer0 - Enable overflow, 8 times prescaler
        TIMSK0 = (1<<TOIE0);                    // Eable timer overflow for Timer0
        TCNT0 = 0x00;                           // Set Timer0 to 0
        TCCR0B = (1<< CS01) ;                   // /8 prescaler

  sei();

//      i2c_beginTransmission(0x27);
//      i2c_writeByte(0x01);
//      i2c_endTransmission(1);

        i2c_master();
        lcd_init();
        lcd_createChar(0x00, emblock);

  wdt_enable(WDTO_8S);

        uint16_t oldsystime = systime; 

        char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
        lcd_setCursor(0, 1);
        lcd_print(strTime);

  for(;;) {
    wdt_reset();
        if (oldsystime != systime) {
                oldsystime = systime;
                char syschar[6];
                itoa(systime, syschar, 10);
                syschar[5] = 0x00;
                lcd_overprint(syschar, 5, 6, 1);

                uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
                lcd_pergraph(potVal, 0, 0);
                //char pot[4];
                //itoa(analogRead(0), pot, 10);
                //ilcd_overprint(pot, 4, 0, 0);
        }
  }
}

uint8_t analogRead(uint8_t pin) {
        ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
        ADCSRA |= (1<<ADSC);            // Start converting

        while (((ADCSRA >> ADSC) & 1)) {}       //Wait until conversion finished
        uint8_t result = ADCH;
        //ADCSRA |= (0<<ADSC);          // Stop converting

        return result;
}


/*
 *
 * Process the Pin Change Interrupt.
 * pcint provides what bank caused the interrupt
 *
 */
void doInt(uint8_t pcint) {

        // Clear the mask so we know we've delth with it
        pcIntMask[pcint] = 0;
}

ISR(TIMER0_OVF_vect) {
        tmr0_ovf++;
        if (tmr0_ovf==255)
                systime++;
}

ISR(PCINT1_vect)
{
        // Save the state and work out which pin caused
        //  the interrupt to occur
        pcIntCurr[1] = PIND;
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
        pcIntLast[1] = pcIntCurr[1];
        doInt(1);
}

ISR(PCINT2_vect)
{
        // Save the state and work out which pin caused
        //  the interrupt to occur
        pcIntCurr[2] = PIND;
        pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
        pcIntLast[2] = pcIntCurr[2];
        doInt(2);
}