/******************************************************************************
Title:    encoder -> PWM
Author:   rue_mohr
Date:     Aug 19 2005
Software: AVR-GCC 3.3 
Hardware: atmega32
  
                         ATmega32 - 40 PIN DIP LAYOUT FOR DECODER AND PWM - 
                                      _______________________
                                      [ 1]PB0         PA0[40] <- up button
                                      [ 2]PB1         PA1[39] <- down button
                                      [ 3]PB2         PA2[38] 
                                      [ 4]PB3         PA3[37] 
                                      [ 5]PB4         PA4[36] 
                                      [ 6]PB5         PA5[35] 
                                      [ 7]PB6         PA6[34] 
                                      [ 8]PB7         PA7[33] 
                             RESET -> [ 9]RST        AREF[32] <- +5V VCC
                            5V VCC -> [10]VCC         GND[31] <- GROUND
                            GROUND -> [11]GND        AVCC[30] <- +5V VCC
                       16 MHZ XTAL -> [12]XTAL1       PC7[29] -> LCD DATA 7
                       16 MHZ XTAL -> [13]XTAL2       PC6[28] -> LCD DATA 6
                                      [14]PD0         PC5[27] -> LCD DATA 5
                                      [15]PD1         PC4[26] -> LCD DATA 4
                                      [16]PD2         PC3[25] -> LCD E  
                                      [17]PD3         PC2[24] -> LCD RS 
                                      [18]PD4         PC1[23] -> LCD RW 
                                      [19]PD5         PC0[22]                  
                                      [20]PD6         PD7[21]  
                                      ______________________                         


what this should do:
it should tell you what buttons went down.

to test this properly, 
  push down button 1 and hold it,   "button 1 went down"
  push button 2 and let it up       "butotn 2 went down"
  let up button 1                   "butotn 2 went down"

buttons shoudl be wired

                  +5V
                  |
              1K resistor
                  |
gnd------button---o---PortA
    
*******************************************************************************/
 
/****************************| INCLUDGEABLES |********************************/
 
#include <avr/io.h>
#include <avr/signal.h>
#include "global.h"
#include "rprintf.h"
#include "timer.h"
#include "lcd.h"

/*****************************| DEFINIATIONS |********************************/
  
#define OUTPUT             1
#define INPUT              0

#define IsHigh(BIT, PORT)    ((PORT & (1<<BIT)) != 0)
#define IsLow(BIT, PORT)     ((PORT & (1<<BIT)) == 0)
#define SetBit(BIT, PORT)     PORT |= (1<<BIT)
#define ClearBit(BIT, PORT)   PORT &= ~(1<<BIT)

// which bit the buttons are on (porta)
#define Button1         0
#define Button2         1
 
/*****************************| VARIABLES |********************************/
 
volatile int AdcValues[8];

 
/************************| FUNCTION DECLARATIONS |*************************/
 
void AnalogInit (void);
int Analog (int n);
 
/****************************| CODE... |***********************************/
 
int main (void)  {

  lcdInit();                  // initialize LCD on PortC        
  rprintfInit(lcdDataWrite);  // direct printf output to LCD
  lcdClear();                 // Clear LCD
  AnalogInit();
   	        
  DDRA = (INPUT << PA0)|(INPUT << PA1)|(INPUT << PA2)|(INPUT << PA3)|(INPUT << PA4)|(INPUT << PA5)|(INPUT << PA6)|(INPUT << PA7);
  // port B for lcd  
  DDRC = (INPUT << PC0)|(INPUT << PC1)|(INPUT << PC2)|(INPUT << PC3)|(INPUT << PC4)|(INPUT << PC5)|(INPUT << PC6)|(INPUT << PC7);
  DDRD = (INPUT << PD0)|(INPUT << PD1)|(INPUT << PD2)|(INPUT << PD3)|(INPUT << PD4)|(INPUT << PD5)|(INPUT << PD6)|(INPUT << PD7);        
  
  sei();
  
  while(1){
    lcdGotoXY(0,0); // Char 1, LCD Line 2
   //        1234567890123456789012345678
    rprintf("1:%x 2:%x 3:%x 4:%x ", Analog(0)/4, Analog(1)/4, Analog(2)/4, Analog(3)/4);
    
    lcdGotoXY(0,1); // Char 1, LCD Line 2
   //        1234567890123456789012345678
    rprintf("5:%x 6:%x 7:%x 8:%x ", Analog(4)/4, Analog(5)/4, Analog(6)/4, Analog(7)/4);
  
  }
  return 0;
}
    
//------------------------| FUNCTIONS |------------------------

void AnalogInit (void) {

  int i;

  // Activate ADC with Prescaler 
  ADCSRA =  1 << ADEN  |
            1 << ADSC  | /* start a converstion, irq will take from here */
            0 << ADATE |
            0 << ADIF  |
            1 << ADIE  | /* enable interrupt */
            1 << ADPS2 |
            0 << ADPS1 |
            0 << ADPS0 ;
                        
            
  for (i = 0; i < 8; AdcValues[i++] = 0);
  
}

int Analog (int n) {
  return AdcValues[n & 7];
}

SIGNAL(SIG_ADC) {
  int i;
  
  i = ADMUX & 7;       // what channel we on?
  AdcValues[i] = ADC;  // save value
  i++;                 // next value
  ADMUX = i & 7;       // select channel
  ADCSRA |= _BV(ADSC); // start next conversion
  return;
}


