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.

Microcontroller Programming » sending data over serial port at given time intervals

July 28, 2011
by kemil
kemil's Avatar

Hi All,

Im trying to send data over the serial port. But instead of sending it every cycle of the while loop i want to send data every approx seven seconds so i have added an interrupt and an in statement but it hasnt worked.

Is there something wrong with my code is it a more fundamental issue to do with printf_P?

#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"

double on;
double time = 0;

int main() {

 TIMSK1 |= (1 << TOIE1); // Enable overflow interrupt 
 sei(); // Enable global interrupts

 TCCR1B |= (1 << CS11);

uart_init();
FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar,....        
_FDEV_SETUP_RW);

stdin = stdout = &uart_stream;

while(1) {

  if( on == 1 )
  {

  printf_P(PSTR("hello\r\n"));
  time = 0;
  on = 0;
  }

  }

  return 0;
 }

 ISR(TIMER1_OVF_vect) 
 {

  time++;

  if (time == 200)
  {
  on = 1;
  }
  }
July 28, 2011
by bretm
bretm's Avatar

"on" needs to be "volatile char" instead of "double". I'd also make "time" a char instead of a double so it only takes one byte instead of four.

August 10, 2011
by kemil
kemil's Avatar

Hi bretm,

Thanks fro your reply. I tried what you said and it didnt work i then changed 'time' from a char to an int and it now works. on a side note.. what the reason you need to make on a volatile char? would a volatile int/ uint_8 work?

thanks kemil

August 10, 2011
by bretm
bretm's Avatar

char is a byte, the same as uint8_t. If it only works with int, which is 16 bits, you must be using a value greater than 255 somewhere, which I couldn't tell from the code posted above.

Post a Reply

Please log in to post a reply.

Did you know that reading a double floating point variable with scanf requires "%lf" for "long float"? Learn more...