Problem with implementing c code on HC908qb8

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Problem with implementing c code on HC908qb8

ソリューションへジャンプ
1,184件の閲覧回数
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

ラベル(1)
0 件の賞賛
返信
1 解決策
935件の閲覧回数
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 件の賞賛
返信
1 返信
936件の閲覧回数
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 件の賞賛
返信