Welcome & Happy Holidays!
Kết quả 1 đến 5 của 5
  1. #1
    Ngày tham gia
    Apr 2016
    Bài viết
    0

    help ngắt timer & 3Lm35 + atmega16

    anh chị kiểm tra giúp e với. code này ko báo lỗi trong codevision nhưng vào proteus nó ko chạy ạ. ngày mai e thuyết trình mất rồi.
    Đề tài: Nghiên cứu bộ Timer 0 (8bit) trong ATMEGA16 và ngắt Timer. Thiết kế mạch VĐK ATMEGA16 đọc 3 cảm biến LM35 vào 3 kênh. Dùng ngắt Timer 0 để cho phép mỗi 2s đọc lần lượt các kênh ADC và hiển thị nhiệt độ ra LCD.


    Mã:
    
    
    /*****************************************************
    This program was produced by the
    CodeWizardAVR V2.05.0 Professional
    Automatic Program Generator
    © Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
    HP InfoTech - CodeVisionAVR C Compiler
    
    Project :
    Version :
    Date    : 11/4/2014
    Author  : NeVaDa
    Company :
    Comments:
    
    
    Chip type               : ATmega16
    Program type            : Application
    AVR Core Clock frequency: 8.000000 MHz
    Memory model            : Small
    External RAM size       : 0
    Data Stack size         : 256
    *****************************************************/
    
    #include <mega16.h>
    
    #include <delay.h>
    
    // Alphanumeric LCD Module functions
    #include <alcd.h>
    #define FIRST_ADC_INPUT 0
    #define LAST_ADC_INPUT 4
    unsigned int adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
    #define ADC_VREF_TYPE 0x00
    int t1,t2,t3;
    int i;
    
    // Timer 0 overflow interrupt service routine
    interrupt [TIM0_OVF] void timer0_ovf_isr(void)
    {
    // Place your code here
        TCNT0=0x04;                   // Khoi tao gia tri ban dau cho TIMER0 =4
        i++;                          // moi lan ngat tran thi i tang len 1
        if(i==80){PORTA.0^=1;i=81; };      // khi dem den 91 thi dao trang thai PORTA.0 gán i=0 de bat dau lai
        if(i==160) {PORTA.1^=1;i=161;};
        if(i==240){PORTA.2^=1;i=0;}
    
    }
    
    
    
    // Read the 8 most significant bits
    // of the AD conversion result
    unsigned char 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 ADCH;
    }
    void hienthilcd(int so,char x,char y)
    {
    char tram,chuc,dv;
    tram=so/100;
    chuc=(so%100)/10;
    dv=(so%10);
    lcd_gotoxy(x,y);
    lcd_putchar(tram+48);
    lcd_putchar(chuc+48);
    lcd_putchar(dv+48);
    }
    
    // Declare your global variables here
    
    void main(void)
    {
    
    PORTA=0x00;
    DDRA=0x00;
    
    PORTB=0x00;
    DDRB=0x00;
    
    PORTC=0x00;
    DDRC=0xFF;
    
    PORTD=0x00;
    DDRD=0x00;
    
    // Timer/Counter 0 initialization
    // Clock source: System Clock
    // Clock value: Timer 0 Stopped
    // Mode: Normal top=0xFF
    // OC0 output: Disconnected
    PORTA=0x00;
    DDRA=0x07;
    
    TCCR0=0x05;        //Dung xung clock lck I/O= 1024 (  CS00=1, CS01=0 và CS02=1)
    TCNT0=0x04;        // Khoi tao timer0 =4
    OCR0=0x00;
    TIMSK=0x01;        // cho phep ngat tran timer 0
    
    
    #asm("sei")        // cho phep ngat toan cuc
    // 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
    // INT2: Off
    MCUCR=0x00;
    MCUCSR=0x00;
    
    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x01;
    
    // USART initialization
    // USART disabled
    UCSRA=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: 1000.000 kHz
    // ADC Voltage Reference: AREF pin
    // ADC Auto Trigger Source: ADC Stopped
    // Only the 8 most significant bits of
    // the AD conversion result are used
    ADMUX=ADC_VREF_TYPE & 0xff;
    ADCSRA=0x83;
    
    // SPI initialization
    // SPI disabled
    SPCR=0x00;
    
    // TWI initialization
    // TWI disabled
    TWCR=0x00;
    
    // Alphanumeric LCD initialization
    // Connections specified in the
    // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
    // RS - PORTC Bit 0
    // RD - PORTC Bit 1
    // EN - PORTC Bit 2
    // D4 - PORTC Bit 4
    // D5 - PORTC Bit 5
    // D6 - PORTC Bit 6
    // D7 - PORTC Bit 7
    // Characters/line: 16
    lcd_init(16);
    
    // Global enable interrupts
    #asm("sei")
    
    while (1)
          {
          // Place your code here
           t1=(adc_data[0])/2;
          t2=(adc_data[1])/2;
          t3=(adc_data[2])/2;
          hienthilcd(t1,0,0);
          hienthilcd(t2,5,0);
          hienthilcd(t3,10,0);
    
          }
    }


    [IMG]http://*************/attachments/44375/[/IMG]


  2. #2
    Ngày tham gia
    Apr 2016
    Bài viết
    0
    Mình không hiểu ý tưởng của bạn lắm. Tại sao lại PORTA.0^=1;

  3. #3
    Ngày tham gia
    Jun 2016
    Bài viết
    0
    PORTA.0^=1. để nó thay đổi giá trị đó bạn. giống như là cho con Led mỗi 2s sáng 1 lần, thay vì con led mình cho Lm35 mỗi 2s lấy 1 lần giá trị

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi Bùi Tuấn Vũ
    PORTA.0^=1. để nó thay đổi giá trị đó bạn. giống như là cho con Led mỗi 2s sáng 1 lần, thay vì con led mình cho Lm35 mỗi 2s lấy 1 lần giá trị
    Đó là bạn output dữ liệu ra thì mới dùng như vậy. Còn ở đây bạn đang nhận dữ liệu vào thì không thể làm như thế được.

    Mình thấy bạn có hàm read_adc mà sao không dùng?

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi dangsonbk
    Đó là bạn output dữ liệu ra thì mới dùng như vậy. Còn ở đây bạn đang nhận dữ liệu vào thì không thể làm như thế được.

    Mình thấy bạn có hàm read_adc mà sao không dùng?
    có vẻ mình đc thông tí ^^! tks

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •  
Múi giờ GMT +7. Bây giờ là 10:40 AM. Diễn đàn sử dụng vBulletin® Phiên bản 4.2.5.
Bản quyền của 2025 vBulletin Solutions, Inc. Tất cả quyền được bảo lưu.
Ban quản trị không chịu trách nhiệm về nội dung do thành viên đăng.