c++ - Calculator Based on ATMega8 using AVRstudi -


about calculator:

basically calculator made calculate resistance of copper , aluminum wires @ ambient temperature using formula r2= r1*(1+alpha(t-25))

here r2 output, r1 value entered user using 4x4 matrix keypad (which include decimal values 12.5 etc), t temperature in degree celsius recorded temperature sensor lm35. alpha copper = 0.0039 alpha aluminum = 0.0042

how should work:

basically temperature recorded calculator give input t. value of resistance @ 25deg c fed user using keypad. keys 0-9 , "." used enter value. after when user presses "+" on keypad, should implement formula copper , show result on lcd, when user presses "-" should implement formula aluminum. let leave "*" "/" , "=" buttons spare time being.

progress till now:

using codes have sent in attachment, able temperature on screen correctly, able see value of r1 (i.e value of resistance @ 25deg c) cannot figure out how use these values output.

please me this. :)

thanks & regards, mohit goyal

#define f_cpu 1000000ul #include <avr/io.h> #include <stdio.h> #include <stdlib.h> #include <util/delay.h> #include "lcd.h" #include "lcd.c" #include <math.h> #define kb_port_out portb #define kb_port_in pinb void port_init(void) {  ddrb  = 0x0f;       //key-board port, higer nibble - input, lower nibble -     output  portb = 0xff;   }  void init_devices(void) { port_init();   mcucr = 0x00;  timsk = 0x00; //timer interrupt sources  }   void initadc() { admux=(1<<refs0); adcsra=(1<<aden)|(1<<adps1)|(1<<adps0);  } uint16_t readadc (uint8_t ch) { ch=ch&0b00000111; admux|=ch; adcsra|=(1<<adsc); while (! (adcsra & (1<<adif))); adcsra|=(1<<adif); return (adc); } void wait () { uint8_t i; (i=0;i<1;i++) _delay_loop_2(0); } void main()    {  char temp[3]; uint16_t adc_result,mv; int t; lcd_init (lcd_disp_on); lcd_clrscr(); initadc(); lcd_gotoxy(0,0); lcd_puts("r1="); lcd_gotoxy(9,0); lcd_puts(",t="); lcd_gotoxy(15,0); lcd_puts("c"); lcd_gotoxy(0,1); lcd_puts("r2="); while(1) { adc_result=readadc(0); mv=(int)(1000.0*5.0*(((float)adc_result)/1023.0)); t=(int)(mv/10); sprintf(temp,"%d",t); lcd_gotoxy(12,0); lcd_puts(temp); wait(); unsigned char res, uppernibble, mycharpointer, keycode, keypressed, j; int a=0, b=0, c=0, d=0, display=0;  init_devices();    lcd_gotoxy(3,0);  while(1)  {     uppernibble = 0xff;      for(j=0; j<4; j++)     {      _delay_ms(1);      kb_port_out = ~(0x01 << j);      _delay_ms(1);                        //delay port o/p settling      uppernibble = kb_port_in | 0x0f;       if (uppernibble != 0xff)      {        _delay_ms(20);                //key debouncing delay        uppernibble = kb_port_in | 0x0f;        if(uppernibble == 0xff) goto out;         keycode = (uppernibble & 0xf0) | (0x0f & ~(0x01 << j));         while (uppernibble != 0xff)          uppernibble = kb_port_in | 0x0f;         _delay_ms(20);                  //key debouncing delay         switch (keycode)            //generating key characetr display on lcd        {         case (0xee): keypressed = "1";          a=1;         b=b*10+1;                      break;         case (0xed): keypressed = "4";         a=4;         b=b*10+4;                      break;         case (0xeb): keypressed = "7";          a=7;         b=b*10+7;                      break;         case (0xe7): keypressed = ".";                        break;         case (0xde): keypressed = "2";          a=2;         b=b*10+2;                      break;         case (0xdd): keypressed = "5";          a=5;         b=b*10+5;                      break;         case (0xdb): keypressed = "8";          a=8;         b=b*10+8;                      break;         case (0xd7): keypressed = "0";          a=0;         b=b*10+0;                      break;         case (0xbe): keypressed = "3";          a=3;         b=b*10+3;                      break;         case (0xbd): keypressed = "6";          a=6;         b=b*10+6;                      break;         case (0xbb): keypressed = "9";          a=9;         b=b*10+9;                      break;         case (0xb7): keypressed = "=";                       break;         case (0x7e): keypressed = "a";                       break;         case (0x7d): keypressed = "b";                       break;         case (0x7b): keypressed = "c";                       break;         case (0x77): keypressed = "d";                       break;         default    : keypressed = "x";         }//end of switch          lcd_puts(keypressed);   lcd_gotoxy(3,1);  lcd_puts(keypressed);                  out:;       }//end of if     }//end of }//end of while(1)    return 0;   }  

}

one way read input read characters in character array(in switch case block append keypressed character array using strcat function). check whether in right format. , convert number in character array float , use in calculation explained in question link

the way append keypressed string:

char s[25]=""; strcat(s,"1") 

there 1 error noticed
change

keypressed="1" 

to

keypressed='1' 

in such cases. "1" const character array. '1' character

or change type of keypressed character array , use strcpy assign string it.

strcpy(keypressed,"1") 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -