| QUOTE |
| Here is some code I wrote to just kind of learn my way around the mega169. I am using the ISP of my STK500 to download code to the Butterfly. I have not tried the Bootloader included with the Butterfly so I have no idea how to download code using the Bootloader. What I did was Read the existing Butterfly hex code into a file (utilizing Studio 4.09 and the STK500) so that I could restore the original code that comes with the Butterfly if I needed to. Now I just use the ISP on the STK500 to download code to the Butterfly. I had to solder a 6 pin header on the Butterfly to do this. Anyway, don't know if this will help you any. Feel free to ask me questions about this code if you need to. Note: PE4 and PE5 mentioned in the code below are found on the USI header of the Butterfly. PE4 = USI header, pin 1, PE5 = USI header, pin 2 Regards, Steve |
| CODE |
;Date 06/30/2004 ;Name Stephen L. Nuttall ;Hardware platform AVR Butterfly/STK500 ;Software platform Assembler/Studio 4 ;Processor mega169 ;Clock default internal RC oscillator/32KHz crystal ; ; The purpose of this code is to sense the level of Port E, bit 4 (PE4) and ; transfer its state to Port E, bit 5 (PE5). ; ; I will use Timer/Counter2 (T/C2) to toggle PE4. T/C2 will be setup to ; utilize the Butterfly 32.768KHz crystal to obtain a frequency of ; 1024Hz. T/C2 will trigger the Output Compare interrupt but will not toggle ; the OC2 pin. Instead PE4 will be manually toggled by the T/C2 Output ; Compare ISR. ; PE4 should therefore be changing states at 1024Hz. I will use the Pin ; change ISR (using PE4 to trigger the PC_INT0 ISR) to toggle PE5. (The ; m169 datasheet says you can trigger a pin change interrupt even if the ; corresponding pin is configured as an output.) ; ; Note: This version of 1khzdetection (1khzdetect_v2) will utilize the "new" ; instruction available with the m169 (and other newer AVR's) that allows ; individual output pins to be "toggled" by using the following instruction: ; sbi PINx,y (where x = port number and y = bit number). This is quite ; cool since with older AVR's it took several instructions to toggle an ; output pin. ; .nolist .include "m169def.inc" .list ; ; .cseg .org 0 ; jmp RESET ; Reset Handler jmp EXT_INT0 ; IRQ0 Handler jmp PC_INT0 ; PCINT0 Handler jmp PC_INT1 ; PCINT0 Handler jmp TIM2_COMP ; Timer2 Compare Handler jmp TIM2_OVF ; Timer2 Overflow Handler jmp TIM1_CAPT ; Timer1 Capture Handler jmp TIM1_COMPA ; Timer1 CompareA Handler jmp TIM1_COMPB ; Timer1 CompareB Handler jmp TIM1_OVF ; Timer1 Overflow Handler jmp TIM0_COMP ; Timer0 Compare Handler jmp TIM0_OVF ; Timer0 Overflow Handler jmp SPI_STC ; SPI Transfer Complete Handler jmp USART_RXC ; USART RX Complete Handler jmp USART_DRE ; USART,UDR Empty Handler jmp USART_TXC ; USART TX Complete Handler jmp USI_STRT ; USI Start Condition Handler jmp USI_OVFL ; USI Overflow Handler jmp ANA_COMP ; Analog Comparator Handler jmp ADC_CC ; ADC Conversion Complete Handler jmp EE_RDY ; EEPROM Ready Handler jmp SPM_RDY ; SPM Ready Handler jmp LCD_SOF ; LCD Start of Frame Handler ; ; Program starts here, set Stack Pointer to top of internal SRAM RESET: ldi r16,high(RAMEND) out SPH,r16 ; ldi r16,low(RAMEND) out SPL,r16 ; ; ; Port setup ldi r16,0x30 ; PE5 & 4 = output, all others = input out ddre,r16 ; ; ; Setup Pin Change Interrupts for PCINT4 (on PE4) ; ldi r16,0x10 sts PCMSK0,r16 ; enable PCINT4 ; ldi r16,0x40 out EIMSK,r16 ; enable PCIE0 ; ; Setup Timer/Counter2 ; Note: STS is used instead of OUT because Timer/Counter 2 registers are ; out of address range of OUT ; ldi r16,0x08 ; select input clock =TOSC (32.768KHz) sts ASSR,r16 ; ldi r16,0x0F ; load OCR2A w/ decimal 15 sts OCR2A,r16 ; measured error was 37Hz with OCR2A = decimal 16 ; measured error was 24Hz with OCR2A = decimal 15, the choice is obvious ; ldi r16,0x02 ; enable Compare match interrupt sts TIMSK2,r16 ldi r16,0x09 ; setup TCCR2A for: CTC mode, disconnect ; OC2A from PB7 and clock w/ no prescaling sts TCCR2A,r16 ; sei ; globally enable interrupts ; ; loop here forever Loop: rjmp Loop ; ;------------------------------------------------------------------------ ;************************************************************************ ; TIM2_COMP: ; ; no flags or registers are affected so, for the time being, I am not saving ; anything ; sbi pine,4 ;toggle porte, bit 4 reti ; ; ; PC_INT0: sbi pine,5 ;toggle porte, bit 5 reti EXT_INT0: reti ; PC_INT1: reti ; TIM2_OVF: reti ; TIM1_CAPT: reti ; TIM1_COMPA: reti ; TIM1_COMPB: reti ; TIM1_OVF: reti ; TIM0_COMP: reti ; TIM0_OVF: reti ; SPI_STC: reti ; USART_RXC: reti ; USART_DRE: reti ; USART_TXC: reti ; USI_STRT: reti ; USI_OVFL: reti ; ANA_COMP: reti ; ADC_CC: reti ; EE_RDY: reti ; SPM_RDY: reti ; LCD_SOF: reti |