yêu cầu bài tập : hiễn thị chuỗi "KHOA CONG NGHE DIEN TU" từ phải sang trái, sao cho màu của mỗi kí tự thay đổi sau mỗi giay ( đỏ-xanh-cam)
code của mình đây, mình viết bằng HT nhé
còn đây là file mô phỏng
C-2LED.DSN
code của mình đây, mình viết bằng HT nhé
Code:
#include <htc.h> #include <math.h> // Cau hinh su dung dao dong noi 4MHz __CONFIG(HS & WDTDIS & PWRTEN & UNPROTECT & BORDIS & LVPDIS); #define _XTAL_FREQ 4000000 // Khai bao bien. // Khai bao du lieu hien thi. const unsigned char a[] = { 0XFF ,0X0 ,0XF7 ,0XEB ,0XDD ,0XBE ,0X7F ,0XFF K ,0XFF ,0X0 ,0XEF ,0XEF ,0XEF ,0XEF ,0X0 ,0XFF H ,0XFF ,0X81 ,0X7E ,0X7E ,0X7E ,0X7E ,0X81 ,0XFF ,0XFF ,0X7 ,0XDB ,0XDD ,0XDD ,0XDB ,0X7 ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0X81 ,0X7E ,0X7E ,0X7E ,0X7E ,0XBD ,0XFF ,0XFF ,0X81 ,0X7E ,0X7E ,0X7E ,0X7E ,0X81 ,0XFF ,0XFF ,0X0 ,0XFD ,0XFB ,0XF7 ,0XEF ,0X0 ,0XFF ,0XFF ,0X81 ,0X7E ,0X7E ,0X6E ,0X6E ,0X8F ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0X0 ,0XFD ,0XFB ,0XF7 ,0XEF ,0X0 ,0XFF ,0XFF ,0X81 ,0X7E ,0X7E ,0X7E ,0X6E ,0X8F ,0XFF ,0XFF ,0X0 ,0XEF ,0XEF ,0XEF ,0XEF ,0X0 ,0XFF ,0XFF ,0X0 ,0X76 ,0X76 ,0X76 ,0X7E ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0X0 ,0X7E ,0X7E ,0X7E ,0X7E ,0X81 ,0XFF ,0XFF ,0XFF ,0X7E ,0X0 ,0X7E ,0XFF ,0XFF ,0XFF ,0XFF ,0X0 ,0X76 ,0X76 ,0X76 ,0X7E ,0XFF ,0XFF ,0XFF ,0X0 ,0XFD ,0XFB ,0XF7 ,0XEF ,0X0 ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFF ,0XFE ,0XFE ,0X0 ,0XFE ,0XFE ,0XFE ,0XFF ,0XFF ,0X80 ,0X7F ,0X7F ,0X7F ,0X7F ,0X80 ,0XFF }; unsigned int k,dat_dis=0,i; // Khai bao ham. // Khi viet CTC nam duoi doan code cua CT chinh thi phai khai bao ten cac // cua CTC su dung vao dau chuong trinh, nguoc lai thi khong can khai bao. // ..................................... // *************************************************** // Ten CTC: init_PORTD. // Thong so dau vao: // Thong so dau ra: // Cong dung: Khoi tao Port D. // *************************************************** void init_PORTD(void) { PORTD = 0X00; // Khoi tao Port D. TRISD = 0X00; // Cau hinh PORTD la ngo ra. } // *************************************************** // Ten CTC: init_PORTB. // Thong so dau vao: // Thong so dau ra: // Cong dung: Khoi tao Port B. // *************************************************** void init_PORTB(void) { PORTB = 0X00; // Khoi tao Port B. TRISB = 0X00; // Cau hinh PORTB la ngo ra. } void init_PORTC(void) { PORTC = 0X00; // Khoi tao PortC. TRISC = 0X00; // Cau hinh PORTC la ngo ra. } // *************************************************** void scanmatrixsta(int dat_dis) { // Khai bao bien. unsigned char scancode; unsigned int k=0,i; // Dinh nghia ham. scancode = 0x01; // Ma quet LED ban dau - 00000001. for(i=dat_dis;i<=dat_dis+7;i++) { if (k==0) { PORTB=a[i]; PORTC=scancode; PORTD=0x00; __delay_ms(1); PORTC=0X00; PORTD=0x00; scancode = scancode << 1; // Dich ma quet LED. k=k+1; } else if(k==1) { PORTB=a[i]; PORTD=scancode; PORTC=0x00; __delay_ms(1); PORTD=0X00; PORTC=0x00; scancode = scancode << 1; // Dich ma quet LED. k=k+1; } else if(k==2) { PORTB=a[i]; PORTC=scancode; PORTD=scancode; __delay_ms(1); PORTC=0X00; PORTD=0X00; scancode = scancode << 1; // Dich ma quet LED. k=k+1; } } } // Chuong trinh chinh. void main (void) { // Khai bao bien. unsigned int i,dat_dis; // Chuong trinh. init_PORTB(); // Cau hinh Port B. init_PORTD(); // Cau hinh Port D. init_PORTC(); // delay; while(1) // Vong lap cho ngat. { for (dat_dis=0;dat_dis<=168;dat_dis++) // Thay doi du lieu hien thi. { for(i=1;i<=30;i++) // Thoi gian cua 1 trang thai man hinh (20 x 8 x 3ms). { scanmatrixsta(dat_dis); // Quet LED de hien thi. } } } }
C-2LED.DSN
Comment