Reprogramming the AAG RS485 Weather Station

I had a chance to play with the weather station and wrote this code to set up the ports and get wind direction. I created an array of bytes to hold the 16 possible states of the Hall effects. Either one Hall effect is triggered or two are when the magnet is between two of them. The 16 states map into a string array that corresponds to the 16 directions.

Next I will get the 1-wire temperature working and wind speed and then it will be at the same point where I took it apart.

AAG wired the DS1850 directly into PD5 with no pull-up resistor so I'm going to re-wire it with one so I can have a proper 1-wire bus to extend outside the enclosure. I don't have too much flash so I'm going to have to work on the 1-wire code to make it as slim as possible.

/*
PC0-PC5 PD6-PD7 = Q1-Q8  Wind Direction
 PD5 = 1-Wire DS18B20
 PD4 = Q9 Wind Speed
 PD2 = DD RS485 Data Direction
 PD1 = TXD
 PD0 - RXD
 ADC7 = P1 Photocell Schematic wrong actually pulls up to 5V
 ADC6 = Voltmeter
 */

#define ADC6  6
#define ADC7  7

const char* windDirection[16] = { // strings for the 16 wind directions
  "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW",\
  "SW", "WSW", "W", "WNW", "NW", "NNW"};

uint8_t WD[16] =  { // Hall effect outputs for 16 wind vane positions
  B01111111, B00111111, B10111111, B10011111, B11011111, B11001111,\
  B11101111, B11100111, B11110111, B11110011, B11111011, B11111001,\
  B11111101, B11111100, B11111110, B01111110};


uint8_t match_wind(uint8_t* mask_array, uint8_t mask){ // match wind direction mask with array of possibles
  for (uint8_t i=0; i<16; i++)
    if (*mask_array++ == mask)
      return i; // return match which corresponds to string array index too
  return 16; // signal no match. 
}

uint8_t make_wind(void){ // build mask for wind direction
  uint8_t temp = PINC; // read Q1-Q6(PC0-PC5)
  temp &= B00111111; // clear bits 6 and 7
  temp |= (PIND & B11000000); // OR on Q7-Q8(PD6-PD7) bits
  return temp;
}

void setup() {
  Serial.begin(9600);
  DDRC &= B11000000; // PC0-PC5 = inputs (Wind Direction)
  PORTC |= B00111111; // switch on pull-up resistors
  DDRD &= B00001011; // PD4, PD5, PD6, PD7 = inputs (Wind Direction and Speed + 1-Wire) PD2 temporarily for RS485 disable
  PORTD |= B11110000; // switch on pull-up resistors
}

void loop(){ 
  int photocell = analogRead(ADC7);
  Serial.print(photocell, DEC);
  delay(1000);
  int voltmeter = analogRead(ADC6);
  Serial.print('\t');
  Serial.println(voltmeter, DEC);
  delay(1000);

  uint8_t d = make_wind(); // ;-)
  d = match_wind(WD, d);
  if (d < 16)
    Serial.println(windDirection[d]);
  else Serial.println("Invalid Direction");
  delay(1000);
}