Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

About the Calculator:

Basically this calculator is made to calculate the resistance of copper and aluminum wires at the ambient temperature using the formula R2= R1*(1+alpha(T-25))

Here R2 will be the output, R1 will be the value entered by the user using a 4x4 matrix keypad (which will include decimal values like 12.5 etc), T is the temperature in degree Celsius recorded by the temperature sensor LM35. alpha for copper = 0.0039 alpha for aluminum = 0.0042

How it should work:

Basically the temperature will be recorded by the calculator which will give the input T. The value of resistance at 25deg C will be fed by the user using keypad. Now the keys 0-9 and "." are used to enter the value. After this when the user presses say "+" on keypad, it should implement the formula for copper and show the result on LCD, similarly when the user presses "-" it should implement the formula for aluminum. Let us leave the "*" "/" and "=" buttons as spare for the time being.

Progress till now:

Using the codes which I have sent you in this attachment, I am able to get the temperature on screen correctly, I am able to see the value of R1 (i.e value of resistance at 25deg C) Now I cannot figure out how to use these values to get the output.

Please help me with 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;
for (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 for 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 to 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 for
}//end of while(1)  

return 0; 
 } 

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
124 views
Welcome To Ask or Share your Answers For Others

1 Answer

One way to read input is read characters in character array(in your switch case block append keypressed to character array using strcat function). Then check whether it is in right format. And then convert the number in character array to float and use it in calculation as explained in question link

The way to append keypressed to string:

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

There is one error I noticed
Change

keypressed="1"

to

keypressed='1'

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

or change the type of keypressed character array and use strcpy to assign any string to it.

strcpy(keypressed,"1")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...