Jump to content
ELFORUM - Forumul Electronistilor
ciocanaru

Exercitii programare de la zero

Recommended Posts

Mda , am uitat teorema impartirii cu rest . Aritmetica de clasa a doua ? :jytuiyu

 

Multumesc pentru ajutor . Am inteles acum despre ce era vorba .

Share this post


Link to post
Share on other sites

Salutare,  daca  mai citeste cineva topicul, am si eu o intrebare. Am facut urmatorul joc de lumini, am mai modificat eu efectele si nu functioneaza cum trebuie programul, nu reia jocul numarul 1 se blocheaza la un efect si il ruleaza doar pe ala. Cum fac sa ruleze doar jocul numarul 1?

 

#define BUTON   RA0_bit
#define INPUT   1
#define OUTPUT  0

#define APASAT  0

#define NR_JOCURI    4

unsigned int  i = 0 ;

//Timer0
//Prescaler 1:4; TMR0 Preload = 6; Actual Interrupt Time : 1 ms
void InitTimer0(){
  OPTION_REG = 0x81;
  TMR0 = 6;
  INTCON = 0xA0;
}

void init_sys(){
  CMCON = 0x07;        // To turn off comparators
  VREN_bit = 0;        // Disable voltage reference

  TRISB = 0b00000000;  // portul B este numai IESIRI
  PORTB = 0b00000000;  // se porneste cu toti pinii port B in stare LOW

  TRISA0_bit = INPUT;  // RA0 este intrare digitala - asigura-te ca ai un rezistor de pull-up connectat de pe pin catre VCC
}

void Interrupt(){
  static unsigned char counter = 0;
  static unsigned char ultima_apasare_reala = 0;

  if (TMR0IF_bit){
    TMR0IF_bit = 0;
    TMR0 = 6;

    // daca chiar si numai odata butonul nu este apasat atunci
    // ori avem bouncing ori butonul nu mai este apasat asadar in 'else' facem variabila counter = 0
   if (BUTON == APASAT){
      counter++;
      /* Daca var. counter ajunge la 10 atunci au trecut 10ms cu butonul apasat,
        deci suntem siguri ca avem o apasare reala
      */
      if (counter >= 10){
        counter = 0;
        if (ultima_apasare_reala == 0){
          // i este un contor care numara circular la fiecare apasare reala pana la NR_JOCURI
          // daca NR_JOCURI este 4 ca in cazul curent atunci variabila i va lua valorile: 0,1,2,,3,0,1,2,3 samd
          if (i >= NR_JOCURI-1) i = 0;
          else i++;
          ultima_apasare_reala = 1;
        }
      }
    }
    else {
      counter = 0;
      ultima_apasare_reala = 0;
    }
  }
}

void joc_lumini_1(){
  /* Aici vom avea definit jocul de lumini nr 0
  */
    int j;

      for (j = 0; j < 8; j++){
    PORTB = (1 << j);
    delay_ms(50);
  }
  for (j = 7; j >= 0; j--){
    PORTB = (1 << j);
    delay_ms(50);
   }
  PORTB = 0xCC;
  delay_ms(300);
  PORTB = 0x33;
  delay_ms(300);
  PORTB = 0x55;
  delay_ms(300);
  PORTB = 0xAA;
  delay_ms(300);
  PORTB = 0x55;
  delay_ms(300);
  PORTB = 0xAA;
  delay_ms(300);
  PORTB = 0xCC;
  delay_ms(300);
  PORTB = 0x33;
  delay_ms(300);
  for (j = 0; j < 8; j++){
    PORTB = (1 << j);
    delay_ms(50);
  }
  for (j = 7; j >= 0; j--){
    PORTB = (1 << j);
    delay_ms(50);
   }


  PORTB = 0xF0;
  delay_ms(500);
  PORTB = 0x0F;
  delay_ms(500);
  PORTB = 0x99;
  delay_ms(500);
  PORTB = 0x66;
  delay_ms(500);
  PORTB = 0x99;
  delay_ms(500);
  PORTB = 0x66;
  delay_ms(500);

  PORTB = 0x80;
  delay_ms(100);
  PORTB = 0xC0;
  delay_ms(100);
  PORTB = 0xE0;
  delay_ms(100);
  PORTB = 0xF0;
  delay_ms(100);
  PORTB = 0xF8;
  delay_ms(100);
  PORTB = 0xFC;
  delay_ms(100);
  PORTB = 0xFE;
  delay_ms(100);
  PORTB = 0xFF;
  delay_ms(100);
                 for (j = 0; j < 8; j++){
    PORTB = (1 << j);
    delay_ms(50);
  }
  for (j = 7; j >= 0; j--){
    PORTB = (1 << j);
    delay_ms(50);
   }
}

void joc_lumini_2(){
  /* Aici vom avea definit jocul de lumini nr 1
  */

PORTB = 0x80;
  delay_ms(100);
  PORTB = 0xC0;
  delay_ms(100);
  PORTB = 0xE0;
  delay_ms(100);
  PORTB = 0xF0;
  delay_ms(100);
  PORTB = 0xF8;
  delay_ms(100);
  PORTB = 0xFC;
  delay_ms(100);
  PORTB = 0xFE;
  delay_ms(100);
  PORTB = 0xFF;
  delay_ms(100);

}

void joc_lumini_3(){
  /* Aici vom avea definit jocul de lumini nr 2
  */
  int j;

  for (j = 0; j < 8; j++){
    PORTB = (1 << j);
    delay_ms(50);
  }
  for (j = 7; j >= 0; j--){
    PORTB = (1 << j);
    delay_ms(50);
   }
    for (j = 0; j < 8; j++){
    PORTB = (1 << j);
    delay_ms(1000);
  }
  for (j = 7; j >= 0; j--){
    PORTB = (1 << j);
    delay_ms(1000);
  }
}
void void joc_lumini_4() {
  /* Aici vom avea definit jocul de lumini nr 3
  */

     PORTB = 0x80;
  delay_ms(100);
  PORTB = 0xC0;
  delay_ms(100);
  PORTB = 0xE0;
  delay_ms(100);
  PORTB = 0xF0;
  delay_ms(100);
  PORTB = 0xF8;
  delay_ms(100);
  PORTB = 0xFC;
  delay_ms(100);
  PORTB = 0xFE;
  delay_ms(100);
  PORTB = 0xFF;
  delay_ms(100);

}

void main(){
  init_sys();
  InitTimer0();

  // bucla infinita
  while (1) {

    switch (i){
      case 0:
        joc_lumini_1();
        break;
      case 1:
        joc_lumini_2();
        break;
      case 2:
        joc_lumini_3();
        break;
      case 3:
        joc_lumini_4();
        break;
    }

  }
}

Edited by The_Reaper

Share this post


Link to post
Share on other sites

joc1 are 8.6 secunde, timp in care nu poate executa alt joc, pana nu termina.

daca ai rabdare, nu ar trebui sa ramana blocat pe joc 1.

Share this post


Link to post
Share on other sites

Eu nu m-am prins unde se modifica i-ul ala din switch(i) (in main()).

Da' la cum e formatat codul, nici nu ma chinui.

Share this post


Link to post
Share on other sites

in intrerupere se modifica i-ul

dar ar trebui facut o versiune fara delay-uri, eventual cu delay pe timer, sa fie responsive.

eventual cu state machines sa fie mai usor de citit, si cu array de parametri (port value, delay value) care se pot procesa toate intr-o iteratie.

Share this post


Link to post
Share on other sites

poti incerca sa vezi ce face codul asta:

#include <pic.h>
#include <pic16f628.h>

#define BUTON RA0
#define INPUT 1
#define OUTPUT 0

#define APASAT 0

#define NR_JOCURI 4

#define SIZE( x ) ( sizeof( x ) / sizeof( x[ 0 ] ) )

static volatile unsigned int i;
static volatile unsigned int s_pos;
static volatile unsigned int counterMs;

struct Step
{
    unsigned char data;
    unsigned short time;
};

const struct Step c_joc1[] =
{
    { 0x01, 50 }, { 0x02, 50 }, { 0x04, 50 }, { 0x08, 50 }, { 0x10, 50 }, { 0x20, 50 }, { 0x40, 50 }, { 0x80, 50 },
    { 0x80, 50 }, { 0x40, 50 }, { 0x20, 50 }, { 0x10, 50 }, { 0x08, 50 }, { 0x04, 50 }, { 0x02, 50 }, { 0x01, 50 },
    { 0xCC, 300 }, { 0x33, 300 }, { 0x55, 300 }, { 0xAA, 300 }, { 0x55, 300 }, { 0xAA, 300 }, { 0xCC, 300 }, { 0x33, 300 },
    { 0x01, 50 }, { 0x02, 50 }, { 0x04, 50 }, { 0x08, 50 }, { 0x10, 50 }, { 0x20, 50 }, { 0x40, 50 }, { 0x80, 50 },
    { 0x80, 50 }, { 0x40, 50 }, { 0x20, 50 }, { 0x10, 50 }, { 0x08, 50 }, { 0x04, 50 }, { 0x02, 50 }, { 0x01, 50 },
    { 0xF0, 500 }, { 0x0F, 500 }, { 0x99, 500 }, { 0x66, 500 }, { 0x99, 500 }, { 0x66, 500 },
    { 0x80, 100 }, { 0xC0, 100 }, { 0xE0, 100 }, { 0xF0, 100 }, { 0xF8, 100 }, { 0xFC, 100 }, { 0xFE, 100 }, { 0xFF, 100 }, 
    { 0x01, 50 }, { 0x02, 50 }, { 0x04, 50 }, { 0x08, 50 }, { 0x10, 50 }, { 0x20, 50 }, { 0x40, 50 }, { 0x80, 50 },
    { 0x80, 50 }, { 0x40, 50 }, { 0x20, 50 }, { 0x10, 50 }, { 0x08, 50 }, { 0x04, 50 }, { 0x02, 50 }, { 0x01, 50 },
};

const struct Step c_joc2[] =
{
    { 0x80, 100 }, { 0xC0, 100 }, { 0xE0, 100 }, { 0xF0, 100 }, { 0xF8, 100 }, { 0xFC, 100 }, { 0xFE, 100 }, { 0xFF, 100 }, 
};

const struct Step c_joc3[] =
{
    { 0x01, 50 }, { 0x02, 50 }, { 0x04, 50 }, { 0x08, 50 }, { 0x10, 50 }, { 0x20, 50 }, { 0x40, 50 }, { 0x80, 50 },
    { 0x80, 50 }, { 0x40, 50 }, { 0x20, 50 }, { 0x10, 50 }, { 0x08, 50 }, { 0x04, 50 }, { 0x02, 50 }, { 0x01, 50 },
    { 0x01, 1000 }, { 0x02, 1000 }, { 0x04, 1000 }, { 0x08, 1000 }, { 0x10, 1000 }, { 0x20, 1000 }, { 0x40, 1000 }, { 0x80, 1000 },
    { 0x80, 1000 }, { 0x40, 1000 }, { 0x20, 1000 }, { 0x10, 1000 }, { 0x08, 1000 }, { 0x04, 1000 }, { 0x02, 1000 }, { 0x01, 1000 },
};

const struct Step c_joc4[] =
{
    { 0x80, 100 }, { 0xC0, 100 }, { 0xE0, 100 }, { 0xF0, 100 }, { 0xF8, 100 }, { 0xFC, 100 }, { 0xFE, 100 }, { 0xFF, 100 }, 
};

const const struct Step* c_jocuri[] = { c_joc1, c_joc2, c_joc3, c_joc4 };
const const int c_jocuriSize[] = { SIZE( c_joc1 ), SIZE( c_joc2 ), SIZE( c_joc3 ), SIZE( c_joc4 ) };

//Timer0
//Prescaler 1:4; TMR0 Preload = 6; Actual Interrupt Time : 1 ms
void InitTimer0()
{
    OPTION_REG = 0x81;
    TMR0 = 6;
    INTCON = 0xA0;
}

void init_sys()
{
    CMCON = 0x07; // To turn off comparators
    VREN = 0; // Disable voltage reference

    TRISB = 0b00000000; // portul B este numai IESIRI
    PORTB = 0b00000000; // se porneste cu toti pinii port B in stare LOW

    TRISA0 = INPUT; // RA0 este intrare digitala - asigura-te ca ai un rezistor de pull-up connectat de pe pin catre VCC
}

void interrupt ISR()
{
    static unsigned char counter = 0;
    static unsigned char ultima_apasare_reala = 0;

    if (TMR0IF)
    {
        TMR0IF = 0;
        TMR0 = 6;
        ++counterMs;

        // daca chiar si numai odata butonul nu este apasat atunci
        // ori avem bouncing ori butonul nu mai este apasat asadar in 'else' facem variabila counter = 0
        if (BUTON == APASAT)
        {
            counter++;
            /* Daca var. counter ajunge la 10 atunci au trecut 10ms cu butonul apasat,
               deci suntem siguri ca avem o apasare reala
            */
            if (counter >= 10)
            {
                counter = 0;
                if (ultima_apasare_reala == 0)
                {
                    // i este un contor care numara circular la fiecare apasare reala pana la NR_JOCURI
                    // daca NR_JOCURI este 4 ca in cazul curent atunci variabila i va lua valorile: 0,1,2,,3,0,1,2,3 samd
                    if (i >= NR_JOCURI - 1)
                        i = 0;
                    else
                        i++;
                    s_pos = 0;
                    ultima_apasare_reala = 1;
                }
            }
        }
        else
        {
            counter = 0;
            ultima_apasare_reala = 0;
        }
    }
}

void main()
{
    init_sys();
    InitTimer0();

    // bucla infinita
    while (1)
    {
        if ( counterMs >= c_jocuri[ i ][s_pos].time )
        {
            counterMs = 0;
            if ( ++s_pos >= c_jocuriSize[ i ] )
                s_pos = 0;
            PORTB = c_jocuri[ i ][s_pos].data;
        }
    }
}

 

Edited by core
verificare pe simulator

Share this post


Link to post
Share on other sites

Compilatorul folosit de @core is Microchip XC8.

Share this post


Link to post
Share on other sites

Sigur?

Atunci ce cauta

#include <pic.h>
#include <pic16f628.h>

in cod?

La xc8 trebuie inclus xc.h, cu restul se descurca singur:

Citat

5.3.3
 Device Header Files
There is one header file that is typically included into each C source file you write. The
file is <xc.h> and is a generic header file that will include other device- and
architecture-specific header files when you build your project.

 

 

Share this post


Link to post
Share on other sites

Da, am folosit xc8.

Am facut modif si pentru mikroc, dar pe simulator ledurile clipesc mai repede la mine, fata de compilarea pe xc8. O fi ceva in neregula cu setarea timer-ului. E xc8, timpul de clipire corespunde.

#define BUTON RA0_bit
#define INPUT 1
#define OUTPUT 0

#define APASAT 0

#define NR_JOCURI 4

#define SIZE( x ) ( sizeof( x ) / sizeof( x[ 0 ] ) )

struct Step
{
    unsigned char portData;
    unsigned short time;
};

static volatile unsigned int i;
static volatile unsigned int s_pos;
static volatile unsigned int counterMs;

const struct Step c_joc1[] =
{
    { 0x01, 50 }, { 0x02, 50 }, { 0x04, 50 }, { 0x08, 50 }, { 0x10, 50 }, { 0x20, 50 }, { 0x40, 50 }, { 0x80, 50 },
    { 0x80, 50 }, { 0x40, 50 }, { 0x20, 50 }, { 0x10, 50 }, { 0x08, 50 }, { 0x04, 50 }, { 0x02, 50 }, { 0x01, 50 },
    { 0xCC, 300 }, { 0x33, 300 }, { 0x55, 300 }, { 0xAA, 300 }, { 0x55, 300 }, { 0xAA, 300 }, { 0xCC, 300 }, { 0x33, 300 },
    { 0x01, 50 }, { 0x02, 50 }, { 0x04, 50 }, { 0x08, 50 }, { 0x10, 50 }, { 0x20, 50 }, { 0x40, 50 }, { 0x80, 50 },
    { 0x80, 50 }, { 0x40, 50 }, { 0x20, 50 }, { 0x10, 50 }, { 0x08, 50 }, { 0x04, 50 }, { 0x02, 50 }, { 0x01, 50 },
    { 0xF0, 500 }, { 0x0F, 500 }, { 0x99, 500 }, { 0x66, 500 }, { 0x99, 500 }, { 0x66, 500 },
    { 0x80, 100 }, { 0xC0, 100 }, { 0xE0, 100 }, { 0xF0, 100 }, { 0xF8, 100 }, { 0xFC, 100 }, { 0xFE, 100 }, { 0xFF, 100 }, 
    { 0x01, 50 }, { 0x02, 50 }, { 0x04, 50 }, { 0x08, 50 }, { 0x10, 50 }, { 0x20, 50 }, { 0x40, 50 }, { 0x80, 50 },
    { 0x80, 50 }, { 0x40, 50 }, { 0x20, 50 }, { 0x10, 50 }, { 0x08, 50 }, { 0x04, 50 }, { 0x02, 50 }, { 0x01, 50 }
};

const struct Step c_joc2[] =
{
    { 0x80, 100 }, { 0xC0, 100 }, { 0xE0, 100 }, { 0xF0, 100 }, { 0xF8, 100 }, { 0xFC, 100 }, { 0xFE, 100 }, { 0xFF, 100 }, 
};

const struct Step c_joc3[] =
{
    { 0x01, 50 }, { 0x02, 50 }, { 0x04, 50 }, { 0x08, 50 }, { 0x10, 50 }, { 0x20, 50 }, { 0x40, 50 }, { 0x80, 50 },
    { 0x80, 50 }, { 0x40, 50 }, { 0x20, 50 }, { 0x10, 50 }, { 0x08, 50 }, { 0x04, 50 }, { 0x02, 50 }, { 0x01, 50 },
    { 0x01, 1000 }, { 0x02, 1000 }, { 0x04, 1000 }, { 0x08, 1000 }, { 0x10, 1000 }, { 0x20, 1000 }, { 0x40, 1000 }, { 0x80, 1000 },
    { 0x80, 1000 }, { 0x40, 1000 }, { 0x20, 1000 }, { 0x10, 1000 }, { 0x08, 1000 }, { 0x04, 1000 }, { 0x02, 1000 }, { 0x01, 1000 },
};

const struct Step c_joc4[] =
{
    { 0x80, 100 }, { 0xC0, 100 }, { 0xE0, 100 }, { 0xF0, 100 }, { 0xF8, 100 }, { 0xFC, 100 }, { 0xFE, 100 }, { 0xFF, 100 }, 
};

const const struct Step* c_jocuri[] = { c_joc1, c_joc2, c_joc3, c_joc4 };
const const int c_jocuriSize[] = { SIZE( c_joc1 ), SIZE( c_joc2 ), SIZE( c_joc3 ), SIZE( c_joc4 ) };

//Timer0
//Prescaler 1:4; TMR0 Preload = 6; Actual Interrupt Time : 1 ms
void InitTimer0()
{
    OPTION_REG = 0x81;
    TMR0       = 6;
    INTCON     = 0xA0;
}

void init_sys()
{
    CMCON = 0x07;  // To turn off comparators
    VREN_bit  = 0;     // Disable voltage reference

    TRISB = 0b00000000; // portul B este numai IESIRI
    PORTB = 0b00000000; // se porneste cu toti pinii port B in stare LOW

    TRISA0_bit = INPUT; // RA0 este intrare digitala - asigura-te ca ai un rezistor de pull-up connectat de pe pin catre VCC
}

void Interrupt()
{
    static unsigned char counter = 0;
    static unsigned char ultima_apasare_reala = 0;

    if (TMR0IF_bit)
    {
        TMR0IF_bit = 0;
        TMR0       = 6;
        ++counterMs;

        // daca chiar si numai odata butonul nu este apasat atunci
        // ori avem bouncing ori butonul nu mai este apasat asadar in 'else' facem variabila counter = 0
        if (BUTON == APASAT)
        {
            counter++;
            /* Daca var. counter ajunge la 10 atunci au trecut 10ms cu butonul apasat,
               deci suntem siguri ca avem o apasare reala
            */
            if (counter >= 10)
            {
                counter = 0;
                if (ultima_apasare_reala == 0)
                {
                    // i este un contor care numara circular la fiecare apasare reala pana la NR_JOCURI
                    // daca NR_JOCURI este 4 ca in cazul curent atunci variabila i va lua valorile: 0,1,2,,3,0,1,2,3 samd
                    if (i >= NR_JOCURI - 1)
                        i = 0;
                    else
                        i++;
                    s_pos = 0;
                    ultima_apasare_reala = 1;
                }
            }
        }
        else
        {
            counter = 0;
            ultima_apasare_reala = 0;
        }
    }
}

void main()
{
    init_sys();
    InitTimer0();

    // bucla infinita
    while (1)
    {
        if ( counterMs >= c_jocuri[ i ][s_pos].time )
        {
            counterMs = 0;
            if ( ++s_pos >= c_jocuriSize[ i ] )
                s_pos = 0;
            PORTB = c_jocuri[ i ][s_pos].portData;
        }
    }
}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.