//*****************************************************************************
// File Name : a2dtest.c
//
// Title : example usage of some avr library functions
// Revision : 1.0
// Notes :
// Target MCU : Atmel AVR series
// Editor Tabs : 4
//
// Revision History:
// When Who Description of change
// ----------- ----------- -----------------------
// 20-Oct-2002 pstang Created the program
//*****************************************************************************
//----- Include Files ---------------------------------------------------------
#include <avr/io.h> // include I/O definitions (port names, pin names, etc)
#include <avr/signal.h> // include "signal" names (interrupt names)
#include <avr/interrupt.h> // include interrupt support
#include "global.h" // include our global settings
#include "uart.h" // include uart function library
#include "rprintf.h" // include printf function library
#include "timer.h" // include timer function library (timing, PWM, etc)
#include "buffer.h" // include buffer function library
#include "a2d.h" // include A/D converter function library
#include "vt100.h" // include VT100 terminal support
#define NOS 20000 // Define number of milliseconds between collects
#define NOC 30 // Define number of collects (1-512) between reset
//global variables
unsigned int volts; // Current value of Battery Volts
unsigned int shuntvolts; // Current value of Shunt Volts
unsigned int pulse; // Pulses per Second
unsigned int windpulse; // Windspeed Pulses per second
unsigned int mode; // Dummy load on or off
//function prototypes
unsigned int ADCaverage (int n); //average over n samples
unsigned int ADCaverage (int n){
unsigned int currentvalue = 0; // incremental value
unsigned int count; // amount of samples to take
for(count=0;count<n;count++) { // initialise array
ADCSRA |= _BV(ADSC); // start conversion
while (ADCSRA & _BV(ADSC) ) {} // wait until conversion completed
currentvalue = (ADC+currentvalue); //running total
}
return (currentvalue/n); //Divide by n for average
}
//----- Begin Code ------------------------------------------------------------
int main(void)
{
unsigned int x = 0;
uartInit();
uartSetBaudRate(4800); // set UART speed to 9600 baud
rprintfInit(uartSendByte);
timerInit();
a2dInit();
vt100ClearScreen();
rprintf("Hi i am alive at least\r\n",shuntvolts,pulse,windpulse);
// configure a2d port (PORTC) as input
// so we can receive analog signals
DDRC = 0x00;
// make sure pull-up resistors are turned off
PORTC = 0x00;
a2dSetPrescaler(ADC_PRESCALE_DIV64);
a2dSetReference(ADC_REFERENCE_256V);
// use a2dConvert8bit(channel#) to get an 8bit a2d reading
// use a2dConvert10bit(channel#) to get a 10bit a2d reading
while (1) // do this until finished or broken
vt100SetCursorPos(1,1);
{
for(x=0; x<NOC; x++) // Repeat NOC amount of times
{
ADMUX = 0; // Select pin ADC0 using MUX temp sensor 1
volts = ADCaverage(1); // Get ADC Value
ADMUX = 1; // Select pin ADC0 using MUX temp sensor 2
shuntvolts = ADCaverage(1); // Get ADC Value
ADMUX = 2; // Select pin ADC0 using MUX temp sensor 3
pulse = ADCaverage(1); // Get ADC Value
ADMUX = 3; // Select pin ADC0 using MUX temp sensor 4
windpulse = ADCaverage(1);// Get ADC Value
// print current top temperatures correct to 1 decimal place (ie. 25.3 )
rprintf("[<V>%d</V><A>%d</A><R>%d</R><S>%d</S><M>0<M>]\r\n",volts,shuntvolts,pulse,windpulse);
timerPause(NOS); // pause for interval between checking routine
}
}
}