Chronometre en C sur Microcontroleur PIC

Contenu du snippet

#define    Start     PORTA.F0
#define    Arret     PORTA.F1
#define    Reset     PORTA.F2
#define    Write     PORTA.F3
#define    Read      PORTA.F6

sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;

char Message1[] = "Chronometre";
char Message2[] = ":";
char Message3[] = ".";
char  *Heure   = "00";
char  *Minute  = "00";
char  *Seconde = "00";
char  *Cent    = "00";
char  Buffer[10];
char H = 0;
char M = 0;
char S = 0;
int Centi = 0;
char counter0 = 0;
char flag0 = 0;
unsigned int counter = 0;
char flag1 = 0;
char N_Centi;
char N_Seconde;
char N_Minute;
char N_Heure;


void InitTimer1();
void Init_PIC();
void Affiche_Txt_LCD();
void Chronometre();
void Calcul_Chrono();
void Affichage_Chrono();
void Write_EEPROM();
void Read_EEPROM();


void main()
{

    Init_PIC();
    Affiche_Txt_LCD();
    InitTimer1();
    N_Centi = EEPROM_Read(0x00);
    N_Seconde = EEPROM_Read(0x01);
    N_Minute = EEPROM_Read(0x02);
    N_Heure = EEPROM_Read(0x03);

    while(1)
    {
        Chronometre();
        Write_EEPROM();
        Read_EEPROM();
    }
}



void Init_PIC()
{

    TRISA = 0xFF;
    TRISB = 0x00;

    PORTA = 0x00;
    PORTB = 0x00;

    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Cmd(_LCD_CURSOR_OFF);

}


void InitTimer1()
{
    T1CON         = 0x01;
    TMR1IF_bit         = 0;
    TMR1H         = 0xD8;
    TMR1L         = 0xF0;
    TMR1IE_bit         = 1;
    INTCON         = 0xC0;
}


void Affiche_Txt_LCD()
{

    Lcd_Out(1,3,Message1);
    Lcd_Out(2,4,heure);
    Lcd_Out(2,6,Message2);
    Lcd_Out(2,7,Minute);
    Lcd_Out(2,9,Message2);
    Lcd_Out(2,10,Seconde);
    Lcd_Out(2,12,Message3);
    Lcd_Out(2,13,Cent);

}


void Interrupt()
{

    if (TMR1IF_bit)
    {

        TMR1IF_bit = 0;
        TMR1H         = 0xD8;
        TMR1L         = 0xF0;
        counter0++;
        counter++;

        if (counter0 == 1)
        {
            flag0 = 1;
            counter0 = 0;
        }

        if ( counter == 100)
        {
            flag1 = 1;
            counter = 0;
        }
    }
}



void Chronometre()
{

    // Start
    if (Start)
    {
    delay_ms(300);
        do
        {

            if (flag0 == 1)
            {

                centi++;
                Cent[0] = (Centi / 10 )% 10 + 48 ;
                Cent[1] = Centi % 10 + 48 ;
                Lcd_out(2, 13, Cent);
                if (centi >= 99) centi = 0;
                flag0 = 0;

            }

            if (flag1 == 1)
            {
                S++;
                Seconde[0] = (S / 10 )% 10 + 48 ;
                Seconde[1] = S % 10 + 48 ;
                Lcd_out(2, 10, Seconde);

                if (S > 59)
                {
                    S = 0;
                    M++;
                    Minute[0] = (M / 10 )% 10 + 48 ;
                    Minute[1] = M % 10 + 48 ;
                    Lcd_out(2, 7, Minute);

                    if (M > 59 )
                    {
                        M = 0;
                        H++;
                        Heure[0] = (H / 10 )% 10 + 48 ;
                        Heure[1] = H % 10 + 48 ;
                        Lcd_out(2, 4, Heure);
                    }
                }
                flag1 = 0;
            }
        }
        while(!Reset && !Arret);


        if (Arret)
        {

            Calcul_Chrono();
            Affichage_Chrono();

        }



        // Reset
        if (Reset)
        {

            S = 0;
            M = 0;
            H = 0;
            Centi = 0;
            Calcul_Chrono();
            Affichage_Chrono();

        }

    }

}


void Calcul_Chrono()
{

    Cent[0] = (Centi / 10 )% 10 + 48 ;
    Cent[1] = Centi % 10 + 48 ;
    Seconde[0] = (S / 10 )% 10 + 48 ;
    Seconde[1] = S % 10 + 48 ;
    Minute[0] = (M / 10 )% 10 + 48 ;
    Minute[1] = M % 10 + 48 ;
    Heure[0] = (H / 10 )% 10 + 48 ;
    Heure[1] = H % 10 + 48 ;

}


void Affichage_Chrono()
{

    Lcd_out(2, 13, Cent);
    Lcd_out(2, 10, Seconde);
    Lcd_out(2, 7, Minute);
    Lcd_out(2, 4, Heure);

}

void Write_EEPROM()
{

    if (!Start && !Arret && Write)
    {

        EEPROM_Write(0x00, Centi);
        delay_ms(10);
        EEPROM_Write(0x01, S);
        delay_ms(10);
        EEPROM_Write(0x02, M);
        delay_ms(10);
        EEPROM_Write(0x03, H);
        delay_ms(10);

    }

}


void Read_EEPROM(){

       
        if (!Start && !Arret && !Arret && Read){
       
        delay_ms(300);

        Lcd_Cmd(_LCD_CLEAR);
        Lcd_Cmd(_LCD_CURSOR_OFF);
        Lcd_out(1, 1, "Meilleur score");
        Lcd_Out(2,6,Message2);
        Lcd_Out(2,9,Message2);
        Lcd_Out(2,12,Message3);

        N_Centi = EEPROM_Read(0x00);
        N_Seconde = EEPROM_Read(0x01);
        N_Minute = EEPROM_Read(0x02);
        N_Heure = EEPROM_Read(0x03);

        Cent[0] = (N_Centi / 10 )% 10 + 48 ;
        Cent[1] = N_Centi % 10 + 48 ;

        Seconde[0] = (N_Seconde / 10 )% 10 + 48 ;
        Seconde[1] = N_Seconde % 10 + 48 ;

        Minute[0] = ( N_Minute / 10 )% 10 + 48 ;
        Minute[1] =  N_Minute % 10 + 48 ;

        Heure[0] = (N_Heure / 10 )% 10 + 48 ;
        Heure[1] = N_Heure % 10 + 48 ;

        Affichage_Chrono();

        delay_ms(3000);
        }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.