/***************************************************** This program was produced by the CodeWizardAVR V1.24.2c Evaluation Automatic Program Generator © Copyright 1998-2004 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.ro e-mail:office@hpinfotech.ro Project : Frisbee Version : Date : 23.05.2004 Author : Sascha Pisar, Claus Bolzinger Company : Comments:Captures PW Values. First High Time, second LOW Time. Capture Starts after Pressing Button. Writes Captured Times to SRAM. After Sram is full, LED flash. After Pressing the Button, Results were transfered over RS 232 to Host. Chip type : ATmega163 Program type : Application Clock frequency : 1,69 MHz Memory model : Small External SRAM size : 0 Data Stack size : 256 *****************************************************/ #include // Standard Input/Output functions #include // Memory Access Funktions #include //Delay Funktions #include #include #define Full PINC.0 /************************************* // Declare your global variables here ***************************************/ bit Capture, Out, Start, Init; // Bit for Interrupt Event unsigned int result; // Result for Capture Value unsigned int *result_p; //Pointer for result unsigned int *Storage_p; //Pointer for Storage unsigned int Start_Adress; // Start Adress of Data Storrage char Run=0; char Values=250; char i=0; /*************************************** //Funktionsprototypen ***************************************/ //void OUT (unsigned int result) void OUT (unsigned int result) { if (Out) { printf("%u",result); //transfer the Result over RS232 to Host putchar(0x09); //Print TAB Out=0; } else { printf("%u",result); //transfer the Result over RS232 to Host //putchar(0x0A); //Print New Line putchar(0x0D); //Print Carry Return Out=1; } } unsigned int get_result (void) { *result_p=ICR1L; // *result_p=(((unsigned int)ICR1H*0xFF)+ICR1L); return (*result_p); } void Edge_Select (void) { if ((TCCR1B & 0x40)!=0) //Whitch Edge ist selected? { // putsf("Rising Edge: "); TCCR1B=0x02; // Set Capture on falling Edge //TCNT1=0; //Timer 1 Value Zero } else { //putsf("Falling Edge: "); TCCR1B=0x42; //Next Interrupt on rising Edge //TCNT1=0; //Timer 1 Value Zero } } //Declarations // Timer 1 input capture interrupt service routine interrupt [TIM1_CAPT] void timer1_capt_isr(void) { Capture=1; //Capture Interrupt Event Occured TCNT1=0; //*result_p=get_result(); // Read Capture Register } void main(void) { /************************************** // Declare your local variables here ****************************************/ Init=1; //Setbit INIT // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTB=0x00; DDRB=0xFF; // Port C initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTC=0xFF; DDRC=0xFF; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=P State5=P State4=P State3=T State2=T State1=T State0=T PORTD=0xFF; DDRD=0x00; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped TCCR0=0x00; TCNT0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer 1 Stopped // Mode: CTC normal // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off GIMSK=0x00; MCUCR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x20; // UART initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // UART Receiver: Off // UART Transmitter: On // UART Baud rate: 115200 UCSRA=0x00; UCSRB=0x08; UBRR=0x01; UBRRHI=0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off // Analog Comparator Output: Off ACSR=0x80; SFIOR=0x00; // Global enable interrupts //#asm("sei") //TCCR1B=0x05; //Start Timer1 with 1024/CK result_p=&result; /*ocates a memory block in the heap for an array of num elements, each element having the size length. On success the function returns a pointer to the start of the memory block, the block being filled with zeroes. If there wasn’t enough contiguous free memory in the heap to allocate, the function returns a null pointer.*/ Storage_p=malloc(Values); // Adress of the allocated Space to the Storage Pointer Start_Adress=Storage_p; //Remember the Start Adress if (Storage_p==0) printf("Stpeicher!"); PORTC.0=0; // Set LED Port C 0 "READY" while (1) { //printf("M"); if (Init &! PIND.2) //Push Start Button { // printf("R"); PORTC.0=1; //Turn Off LED Start=1; //Setbit Start // delay_ms(1000); TCCR1B=0x01; //Start Timer1 with CK Init=0; Out=1; #asm("sei") while (Run=Values) { // printf("g"); PORTC.1=0; } } if (!PORTC.1 &! PIND.3) { //printf("A"); Storage_p=Start_Adress; while (i