اطلاعیه

Collapse
No announcement yet.

بررسی یک کد ساده برای دماسنج

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    بررسی یک کد ساده برای دماسنج

    سلام
    من این کد رو در پروتئوس تست کردم درست کار می کنه ولی در عمل خیر
    من در عمل از یک میکرو مگا8 و یک ال سی دی استفاه کردم و دو عدد ال ای دی که با دما بالاوپایین شدن مثلا قرمز و سبز روشن بشه با شرطی که گذاشتم .
    کد:
    /*****************************************************
    This program was produced by the
    CodeWizardAVR V2.05.3 Standard
    Automatic Program Generator
    © Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
    http://www.hpinfotech.com
    
    Project : 
    Version : 
    Date  : 07/04/2015
    Author : PerTic@n
    Company : If You Like This Software,Buy It
    Comments: 
    
    
    Chip type        : ATmega8
    Program type      : Application
    AVR Core Clock frequency: 8.000000 MHz
    Memory model      : Small
    External RAM size    : 0
    Data Stack size     : 256
    *****************************************************/
    
    #include <mega8.h>
    
    #include <delay.h>
     #include <stdlib.h>
    // Alphanumeric LCD functions
    #include <alcd.h>
    
    #define ADC_VREF_TYPE 0x00
    
    // Read the AD conversion result
    unsigned int read_adc(unsigned char adc_input)
    {
    ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
    // Delay needed for the stabilization of the ADC input voltage
    delay_us(10);
    // Start the AD conversion
    ADCSRA|=0x40;
    // Wait for the AD conversion to complete
    while ((ADCSRA & 0x10)==0);
    ADCSRA|=0x10;
    return ADCW;
    }
    
    // Declare your global variables here
     char s[10];
     int d,vol;
    void main(void)
    {
    // Declare your local variables here
    
    // Input/Output Ports initialization
    // Port B initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
    PORTB=0x00;
    DDRB=0x00;
    
    // Port C initialization
    // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
    // State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
    PORTC=0x00;
    DDRC=0x00;
    
    // Port D initialization
    // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
    // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
    PORTD=0x00;
    DDRD=0xFF;
    
    // Timer/Counter 0 initialization
    // Clock source: System Clock
    // Clock value: Timer 0 Stopped
    TCCR0=0x00;
    TCNT0=0x00;
    
    // Timer/Counter 1 initialization
    // Clock source: System Clock
    // Clock value: Timer1 Stopped
    // Mode: Normal top=0xFFFF
    // OC1A output: Discon.
    // OC1B output: Discon.
    // Noise Canceler: Off
    // Input Capture on Falling Edge
    // Timer1 Overflow Interrupt: Off
    // Input Capture Interrupt: Off
    // Compare A Match Interrupt: Off
    // Compare B Match Interrupt: Off
    TCCR1A=0x00;
    TCCR1B=0x00;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;
    
    // Timer/Counter 2 initialization
    // Clock source: System Clock
    // Clock value: Timer2 Stopped
    // Mode: Normal top=0xFF
    // OC2 output: Disconnected
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;
    
    // External Interrupt(s) initialization
    // INT0: Off
    // INT1: Off
    MCUCR=0x00;
    
    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x00;
    
    // USART initialization
    // USART disabled
    UCSRB=0x00;
    
    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;
    
    // ADC initialization
    // ADC Clock frequency: 125.000 kHz
    // ADC Voltage Reference: AREF pin
    ADMUX=ADC_VREF_TYPE & 0xff;
    ADCSRA=0x86;
    
    // SPI initialization
    // SPI disabled
    SPCR=0x00;
    
    // TWI initialization
    // TWI disabled
    TWCR=0x00;
    
    // Alphanumeric LCD initialization
    // Connections are specified in the
    // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
    // RS - PORTB Bit 0
    // RD - PORTB Bit 1
    // EN - PORTB Bit 2
    // D4 - PORTB Bit 4
    // D5 - PORTB Bit 5
    // D6 - PORTB Bit 6
    // D7 - PORTB Bit 7
    // Characters/line: 12
    lcd_init(12);
    lcd_gotoxy(0,0);
                  lcd_putsf("salam");
                  delay_ms(1000);
                  lcd_clear();
    
    while (1)
       {
       d=read_adc(0);
       vol=(5*d)/1023;
          itoa(vol,s);    
          lcd_gotoxy(0,0);  
          lcd_puts(s);
               if(s>50)
               {
             PORTD.0=1;
             }
             if(s<50)
             {
             PORTD.1=0;
             }
             
            
    
       }
    }

    #2
    پاسخ : بررسی یک کد ساده برای دماسنج

    اینم پروتئوس برنامه

    دیدگاه


      #3
      پاسخ : بررسی یک کد ساده برای دماسنج

      با سلام
      شما در شبیه ساز چرا به پایه 23 (ADC0) ولتاژ +3 ولت وصل کردید ..؟
      اون رو حذف کنید،ببینید چه نتیجه ی می گیرید.
      باتشکر.

      دیدگاه


        #4
        پاسخ : بررسی یک کد ساده برای دماسنج

        حالا تو شبیه ساز اونم حذف کنم مساله ی نیست مساله تو عمل هست

        دیدگاه


          #5
          پاسخ : بررسی یک کد ساده برای دماسنج

          اخه شما اومدی 3 ولت به adcدادی خوب معلومه سنسور دما اصلا کار نمیکند.
          شما اون سه ولت رو قطع کن اگه کار نکرد برو سراغ برنامه
          Telegram:
          @reza_br_2489


          Instagram:
          _amp_c

          دیدگاه


            #6
            پاسخ : بررسی یک کد ساده برای دماسنج

            سلام بر شما
            --
            خوب یک سری تغییراتی بهش دادم و یک سری ملزوماتی بهش اضافه کردم .
            الان فایل پرتئوس خیلی خوب جواب میده .. و اگر طبق نقشه روی برد برد پیدا سازی کنید،بدون مشکل جواب خواهد داد(انشالله)
            (کمی توضیحات فارسی هم براتون نوشتم )

            عکس پرتئوس دماسنج

            کدها
            [code=c]/************************************************** *****
            Project :Temp lm35
            Version :1.0
            Date : 07/05/2015
            Author : Sinaset
            Company :
            Comments:

            Chip type : ATmega8A
            Program type : Application
            AVR Core Clock frequency: 8.000000 MHz
            Memory model : Small
            External RAM size : 0
            Data Stack size : 256
            ************************************************** *****/

            #include <mega8.h>

            #include <delay.h>
            #include <stdlib.h>
            #include <stdio.h>
            #include <alcd.h>
            #define ADC_VREF_TYPE ((0<<REFS1) | (0<<REFS0) | (0<<ADLAR)) //به عنوان ولتاژ مرجعAREF تایین پایه

            unsigned int read_adc(unsigned char adc_input)
            {
            ADMUX=adc_input | ADC_VREF_TYPE;
            delay_us(10);
            ADCSRA|=(1<<ADSC);
            while ((ADCSRA & (1<<ADIF))==0);
            ADCSRA|=(1<<ADIF);
            return ADCW;
            }

            void main(void)
            {
            char s[16];
            int vol;
            DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) | (0<<DDB0);
            // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
            PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);
            DDRC=(0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
            PORTC=(0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
            DDRD=(0<<DDD7) | (0<<DDD6) | (0<<DDD5) | (0<<DDD4) | (0<<DDD3) | (0<<DDD2) | (1<<DDD1) | (1<<DDD0);
            PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
            TCCR0=(0<<CS02) | (0<<CS01) | (0<<CS00);
            TCNT0=0x00;
            // Analog Comparator initialization
            // Analog Comparator: Off
            // The Analog Comparator's positive input is
            // connected to the AIN0 pin
            // The Analog Comparator's negative input is
            // connected to the AIN1 pin
            ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0);
            // ADC initialization
            // ADC Clock frequency: 125.000 kHz
            // ADC Voltage Reference: AREF pin
            ADMUX=ADC_VREF_TYPE;
            ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADFR) | (0<<ADIF) | (0<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (0<<ADPS0);
            SFIOR=(0<<ACME);
            // SPI initialization
            // SPI disabled
            SPCR=(0<<SPIE) | (0<<SPE) | (0<<DORD) | (0<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0);
            lcd_init(16); //تعداد ستون های ال سی دی
            lcd_putsf("BE NAME KHODA" //پیام خوش آمد گویی و شروع با نام خدا
            delay_ms(100); //مدت زمان نمایش پیام
            lcd_clear(); //پاک کردن پیام
            while (1)
            {
            vol=read_adc(0)*.49; //فرمول بدست اوردن دما از سنسور
            itoa(vol,s);
            sprintf(s,"Temp=%d % %c",vol);
            lcd_gotoxy(0,0); //تایین موقعیت متن
            lcd_puts(s); //نمایش متن
            delay_ms(50); //تاخیر نمایش

            if(vol>=50) //درصورتی که دما بیشتر از 50 درجه سانتی گراد بود ،چراغ قرمز روشن شود
            {
            PORTD.0=1;
            PORTD.1=0;
            lcd_gotoxy(0,1);
            lcd_putsf("LED:Red "
            }
            if(vol<=50) //درصورتی که دما کمتر از 50 درجه سانتی گراد بود،چراغ قرمز روشن شود
            {
            PORTD.0=0;
            PORTD.1=1;
            lcd_gotoxy(0,1);
            lcd_putsf("LED:GREEN "
            }
            }
            }
            [/code]

            برای دانلود فایل پرتئوس و کدها هم می تونید به این ادرس مراجعه کنید.
            سوالی بود درخدمتون هستم
            باتشکر.

            دیدگاه

            لطفا صبر کنید...