//====================================// Progarm :Up Down Counter// Author :Somlak Mangnimit// Date :24/01/2012// Device :Pic16f876//====================================#include <htc.h>#define _XTAL_FREQ 20000000__CONFIG(FOSC_HS&WDTE_OFF&PWRTE_OFF&LVP_OFF);#define Segment_Port PORTB#define Common_Digit1_Hi PORTAbits.RA2 = 1#define Common_Digit2_Hi PORTAbits.RA3 = 1#define Common_Digit1_Low PORTAbits.RA2 = 0#define Common_Digit2_Low PORTAbits.RA3 = 0#define Switch_Up PORTAbits.RA0#define Switch_Down PORTAbits.RA1const unsigned char seg_tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};volatile unsigned char Count,system_count,Button_state;volatile bit digit_sel;//====================================// Scan//====================================void Scan(void){while(!TMR0IF);TMR0IF = 0;TMR0 = 100;Common_Digit1_Low;Common_Digit2_Low;digit_sel = ~digit_sel; if(digit_sel){ Segment_Port = ~seg_tab[(Count/10)%10]; Common_Digit1_Hi; } else{ Segment_Port = ~seg_tab[Count%10]; Common_Digit2_Hi; }}//====================================// Setup//====================================void Setup(void){PORTA = 0x00;PORTA = 0x00;TRISA = 0x03;TRISB = 0x00;PSA = 0; //Timer0 PrescalerPS2 = 1;PS1 = 0;PS0 = 0; //1:32T0CS = 0; //Internal clock sourceTMR0 = 100;ADCON1 =0x06;}//====================================// Main//====================================void main(void){Setup(); while(1){ Scan(); system_count++;//---------- Button State 0 ---------- if(Button_state==0){ if(!Switch_Up||!Switch_Down){ Button_state = 1; system_count = 0; } }//---------- Button State 1 ---------- if(Button_state==1&&system_count==10){ if(!Switch_Up){ if(Count<99){Count++;} Button_state = 2; } else if(!Switch_Down){ if(Count){Count--;} Button_state = 2; } else{ Button_state = 0; } }//---------- Button State 2 ---------- if(Button_state==2){ if(Switch_Up&&Switch_Down){ Button_state = 0; } } }}