Thông báo

Collapse
No announcement yet.

giúp mình với Tạo xung chùm dùng avr

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • giúp mình với Tạo xung chùm dùng avr

    loay hoay mãi không làm được với avr: mình cần tạo xung chùm tần số 5khz nhưng sao code lỗi ở đâu mô phỏng hoài không ra 5khz. các bác giúp với. mình tự học avr. timer0 ở chế độ so sánh ctc
    // Khai bao thu vien su dung chip ATmega16
    #include <mega16.h>
    /************************************************** **********************
    Khai bao cac bien su dung trong chuong trinh
    y: bien tuong duong voi goc mo alpha
    j: chi so cua mang
    i: bien dem so chu ky
    a: dung de luu gia tri thanh ghi TCNT1 khi goc mo lon
    ************************************************** **********************/
    unsigned int y=0x74,a,i,j=1000;
    /************************************************** **********************
    bang tra cac gia tri ung voi cac gocmo, thoi gian khoi dong la 20s
    ************************************************** **********************/
    flash unsigned char bang[]={
    0x1A, 0x1A, 0x1A, 0x1A,0x1A, 0x1A, 0x1A, 0x1A,0x1A, 0x1A, 0x1A, 0x1A,0x1A,
    0x1A, 0x1A,
    0x1B, 0x1B,0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B,
    0x1B, 0x1B,
    0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
    0x1C, 0x1C,
    0x1D, 0x1D, 0x1D, 0x1D, 0x1D, 0x1D, 0x1D, 0x1D, 0x1D, 0x1D, 0x1D, 0x1D, 0x1D,
    0x1D, 0x1D,
    0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E,
    0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F,
    0x20, 0x20, 0x20, 0x20,0x20, 0x20, 0x20, 0x20,0x20, 0x20, 0x20, 0x20,0x20, 0x20,
    0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
    0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
    0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23,
    0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27,
    0x29, 0x29, 0x29, 0x29, 0x29, 0x29, 0x29, 0x29, 0x29, 0x29, 0x29,
    0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
    0x2B, 0x2B, 0x2B, 0x2B, 0x2B, 0x2B, 0x2B, 0x2B, 0x2B, 0x2B, 0x2B, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C,
    0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2F, 0x2F, 0x2F, 0x2F, 0x2F, 0x2F, 0x2F, 0x2F, 0x2F, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
    0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
    0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
    0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38,
    0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39,
    0x3A, 0x3A, 0x3A, 0x3A, 0x3A, 0x3A, 0x3A, 0x3A, 0x3A,
    0x3B, 0x3B, 0x3B, 0x3B, 0x3B, 0x3B, 0x3B, 0x3B, 0x3B,
    0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C,
    0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D,
    0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E,
    0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
    0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
    0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43,
    0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
    0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45,
    0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46,
    0x47, 0x47, 0x47, 0x47, 0x47, 0x47, 0x47, 0x47, 0x47,
    0x48, 0x48, 0x48, 0x48, 0x48, 0x48, 0x48, 0x48, 0x48,
    0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,
    0x4A, 0x4A, 0x4A, 0x4A, 0x4A, 0x4A, 0x4A, 0x4A, 0x4A,
    0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
    0x4C, 0x4C, 0x4C, 0x4C, 0x4C, 0x4C, 0x4C, 0x4C, 0x4C,
    0x4D, 0x4D, 0x4D, 0x4D, 0x4D, 0x4D, 0x4D, 0x4D, 0x4D,
    0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E,
    0x4F, 0x4F, 0x4F, 0x4F, 0x4F, 0x4F, 0x4F, 0x4F, 0x4F,
    0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50,
    0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x52, 0x52, 0x52, 0x52, 0x52, 0x52, 0x52, 0x52, 0x52, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
    0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57,
    0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
    0x5D, 0x5D, 0x5D, 0x5D, 0x5D, 0x5D, 0x5D, 0x5D, 0x5D,
    0x5E, 0x5E, 0x5E, 0x5E, 0x5E, 0x5E, 0x5E, 0x5E, 0x5E,
    0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67,
    0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
    0x68,
    0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69,
    0x69,
    0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A,
    0x6A, 0x6A,
    0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B, 0x6B,
    0x6B, 0x6B,
    0x6C, 0x6C, 0x6C, 0x6C,0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C, 0x6C,
    0x6C, 0x6C,
    0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D, 0x6D,
    0x6D, 0x6D,
    0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E, 0x6E,
    0x6E, 0x6E,
    0x6F, 0x6F, 0x6F, 0x6F, 0x6F, 0x6F, 0x6F, 0x6F, 0x6F, 0x6F, 0x6F, 0x6F, 0x6F, 0x6F,
    0x6F,
    0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70,
    0x70, 0x70, 0x70, 0x70,
    0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71,
    0x71, 0x71, 0x71, 0x71,
    0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
    0x72, 0x72, 0x72, 0x72,
    0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73,
    0x73, 0x73, 0x73, 0x73,
    0x74, 0x74, 0x74, 0x74, 0x74, 0x74, 0x74, 0x74, 0x74, 0x74, 0x74, 0x74, 0x74, 0x74,
    0x74, 0x74, 0x74, 0x74
    };
    /************************************************** **********************
    CHUONG TRINH PHUC VU NGAT INPUTCAPTURE (TIMER1)
    ************************************************** **********************/
    interrupt [TIM1_CAPT] void timer1_capt_isr(void)
    {
    i++;
    if (j==0)
    {
    PORTD.1=0x01;y=0x1A;}
    else
    {j=j-1;
    y=bang[j];}
    }
    /************************************************** **********************
    CHUONG TRINH PHUC VU MGAT SO SANH (TIMER0)
    (xay ra khi TCNT0 = OCR0)
    ************************************************** **********************/
    interrupt [TIM0_COMP] void timer0_comp_isr(void)
    {
    PORTD.0^=1;
    }
    /************************************************** **********************
    CHUONG TRINH CHINH
    ************************************************** **********************/
    void main(void)
    {
    /************************************************** **********************
    DAT CHE DO HOAT DONG CHO CAC CONG VAO - RA
    PORTA: Dat la cac dau vao (khong su dung)
    PORTB: Dat la cac dau vao (khong su dung)
    PORTC: Dat la cac dau ra (xuat xung dieu khien)
    PORTD: - PORTD.6: dat lam dau vao (nhan tin hieu dong pha)
    - PORTD.0: dat lam dau ra (phat tin hieu xung chum)
    - PORTD.1: dat lam dau ra (phat tin hieu dieu khien ro le)
    ************************************************** **********************/
    PORTC=0x00;
    DDRC=0xFF;
    PORTD=0x00;
    DDRD=0x03;
    /************************************************** **********************
    DAT CHE DO HOAT DONG CHO TIMER0
    - che do hoat dong CTC top = OCR0
    - xung nhip la xung nhip hoat dong cua chip
    - prescaler = 1/1024
    - OCR0 = 0x02
    ************************************************** **********************/
    TCCR0=0x0D;
    TCNT0=0x00;
    OCR0=0x02;
    /************************************************** *********************
    DAT CHE DO HOAT DONG CHO TIMER1
    - che do hoat dong normal top = 0xFFFF
    - xung nhip la xung nhip hoat dong cua chip
    - prescaler = 1/1024
    - ngat input capture khi co suon len o chan ICP (PORTD.6)
    ************************************************** **********************/
    TCCR1A=0x00;
    TCCR1B=0x45;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;
    /************************************************** *********************/
    CHO PHEP XAY RA NGAT
    ************************************************** **********************/
    TIMSK=0x22;
    #asm("sei")
    /************************************************** **********************
    VONG LAP VO TAN
    so sanh gia tri cua thanh ghi TCNT1 de phat xung ra PORTC
    ************************************************** **********************/
    while (1)
    {
    if (TCNT1==ICR1+y){PORTB=0x09;PORTC=0x09;}
    if (TCNT1==ICR1+y+0x34){PORTB=0x21;PORTC=0x21;}
    if (TCNT1==ICR1+y+0x68) {PORTB=0x24;PORTC=0x24;}
    if (TCNT1==ICR1+y+0x9C) {PORTB=0x06;a=TCNT1;PORTC=0x06;}
    if (TCNT1==ICR1+y+D0) {PORTB=0x12;PORTC=0x12;}
    if (TCNT1==ICR1+y+104) {PORTB=0x18;PORTC=0x18;}
    if (i>=1)
    {
    if (TCNT1==a+0x34){PORTB=0x12;PORTC=0x12;}
    if (TCNT1==a+0x68){PORTB=0x18;PORTC=0x18;}
    if (TCNT1==ICR1+y){PORTB=0x09;PORTC=0x09;}
    if (TCNT1==ICR1+y+0x34){PORTB=0x21;PORTC=0x21;}
    if (TCNT1==ICR1+y+0x68) {PORTB=0x24;PORTC=0x24;}
    if (TCNT1==ICR1+y+0x9C) {PORTB=0x06;a=TCNT1;PORTC=0x06;}
    if (TCNT1==ICR1+y+0xD0) {PORTB=0x12;PORTC=0x12;}
    if (TCNT1==ICR1+y+0x104) {PORTB=0x18;PORTC=0x18;}
    }
    };
    }

Về tác giả

Collapse

hvktqs Tìm hiểu thêm về hvktqs

Bài viết mới nhất

Collapse

Đang tải...
X