All pastes #222513 Raw Edit

Unnamed

public c v1 · immutable
#222513 ·published 2006-10-26 10:34 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#define on  0#define off 1//global variablesunsigned int volts;       // Current value of Battery Voltsunsigned int shuntvolts;  // Current value of Shunt Volts (AMPS)unsigned int mode;        // Dummy load on or offvolatile u16 Int0Count;volatile u16 Int1Count;u16 rpm;                   // AC Volts IN frequency per 0 secondsu16 windpulse;             // Windspeed Pulses per 0 secondsu16 direction;              // Wind Direction// functions void SwitchToCharging(void);void SwitchToLoad(void);void rpmSetup(void);int windDirection(void);void checkDirection(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(5000);       // wait 0 seconds	rpm       = Int0Count;  // get counter values	windpulse = Int1Count;  // get counter values	checkDirection();		if (volts > 620) { mode = 0; SwitchToLoad(); }        if (volts < 580) { mode = 1;  SwitchToCharging(); }    vt100SetCursorPos(1,1);    rprintf("[<V>%d</V><I>%d</I><R>%d</R><S>%d</S><M>%d<M><D>%d</D>]\r\n",volts,shuntvolts,rpm,windpulse,mode,direction);    } } 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         {      }}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;}