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.

Project Help and Ideas » Tempsensor+Clock+Scrolling Message???

January 22, 2011
by SirHobbes3
SirHobbes3's Avatar

So i've got this idea to incorporate the tempsensor wit the clock and then write a scrolling message on the bottom line, would this work, and any ideas on how to incorporate all the programming toegther?

January 22, 2011
by Rick_S
Rick_S's Avatar

I've found that the desire to do something is generally a good driving factor to learn how. With that said, I'll confirm that it is absolutely possible to do what you ask. I might even suggest looking at the temperature program, and the real time clock tutorial. Maybe even go a step farther to say I have seen code in the forums posted for scrolling text on the LCD. Lastly, I would venture a guess that if one were to take pieces from all three, adjust the code a wee bit as needed, that a working project like that could be accomplished.

:D

PS, I'm not trying to be a smart #ss, just giving you some ideas on how to accomplish your task. Once you have some code that you've put together, if you can't figure out how to get it working to your satisfaction, post the code. Then, if I can help, I'll be more than happy to work with you with your program to get it doing what you want. Otherwise, I'd just be doing your project for you...

Rick

January 26, 2011
by SirHobbes3
SirHobbes3's Avatar

So, i was wondering, to make this work, (im not saying that anyone else has to go out and write the code yo find out) would we put all the codes into one flash, or would we need to somehow combine all the codes into one, this would make a big code either way.

Sir Hobbes3

January 26, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi SirHobbes3,

I would say the best way to go about it is to start a new project for yourself from scratch and then just copy over the parts that you need to make what you want happen. This way you avoid having alot of code that you don't need in your program, and by putting it together from the beginning you will learn a lot more!

Humberto

January 26, 2011
by n3ueaEMTP
n3ueaEMTP's Avatar

SirHobbes3, I have to admit that Humberto's suggestion is basicly what I've done for my main project. It has a bits and pieces of virtually every program that came with the initial NerdKits CD. The Traffic Light project, the Tempsensor project, and the dip_arithmetic codes just to name a few. I found the trickiest part was making all the pieces of code work together but that's when I learned the most. I just getting started on branching out into the world beyond my kit and trying to interface a DS1307 RTC via the TWI(I2C) protocol.

The best part? When I've gotten stuck, I've asked questions here and the community has been a great help!!!

Chris B.

January 27, 2011
by Rick_S
Rick_S's Avatar

Not to Hijack the topic... my apologies in advance... Speaking of RTC chips, Chris, I just got in a couple of sample DS3232SN's from Maxim. I'm still waiting on my SOIC to Dip adapter boards. The DS3232 takes the RTC one step farther in that it has a +/- 2ppm crystal with on chip temperature compensation making it EXTREMELY accurate. It also allows you access to the onboard temperature unit it uses for it's compensation giving you an great RTC with a decent temp reading as well all on one chip. I can't wait to fire it up... Patience Rick... :D

Hijack off

SirHobbes3, Humberto restated (in a much nicer manner :D) pretty much what I was trying to say. Once you start understanding what the bits and pieces of the code in the examples and tutorials do, you'll see how to piece together a project like what you want. I've often found the best way to learn what a given program does is try to think like the computer. Go line by line, write down any change in a variable, jump to any function, and follow the code. After studying a program for a while the lights will brighten. Also a VERY important step when doing that with a microcontroller is to have the datasheet open and when you get to the portion of the program, for example, that is setting up the ADC for the temperature reading, read that section of the datasheet so you know what the registers are being set to and why. Before you know it, you'll be one of the many here helping out someone else who is just starting.

Rick

January 27, 2011
by SirHobbes3
SirHobbes3's Avatar

Ok, thanks guys, i'll try and get started on this,

post back soon!!

Sir Hobbes3

January 27, 2011
by SirHobbes3
SirHobbes3's Avatar

ok, so i've got the stuff put together and all, (will post that soon) and i need to figure out how to create a makefile so i can send the program to the microcon., i need to figure this out. So where would i even start?????

The tutorial on the realtime clock notes that you can start from one of the other makefiles in the nerdkits code.

January 27, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi SirHobbes3,

Your best bet is to take a Makefile that already exists like the initialload Makefile and copy it over to the directory with your program in it. Then do a replace all from initialload to whatever it is you named your projects .c file. For example if I made a project called foo, I would copy over the initialload Makefile to the foo directory (where foo.c exists) and then replace-all initialload with foo. Please take a moment to take a look at what changed, and how making that change makes the new Makefile work.

Humberto

January 28, 2011
by SirHobbes3
SirHobbes3's Avatar

ok, thanks Humberto!

i'll try this.

January 28, 2011
by SirHobbes3
SirHobbes3's Avatar

ok, so my makefile is good, but i'm working out the chunks in my quote quote, stcc, scroll, temp, clock, combine. So ill have it in the next post

January 28, 2011
by SirHobbes3
SirHobbes3's Avatar
#define F_CPU 14745600 
#include <stdio.h>

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

    #include <avr/interrupt.h> 
    #include <avr/pgmspace.h> 
    #include <avr/io.h> 
    //#include <avr/delay.h> 
    #include <util/delay.h>

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

    int main() { // LED as output DDRC |= (1<<PC5);

    while(1)

    { 
    // fire up the LCD 
    lcd_init(); 
    lcd_home();

    // print message to screen  
    lcd_write_string(PSTR("Page1Page1Page1"));  
    lcd_line_two();  
    lcd_write_string(PSTR("Page1Page1Page1"));  
    lcd_line_three();  
    lcd_write_string(PSTR("Page1Page1Page1"));  
    lcd_line_four();  
    lcd_write_string(PSTR("********************"));

delay_ms(1000);  
lcd_line_one();  
lcd_write_string(PSTR("Hello!"));  
lcd_line_two();  
lcd_write_string(PSTR("It's going to be a "));  
lcd_line_three();  
lcd_write_string(PSTR(" beautiful day"));  
lcd_line_four(); 
lcd_write_string(PSTR("today!"));   //space is needed or first line next page is truncated

delay_ms(1000);  
lcd_line_one();  
lcd_write_string(PSTR(""));  
lcd_line_two(); 
lcd_write_string(PSTR("  Page3Page3Page3"));  
lcd_line_three();  
lcd_write_string(PSTR("  Page3Page3Page3"));  
lcd_line_four();  
lcd_write_string(PSTR("LLLLLLLLLLLLLLLLLLLL"));

delay_ms(1000);  
lcd_line_one();  
lcd_write_string(PSTR("   Page4Page4Page4"));  
lcd_line_two();  
lcd_write_string(PSTR("   Page4Page4Page4"));  
lcd_line_three();  
lcd_write_string(PSTR("   Page4Page4Page4"));  
lcd_line_four();  
lcd_write_string(PSTR("WWWWWWWWWWWWWWWWWWWW ")); //space is needed or first line next page is truncated

delay_ms(1000);  
lcd_line_one();  
lcd_write_string(PSTR("Page5Page5Page5"));  
lcd_line_two();  
lcd_write_string(PSTR("    Page5Page5Page5"));  
lcd_line_three();  
lcd_write_string(PSTR("    Page5Page5Page5"));  
lcd_line_four();  
lcd_write_string(PSTR("uuuuuuuuuuuuuuuuuuuu")); 
delay_ms(1000);

    // write message to serial port 
    // printf_P(PSTR("%.2f degrees F\r\n"), temp_avg); 
} 
return 0; }

// tempsensor.c
// for NerdKits with ATmega168
// mrobbins@mit.edu
//part 2, tempsensor

// PIN DEFINITIONS:
//
// PC0 -- temperature sensor analog input

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() {
  // read from ADC, waiting for conversion to finish
  // (assumes someone else asked for a conversion.)
  // 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);

  // set ADSC bit to get the *next* conversion started
  ADCSRA |= (1<<ADSC);

  return result;
}

double sampleToFahrenheit(uint16_t sample) {
  // conversion ratio in DEGREES/STEP:
  // (5000 mV / 1024 steps) * (1 degree / 10mV)
  //    ^^^^^^^^^^^      ^^^^^^^^^^
  //     from ADC         from LM34
  return sample * (5000.0 / 1024.0 / 10.0);  
}

int main() {
  // start up the LCD
  lcd_init();
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  lcd_home();

  // start up the Analog to Digital Converter
  adc_init();

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

  // holder variables for temperature data
  uint16_t last_sample = 0;
  double this_temp;
  double temp_avg;
  uint8_t i;

  while(1) {
    // take 100 samples and average them!
    temp_avg = 0.0;
    for(i=0; i<100; i++) {
      last_sample = adc_read();
      this_temp = sampleToFahrenheit(last_sample);

      // add this contribution to the average
      temp_avg = temp_avg + this_temp/100.0;
    }

    //part 3, realtime clock

    // PIN DEFINITIONS:

void realtimeclock_setup() {
  // setup Timer0:
  // CTC (Clear Timer on Compare Match mode)
  // TOP set by OCR0A register
  TCCR0A |= (1<<WGM01);
  // clocked from CLK/1024
  // which is 14745600/1024, or 14400 increments per second
  TCCR0B |= (1<<CS02) | (1<<CS00);
  // set TOP to 143
  // because it counts 0, 1, 2, ... 142, 143, 0, 1, 2 ...
  // so 0 through 143 equals 144 events
  OCR0A = 143;
  // enable interrupt on compare event
  // (14400 / 144 = 100 per second)
  TIMSK0 |= (1<<OCIE0A);
}

// the_time will store the elapsed time
// in hundredths of a second.
// (100 = 1 second)
// 
// note that this will overflow in approximately 248 days!
//
// This variable is marked "volatile" because it is modified
// by an interrupt handler.  Without the "volatile" marking,
// the compiler might just assume that it doesn't change in 
// the flow of any given function (if the compiler doesn't
// see any code in that function modifying it -- sounds 
// reasonable, normally!).
//
// But with "volatile", it will always read it from memory 
// instead of making that assumption.
volatile int32_t the_time;

SIGNAL(SIG_OUTPUT_COMPARE0A) {
  // when Timer0 gets to its Output Compare value,
  // one one-hundredth of a second has elapsed (0.01 seconds).
  the_time++;
}

int main() {
  realtimeclock_setup();

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

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

  // turn on interrupt handler
  sei();

  while(1) {
    lcd_home();
    fprintf_P(&lcd_stream, PSTR("%16.2f sec"), (double) the_time / 100.0);
  }

  return 0;
}

I know, my code is probaly pretty long and drawn out, there are porbaly large chunks (sections) that need some work, im just a beginner at this stuff, but it's fun.

January 28, 2011
by SirHobbes3
SirHobbes3's Avatar

i may have to reorder some stuff, or combine, note that this is my first attempt. Like a rough draft almost.

Also, if my code works, im getting a message like this in command prompt.

Pic

January 29, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi SirHobbes3,

I very much suggest you take a step back and try to go a little slower as you move forward. When developing new projects it is always a good idea to get one thing working at a time and build your features up one by one. This lets you separate the problems in your head and solve one at a time, reducing the complexity of your project.

In your project I see three completely different main() method. As you remember from the NerdKits Guide the main() method is where your program starts, so giving it three main() methods from three different programs will not work.

Humberto

February 21, 2011
by SirHobbes3
SirHobbes3's Avatar

ok, i kinda forgot to remember that,sorry for taking sooooooo long to post back, my computer has had probs installing vista sp1, so i fixed that and got sp2 and everything is good.

So now back to business, right no, i am going to try and do each thing INDIVIDUALLY, then get them combined and the makefile ready to go, so i'll touch in with you guys this weekend maybe, Saturday the 26?

SirHobbes3

February 21, 2011
by SirHobbes3
SirHobbes3's Avatar

that "no" is actually "now" in the phrase :"right now"

sorry

February 26, 2011
by SirHobbes3
SirHobbes3's Avatar

hey guys

im still working on the srolling message, i might have it done today, i'll check back in here soon.

SirHobbes3

March 05, 2011
by SirHobbes3
SirHobbes3's Avatar

Ok, hello guys! I have successfully figured out the scrolling message AND tempsensor (which i did when i first got the nerdkits) code!! So i will soon have the realtime clock code. I will begin "combining" them soon...................

Sincerely, SirHobbes3

March 05, 2011
by SirHobbes3
SirHobbes3's Avatar

YAY, ok so i've got all the codes working now (YAY!!) and im gonna begin working on combining them sometime soon.

I will check back in soon.

Sincerely, SirHobbes3

March 05, 2011
by Ralphxyz
Ralphxyz's Avatar

Can not wait to see what you have done!!

Ralph

Post a Reply

Please log in to post a reply.

Did you know that Pulse Width Modulation (PWM) can be used to control the speed of a motor digitally? Learn more...