NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Weighscale code not working

August 25, 2010
by Ralphxyz
Ralphxyz's Avatar

So what I am trying to do is to Read the Sense S+ S- voltage from a Wheatstone Bridge made up of four strain gages (transducers/cells). This all relates to my other Strain Gage threads and op-amp and send the reading to the PC command line and to the LCD display.

All I get is a series of "757" sent to the terminal one after another rather or not there is any weight on the scale.

I also am sending adc_read() to the LCD, all I get there is a single 757.

I tried the Nerdkit Weighscale code without modifications but that would not compile.

My code does compile.

So here is my modified code. It probable is something obvious.

#define F_CPU 14745600

#include <stdio.h>
#include <math.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"

// PIN DEFINITIONS:
// PC0 -- analog in
//
// PD4 - bridge excite
// PD3 - bridge excite
void adc_init() {
  // set analog to digital converter
  // for external reference (5v), single ended input ADC0
  ADMUX = 0;

  // set analog to digital converter
  // to be enabled, with a clock prescale of 1/128
  // so that the ADC clock runs at 115.2kHz.
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);

  // fire a conversion just to get the ADC warmed up
  ADCSRA |= (1<<ADSC);
}

uint16_t adc_read() {
  // set ADSC bit to get the *next* conversion started
  // ADCSRA |= (1<<ADSC);  //done above allready

  // read from ADC, waiting for conversion to finish
  // wait for it to be cleared
  while(ADCSRA & (1<<ADSC)) {
    // do nothing... just hold your breath.
  }
  // bit is cleared, so we have a result.

  // read from the ADCL/ADCH registers, and combine the result
  // Note: ADCL must be read first (datasheet pp. 259)
  uint16_t result = ADCL;
  uint16_t temp = ADCH;
  result = result + (temp<<8);

  return result;
}

// tempsensor has the sampleToFahrenheit code here

int main() {

  lcd_init();
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  lcd_home();

  // set PD3, PD4 as outputs
  DDRD |= (1<<PD3) | (1<<PD4);   // pin 5 & 6

  // init ADC
  adc_init();

  // init serial port
  uart_init();
  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;

  int16_t reading;
  while(1) {
    // set polarity +-
    PORTD |= (1<<PD3);
    PORTD &= ~(1<<PD4);
    // wait 5 time constants (bw=12kHz, T=13.2us)
    delay_us(66);
    // take reading
    //reading = adc_read();
    printf_P(PSTR("%d "), adc_read());
    // write message to LCD
    lcd_home();
    lcd_write_string(PSTR("Reading: "));
    lcd_line_two();
    fprintf_P(&lcd_stream, PSTR("%d"), adc_read());
        // set polarity -+
    PORTD |= (1<<PD4);
    PORTD &= ~(1<<PD3);
    // wait 5 time constants (bw=12kHz, T=13.2us)
    delay_us(66);
  }

  return 0;
}
August 26, 2010
by Ralphxyz
Ralphxyz's Avatar

I have found "most" of my problem.

The Nerdkit Weighscale code actually does compile, I am having ongoing problems with Mac OS X. Whenever I run the Terminal screen to see MCU output on/in the Mac Terminal the Terminal does not terminate correctly and I have to reboot every time I want to compile a program. I have tried using Ctrl-A Ctrl-/ to stop the screen but it does not seem to work.

Ralph

August 26, 2010
by Ralphxyz
Ralphxyz's Avatar

Duh, to stop the screen Terminal use Ctrl-a Ctrl- not Ctrl-/

Ralph

August 26, 2010
by Ralphxyz
Ralphxyz's Avatar

Darn that's Ctrl-a Ctrl-\

Ralph

August 26, 2010
by mongo
mongo's Avatar

Having one of those days myself...

Post a Reply

Please log in to post a reply.

Did you know that you can build a digital read out (DRO) for a lathe or milling machine? Learn more...