BASIC4MCU | 2017년 128강좌 | [BK-AVR128] LED & FND 타이머2 인터럽트로 구동
페이지 정보
작성자 키트 작성일2017-09-12 15:07 조회2,100회 댓글0건본문
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 #include <mega128.h>#include <delay.h>//#define U08 unsigned char#define U16 unsigned int#define FU08 flash unsigned char//#define LCD_PORT PORTA#define LCD_C_WR PORTB=(PORTB&0x1F)|0x80#define LCD_D_WR PORTB=(PORTB&0x1F)|0xA0//#define LCD_EN_1 PORTB|=0x80#define LCD_EN_0 PORTB&=~0x80////#define LCD_RW_1 PORTB|=0x40//#define LCD_RW_0 PORTB&=~0x40////#define LCD_RS_1 PORTB|=0x20//#define LCD_RS_0 PORTB&=~0x20//U08 fnd[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF}; //애노드U08 led_buf=0,fnd_buf[8]={0,},digit=5;//void LCD_DATA(U08 d){ LCD_PORT=d; LCD_D_WR; delay_us(1); LCD_EN_0; delay_us(50); }void LCD_CMD (U08 c){ LCD_PORT=c; LCD_C_WR; delay_us(1); LCD_EN_0; delay_ms( 3); }void LCD_POS(U08 c,U08 r){ LCD_CMD(0x80|(c+r*0x40)); }void LCD_CLR(void){ LCD_CMD(1); }void LCD_STR (U08 *s){ while(*s)LCD_DATA(*s++); }void LCD_STRf(FU08 *s){ while(*s)LCD_DATA(*s++); }//void LCD_INIT(void){delay_ms(50);LCD_CMD(0x38); LCD_CMD(0x38); LCD_CMD(0x38);LCD_CMD(0x0E); LCD_CMD(0x06); LCD_CLR();}//interrupt [TIM2_COMP] void timer2_comp_isr(void){ //100usstatic U08 cnt2=0,fnd_cnt=0;switch(cnt2){case 0: PORTA=~led_buf; PORTF|=8; PORTF&=~8; break; // led outcase 1: // fnd outPORTA=0; PORTF|=4; PORTF&=~4; // common offPORTC=fnd[fnd_buf[fnd_cnt]]; // segment dataif(fnd_cnt==digit)PORTC&=~0x80; // digit outPORTF|=2;PORTF&=~2; // 573 segment outPORTA=1<<fnd_cnt; PORTF|=4; PORTF&=~4; // common outif(++fnd_cnt>7)fnd_cnt=0;break;case 2: break;case 3: break;case 4: break;case 5: break;case 6: break;case 7: break;case 8: break;case 9: break;}if(++cnt2>9)cnt2=0;}//void main(void){DDRA=0xFF; // FND_COMMON & LED & lcd dataDDRC=0xFF; // FND_SEGMENTDDRB=0xE0; // LCD controlDDRF=0x0E; // 573_selled_buf=0; // led offTCCR2=0x0B; OCR2= 24; TIMSK=0x80; //16000000/64/(1+ 24),100usLCD_INIT();SREG=0x80;while(1){led_buf=1; fnd_buf[0]=1; LCD_POS(0,0); LCD_STRf("1"); delay_ms(500);led_buf=0; fnd_buf[0]=0; LCD_POS(0,0); LCD_STRf("0"); delay_ms(500);}}타이머2 인터럽트 주기를 100us로 설정하고1ms 주기로 100us마다 10개로 나누어서 시분할 구동을 합니다.처음 0~100us는 LED다음 100~200us는 FND..나머지는 비어있습니다.LED만 보면 1ms마다 값을 출력하고FND만 보면 1ms마다 한 자리씩 차례로 출력합니다.비어있는 구간은 나중에 다른 것들을 구동해야겠습니다.^^LED 출력은 여기에 여러개의 예제가 있으니 돌려보세요동작시켜본 분은 답글로 인증샷 또는 동영상 남겨주세요^^
댓글 0
조회수 2,100등록된 댓글이 없습니다.