0

I have a question about how to reading RPM and send value with serial It's my code:

char init(void)
{
  UBRRH=(uint8_t) (UBRR_CALC>>8);
  UBRRL=(uint8_t) UBRR_CALC;
  UCSRB=(1<<RXEN)|(1<<TXEN);
  UCSRC=(1<<URSEL)|(3<<UCSZ0);
  return 0;
}

void send(unsigned char x)
{
  while(!(UCSRA&(1<<UDRE))){}
  UDR=x;
}

void sendstring(char *s)
{
  while(*s)
  {
    send(*s);
    s++;
  }
}

volatile uint16_t count=0;    //Main revolution counter   
volatile uint16_t rps=0;    //Revolution per second

void Wait()
{
  uint8_t i;

  for(i=0;i<2;i++)
  {
    _delay_loop_2(0);
  }
}

int main(void)
{
  Wait();
  Wait();
  Wait();
  Wait();

  //Init INT0
  MCUCR|=(1<<ISC01);   //Falling edge on INT0 triggers interrupt.
  GICR|=(1<<INT0);  //Enable INT0 interrupt

  //Timer1 is used as 1 sec time base
  //Timer Clock = 1/1024 of sys clock
  //Mode = CTC (Clear Timer On Compare)
  TCCR1B|=((1<<WGM12)|(1<<CS12)|(1<<CS10));

  //Compare value=976
  OCR1A=976;
  TIMSK|=(1<<OCIE1A);  //Output compare 1A interrupt enable

  //Enable interrupts globaly
  sei();

  while(1)
  {
  }
}

ISR(INT0_vect)
{
  //CPU Jumps here automatically when INT0 pin detect a falling edge
  count++;
}

ISR(TIMER1_COMPA_vect)
{
  //CPU Jumps here every 1 sec exactly!
  rps=count;
  send(rps);
  count=0;
}

Can we send serial data when interrupt counter and timer, i have error when try it ?

And what is the best methode to read RPM and send value with serial

vega8
  • 534
  • 1
  • 5
  • 25
Lukis triya
  • 1
  • 1
  • 4
  • Your question is somewhat difficult to answer with the amount of information you provided about what you're trying to do. From your other post [link](http://stackoverflow.com/questions/34408272/) I assume your question is about obtaining the rpm of a brushless motor. – vega8 Dec 31 '15 at 15:27
  • You might be able to find the answer to your question in [Atmel application note AVR443](http://www.atmel.com/images/atmel-2596-sensor-based-control-of-three-phase-brushless-dc-motors_application-note_avr443.pdf) and the corresponding [sample code](http://www.atmel.com/Images/AVR443.zip). – vega8 Dec 31 '15 at 15:35
  • 1
    What error do you have when you try it? What doesn't work? Is it as simple as that you never call init(), thus your UART isn't ready to send? – Ross Dec 31 '15 at 17:29

1 Answers1

0

One potential problem is that rps is defined as int16_t and your send routine is expecting an unsigned char.

Is the range 0-255 high enough for your RPS?

Do you want the RPS to be human readable or are you sending it to another computer or micro-controller?

It is possible to send serial in you interrupt routines but you need to be careful since the serial routines are not usually re-entrant.

Jeff
  • 1,364
  • 1
  • 8
  • 17