1、/采用 ICC-AVR 作为编译环境/ Target : M16/ Crystal: 7.3728Mhz#include #include #define uchar unsigned char#define uint unsigned intvoid port_init(void)PORTA = 0x50;DDRA = 0xF0;PORTB = 0x00;DDRB = 0x00;PORTC = 0x00; /m103 output onlyDDRC = 0x00;PORTD = 0x00;DDRD = 0x30;void init_devices(void)CLI(); /disable a
2、ll interruptsport_init();MCUCR = 0x00;GICR = 0x00;TIMSK = 0x00; /timer interrupt sourcesSEI(); /re-enable interrupts/void timer1_init(void)TCCR1B =0x00;TCNT1H = 0x00;TCNT1L = 0x00;OCR1AH = 0x00;OCR1AL = 0xFF;OCR1BH = 0x00;OCR1BL = 0xFF;ICR1H = 0x00;ICR1L = 0xFF;TCCR1A = 0xA1;TCCR1B = 0x09;void lefts
3、peed(uchar tempocra)OCR1AH = 0x00;OCR1AL = tempocra;void rightspeed(uchar tempocrb)OCR1BH = 0x00;OCR1BL = tempocrb;void delayms(uint MS) uint i,j;for( i=0;iMS;i+)for(j=0;j60;j+); void revisel(uchar discrepany) if (discrepany = 0)PORTA = 0x50;leftspeed(222);rightspeed(222);if (discrepany = 1)PORTA =
4、0x50;leftspeed(175);rightspeed(220);if (discrepany = 2) PORTA = 0x40;rightspeed(230);if (discrepany = 3)PORTA=0x50;rightspeed(225);leftspeed(170);if (discrepany = 4)PORTA = 0x50;leftspeed(220);rightspeed(175);if (discrepany = 5)PORTA = 0x50;leftspeed(225);rightspeed(170);if (discrepany = 6)PORTA =0x
5、10;leftspeed(230);if (discrepany = 7) PORTA =0xA0;rightspeed(195);leftspeed(195);delayms(3);if (discrepany = 8)PORTA =0x50;rightspeed(210);leftspeed(210);void inliner_1()uchar state1;state1 = 0x0Fswitch (state1)case 0x06: revisel(0);break;case 0x02: revisel(1);break;case 0x07: revisel(2);break;case 0x01: revisel(3);break;case 0x04: revisel(4);break;case 0x08: revisel(5);break;case 0x0E: revisel(6);break;case 0x00: revisel(7);break;case 0xFF: revisel(8);break;case 0x03: revisel(3);break;case 0x0C: revisel(5);break; void main()init_devices();timer1_init();while(1)inliner_1 ();