V
V
Vladimir Vladimirovich2017-05-18 23:36:13
Atmel AVR
Vladimir Vladimirovich, 2017-05-18 23:36:13

How to fix a calculation error?

Scheme: 7f4ad26795014d7c95a8c8bae501fe2d.BMP
"Even" temperature outputs as needed. But here, for example, 9 breasts - no. The order of operation with a sequential increase by a degree is 8,8,10,10,12 and so on. What could be the problem? The catch is that I need to work with a 2-byte number.
Code itself:
isis+atmel studio project

#include "main.h"

int speed=1;
//----------------------------------------
void port_ini(void)
{
  DDRD=0xFF;
  PORTD=0x00;

  DDRC=0xFF;
  PORTC=0x00;

  DDRB    = (1 << PB1)|(1 << PB2)|(1 << PB0);
  
  //----------
  DDRA=0xFF;
  PORTA=0xFF;
  
}

void spi_init(void){
  volatile char IOReg;
  SPCR = ((1<<SPE)|(1<<MSTR));//Включим шину, объявим ведущим
    IOReg   = SPSR;               // очистить бит SPIF в регистре SPSR
    IOReg   = SPDR;
}
void spi_read_array(uint8_t num, uint8_t *data)
{
  PORTA &= ~(1<<PA7);
  while(num--){
    SPDR = *data;
    while(!(SPSR & (1<<SPIF)));
    *data++ = SPDR;
  }
  PORTA |= (1<<PA7);

}
//----------------------------------------
int main(void)
{
  
  unsigned char c;
  unsigned char tempertur;
  c=0b10000000;
  int adc_value;
  float n;
  port_ini(); //Инициализируем порты
  LCD_ini();  //Инициализируем дисплей
  ADC_Init(); //Инициализируем АЦП
  clearlcd(); //Очистим дисплей
  //PORTA=c;
  spi_init();
  setpos(0,0);
  
   signed short int buf[2] = {0,0};
  
    while(1)
    {	
    spi_read_array(2, buf);
      setpos(0,0);
    
    buf[0]<<=8;
    buf[0]+=buf[1];
    buf[0]>>=3;
    n= (float)buf[0];
    
    n*=0.0625;
    
    
    
    sendcharlcd(((unsigned char)n%1000)/100+0x30);//Преобразуем число в код числа
    sendcharlcd(((unsigned char)n%100)/10+0x30);//Преобразуем число в код числа
    sendcharlcd(((unsigned char)n%10)+0x30);//Преобразуем число в код числа
    
    
    //speed=buf[0];
    if(buf[0]>=10){
      setpos(1,5);
      str_lcd("vorvard");
      normal();
      setpos(1,5);
      str_lcd("reverse");
      reverse();
    }
    
    buf[0]=0;
    buf[1]=0;
    _delay_ms(50);
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Ocelot, 2017-05-18
@Ocelot

buf is declared signed and then a bit shift is applied to it, which is generally undefined behavior.

1
15432, 2017-05-19
@15432

You have clearly written, if N is odd, add one. So it turns out 8, 8, 10, 10.
One is added to all odd ones.

if((unsigned char)n%2!=0){
      n+=1; 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question