Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************
- Projet : nv_simu
- Fichier(s) : nv_simu.c
- Objet :
- Cible : Atmega8
- frequ : 8MHz
- Date création : 18/05/2010
- **********************************************************/
- // Fichiers à inclure :
- #include <stdlib.h>
- #include <stdio.h>
- #include <avr/io.h>
- #include <avr/pgmspace.h>
- #include <avr/interrupt.h>
- // Equivalences :
- #define BIT0 0b00000001
- #define BIT1 0b00000010
- #define BIT2 0b00000100
- #define BIT3 0b00001000
- #define BIT4 0b00010000
- #define BIT5 0b00100000
- #define BIT6 0b01000000
- #define BIT7 0b10000000
- #define nv_lon_1h 24
- #define nv_lon_1l 21
- #define nv_lon_2h 24
- #define nv_ron_1h 23
- #define nv_ron_1l 78
- #define nv_ron_2h 40
- #define nv_loff_1h 43
- #define nv_roff_1h 24
- #define nv_roff_1l 46
- #define nv_roff_2h 31
- #define nv_off_1h 68
- #define nv_off_1l 450
- #define nv_off_2h 68
- #define nv_darktime 500
- #define nv_nbrpuls_l 4
- #define nv_nbrpuls_r 3
- #define nv_nbrpuls_off 2
- //function prototypes :
- void out_left(void);
- void out_right(void);
- void out_off(void);
- // constantes :
- // definition des entrées/sorties :
- #define DDC_in ((PIND & BIT2) >> 2) //ddc in sur PORTD.2 (INT0)
- #define off_in ((PIND & BIT3) >> 3) //off_in sur PORTD.3
- //variables globales :
- volatile unsigned char state_ddc_in;
- volatile unsigned int t_ddc_rise;
- volatile unsigned int t_ddc_fall;
- volatile unsigned int per_high;
- volatile unsigned int delay;
- volatile unsigned int t[7];
- volatile unsigned char no_pulse=0;
- volatile unsigned char nbr_pulses=0;
- // ============================== ZONE PROGRAMME ==============================
- int main(void)
- {
- //Config portB
- DDRB=0b00000001; //PB0 en sortie
- PORTB=0x01;
- //config timer1
- TCCR1A=0b00000000; //Mode normal
- TCCR1B=0b00000010; //clk timer1=clk/8 Fmin = 62Hz
- //Configuration interruption externes
- MCUCR=0b00000001; //INT0 actif sur changement d'état
- //Autorisation interruptions
- GICR=0b01000000; //Autorisation INT0
- TIMSK=0b00011000; //Autorisation interruption T2OVF, T1compare1A,T1compare1B, T0_OVF
- sei (); //Valide toutes les interruptions
- //initialisation des variables
- //message serie
- while (1)
- {
- delay = per_high - 1000; //Avance de 1ms
- }
- }
- void out_left(void)
- {
- //right off
- t[0]= nv_roff_1h-10; //pulse1 etat haut
- t[1]= nv_roff_1l-10; //pulse1 etat bas
- t[2]= nv_roff_2h-10; //pulse2 etat haut
- //dark time
- t[3]= nv_darktime-10;
- //lefton
- t[4]= nv_lon_1h-10; //pulse1 etat haut
- t[5]= nv_lon_1l-10; //pulse1 etat bas
- t[6]= nv_lon_2h-10; //pulse2 etat haut
- nbr_pulses= (nv_nbrpuls_l*2)-1;
- PORTB &= ~BIT0; //IR_out à 1
- OCR1B=TCNT1+t[0]; //charger compare1B
- }
- void out_right(void)
- {
- //left off
- t[0]= nv_loff_1h-10; //pulse1 etat haut
- //dark_time
- t[1]= nv_darktime-10;
- //right on
- t[2]= nv_ron_1h-10; //pulse1 etat haut
- t[3]= nv_ron_1l-10; //pulse1 etat bas
- t[4]= nv_ron_2h-10; //pulse2 etat haut
- nbr_pulses= (nv_nbrpuls_r*2)-1;
- PORTB &= ~BIT0; //IR_out à 1
- OCR1B=TCNT1+t[0]; //charger compare1B
- }
- void out_off(void)
- {
- t[0]= nv_off_1h-10; //pulse1 etat haut
- t[1]= nv_off_1l-10;
- t[2]= nv_off_2h-10; //pulse1 etat haut
- nbr_pulses= (nv_nbrpuls_off*2)-1;
- PORTB &= ~BIT0; //IR_out à 1
- OCR1B=TCNT1+t[0]; //charger compare1B
- }
- // Interruption INT0
- SIGNAL(SIG_INTERRUPT0)
- {
- if(DDC_in) //si front montant de DDC
- {
- t_ddc_rise = TCNT1;
- OCR1A = t_ddc_rise + delay;
- state_ddc_in = 1;
- }
- else //front descendant de DDC
- {
- t_ddc_fall = TCNT1;
- if (t_ddc_rise > t_ddc_fall)
- per_high = (0xFFFF - t_ddc_rise) + t_ddc_fall;
- else
- per_high = t_ddc_fall - t_ddc_rise;
- OCR1A = t_ddc_fall + delay;
- state_ddc_in = 0; //delai sur front descendant
- }
- }
- //compare Timer1A
- SIGNAL(SIG_OUTPUT_COMPARE1A)
- {
- if (delay != 0)
- {
- if(off_in == 0)
- out_off();
- else if(state_ddc_in)
- out_right();
- else
- out_left();
- }
- }
- //compare Timer1B
- SIGNAL(SIG_OUTPUT_COMPARE1B)
- {
- if(no_pulse < nbr_pulses)
- {
- switch(no_pulse % 2)
- {
- case 0: //fin de pulse
- PORTB |= BIT0; //mettre PORTB.1 à 0
- no_pulse++;
- OCR1B=TCNT1+t[no_pulse]; //charger compare1B
- break;
- case 1: //debut de pulse
- PORTB &= ~BIT0; //mettre PORTB.1 à 1
- no_pulse++;
- OCR1B=TCNT1+t[no_pulse]; //charger compare1B
- break;
- default:
- break;
- }
- }
- if( no_pulse == nbr_pulses) //émission terminée
- {
- nbr_pulses=0;
- no_pulse=0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement