البرنامج الأول باستخدام 2 وحدة عرض أى أن مدى التزايد والتناقص هو 0-99:
كود:
/*
UP Down Counter with two 7-Segment Display Multiplexing
In this project two common anode 7-segment LED displays are connected to
PORTD of a PIC16F877A * 4MHz .
Digit 1 (left digit) enable pin is connected to port pin RA0 ( TENS)
and digit 2 (right digit) enable pin is connected to port pin RA1 (ONES) .
The program counts up -down from buttons at RB0 , RB1 .
The display is updated in a timer interrupt service routine at every 5ms by :
Reload TMR0=100 & prescaler ration 1:32>> imer interrupt service routine at
(255-100) *32us=5ms = scanning time
MikroC PRO
ENG.F.ABDELAZIZ
http://www.eeecb.com/vb/index.php
*/
#define TENS_DIG PORTA.F0 //tens unit
#define ONES_DIG PORTA.F1//ones unit
//Global variable
unsigned char Count = 0;
unsigned char Flag = 0;
bit oldstate1;
bit oldstate2;
//-Function to Return mask for common anode 7-seg. display
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
} //case end
}
كود:
// TMR0 timer interrupt service routine. The program jumps to the ISR at every 5ms.
void interrupt ()
{
//Local variable
unsigned char TENS_SEG, ONES_SEG; // tens= left unit &ones= right unit
TMR0 = 100; // Re-load TMR0>>255-100=155usx32(prescaler)=5ms
INTCON = 0x20; // >>>>Set T0IE and clear T0IF<<<<
Flag = ~ Flag; // Toggle Flag as switch
if(Flag == 0) // Do digit 1
{
ONES_DIG = 1; // Disable digit 2
TENS_SEG = Count / 10; // TENS_SEG >>tens
PORTD = mask(TENS_SEG); // Send to PORTD
TENS_DIG = 0; // Enable digit 1>>RA0
}
else
{ // Do digit 2
TENS_DIG = 1; // Disable digit 1
ONES_SEG = Count % 10; // ONES_SEG >> ones
PORTD = mask(ONES_SEG); // Send to PORTD
ONES_DIG = 0; // Enable digit 2 >>RA1
}
}
//
// Start of MAIN Program. configure PORTA and PORTD as outputs.
// In addition, configure TMR0 to interrupt at every 10ms
//
void main()
{
ADCON1=0X07; //set A PORT general I/O
TRISD= 0; // PORTD are outputs
TRISA = 0; // RA0, RA1 are outputs
TENS_DIG = 1; // Disable digit 1
ONES_DIG = 1; // Disable digit 2
// Configure TMR0 timer interrupt
OPTION_REG = 0xC4; // Prescaler = 32
TMR0 = 100; // Load TMR0L with 100
INTCON = 0xA0; // Enable TMR0 interrupt
//Delay_ms(1000);
for(;;) // Endless loop
{
//UP Count
if (Button(&PORTB, 0, 1, 0)) { // Button 0 : Detect logical 0
oldstate1 = 1; // Update flag
}
if (oldstate1 && Button(&PORTB, 0, 1, 1)) { // Detect zero-to-one transition
Count = Count +1;
oldstate1 = 0; // Update flag
}
//DOWN Count
if(Button(&PORTB,1,1,0)){ // Button 1 : Detect logical 0
oldstate2 = 1; // Update flag
}
if (oldstate2 && Button(&PORTB, 1, 1, 1)) { // Detect zero-to-one transition
Count = Count -1;
oldstate2 = 0; // Update flag
}
if (Count > 99) // if it's more than 99 go to 0;
Count = 0;
}
}