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

Project Program

Project Program

The program is called LED2.C, and the program listing is given in Figure 6.10. 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. Variable J is used as the dice number. Variable Pattern is the data sent to the LEDs. Program then enters an endless for loop where the value of variable J is incremented very fast between 1 and 6. When the push-button switch is pressed, the LED pattern corresponding to the current value of J is read from the array and sent to the LEDs. The LEDs remain in this state for 3 seconds (using function Delay_ms with the argument set to 3000ms), after which they all turn OFF. The system is then ready to generate a new dice number.

/*****************************************************************************
                                 SIMPLE DICE
                                 ===========
In this project 7 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.
Author: Dogan Ibrahim
Date:   July 2007
File:   LED2.C
*****************************************************************************/
#define Switch PORTB.F0
#define Pressed 0
void main() {
 unsigned char J = 1;
 unsigned char Pattern;
 unsigned char DICE[] = {0,0x08,0x22,0x2A,0x55,0x5D,0x77};
 TRISC = 0;             // PORTC outputs
 TRISB = 1;             // RB0 input
 PORTC = 0;             // Turn OFF all LEDs
 for(;;)                // Endless loop
 {
  if(Switch == Pressed) // Is switch pressed ?
  {
   Pattern = DICE[J];   // Get LED pattern
   PORTC = Pattern;     // Turn on LEDs
   Delay_ms(3000);      // Delay 3 second
   PORTC = 0;           // Turn OFF all LEDs
   J = 0;               // Initialize J
  }
  J++; // Increment J
  if (J == 7) J = 1; // Back to 1 if > 6
 }
}


Figure 6.10: Program listing

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


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