/***************************************************** This program was produced by the CodeWizardAVR V1.24.2c Professional Automatic Program Generator © Copyright 1998-2004 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.ro e-mail:office@hpinfotech.ro Project : Version : Date : 03/10/2011 Author : F4CG Company : F4CG Comments: Chip type : ATmega8535 Program type : Application Clock frequency : 16,000000 MHz Memory model : Small External SRAM size : 0 Data Stack size : 128 *****************************************************/ #include #include #include #include #include #define Vert PIND.6 #define Rouge PIND.7 #define Bleu PINB.5 #define CaptD PIND.4 #define CaptA PIND.5 #define ENABLE PORTD.7 // Alphanumeric LCD Module functions #asm .equ __lcd_port=0x15 ;PORTC #endasm #include // Declare your global variables here unsigned char car, CarEmis; int flagdec,drapeau,dizaine, unite, dizieme, centieme, secondeV, dixiemeV, centiemeV, milliemeV; char tampon[16],TamponV[16]; flash const unsigned char valeur_constant[]= { 0xEE,0x82,0xDC,0xD6,0xB2,0x76,0x7E,0xC2,0xFE,0xF6, 0xFA,0xFE,0x6C,0xEE,0x7C,0xF8,0x7E,0xBA,0x82,0x86, 0x3C,0x2C,0xEA,0x1A,0xEE,0xF8,0xF2,0xFA,0x76,0x3C, 0x0E,0xAE,0xDC}; flash const unsigned char adresse_constant[16]= {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15}; void Afficheur(unsigned char adresse, unsigned char caractere, unsigned char point) // Fonction d'affichage classique( temps, chrono, vitesse) { if ( point == 1 ) { PORTA = (valeur_constant[caractere] | 0x01); // Un point est rajouté après le caractère } else { PORTA=valeur_constant[caractere]; //Un caractere tout seul } PORTB=(0b00010000|adresse_constant[adresse & 0x0F]); PORTB.4=1; PORTB.4=0; PORTB.4=1; PORTA=0x00; } void Afficheur2(unsigned char adresse, unsigned char caractere, unsigned char point) //Fonction d'affichage du message F A U H { if ( point == 1 ) { PORTA = (caractere | 0x01); // Un point est rajouté après le caractère } else { PORTA=caractere; //Un caractere tout seul } PORTB=(0b00010000|adresse_constant[adresse & 0x0F]); PORTB.4=1; PORTB.4=0; PORTB.4=1; PORTA=0x00; } unsigned char USART_Receive( void ) // Fonction de rececption de caractère RS232 { /* Wait for data to be received */ while ( !(UCSRA & 0x80) ) // Test de RXC bit7 ; /* Get and return received data from buffer */ return UDR; } void USART_Transmit( unsigned char data ) // Fonction de transmission de caractère RS232 { /* Wait for empty transmit buffer */ while ( !( UCSRA & (0x20)) ) // Test de UDRE bit 5 ; /* Put data into buffer, sends the data */ UDR = data; } interrupt [TIM1_COMPA] void timer1_compa_isr(void) //Fonction d'interruption du Timer 1 { if(drapeau == 1) { if( flagdec == 0) { dizaine=0; unite=9; dizieme=9; centieme=9; flagdec=1; } if( unite == 0 && dizieme == 0 && centieme == 0) //Fonction de decompte au départ : 10s à 0 { } else if( dizieme == 0 && centieme == 0 && unite !=0) { dizieme=9; centieme=9; unite--; } else if( centieme == 0) { centieme=9; dizieme--; } else centieme--; } else { centieme++; if( centieme>9) { dizieme++; centieme = 0; if( dizieme>9) //Fonction réalisant le chronométrage du temps { unite++; dizieme=0; if(unite>9) { dizaine++; unite=0; if(dizaine == 6) { dizaine = 0; } } } } } Afficheur(0,dizaine,0); Afficheur(1,unite,1); Afficheur(2,dizieme,0); Afficheur(3,centieme,0); } interrupt [TIM0_COMP] void timer0_comp_isr(void) //Fonction d'interruption du Timer0 { milliemeV++; if( milliemeV>9) { centiemeV++; milliemeV = 0; if( centiemeV>9) { //Fonction permettant le calcul du temps effectué par le kart dixiemeV++; //pour en déduire la vitesse centiemeV=0; if(dixiemeV>9) { secondeV++; dixiemeV=0; if(secondeV>9) { secondeV= 0; } } } } } void Chronometre(int Flag ) { if( Flag == 1) { ENABLE=0; //Autorisation de l'affichage TCCR1B = 0x0A; //Autorisation du Timer } else if( Flag == 0) { TCCR1B = 0x08; //Arret du Timer ENABLE = 0; //Affichage du message: Afficheur2(0,0x78,1); //F Afficheur2(1,0xFB,1); //A Afficheur2(2,0xAE,1); //U Afficheur2(3,0xBA,1); //H } else if (Flag == 2) { TCCR1B = 0x08; ENABLE = 0; dizaine = 0; //Remise a zéro de l'affichage unite = 0; dizieme = 0; centieme = 0; Afficheur(0,dizaine,0); Afficheur(1,unite,1); Afficheur(2,dizieme,0); Afficheur(3,centieme,0); } else if(Flag == 4) { TCCR1B = 0x08; ENABLE = 0; dizaine = 1; unite = 0; //Mise a 10.00 de l'affichage avant procédure de décompte pour départ dizieme = 0; centieme = 0; Afficheur(0,dizaine,0); Afficheur(1,unite,1); Afficheur(2,dizieme,0); Afficheur(3,centieme,0); } } void main(void) { // Input/Output Ports initialization // Port A 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 PORTA=0x00; DDRA=0xFF; // Port B initialization // Func7=In Func6=In Func5=In Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=T State6=T State5=T State4=0 State3=0 State2=0 State1=0 State0=0 PORTB=0x00; DDRB=0x1F; // Port C 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 PORTC=0x00; DDRC=0x00; // Port D initialization // Func7=Out Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=In // State7=1 State6=T State5=T State4=T State3=T State2=T State1=0 State0=T PORTD=0x80; DDRD=0xC2; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 250,000 kHz // Mode: Normal top=FFh // OC0 output: Toggle on compare match TCCR0=0x08; TCNT0=0x00; OCR0=0xFA; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 2000,000 kHz // Mode: CTC top=OCR1A // OC1A output: Toggle // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge TCCR1A=0x40; TCCR1B=0x0A; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x4E; OCR1AL=0x20; 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ù // INT2: Off MCUCR=0x00; MCUCSR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x12; // USART initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: On // USART Transmitter: On // USART Mode: Asynchronous // USART Baud rate: 9600 UCSRA=0x00; UCSRB=0x18; UCSRC=0x86; UBRRH=0x00; UBRRL=0x67; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off // Analog Comparator Output: Off ACSR=0x80; SFIOR=0x00; // LCD module initialization lcd_init(16); #asm("sei") while (1) { int flag,DecimalV,EntiereV,CentaineV,UniteV,DizaineV,i,flag2; float vitesse, Temps; while( CaptD == 1) { secondeV=0; dixiemeV=0; centiemeV=0; milliemeV=0; if( flag2 != 1) { car=USART_Receive(); //Appel de la fonction de reception de caractère } if (car == 'R') //Remise a zéro du chronomètre { flag = 2; drapeau=0; flagdec=0; } //Départ du kart sur l'épreuve if (car == 'M') { flag = 1; flag2=1; } if (car == 'A') //Faux départ { flag = 0; flagdec=0; } if(car == 'D') //Kart se présentant au départ { flag=4; CarEmis = 'C'; USART_Transmit(CarEmis); } if(car == 'P') //Procédure d'attente des 10s au départ { drapeau=1; flag=1; } Chronometre(flag); CarEmis = 'C'; USART_Transmit(CarEmis); lcd_gotoxy(0,0); sprintf(TamponV," %c %c", CarEmis, car); lcd_puts(TamponV); } TCCR1B = 0x08; ENABLE=0; if( CaptA == 1) { TCCR0=0x0B; } else if( CaptA == 0) { TCCR0=0x08; sprintf(tampon,"%d.%d%d%d", secondeV, dixiemeV, centiemeV, milliemeV); Temps = atof(tampon); vitesse=1.44/ Temps; EntiereV=(int)vitesse; DecimalV=(int)vitesse%10; //Calcul de la vitesse du kart lcd_gotoxy(0,2); lcd_puts(tampon); UniteV = EntiereV - ((EntiereV / 10) * 10); DizaineV = EntiereV / 10 - ((EntiereV / 100) * 10); CentaineV = EntiereV/100; for(i=0;i<5;i++) //Affichage de l'alternance Temps-Vitesse 5 fois { //avec une alternance toutes les 2s Afficheur(0,CentaineV,0); Afficheur(1,DizaineV,0); Afficheur(2,UniteV,1); Afficheur(3,DecimalV,0); CarEmis = 'C'; USART_Transmit(CarEmis); delay_ms(2000); Afficheur(0,dizaine,0); Afficheur(1,unite,1); Afficheur(2,dizieme,0); Afficheur(3,centieme,0); delay_ms(2000); CarEmis = 'C'; USART_Transmit(CarEmis); } CarEmis = 'F'; USART_Transmit(CarEmis); flag2=0; lcd_gotoxy(0,0); sprintf(TamponV," %c %c", CarEmis, car); lcd_puts(TamponV); } } }