BASIC4MCU | AVR | 외부인터럽트 | Atmega128 코드비젼 인터럽트 함수
페이지 정보
작성자 키트 작성일2017-08-29 09:15 조회3,912회 댓글0건본문
//=====================================================
// 코드비젼 컴파일러
//=====================================================// 코드 최적화 금지
#pragma opt-
//=====================================================#include <mega128.h>
#include <delay.h>
//=====================================================
#asm
.equ __lcd_port=0x1B ;포트A
#endasm#include <lcd.h>
//=====================================================lcd_gotoxy(0,0); lcd_putsf("basic4mcu.com"); // 첫째줄 표시
lcd_gotoxy(0,1); lcd_putsf("LCD test "); // 둘째줄 표시
.equ __lcd_port=0x1B ;PORTA
.equ __lcd_port=0x18 ;PORTB
.equ __lcd_port=0x15 ;PORTC
.equ __lcd_port=0x12 ;PORTD
.equ __lcd_port=0x03 ;PORTE
unsigned char lcd_init(16);
unsigned char lcd_read_byte(unsigned char addr);
void lcd_clear();
void lcd_gotoxy(unsigned char x, unsigned char y);
void lcd_putchar(char c);
void lcd_puts(char *str);
void lcd_putsf(char flash *str);
//=====================================================// 타이머0 인터럽트
interrupt [TIM0_OVF] void timer0_ovf_isr(void){ // 오버플로우 인터럽트
TCNT0=131;
}
interrupt [TIM0_COMP] void timer0_comp_isr(void){ } // 매치 인터럽트
//=====================================================// 타이머1 인터럽트
interrupt [TIM1_OVF] void timer1_ovf_isr(void){ // 오버플로우 인터럽트
TCNT1=49536;
}
interrupt [TIM1_COMPA] void timer1_compa_isr(void){ } // A매치 인터럽트
//=====================================================// 시리얼0 수신 인터럽트
void TX0_CH(char ch){ while(!(UCSR0A&0x20)); UDR0=ch; } // 송신함수
char RX0_CH(void) { while(!(UCSR0A&0x80)); return UDR0; } // 수신함수
interrupt [USART0_RXC] void usart0_rx_isr(void){ // 수신 인터럽트
unsigned char R0data;
R0data=UDR0;
}
//=====================================================// 시리얼1 수신 인터럽트
void TX1_CH(char ch){ while(!(UCSR1A&0x20)); UDR1=ch; } // 송신함수
char RX1_CH(void) { while(!(UCSR1A&0x80)); return UDR1; } // 수신함수
interrupt [USART1_RXC] void usart1_rx_isr(void){ // 수신 인터럽트
unsigned char R1data;
R1data=UDR1;
}
//=====================================================// ADC 인터럽트
unsigned int ADC_Value; //전역변수
//=====================================================interrupt [ADC_INT] void adc_isr(void){
ADC_Value=ADCW;
ADCSRA|=0x40; // ADC Start
}
//=====================================================// 외부인터럽트0
interrupt [EXT_INT0] void ext_int0_isr(void){}
//=====================================================// 외부인터럽트1
interrupt [EXT_INT1] void ext_int1_isr(void){}
//=====================================================
//=====================================================
// 메인함수
void main(void){
//=====================================================// UBRRnL 9600 19200 38400 57600 115200
// 11.0592MHz 71 35 17 11 5
// 14.7456MHz 95 47 23 15 7
// 16MHz 103 51 25 16 8
//=====================================================// 시리얼0 (128은 ISP와 공용으로 사용되므로 주의)
DDRE=2; // TXD0 PORTE.1
UCSR0B=0x98; UBRR0H=0; UBRR0L=103; //9600, 송신 함수, 수신인터럽트
UCSR0B=0x18; UBRR0H=0; UBRR0L=103; //9600, 송신, 수신 함수
// 시리얼1
DDRD=8; // TXD1 PORTD.3
UCSR1B=0x98; UBRR1H=0; UBRR1L=103; //9600, 송신 함수, 수신인터럽트
UCSR1B=0x18; UBRR1H=0; UBRR1L=103; //9600, 송신, 수신 함수
//=====================================================// TCCR0 FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00
// TCCR0=1:1/1, TCCR0=2:1/8, TCCR0=3:1/32, TCCR0=4:1/64, TCCR0=5:1/128, TCCR0=6:1/256, TCCR0=7:1/1024
//
// Compare Output Mode & Waveform Generation Mode
// TCCR1A COM1A1 COM1A0 COM1B1 COM1B0 COM1C1 COM1C0 WGM11 WGM10
//
// TCCR1B ICNC1 ICES1 ? WGM13 WGM12 CS12 CS11 CS10
// TCCR1B=1:1/1, TCCR1B=2:1/8, TCCR1B=3:1/64, TCCR1B=4:1/256, TCCR1B=5:1/1024
//
// TIMSK OCIE2 TOIE2 TICIE1 OCIE1A OCIE1B TOIE1 OCIE0 TOIE0// Compare Overflow InputCapture CompareA CompareB Overflow Compare Overflow
//=====================================================// 타이머0 오버플로우 인터럽트
TCCR0=4; TCNT0=6; TIMSK=1; //16000000/ 64/250=1000Hz=1ms, (256-250)=6
TCCR0=5; TCNT0=131; TIMSK=1; //16000000/ 128/125=1000Hz=1ms, (256-125)=131
TCCR0=5; TCNT0=6; TIMSK=1; //16000000/ 128/250= 500Hz=2ms, (256-250)=6
TCCR0=6; TCNT0=6; TIMSK=1; //16000000/ 256/250= 250Hz=4ms, (256-250)=6
TCCR0=7; TCNT0=6; TIMSK=1; //16000000/1024/250=62.5Hz=16ms, (256-250)=6// 타이머0 매치 인터럽트
TCCR0=0x0B; OCR0= 49; TIMSK=2; //16000000/ 32/(1+ 49)=10000Hz=100us
TCCR0=0x0C; OCR0=249; TIMSK=2; //16000000/ 64/(1+249)= 1000Hz=1ms
TCCR0=0x0D; OCR0=124; TIMSK=2; //16000000/ 128/(1+124)= 1000Hz=1ms
TCCR0=0x0D; OCR0=249; TIMSK=2; //16000000/ 128/(1+249)= 500Hz=2ms
TCCR0=0x0E; OCR0=249; TIMSK=2; //16000000/ 256/(1+249)= 250Hz=4ms
TCCR0=0x0F; OCR0=155; TIMSK=2; //16000000/1024/(1+155)= 100.16..Hz=9.984ms
TCCR0=0x0F; OCR0=249; TIMSK=2; //16000000/1024/(1+249)= 62.5Hz=16ms// 타이머1 오버플로우 인터럽트
TCCR1B=1; TCNT1=49536; TIMSK=4; //16000000/ 1/16000=1000Hz=1ms, (65536-16000)=49536
TCCR1B=1; TCNT1=33536; TIMSK=4; //16000000/ 1/32000= 500Hz=2ms, (65536-32000)=33536
TCCR1B=2; TCNT1=63536; TIMSK=4; //16000000/ 8/ 2000=1000Hz=1ms, (65536- 2000)=63536
TCCR1B=2; TCNT1=61536; TIMSK=4; //16000000/ 8/ 4000= 500Hz=2ms, (65536- 4000)=61536
TCCR1B=2; TCNT1=45536; TIMSK=4; //16000000/ 8/10000= 200Hz=5ms, (65536-10000)=45536
TCCR1B=2; TCNT1=35536; TIMSK=4; //16000000/ 8/20000= 100Hz=10ms, (65536-20000)=35536
TCCR1B=4; TCNT1=59286; TIMSK=4; //16000000/256/ 6250= 10Hz=100ms,(65536- 6250)=59286
TCCR1B=4; TCNT1= 3036; TIMSK=4; //16000000/256/62500= 1Hz=1sec, (65536-62500)= 3036// 타이머1 A매치 인터럽트
TCCR1B=0x09; OCR1A=1474; TIMSK=0x10; //14745600/ 1/(1+ 1474)=9997.01695Hz=100.02984us
TCCR1B=0x09; OCR1A=1599; TIMSK=0x10; //16000000/ 1/(1+ 1999)=10KHz=100us
TCCR1B=0x09; OCR1A=15999; TIMSK=0x10; //16000000/ 1/(1+15999)=1000Hz=1ms
TCCR1B=0x09; OCR1A=31999; TIMSK=0x10; //16000000/ 1/(1+31999)= 500Hz=2ms
TCCR1B=0x0A; OCR1A= 1999; TIMSK=0x10; //16000000/ 8/(1+ 1999)=1000Hz=1ms
TCCR1B=0x0A; OCR1A= 3999; TIMSK=0x10; //16000000/ 8/(1+ 3999)= 500Hz=2ms
TCCR1B=0x0A; OCR1A= 9999; TIMSK=0x10; //16000000/ 8/(1+ 9999)= 200Hz=5ms
TCCR1B=0x0A; OCR1A=19999; TIMSK=0x10; //16000000/ 8/(1+19999)= 100Hz=10ms
TCCR1B=0x0C; OCR1A= 6249; TIMSK=0x10; //16000000/256/(1+ 6249)= 10Hz=100ms
TCCR1B=0x0C; OCR1A=31249; TIMSK=0x10; //16000000/256/(1+31249)= 2Hz=500ms
TCCR1B=0x0C; OCR1A=62499; TIMSK=0x10; //16000000/256/(1+62499)=1Hz=1sec// 타이머2 매치 인터럽트
TCCR2=0x0B; OCR2=249; TIMSK=0x80; //16000000/ 64/(1+249)=1000Hz=1ms//=====================================================
// ADMUX REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0
// ADMUX 0x00: Vref사용, 내부AREF off, 0x40: AVCC사용, AREF의 캐피사터사용, 0xC0:Internal 2.56V사용
//
// ADCSRA ADEN ADSC ADFR ADIF ADIE ADPS2 ADPS1 ADPS0
// ADC Enable Start Conv Free Running Interrupt Flag Interrupt Enable
// Prescal=0:1/2, Prescal=1:1/2, Prescal=2:1/4, Prescal=3:1/8, Prescal=4:1/16, Prescal=5:1/32, Prescal=6:1/64, Prescal=7:1/128
//
// ADC
// ADCSRA=0xCF; // 인터럽트
ADCSRA=0xE7; // 프리런닝
//=====================================================// EIMSK INT7 INT6 INT5 INT4 INT3 INT2 INT1 IINT0
// EICRA ISC31 ISC30 ISC21 ISC20 ISC11 ISC10 ISC01 ISC00
// EICRB ISC71 ISC70 ISC61 ISC60 ISC51 ISC50 ISC41 ISC40
// ISC=0:low level, ISC=2:falling edge, ISC=3:rising edge
//
// 외부 인터럽트0,1
EICRA=0x0a; EICRB=0x0a; EIMSK=0x03; EIFR=0xFF; //falling edge
//=====================================================
#asm("sei")
//=====================================================while(1){}
}
댓글 0
조회수 3,912등록된 댓글이 없습니다.