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

Modified Program

Modified Program

Note that the program can be made more readable if we create a function to display the required number and then call this function from the main program. Figure 6.28 shows the modified program (called SEVEN2.C). A function called Display is created with an argument called no. The function gets the bit pattern from local array SEGMENT indexed by no, inverts it, and then returns the resulting bit pattern to the calling program.

/*****************************************************************************
                              7-SEGMENT DISPLAY
                              =================
In this project a common anode 7-segment LED display is connected to
PORTC of a PIC18F452 microcontroller and the microcontroller is
operated from a 4MHz resonator. The program displays numbers 0 to 9 on
the display with a one second delay between each output.
In this version of the program a function called Display is used to display
the number.
Author: Dogan Ibrahim
Date:   July 2007
File:   SEVEN2.C
*******************************************************************************/
//
// This function displays a number on the 7-segment LED.
// The number is passed in the argument list of the function.
//
unsigned char Display(unsigned char no) {
 unsigned char Pattern;
 unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,
  0x7D,0x07,0x7F,0x6F};
 Pattern = SEGMENT[no];
 Pattern = ~Pattern; // Pattern to return
 return (Pattern);
}
//
// Start of MAIN Program
//
void main() {
 unsigned char Cnt = 0;
 TRISC = 0;               // PORTC are outputs
 for(;;)                  // Endless loop
 {
  PORTC = Display(Cnt);   // Send to PORTC
  Cnt++;
  if (Cnt == 10) Cnt = 0; // Cnt is between 0 and 9
  Delay_ms(1000);         // 1 second delay
 }
}


Figure 6.28: Modified program listing

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


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