Problem with implementing c code on HC908qb8

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem with implementing c code on HC908qb8

Jump to solution
1,179 Views
mateuszjagocha
Contributor I

Hi,

I'm currently working on simple memory game. I have code, which was working correctly on Atmega8. But when i've portet it onto HC908qb i have issues. When i run it, the MCU jumps into odczyt_klaw() but don't wait for my respond and puts some different data into my tab_kop[] and all diodes start to flash (game over)...
Anyone could help?
I've attached code to this post

Labels (1)
0 Kudos
Reply
1 Solution
930 Views
bigmac
Specialist III

Hello,

Firstly, your attempted macros will not work as you intend.  The following macros should work correctly.

#define KEY1  PTA_PTA0

#define KEY2  PTA_PTA1

#define KEY3  PTA_PTA2

#define KEY4  PTA_PTA3

#define LED1  PTB_PTB0

#define LED2  PTB_PTB1

#define LED3  PTB_PTB2

#define LED4  PTB_PTB3

LED1 = 1;  // Turn the LED on (presumably)

Additionally, the tab_oryg[] array will reside in RAM.  Flash memory may be more appropriate for this table.  You may also want to consider whether the variable should be an unsigned 8-bit type, rather than a signed 16-bit type, as you currently have.

const int tab_oryg[] = {

  4, 2, 4, 8, 1, 2, 4, 2, 8, 1 , 2, 4, 2, 8, 1, 8, 2, 4, 1, 8

};

The primary problem with your code would seem to be that the odczyt_klaw() function does not exit, because of the presence of the while(1) loop, and the absence of a break;Eventually, COP timeout and reset will occur.

Here is part of the code, re-written to make use of the above macros -

void odczyt_klaw( void)

{

   a = 10;

   //PTA = 0xFF;

  

   while (1) {

     if (!key_lock && !KEY1) {

       key_lock = 1;

       LED1 = 1;

     }

     else if (key_lock && KEY1) {

       key_lock++;

       if (!key_lock) {

         PTB = 0x00;

         a = 0;

       }

     }

   ...

   }

Since key_lock is a 16-bit variable, the "else" part of the statement would need to execute 65535 times before the variable again reaches zero, and the LEDs are turned off.  Since the while loop is not "paced" by using a delay function, the total period will be compiler dependent, and will also depend on which of the remaining code, within the while loop, is being executed.  Presumably, this is not what you had intended.

Regards,

Mac

View solution in original post

0 Kudos
Reply
1 Reply
931 Views
bigmac
Specialist III

Hello,

Firstly, your attempted macros will not work as you intend.  The following macros should work correctly.

#define KEY1  PTA_PTA0

#define KEY2  PTA_PTA1

#define KEY3  PTA_PTA2

#define KEY4  PTA_PTA3

#define LED1  PTB_PTB0

#define LED2  PTB_PTB1

#define LED3  PTB_PTB2

#define LED4  PTB_PTB3

LED1 = 1;  // Turn the LED on (presumably)

Additionally, the tab_oryg[] array will reside in RAM.  Flash memory may be more appropriate for this table.  You may also want to consider whether the variable should be an unsigned 8-bit type, rather than a signed 16-bit type, as you currently have.

const int tab_oryg[] = {

  4, 2, 4, 8, 1, 2, 4, 2, 8, 1 , 2, 4, 2, 8, 1, 8, 2, 4, 1, 8

};

The primary problem with your code would seem to be that the odczyt_klaw() function does not exit, because of the presence of the while(1) loop, and the absence of a break;Eventually, COP timeout and reset will occur.

Here is part of the code, re-written to make use of the above macros -

void odczyt_klaw( void)

{

   a = 10;

   //PTA = 0xFF;

  

   while (1) {

     if (!key_lock && !KEY1) {

       key_lock = 1;

       LED1 = 1;

     }

     else if (key_lock && KEY1) {

       key_lock++;

       if (!key_lock) {

         PTB = 0x00;

         a = 0;

       }

     }

   ...

   }

Since key_lock is a 16-bit variable, the "else" part of the statement would need to execute 65535 times before the variable again reaches zero, and the LEDs are turned off.  Since the while loop is not "paced" by using a delay function, the total period will be compiler dependent, and will also depend on which of the remaining code, within the while loop, is being executed.  Presumably, this is not what you had intended.

Regards,

Mac

0 Kudos
Reply