Книга: Advanced PIC Microcontroller Projects in C

Modifying the Program

Modifying the Program

The program given in Figure 6.18 can made more efficient by combining the two dice nibbles into a single table value as described here.

There are thirty-six possible combinations of the two dice values. Referring to Table 6.4, Table 6.5, and Figure 6.16, we can create Table 6.6 to show all the possible two-dice values and the corresponding numbers to be sent to PORTC.

Table 6.6: Two-dice combinations and the number to be sent to PORTC

Dice numbers PORTC value Dice numbers PORTC value
1,1 0x88 4,1 0x86
1,2 0x18 4,2 0x16
1,3 0x98 4,3 0x96
1,4 0x68 4,4 0x66
1,5 0xE8 4,5 0xE6
1,6 0x78 4,6 0x76
2,1 0x81 5,1 0x8E
2,2 0x11 5,2 0x1E
2,3 0x91 5,3 0x9E
2,4 0x61 5,4 0x6E
2,5 0xE1 5,5 0xEE
2,6 0x71 5,6 0x7E
3,1 0x89 6,1 0x87
3,2 0x19 6,2 0x17
3,3 0x99 6,3 0x97
3,4 0x69 6,4 0x67
3,5 0xE9 6,5 0xE7
3,6 0x79 6,6 0x77

The modified program (program name LED6.C) is given in Figure 6.19. In this program array DICE contains the thirty-six possible dice values. The program enters an endless for loop, and inside this loop the state of the push-button switch is checked. Also, a variable is incremented from 1 to 36. When the button is pressed, the value of this variable is used as an index to array DICE to determine the bit pattern to be sent to PORTC. As before, the program displays the dice numbers for 3 seconds and then turns OFF all LEDs to indicate that it is ready.

/*****************************************************************************
                  TWO DICE - USING FEWER I/O PINS
                   =============================
In this project LEDs are connected to PORTC of a PIC18F452 microcontroller
and the microcontroller is operated from a 4MHz resonator. The LEDs are
organized as the faces of a real dice. When a push-button switch connected to
RB0 is pressed a dice pattern is displayed on the LEDs. The display remains in
this state for 3 seconds and after this period the LEDs all turn OFF to
indicate that the system is ready for the button to be pressed again.
In this program a pseudorandom number generator function is
used to generate the dice numbers between 1 and 6.
Author: Dogan Ibrahim
Date:   July 2007
File:   LED6.C
******************************************************************************************/
#define Switch PORTB.F0
#define Pressed 0
//
// Start of MAIN program
//
void main() {
 unsigned char Pattern, J = 1;
 unsigned char DICE[] = {0,0x88,0x18,0x98,0x68,0xE8,0x78,
  0x81,0x11,0x91,0x61,0xE1,0x71,
  0x89,0x19,0x99,0x69,0xE9,0x79,
  0x86,0x16,0x96,0x66,0xE6,0x76,
  0x8E,0x1E,0x9E,0x6E,0xEE,0x7E,
  0x87,0x17,0x97,0x67,0xE7,0x77};
 TRISC = 0;              // PORTC are outputs
 TRISB = 1;              // RB0 input
 PORTC = 0;              // Turn OFF all LEDs
 for(;;)                 // Endless loop
 {
  if (Switch == Pressed) // Is switch pressed ?
  {
   Pattern = DICE[J];    // Number to send to PORTC
   PORTC = Pattern;      // send to PORTC
   Delay_ms(3000);       // 3 seconds delay
   PORTC = 0;            // Clear PORTC
  }
  J++;                   // Increment J
  if (J == 37) J = 1;    // If J = 37, reset to 1
 }
}


Figure 6.19: Modified program

Оглавление книги


Генерация: 1.153. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз