All pastes #247108 Raw Edit

Something

public c v1 · immutable
#247108 ·published 2006-11-13 01:17 UTC
rendered paste body
//*****************************************************************************// File Name    : datalogger.c// // Title                : Windlogger// Revision          : 0.2  22-10-2006// Revision History:// When   Who                  Description of change// -----------  -----------       -----------------------// 20-Oct-2002  pstang        Created the program// Oct - 2006 RifRaf changes stuff//***************************************************************************** //----- Include Files ---------------------------------------------------------#include <avr/io.h>            // include I/O definitions (port names, pin names, etc)#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 "a2d.h"               // include A/D converter function library#include "vt100.h"             // include VT100 terminal support#include "lcd.h"               // include Character LCD support#include "extint.h"               // include external interrupt library#define SetBit(BIT, PORT)        PORT |= (1<<BIT)#define ClearBit(BIT, PORT)      PORT &= ~(1<<BIT)#define IsHigh(BIT, PORT)       (PORT & (1<<BIT)) != 1#define IsLow(BIT, PORT)        (PORT & (1<<BIT)) == 1#define on  0#define off 1// **********// Menu Code#define NONE 0#define FIRST 1#define SECOND 2#define THIRD 3static char do_task = NONE;// **********//User Variablesu16 RPMDivider       = 28;u16 WSDivider        = 2.5;u16 VOLTDivider      = 45;u16 WindVoltOffset   = 0;u16 SolarVoltOffset  = 0;u16 OutputVoltOffset = 0;u16 WindAmpDivider   = 175;u16 SolarAmpDivider  = 175;u16 OutputAmpDivider = 175;//global variablesunsigned int volts,shuntvolts,solarvolts,outvolts,recttemp,outtemp,insidetemp,barometer;       // Current valuesunsigned int battvolts,windamp,solaramp,outamp,recttempOut,outtempOut,insidetempOut,barometerOut;       // Current valuesunsigned int maxvolts,maxshuntvolts,maxsolarvolts,maxoutvolts,maxrecttemp,maxouttemp,maxinsidetemp,maxbarometer;       // Max valuesunsigned int minvolts,minshuntvolts,minsolarvolts,minoutvolts,minrecttemp,minouttemp,mininsidetemp,minbarometer;       // Min valuesunsigned int windWatts;unsigned int mode;        // Dummy load on or offvolatile u16 Int0Count;volatile u16 Int1Count;u16 rpm, wmrpm;                   // AC Volts IN frequency per 0 secondsu16 windpulse, speed;             // Windspeed Pulses per 0 secondsu16 direction;              // Wind Directionu16 maxwmrpm, minwmrpm, maxspeed, minspeed;  // max and mins// functions void SwitchToCharging(void);void SwitchToLoad(void);void Setup(void);void rpmSetup(void);void SetMinMax(void);void checkDirection(void);void myInt0Handler(void);void myInt1Handler(void);void menu(void); //----- Begin Code ------------------------------------------------------------void Function_1(void);  // define these for the menu itemsvoid Function_2(void);  // change the names to suitvoid Function_3(void);void Setup(void)    {    uartInit();    uartSetBaudRate(4800);            // set UART speed to 9600 baud    rprintfInit(uartSendByte);    timerInit();    a2dInit();    lcdInit();                        // initialize LCD	DDRA  = 0x00;    // configure a2d port (PORTA) as input so we can receive analog signals	PORTA = 0x00;   // make sure pull-up resistors are turned off	DDRB  = 0xFF;	PORTB = 0xFF;    //DDRC  = 0xF0;	//PORTC = 0xFC;	//DDRD  = 0xFC;    //PORTD = 0xF0;             a2dSetPrescaler(ADC_PRESCALE_DIV64);    a2dSetReference(ADC_REFERENCE_256V);	} int main(void){    Setup();	rpmSetup();    SwitchToCharging(); mode = 1;	    rprintfInit(lcdDataWrite);        // direct printf output to LCD    lcdClear();                       // clear LCD    lcdGotoXY(0,0);                   //goto line 1 , pos 1    rprintf("AVR WINDLOGGER 2006");        //welcome message  	lcdGotoXY(0,1);                   //goto line 2 , pos 1    rprintf("Wind Solar Load Mon");     //welcome message      timerPause(3000);                 while (1)                 // do this until finished or broken    {// **********************// This goes in the main function	menu(); // call menu function and read button pin pushed	switch(do_task)	{		case NONE:		//0			break;		case FIRST:		//1			do_task = NONE;   // reset the menu			Function_1();			break;		case SECOND:	//2			do_task = NONE;  // reset the menu			Function_2();			break;		case THIRD:		//3			Function_3();			break;		default:		break;	}// **********************    volts      = a2dConvert10bit(0);    // Get ADC Value    shuntvolts = a2dConvert10bit(1);    // Get ADC Value    solarvolts = a2dConvert10bit(2);    // Get ADC Value    outvolts   = a2dConvert10bit(3);    // Get ADC Value    recttemp   = a2dConvert10bit(4);    // Get ADC Value    outtemp    = a2dConvert10bit(5);    // Get ADC Value    insidetemp = a2dConvert10bit(6);    // Get ADC Value    barometer  = a2dConvert10bit(7);    // Get ADC Value	    if (volts > 620) { mode = 0; SwitchToLoad(); }        if (volts < 580) { mode = 1;  SwitchToCharging(); }    Int0Count = 0;          // reset interrupt counters    Int1Count = 0;          // reset interrupt counters    timerPause(3000);       // wait 3 seconds    rpm       = Int0Count;  // get counter values    windpulse = Int1Count;  // get counter values    checkDirection();    rprintfInit(uartSendByte);    vt100SetCursorPos(1,1);       rprintf("[<V>%d</V><I>%d</I><R>%d</R><S>%d</S><M>%d<M><D>%d</D><T1>%d</T1><T2>%d</T2><T3>%d</T3><B>%d</B>]\r\n",volts,shuntvolts,rpm,windpulse,mode,direction,recttemp,outtemp,insidetemp,barometer);    	SetMinMax();    	//Update LCD    rprintfInit(lcdDataWrite);        // direct printf output to LCD    lcdClear();	lcdGotoXY(0,0);                   //goto line 1 , pos 1    rprintf("V%d.%d A%d.%d W%d.%d",battvolts/10,battvolts%10,windamp/10,windamp%10,windWatts/10,windWatts%10);    lcdGotoXY(0,1);                   //goto line 2 , pos 1    rprintf("%dRPM %dKph %d %d",wmrpm,speed, outtempOut, insidetempOut);    } } void checkDirection(void)    {        if (0) {      }        else if ( !(PINC & (1<<PC5)) & !(PINC & (1<<PC4)) & !(PIND & (1<<PD7))) {direction =  1; } // NNNE        else if ( !(PINC & (1<<PC5)) & !(PINC & (1<<PC4)) & !(PINC & (1<<PC3))) {direction =  5; }        else if ( !(PINC & (1<<PC4)) & !(PINC & (1<<PC3)) & !(PINC & (1<<PC2))) {direction =  9; }        else if ( !(PINC & (1<<PC3)) & !(PINC & (1<<PC2)) & !(PIND & (1<<PD4))) {direction = 13; }        else if ( !(PINC & (1<<PC2)) & !(PIND & (1<<PD4)) & !(PIND & (1<<PD5))) {direction = 17; }        else if ( !(PIND & (1<<PD4)) & !(PIND & (1<<PD5)) & !(PIND & (1<<PD6))) {direction = 21; }        else if ( !(PIND & (1<<PD5)) & !(PIND & (1<<PD6)) & !(PIND & (1<<PD7))) {direction = 25; }        else if ( !(PINC & (1<<PC5)) & !(PIND & (1<<PD6)) & !(PIND & (1<<PD7))) {direction = 29; }        else if ( !(PINC & (1<<PC4)) & !(PIND & (1<<PD7)))                       {direction =  0; } // N        else if ( !(PINC & (1<<PC5)) & !(PINC & (1<<PC4)))                       {direction =  2; } // NNE        else if ( !(PINC & (1<<PC5)) & !(PINC & (1<<PC3)))                       {direction =  4; }        else if ( !(PINC & (1<<PC4)) & !(PINC & (1<<PC3)))                       {direction =  6; }        else if ( !(PINC & (1<<PC4)) & !(PINC & (1<<PC2)))                       {direction =  8; } // E        else if ( !(PINC & (1<<PC3)) & !(PINC & (1<<PC2)))                       {direction = 10; }        else if ( !(PINC & (1<<PC3)) & !(PIND & (1<<PD4)))                       {direction = 12; }        else if ( !(PINC & (1<<PC2)) & !(PIND & (1<<PD4)))                       {direction = 14; }        else if ( !(PINC & (1<<PC2)) & !(PIND & (1<<PD5)))                       {direction = 16; } // S        else if ( !(PIND & (1<<PD4)) & !(PIND & (1<<PD5)))                       {direction = 18; }        else if ( !(PIND & (1<<PD4)) & !(PIND & (1<<PD6)))                       {direction = 20; }        else if ( !(PIND & (1<<PD5)) & !(PIND & (1<<PD6)))                       {direction = 22; }        else if ( !(PIND & (1<<PD5)) & !(PIND & (1<<PD7)))                       {direction = 24; } // W        else if ( !(PIND & (1<<PD6)) & !(PIND & (1<<PD7)))                       {direction = 26; }        else if ( !(PINC & (1<<PC5)) & !(PIND & (1<<PD6)))                       {direction = 28; }        else if ( !(PINC & (1<<PC5)) & !(PIND & (1<<PD7)))                       {direction = 30; }        else if ( !(PINC & (1<<PC5)))                                             {direction =  3; } // ENEE        else if ( !(PINC & (1<<PC4)))                                             {direction =  7; }        else if ( !(PINC & (1<<PC3)))                                             {direction = 11; }        else if ( !(PINC & (1<<PC2)))                                             {direction = 15; }        else if ( !(PIND & (1<<PD4)))                                             {direction = 19; }        else if ( !(PIND & (1<<PD5)))                                             {direction = 23; }        else if ( !(PIND & (1<<PD6)))                                             {direction = 27; }        else if ( !(PIND & (1<<PD7)))                                             {direction = 31; }        else         { direction = -1;      }}void SetMinMax(void)   //set maximums and minimums{     wmrpm = rpm/RPMDivider*60;            if (wmrpm > maxwmrpm) { maxwmrpm = wmrpm; }        if (wmrpm <= minwmrpm) { minwmrpm = wmrpm; }    speed = windpulse/WSDivider;            if (speed > maxspeed) { maxspeed = speed; }        if (speed <= minspeed) { minspeed = speed; }    battvolts =  volts/VOLTDivider*10;        if (battvolts > maxvolts) { maxvolts = battvolts; }        if (battvolts <= minvolts) { minvolts = battvolts; }    windamp   = (shuntvolts - WindVoltOffset) / WindAmpDivider*10;        if (windamp > maxshuntvolts) { maxshuntvolts = windamp; }        if (windamp <= minshuntvolts) { minshuntvolts = windamp; }	windWatts = (battvolts/10) * (windamp/10)*10;    solaramp  = (solarvolts - SolarVoltOffset) / SolarAmpDivider*10;        if (solaramp > maxsolarvolts) { maxsolarvolts = solaramp; }        if (solaramp <= minsolarvolts) { minsolarvolts = solaramp; }    outamp   =  (outvolts - OutputVoltOffset) / OutputAmpDivider*10;        if (outamp > maxoutvolts) { maxoutvolts = outamp; }        if (outamp <= minoutvolts) { minoutvolts = outamp; }    recttempOut   =  recttemp*0.254;        if (recttempOut > maxrecttemp) { maxrecttemp = recttempOut; }        if (recttempOut <= minrecttemp) { minrecttemp = recttempOut; }    outtempOut    =  outtemp*0.254;        if (outtempOut > maxouttemp) { maxouttemp = outtempOut; }        if (outtempOut <= minouttemp) { minouttemp = outtempOut; }    insidetempOut =  insidetemp*0.254;        if (insidetempOut > maxinsidetemp) { maxinsidetemp = insidetempOut; }        if (insidetempOut <= mininsidetemp) { mininsidetemp = insidetempOut; }    barometerOut  =  barometer*0.254;        if (barometerOut > maxbarometer) { maxbarometer = barometerOut; }        if (barometerOut <= minbarometer) { minbarometer = barometerOut; }}void rpmSetup(void){    extintInit();    extintConfigure(EXTINT0, EXTINT_EDGE_RISING);    extintConfigure(EXTINT1, EXTINT_EDGE_RISING);    extintAttach(EXTINT0, myInt0Handler);    extintAttach(EXTINT1, myInt1Handler);    sbi(GIMSK, INT0);    sbi(GIMSK, INT1);}void myInt0Handler(void){    Int0Count++;}void myInt1Handler(void){    Int1Count++;}void SwitchToCharging(void) {   ClearBit(4, PORTB) ;   return;} void SwitchToLoad(void) {   SetBit(4, PORTB) ;  return;} void Function_1(void){}  // define these for the menu itemsvoid Function_2(void){}  // change the names to suitvoid Function_3(void){}// **********************void menu(void){int y;static char x = 0;char *menu[] = {"None                ",  // Change these items to match your menu selections"Do Function 1       ","Do Function 2       ","Do Function 3       "};                                // Change this to your port pin for up button	if ( !(PINB & (1<<PB0)) )  	// UP Menu switch pressed ? (active low)	{		x++;		if(x > 3) x=0;         // roll over menu (scroll)		lcdGotoXY(0,1);        //goto line 2 , pos 1		rprintf(" menu[x] ");  // display menu item		for(y=0;y<10000;y++); // debounce delay ? have to check for sure	}                                // Change this to your port pin for up button	if ( !(PINB & (1<<PB1)) )	// Down Menu switch pressed ? (active low)	{		x--;		if(x < 0) x=3;         // roll under menu (scroll)		lcdGotoXY(0,1);        //goto line 2 , pos 1		rprintf(" menu[x] ");  // display menu item		for(y=0;y<10000;y++); // debounce delay ? have to check for sure	}                                // Change this to your port pin for up button	if ( !(PINB & (1<<PB3)) )	// Run Menu switch pressed ? (active low)	{		do_task = x;	        // Update do task for the Switch statement		for(y=0;y<10000;y++); // debounce delay ? have to check for sure	}}// ********************