基于使用AVRstudi ATMEGA8计算器 [英] Calculator Based on ATMega8 using AVRstudi

查看:252
本文介绍了基于使用AVRstudi ATMEGA8计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于计算器:

基本上,此计算器由使用公式在环境温度来计算铜和铝导线的电阻
R 2 = R 1 *(1 +α(T-25))

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))

下面R2将是输出中,R1将使用4×4矩阵键盘(其中将包括像12.5等等十进制值)由用户输入的值,T是由温度传感器LM35记录在摄氏度的温度。
阿尔法铜= 0.0039
阿尔法铝= 0.0042

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

应该是如何工作的:

基本上温度将由计算器这将给输入T.电阻的值是在25degÇ将通过使用键盘的用户供给被记录。
现在键0-9和。用于输入值。
在此之后,当用户presses说键盘上的+,它应该实现对铜的配方,并显示在LCD上的结果,同样,当用户presses - 它应该实现对铝的公式。让我们离开*,/和=键作为备用暂且。

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.

到现在为止进展:

使用codeS这是我送你的这种执着,我能够正确地在屏幕上得到的温度,我能看到R1的值(即在25deg C抵抗值)现在我不能弄清楚如何使用这些值来获得输出。

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.

请帮助我。 :)

感谢和放大器;问候,
莫希特戈亚尔

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; 
 } 

}

推荐答案

在字符数组读取读取输入的一种方式字符(在交换机的情况下块追加键$ P $使用pssed字符数组 strcat的功能)。然后检查它是否在正确的格式。然后转换成字符数组数浮动,用它计算在<一个解释href=\"http://stackoverflow.com/questions/18494218/how-to-convert-char-into-float/18494338#18494338\">question链接结果

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

追加pssed字符串键$ P $的方式:

The way to append keypressed to string:

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

有一个错误,我注意到结果更改

There is one error I noticed
Change

keypressed="1"

keypressed='1'

在所有这些情况。 1是常量字符数组。 '1'字

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

或更改键pressed字符数组的类型和使用的strcpy为任何字符串分配给它。

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

strcpy(keypressed,"1")

这篇关于基于使用AVRstudi ATMEGA8计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆