基于ATMega8的计算器使用AVRstudi

时间:2013-08-28 14:35:03

标签: c++ c calculator avr

关于计算器:

基本上,这个计算器用于计算环境温度下铜线和铝线的电阻值 R2 = R1 *(1 +α(T-25))

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

它应该如何运作:

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

进展到现在:

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

请帮我解决这个问题。 :)

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

}

1 个答案:

答案 0 :(得分:0)

读取输入的一种方法是在字符数组中读取字符(在开关案例块中使用strcat函数附加到字符数组中)。然后检查它是否格式正确。然后将字符数组中的数字转换为float并在计算中使用它,如question link

中所述

将keypressed附加到字符串的方法:

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

我注意到有一个错误发生了变化

keypressed="1"

keypressed='1'

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

或更改keypressed字符数组的类型,并使用strcpy为其指定任何字符串。

strcpy(keypressed,"1")