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

Project Program

Project Program

The program is called LED5.C, and the program listing is given in Figure 6.18. At the beginning of the program Switch is defined as bit 0 of PORTB, and Pressed is defined as 0. The relationships between the dice numbers and the LEDs to be turned on are stored in an array called DICE as in Project 2. Variable Pattern is the data sent to the LEDs. The program enters an endless for loop where the state of the push-button switch is checked continuously. When the switch is pressed, two random numbers are generated by calling function Number. Variables L and U store the lower and higher nibbles of the bit pattern to be sent to PORTC. The bit pattern to be sent to PORTC is then determined using the method described in the Project Hardware section and stored in variable R. This bit pattern is then sent to PORTC to display both dice numbers at the same time. The dice numbers are displayed for 3 seconds, after which the LEDs are turned OFF to indicate that the system 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:   LED5.C
****************************************************************************/
#define Switch PORTB.F0
#define Pressed 0
//
// This function generates a pseudo random integer number
// between 1 and Lim
//
unsigned char Number(int Lim, int Y) {
 unsigned char Result;
 static unsigned int Y;
 Y = (Y * 32719 + 3) % 32749;
 Result = ((Y % Lim) + 1);
 return Result;
}
//
// Start of MAIN program
//
void main() {
 unsigned char J,L,U,R,Seed = 1;
 unsigned char DICE[] = {0,0x08,0x01,0x09,0x06,0x0E,0x07};
 TRISC = 0;              // PORTC are outputs
 TRISB = 1;              // RB0 input
 PORTC = 0;              // Turn OFF all LEDs
 for(;;)                 // Endless loop
 {
  if (Switch == Pressed) // Is switch pressed ?
  {
   J = Number(6,seed);   // Generate first dice number
   L = DICE[J];          // Get LED pattern
   J = Number(6,seed);   // Generate second dice number
   U = DICE[J];          // Get LED pattern
   R = 16*U + L;         // Bit pattern to send to PORTC
   PORTC = R;            // Turn on LEDs for both dice
   Delay_ms(3000);       // Delay 3 seconds
   PORTC = 0;            // Turn OFF all LEDs
  }
 }
}


Figure 6.18: Program listing

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


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