Files @ e9fb1e1bfee0
Branch filter:

Location: seniordesign-firmware/slave/slave/lib/inputOutput.c

mkroening@CL-ENS241-07.cedarville.edu
Camera functions
/*
 * io.c
 *
 * Created: 11/7/2012 7:17:52 PM
 *  Author: kripperger
 */ 

#include <avr/io.h>
#include "../config.h"
#include "inputOutput.h"
#include "led.h"
#include "sensors.h"

int8_t	moduleID;	// Slave Module ID from rotary dip switch (or EEPROM)

 void io_configure()
 {
	// Configure ports/pins
	DDRB |= (1 << DDB4);		// Set PB4 to Output for Heater (also allows SCK (on SPI bus) to operate)

	DDRC &= ~(1 << DDC2);		// Set PC2 to input for rotary dip
	DDRC &= ~(1 << DDC3);		// Set PC3 to input for rotary dip
	DDRC &= ~(1 << DDC4);		// Set PC4 to input for rotary dip
	DDRC &= ~(1 << DDC5);		// Set PC5 to input for rotary dip
	
	DDRA &= ~(1 << DDA7);		// Set PA7 to input for battery voltage divider

	//ADC register configurations for battery level detection on PA7
	ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);	// Set prescaler for ADC, 128 gives ADC freq of 125 KHz
	ADMUX |= (1 << REFS0);									// Set ADC reference voltage to AVCC
	//ADMUX |= (1 << ADLAR);								// Sets 10 bit ADC to 8 bit
	ADMUX |= (1 << MUX2) | (1 << MUX1) | (1 << MUX0);		// Select ADC7 as the conversion channel
	ADCSRA |= (1 << ADATE);									// Enables auto trigger, determined in ADCSRB bits ADTS
	//ADCSRA |= (1 << ADIF);								// 
	//ADCSRA |= (1 << ADIE);								// ADC interrupt enable set
	ADCSRB &= ~((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0));// Set ADC auto trigger source to free running mode
	ADCSRA |= (1 << ADEN);									// Enable ADC
	ADCSRA |= (1 << ADSC);									// Start ADC measurements.  ADC should now continuously run conversions, which are stored in ADCH 0x79
	
 }
 
  
 void io_readModuleId()
 {
	// Get ID from rotary dip and return it. 
	moduleID = 0;
	
	/*
	// This method is temporary as the next release will read the module ID from EEPROM
	PORTC |= (1 << PC2);	// Pull pins on rotary dip high
	PORTC |= (1 << PC3);	// Pull pins on rotary dip high
	PORTC |= (1 << PC4);	// Pull pins on rotary dip high
	PORTC |= (1 << PC5);	// Pull pins on rotary dip high
	
	moduleID = ((PINC & 0b00011100) >> 2);		// Read Dip Encoder
	moduleID = ~moduleID;						//Invert Dip reading
	moduleID = (moduleID & 0b0111);				//Mask bits
	*/

	while(moduleID==0)           //UNCOMMENT ALL THIS
	{
		moduleID = i2c_read(EEPROM_ADDR, 0x05);
	}
 }
 
 
 uint8_t io_getModuleId()
 {
	return moduleID;
 }
 
 
 void io_heaterOn()
 {		
	PORTB |= (1 << PB4);	//ON
 }
 
 void io_heaterOff()
 {
	PORTB &= ~(1 << PB4);	//OFF
 }
 
 uint8_t io_heaterStatus()
 {
	 uint8_t state;
	 state = 0;
	 
	 state = ((PORTB & 0b00010000) >> 4);
	 
	 return state;
 }
 
  void io_regulateTemp()
  {
	  // Gets board temperature and enables heater if below threshold
	  if (sensors_getBoardTemp() <= HEATER_THRESHOLD)
	  {
		  io_heaterOn();
		  led_on(3);
	  } 
	  else if (sensors_getBoardTemp() > (HEATER_THRESHOLD + 5))
	  {
		  io_heaterOff();
		  led_off(3);
	  }
  }