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

Project Program

Project Program

The program is called LED4.C, and the program listing is given in Figure 6.15. 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. 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. The bit patterns to be sent to the LEDs are then determined and sent to PORTC and PORTD. The program then repeats inside the endless loop, checking the state of the push-button switch.

/*********************************************************************
                               TWO DICE
                               ========
In this project 7 LEDs are connected to PORTC of a PIC18F452 microcontroller
and 7 LEDs to PORTD. 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:   LED4.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,Pattern,Seed = 1;
 unsigned char DICE[] = {0,0x08,0x22,0x2A,0x55,0x5D,0x77};
 TRISC = 0;              // PORTC are outputs
 TRISD = 0;              // PORTD are outputs
 TRISB = 1;              // RB0 input
 PORTC = 0;              // Turn OFF all LEDs
 PORTD = 0;              // Turn OFF all LEDs
 for(;;)                 // Endless loop
 {
  if (Switch == Pressed) // Is switch pressed ?
  {
   J = Number(6,seed);   // Generate first dice number
   Pattern = DICE[J];    // Get LED pattern
   PORTC = Pattern;      // Turn on LEDs for first dice
   J = Number(6,seed);   // Generate second dice number
   Pattern = DICE[J];    // Get LED pattern
   PORTD = Pattern;      // Turn on LEDs for second dice
   Delay_ms(3000);       // Delay 3 seconds
   PORTC = 0;            // Turn OFF all LEDs
   PORTD = 0;            // Turn OFF all LEDS
  }
 }
}


Figure 6.15: Program listing

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

Оглавление статьи/книги

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