Trong bài này sẽ hướng dẫn quét led 7 đoạn bằng timer
Đoạn chương trình mẫu:
/* ytuongnhanh.vn*/ #include <16f877a.h> #device *=16 ADC=10 #FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT #use delay(clock=12000000) unsigned char Code7Seg[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90}; unsigned char quet[]={0b11111110,0b11111101, 0b11111011, 0b11110111,0b11101111,0b11011111}; int digit=0; int temp; #int_timer0 void segment_mux_isr() { if(digit==4) digit=0;//da quet het 4 led switch(digit) { case 0: { OUTPUT_a(0xff) ; OUTPUT_d(Code7Seg[0]); // hien thi so 0 OUTPUT_a(quet[digit]) ; digit++; break; } case 1: { OUTPUT_a(0xff) ; OUTPUT_d(Code7Seg[temp/100]); //Hien thi phan tram cua bien temp OUTPUT_a(quet[digit]) ; digit++; break; } case 2: { OUTPUT_a(0xff) ; OUTPUT_d(Code7Seg[(temp%100)/10]); //hien thi hang chuc cua bien temp OUTPUT_a(quet[digit]) ; digit++; break; } case 3: { OUTPUT_a(0xff) ; OUTPUT_d(Code7Seg[(temp%10)]); //hien thi hang don vi cua bien temp OUTPUT_a(quet[digit]) ; digit++; break; } } } void main(void) { setup_timer_0(RTCC_DIV_2 | RTCC_INTERNAL | RTCC_8_BIT); // timer 0 interrupts enable_interrupts(INT_TIMER0); enable_interrupts(GLOBAL); while(true) { temp=178; //xuat so 178 ra led 7 doan } } |
Không có nhận xét nào:
Đăng nhận xét