.module Mavric-II.C .area data(ram, con, rel) 0000 _ulTimerTick:: 0000 .blkb 4 .area idata 0000 00000000 .word 0,0 .area data(ram, con, rel) 0004 _ucTimerFlag:: 0004 .blkb 1 .area idata 0004 01 .byte 1 .area data(ram, con, rel) .area text(rom, con, rel) .even 0000 _port_init:: 0000 ; /* 0000 ; ** File: MAVRIC-II.C 0000 ; ** 0000 ; ** Purpose: Demo Program in ICCAVR for BDMICO's MAVRIC-II board. 0000 ; ** http://www.bdmicro.com 0000 ; ** 0000 ; ** Chip: ATMEGA128 running at 16MHz 0000 ; ** 0000 ; ** Version: 1.0.0, 3:rd of December 2003 0000 ; ** 0000 ; ** Author: Lars Wictorsson 0000 ; ** LAWICEL / SWEDEN 0000 ; ** http://www.lawicel.com lars@lawicel.com 0000 ; ** 0000 ; ** Copyright: The copyright to the computer program(s) herein is the 0000 ; ** property of LAWICEL HB, Sweden. The program(s) may be used 0000 ; ** and/or copied only with the written permission of LAWICEL HB 0000 ; ** in accordance with the terms and conditions stipulated in 0000 ; ** the agreement/contract under which the program(s) have been 0000 ; ** supplied. 0000 ; ** 0000 ; ** Support: No Support is given on this free demo program. 0000 ; ** 0000 ; ** Remarks: This program is tested with ICCAVR version 6.29. 0000 ; ** 0000 ; ** History: 2003-12-03 1.0.0 Created (LWI) 0000 ; */ 0000 ; 0000 ; #include 0000 ; #include 0000 ; 0000 ; #define LED_ON PORTB |= 0x01 0000 ; #define LED_OFF PORTB &= ~0x01 0000 ; 0000 ; unsigned long ulTimerTick = 0; 0000 ; unsigned char ucTimerFlag = 1; 0000 ; 0000 ; // 0000 ; // All ports are set to inputs, except PB0 which is set to output and off. 0000 ; // 0000 ; void port_init(void) 0000 ; { 0000 ; PORTA = 0xFF; 0000 8FEF ldi R24,255 0002 8BBB out 0x1b,R24 0004 ; DDRA = 0x00; 0004 2224 clr R2 0006 2ABA out 0x1a,R2 0008 ; PORTB = 0xFE; 0008 8EEF ldi R24,254 000A 88BB out 0x18,R24 000C ; DDRB = 0x01; 000C 81E0 ldi R24,1 000E 87BB out 0x17,R24 0010 ; PORTC = 0xFF; //m103 output only 0010 8FEF ldi R24,255 0012 85BB out 0x15,R24 0014 ; DDRC = 0x00; 0014 24BA out 0x14,R2 0016 ; PORTD = 0xFF; 0016 82BB out 0x12,R24 0018 ; DDRD = 0x00; 0018 21BA out 0x11,R2 001A ; PORTE = 0xFF; 001A 83B9 out 0x3,R24 001C ; DDRE = 0x00; 001C 22B8 out 0x2,R2 001E ; PORTF = 0xFF; 001E 80936200 sts 98,R24 0022 ; DDRF = 0x00; 0022 20926100 sts 97,R2 0026 ; PORTG = 0x1F; 0026 8FE1 ldi R24,31 0028 80936500 sts 101,R24 002C ; DDRG = 0x00; 002C 20926400 sts 100,R2 0030 ; } 0030 L1: 0030 .dbline 0 ; func end 0030 0895 ret .even 0032 _timer1_init:: 0032 ; 0032 ; // TIMER1 initialisation - prescale:1 0032 ; // WGM: 0) Normal, TOP=0xFFFF 0032 ; // desired value: 1000Hz 0032 ; // actual value: 1000,000Hz (0,0%) 0032 ; void timer1_init(void) 0032 ; { 0032 ; TCCR1B = 0x00; //stop 0032 2224 clr R2 0034 2EBC out 0x2e,R2 0036 ; TCNT1H = 0xC1; //setup 0036 81EC ldi R24,193 0038 8DBD out 0x2d,R24 003A ; TCNT1L = 0x80; 003A 80E8 ldi R24,128 003C 8CBD out 0x2c,R24 003E ; OCR1AH = 0x3E; 003E 8EE3 ldi R24,62 0040 8BBD out 0x2b,R24 0042 ; OCR1AL = 0x80; 0042 80E8 ldi R24,128 0044 8ABD out 0x2a,R24 0046 ; OCR1BH = 0x3E; 0046 8EE3 ldi R24,62 0048 89BD out 0x29,R24 004A ; OCR1BL = 0x80; 004A 80E8 ldi R24,128 004C 88BD out 0x28,R24 004E ; OCR1CH = 0x3E; 004E 8EE3 ldi R24,62 0050 80937900 sts 121,R24 0054 ; OCR1CL = 0x80; 0054 80E8 ldi R24,128 0056 80937800 sts 120,R24 005A ; ICR1H = 0x3E; 005A 8EE3 ldi R24,62 005C 87BD out 0x27,R24 005E ; ICR1L = 0x80; 005E 80E8 ldi R24,128 0060 86BD out 0x26,R24 0062 ; TCCR1A = 0x00; 0062 2FBC out 0x2f,R2 0064 ; TCCR1B = 0x01; //start Timer 0064 81E0 ldi R24,1 0066 8EBD out 0x2e,R24 0068 ; } 0068 L2: 0068 .dbline 0 ; func end 0068 0895 ret .area vector(rom, abs) .org 56 0038 0C943500 jmp _timer1_ovf_isr .area text(rom, con, rel) .even 006A _timer1_ovf_isr:: 006A 0A92 st -y,R0 006C 1A92 st -y,R1 006E 2A92 st -y,R2 0070 3A92 st -y,R3 0072 4A92 st -y,R4 0074 5A92 st -y,R5 0076 8A93 st -y,R24 0078 EA93 st -y,R30 007A 0FB6 in R0,0x3f 007C 0A92 st -y,R0 007E 0E940000 xcall push_gset2 0082 ; 0082 ; #pragma interrupt_handler timer1_ovf_isr:15 0082 ; void timer1_ovf_isr(void) 0082 ; { 0082 ; //TIMER1 has overflowed 0082 ; TCNT1H = 0xC1; //reload counter high value 0082 81EC ldi R24,193 0084 8DBD out 0x2d,R24 0086 ; TCNT1L = 0x80; //reload counter low value 0086 80E8 ldi R24,128 0088 8CBD out 0x2c,R24 008A ; if (ulTimerTick > 0) { 008A 40E0 ldi R20,0 008C 50E0 ldi R21,0 008E 60E0 ldi R22,0 0090 70E0 ldi R23,0 0092 40900200 lds R4,_ulTimerTick+2 0096 50900300 lds R5,_ulTimerTick+2+1 009A 20900000 lds R2,_ulTimerTick 009E 30900100 lds R3,_ulTimerTick+1 00A2 2416 cp R2,R20 00A4 3506 cpc R3,R21 00A6 4606 cpc R4,R22 00A8 5706 cpc R5,R23 00AA 89F0 breq L4 00AC ; ulTimerTick--; 00AC 41E0 ldi R20,1 00AE 50E0 ldi R21,0 00B0 60E0 ldi R22,0 00B2 70E0 ldi R23,0 00B4 241A sub R2,R20 00B6 350A sbc R3,R21 00B8 460A sbc R4,R22 00BA 570A sbc R5,R23 00BC 30920100 sts _ulTimerTick+1,R3 00C0 20920000 sts _ulTimerTick,R2 00C4 50920300 sts _ulTimerTick+2+1,R5 00C8 40920200 sts _ulTimerTick+2,R4 00CC ; } 00CC 03C0 xjmp L5 00CE L4: 00CE ; else { 00CE ; ucTimerFlag = 1; 00CE 81E0 ldi R24,1 00D0 80930400 sts _ucTimerFlag,R24 00D4 ; } 00D4 L5: 00D4 ; } 00D4 L3: 00D4 0E940000 xcall pop_gset2 00D8 0990 ld R0,y+ 00DA 0FBE out 0x3f,R0 00DC E991 ld R30,y+ 00DE 8991 ld R24,y+ 00E0 5990 ld R5,y+ 00E2 4990 ld R4,y+ 00E4 3990 ld R3,y+ 00E6 2990 ld R2,y+ 00E8 1990 ld R1,y+ 00EA 0990 ld R0,y+ 00EC .dbline 0 ; func end 00EC 1895 reti 00EE ; ulTime -> y+0 .even 00EE _WaitMS:: 00EE 0E940000 xcall push_arg4 00F2 ; 00F2 ; // 00F2 ; // Routine waits n Milliseconds from Timer 1. 00F2 ; // 00F2 ; void WaitMS(unsigned long ulTime) 00F2 ; { 00F2 ; CLI(); 00F2 F894 cli 00F4 ; ulTimerTick = ulTime; 00F4 FE01 movw R30,R28 00F6 2080 ldd R2,z+0 00F8 3180 ldd R3,z+1 00FA 4280 ldd R4,z+2 00FC 5380 ldd R5,z+3 00FE 30920100 sts _ulTimerTick+1,R3 0102 20920000 sts _ulTimerTick,R2 0106 50920300 sts _ulTimerTick+2+1,R5 010A 40920200 sts _ulTimerTick+2,R4 010E ; ucTimerFlag = 0; 010E 2224 clr R2 0110 20920400 sts _ucTimerFlag,R2 0114 ; SEI(); 0114 7894 sei 0116 L7: 0116 L8: 0116 ; while(ucTimerFlag == 0); 0116 20900400 lds R2,_ucTimerFlag 011A 2220 tst R2 011C E1F3 breq L7 011E ; } 011E L6: 011E 2496 adiw R28,4 0120 .dbline 0 ; func end 0120 0895 ret .even 0122 _uart0_init:: 0122 ; 0122 ; // UART0 initialisation 0122 ; // desired baud rate: 9600 0122 ; // actual: baud rate:9615 (0,2%) 0122 ; // char size: 8 bit 0122 ; // parity: Disabled 0122 ; void uart0_init(void) 0122 ; { 0122 ; UCSR0B = 0x00; //disable while setting baud rate 0122 2224 clr R2 0124 2AB8 out 0xa,R2 0126 ; UCSR0A = 0x00; 0126 2BB8 out 0xb,R2 0128 ; UCSR0C = 0x06; 0128 86E0 ldi R24,6 012A 80939500 sts 149,R24 012E ; UBRR0L = 0x67; //set baud rate lo 012E 87E6 ldi R24,103 0130 89B9 out 0x9,R24 0132 ; UBRR0H = 0x00; //set baud rate hi 0132 20929000 sts 144,R2 0136 ; UCSR0B = 0x18; 0136 88E1 ldi R24,24 0138 8AB9 out 0xa,R24 013A ; } 013A L10: 013A .dbline 0 ; func end 013A 0895 ret .even 013C _init_devices:: 013C ; 013C ; //call this routine to initialise all peripherals 013C ; void init_devices(void) 013C ; { 013C ; //stop errant interrupts until set up 013C ; CLI(); //disable all interrupts 013C F894 cli 013E ; XDIV = 0x00; //xtal divider 013E 2224 clr R2 0140 2CBE out 0x3c,R2 0142 ; XMCRA = 0x00; //external memory 0142 20926D00 sts 109,R2 0146 ; port_init(); 0146 5CDF xcall _port_init 0148 ; timer1_init(); 0148 74DF xcall _timer1_init 014A ; uart0_init(); 014A EBDF xcall _uart0_init 014C ; 014C ; MCUCR = 0x00; 014C 2224 clr R2 014E 25BE out 0x35,R2 0150 ; EICRA = 0x00; //extended ext ints 0150 20926A00 sts 106,R2 0154 ; EICRB = 0x00; //extended ext ints 0154 2ABE out 0x3a,R2 0156 ; EIMSK = 0x00; 0156 29BE out 0x39,R2 0158 ; TIMSK = 0x04; //timer interrupt sources 0158 84E0 ldi R24,4 015A 87BF out 0x37,R24 015C ; ETIMSK = 0x00; //extended timer interrupt sources 015C 20927D00 sts 125,R2 0160 ; SEI(); //re-enable interrupts 0160 7894 sei 0162 ; //all peripherals are now initialised 0162 ; } 0162 L11: 0162 .dbline 0 ; func end 0162 0895 ret .even 0164 _main:: 0164 ; 0164 ; // 0164 ; void main(void) 0164 ; { 0164 ; init_devices(); 0164 EBDF xcall _init_devices 0166 ; //insert your functional code here... 0166 ; 0166 ; puts("Testing MAVRIC-II Board from BDMICRO www.bdmicro.com\r"); 0166 00E0 ldi R16,L13 016A 0E940000 xcall _puts 016E 0CC0 xjmp L15 0170 L14: 0170 C09A sbi 0x18,0 0172 0AEF ldi R16,250 0174 10E0 ldi R17,0 0176 20E0 ldi R18,0 0178 30E0 ldi R19,0 017A B9DF xcall _WaitMS 017C C098 cbi 0x18,0 017E 0EEE ldi R16,238 0180 12E0 ldi R17,2 0182 20E0 ldi R18,0 0184 30E0 ldi R19,0 0186 B3DF xcall _WaitMS 0188 L15: 0188 ; 0188 ; while(1) { // A forever loop 0188 F3CF xjmp L14 018A X0: 018A ; LED_ON; 018A ; WaitMS(250L); 018A ; LED_OFF; 018A ; WaitMS(750L); 018A ; } 018A ; } 018A L12: 018A .dbline 0 ; func end 018A 0895 ret .area data(ram, con, rel) 0005 L13: 0005 .blkb 54 .area idata 0005 54657374696E67204D41565249432D49 .byte 'T,'e,'s,'t,'i,'n,'g,32,'M,'A,'V,'R,'I,'C,45,'I 0015 4920426F6172642066726F6D2042444D .byte 'I,32,'B,'o,'a,'r,'d,32,'f,'r,'o,'m,32,'B,'D,'M 0025 4943524F207777772E62646D6963726F .byte 'I,'C,'R,'O,32,'w,'w,'w,46,'b,'d,'m,'i,'c,'r,'o 0035 2E636F6D0D00 .byte 46,'c,'o,'m,13,0 .area data(ram, con, rel)