-1

关于计算器:

基本上,这个计算器是用来计算铜线和铝线在环境温度下的电阻,使用公式 R2= R1*(1+alpha(T-25))

这里 R2 是输出,R1 是用户使用 4x4 矩阵键盘输入的值(包括十进制值,如 12.5 等),T 是温度传感器 LM35 记录的摄氏温度。铜的 alpha = 0.0039 铝的 alpha = 0.0042

它应该如何工作:

基本上,温度将由计算器记录,该计算器将给出输入 T。用户使用键盘输入 25 摄氏度时的电阻值。现在是键 0-9 和“。” 用于输入值。此后,当用户在键盘上按“+”时,它应该执行铜的公式并在 LCD 上显示结果,同样,当用户按“-”时,它应该执行铝的公式。让我们暂时将“*”“/”和“=”按钮留作备用。

目前进展:

使用我在此附件中发送给您的代码,我能够正确地在屏幕上获得温度,我能够看到 R1 的值(即 25 摄氏度时的电阻值)现在我不知道如何使用这些值来获得输出。

请帮我解决一下这个。:)

谢谢和问候, 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; 
 } 

}

4

1 回答 1

0

读取输入的一种方法是读取字符数组中的字符(在您的 switch case 块中,使用strcat函数将按键追加到字符数组)。然后检查它的格式是否正确。然后将字符数组中的数字转换为浮点数并在计算中使用它,如问题链接中所述

将按键附加到字符串的方法:

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

我注意到一个错误
更改

keypressed="1"

keypressed='1'

在所有这些情况下。“1”是 const 字符数组。'1' 是字符

或更改按键字符数组的类型并使用 strcpy 为其分配任何字符串。

strcpy(keypressed,"1")
于 2013-08-28T18:22:47.303 回答