كود:
/*
* Project name: Contact less digital tachometer
* Test configuration:
MCU: PIC16F628A runs @ 4.0 MHz external crystal oscillator
The common cathodes of the four seven segment dispalys are
controlled by RA0, RA1, RA2 and RA3
*/
//-------------- Function to Return mask for common cathode 7-seg. display
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
} //case end
}
sbit IR_Tx at RB7_bit;
sbit DD0_Set at RA0_bit;
sbit DD1_Set at RA1_bit;
sbit DD2_Set at RA2_bit;
sbit DD3_Set at RA3_bit;
unsigned short i, DD0, DD1, DD2, DD3;
unsigned int Sample1, Sample2, Sample3, RPM;
void main() {
TRISB = 0b00000000; // Set PORTB direction to be output
TRISA = 0b00110000; // Set PORTA direction to be output
PORTB = 0x00; // Turn OFF LEDs on PORTB
CMCON = 7 ; // Disable comparators
RPM = 0; // Initial Value of Counter
OPTION_REG = 0b00111000; // TOCS=1 for Counter mode, PSA=1 for 1:1
IR_Tx = 0; // Turn OFF IR
do {
DD0 = RPM%10;
DD0 = mask(DD0);
DD1 = (RPM/10)%10;
DD1 = mask(DD1);
DD2 = (RPM/100)%10;
DD2 = mask(DD2);
DD3 = (RPM/1000);
DD3 = mask(DD3);
for (i = 0; i<=100; i++) {
PORTB = DD0;
DD0_Set = 1;
DD1_Set = 0;
DD2_Set = 0;
DD3_Set = 0;
delay_ms(5);
PORTB = DD1;
DD0_Set = 0;
DD1_Set = 1;
DD2_Set = 0;
DD3_Set = 0;
delay_ms(5);
PORTB = DD2;
DD0_Set = 0;
DD1_Set = 0;
DD2_Set = 1;
DD3_Set = 0;
delay_ms(5);
PORTB = DD3;
DD0_Set = 0;
DD1_Set = 0;
DD2_Set = 0;
DD3_Set = 1;
delay_ms(5);
}
DD3_Set = 0;
// First Sample
TMR0=0;
IR_Tx = 1;
Delay_ms(1000); // Delay 1 Sec
IR_Tx = 0;
Sample1 = TMR0*60;
// Second Sample
TMR0=0;
IR_Tx = 1;
Delay_ms(1000); // Delay 1 Sec
IR_Tx = 0;
Sample2 = TMR0*60;
// Third Sample
TMR0=0;
IR_Tx = 1;
Delay_ms(1000); // Delay 1 Sec
IR_Tx = 0;
Sample3 = TMR0*60;
RPM = Sample1 + Sample2 + Sample3;
RPM = RPM/3;
} while(1); // endless loop
}