Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Архив1 / doc200 / програма_мікроконтролера

.doc
Скачиваний:
17
Добавлен:
01.08.2013
Размер:
25.6 Кб
Скачать

// Target : M8 // Crystal: 4.0000Mhz #include <iom8v.h> #include <macros.h> #define S1 PС0 #define S2 PС1 void DelayMcs (unsigned int t_mcs) { while(t_mcs--) { asm("nop"); asm("nop"); asm("nop"); asm("nop"); }; } void port_init(void) { PORTB = 0x00; DDRB = 0x06; PORTC = 0x03; //m103 output only DDRC = 0x00; PORTD = 0x00; DDRD = 0x00; } //TIMER1 initialize - prescale:1 // WGM: 12) CTC, TOP=ICRn // desired value: 40KHz // actual value: 40,000KHz (0,0%) void timer1_init(void) { TCCR1B = 0x00; //stop TCNT1H = 0xFF; //setup TCNT1L = 0x9D; OCR1AH = 0x00; OCR1AL = 0x63; OCR1BH = 0x00; OCR1BL = 0x63; ICR1H = 0x00; ICR1L = 0x63; TCCR1A = 0x50; TCCR1B = 0x19; //start Timer } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); timer1_init(); MCUCR = 0x00; GICR = 0x00; TIMSK = 0x00; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } void main(void) { init_devices(); while(1) { if (!(PINC & (1<<PC0))) { DelayMcs(4000); if (!(PINC & (1<<PC0))) OCR1A+=2; if (OCR1A > 0x04E1) OCR1A = 0x04E1; } if (!(PINC & (1<<PC1))) { DelayMcs(4000); if (!(PINC & (1<<PC1))) OCR1A-=2; if (OCR1A <= 0x02) OCR1A = 0x02; } } }