All pastes #214773 Raw Edit

Something

public text v1 · immutable
#214773 ·published 2006-10-22 06:31 UTC
rendered paste body
//*****************************************************************************
// File Name    : datalogger.c
// 
// Title                : Windlogger
// Revision          : 0.3  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/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 "a2d.h"               // include A/D converter function library
#include "vt100.h"             // include VT100 terminal 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

typedef struct FSM_State_s { 
  unsigned TPD7 : 1;
  unsigned TPD6 : 1;
  unsigned TPD5 : 1;
  unsigned TPD4 : 1;
  unsigned TPC2 : 1;
  unsigned TPC3 : 1;
  unsigned TPC4 : 1;
  unsigned TPC5 : 1;
} FSM_State_t;
 
typedef unsigned int  PTRType; 
    
typedef struct stateEntry_s {  
    PTRType       next;            
    FSM_State_t   state;           
 } stateEntry_t;                   
 
typedef struct stateBities_s {  
    union {            
        FSM_State_t   state;           
        unsigned stateBits;
    };
 } stateBities_t;             
#define on  0
#define off 1

//------------
  //    PC5  ,  PC4  ,  PC3  ,  PC2  ,  PD4  ,  PD5  ,  PD6  ,  PD7  ,
stateEntry_t stateTable[] = {   
  {   0, { on   , on   , on   , off  , off  , off  , off  , off  }},  /*   index: 31   0  */
  {   1, { on   , on   , off  , off  , off  , off  , off  , on   }},  /*   index: 62   1  */
  {   2, { on   , on   , off  , off  , off  , off  , off  , off  }},  /*   index: 63   2  */
  {   3, { on   , off  , on   , off  , off  , off  , off  , off  }},  /*   index: 95   3  */
  {   4, { on   , off  , off  , off  , off  , on   , off  , off  }},  /*   index: 123  4  */
  {   5, { on   , off  , off  , off  , off  , off  , on   , on   }},  /*   index: 124  5  */
  {   6, { on   , off  , off  , off  , off  , off  , off  , on   }},  /*   index: 126  6  */
  {   7, { on   , off  , off  , off  , off  , off  , off  , off  }},  /*   index: 127  7  */
  {   8, { off  , on   , on   , on   , off  , off  , off  , off  }},  /*   index: 143  8  */
  {   9, { off  , on   , on   , off  , off  , off  , off  , off  }},  /*   index: 159  9  */
  {  10, { off  , on   , off  , on   , off  , off  , off  , off  }},  /*   index: 175  10 */
  {  11, { off  , on   , off  , off  , off  , off  , off  , on   }},  /*   index: 190  11 */
  {  12, { off  , on   , off  , off  , off  , off  , off  , off  }},  /*   index: 191  12 */
  {  13, { off  , off  , on   , on   , on   , off  , off  , off  }},  /*   index: 199  13 */
  {  14, { off  , off  , on   , on   , off  , off  , off  , off  }},  /*   index: 207  14 */
  {  15, { off  , off  , on   , off  , on   , off  , off  , off  }},  /*   index: 215  15 */
  {  16, { off  , off  , on   , off  , off  , off  , off  , off  }},  /*   index: 223  16 */
  {  17, { off  , off  , off  , on   , on   , on   , off  , off  }},  /*   index: 227  17 */
  {  18, { off  , off  , off  , on   , on   , off  , off  , off  }},  /*   index: 231  18 */
  {  19, { off  , off  , off  , on   , off  , on   , off  , off  }},  /*   index: 235  19 */
  {  20, { off  , off  , off  , on   , off  , off  , off  , off  }},  /*   index: 239  20 */
  {  21, { off  , off  , off  , off  , on   , on   , on   , on   }},  /*   index: 240  21 */
  {  22, { off  , off  , off  , off  , on   , on   , off  , off  }},  /*   index: 243  22 */
  {  23, { off  , off  , off  , off  , on   , off  , on   , off  }},  /*   index: 245  23 */
  {  24, { off  , off  , off  , off  , on   , off  , off  , off  }},  /*   index: 247  24 */
  {  25, { off  , off  , off  , off  , off  , on   , on   , on   }},  /*   index: 248  25 */
  {  26, { off  , off  , off  , off  , off  , on   , on   , off  }},  /*   index: 249  26 */
  {  27, { off  , off  , off  , off  , off  , on   , off  , on   }},  /*   index: 250  27 */
  {  28, { off  , off  , off  , off  , off  , on   , off  , off  }},  /*   index: 251  28 */
  {  29, { off  , off  , off  , off  , off  , off  , on   , on   }},  /*   index: 252  29 */
  {  30, { off  , off  , off  , off  , off  , off  , on   , off  }},  /*   index: 253  30 */
  {  31, { off  , off  , off  , off  , off  , off  , off  , on   }}   /*   index: 254  31 */
 }; 

 
//global variables
unsigned int volts;       // Current value of Battery Volts
unsigned int shuntvolts;  // Current value of Shunt Volts (AMPS)
unsigned int mode;        // Dummy load on or off
volatile u16 Int0Count;
volatile u16 Int1Count;
u16 rpm;                   // AC Volts IN frequency per 0 seconds
u16 windpulse;             // Windspeed Pulses per 0 seconds
u16 direction;              // Wind Direction

// functions 
void SwitchToCharging(void);
void SwitchToLoad(void);
void rpmSetup(void);
int windDirection(void);
void myInt0Handler(void);
void myInt1Handler(void);
 
//----- Begin Code ------------------------------------------------------------
 
int main(void)
{
    uartInit();
    uartSetBaudRate(4800);    // set UART speed to 9600 baud
    rprintfInit(uartSendByte);
    timerInit();
    a2dInit();
 
    vt100ClearScreen();
	//rprintf("hello");
            
    DDRC = 0x00;
    DDRD = 0xF0;
	PORTC = 0x00;
	PORTD = 0xF0;
		 
    a2dSetPrescaler(ADC_PRESCALE_DIV64);
    a2dSetReference(ADC_REFERENCE_AVCC);
	rpmSetup();
	SwitchToCharging(); mode = 1;
         
    while (1)                 // do this until finished or broken
    {
    volts      = a2dConvert10bit(0);    // Get ADC Value
    shuntvolts = a2dConvert10bit(1);    // Get ADC Value
	Int0Count = 0;          // reset interrupt counters
	Int1Count = 0;          // reset interrupt counters
	timerPause(3000);       // wait 0 seconds
	windDirection();
	rpm       = Int0Count;  // get counter values
	windpulse = Int1Count;  // get counter values
	
	
		if (volts > 580) { mode = 0; SwitchToLoad(); }
        if (volts < 540) { mode = 1;  SwitchToCharging(); }
    vt100SetCursorPos(1,1);
    rprintf("[<V>%d</V><A>%d</A><R>%d</R><S>%d</S><M>%d<M><W>%d</W>]\r\n",volts,shuntvolts,rpm,windpulse,mode,direction);
    } 
}
 
int windDirection() {
 
  stateEntry_t test;
  int n;
    
  test.state.TPD7 = IsHigh(7, PORTD);
  test.state.TPD6 = IsHigh(6, PORTD);
  test.state.TPD5 = IsHigh(5, PORTD);
  test.state.TPD4 = IsHigh(4, PORTD);
  test.state.TPC2 = IsHigh(2, PORTC);
  test.state.TPC3 = IsHigh(3, PORTC);
  test.state.TPC4 = IsHigh(4, PORTC);
  test.state.TPC5 = IsHigh(5, PORTC);   
 
  for (n = 0; n < 32; n++) {
  if( stateTable[n].stateBits == test.stateBits ) {
    return stateTable[n].next;
    }
  }
    
  return -1; // we dont know!?
  
} 

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(0, PORTB) ; 
  return;
}
 
void SwitchToLoad(void) { 
  SetBit(0, PORTB) ;
  return;
}