//****************************************************************************************** // PIC18F14K50 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H22.07.10 // History : // Laungage : MPLAB C18 //****************************************************************************************** // (1)Description // //****************************************************************************************** #include #include #include #include #include #include #include #include // //Define configration // #pragma config CPUDIV=NOCLKDIV //CPU System Clock Selection bit(No CPU System Clock divide) #pragma config USBDIV = OFF //USB Clock Selection bit(USB Clock comes directly from the OSC1/OSC2 oscillator block; no divide) #pragma config FOSC = HS //Oscillator Selection bits(HS oscillator) #pragma config PLLEN = ON //X PLL Enable bit(Oscillator multiplied by 4) #pragma config PCLKEN = OFF //Primary Clock Enable Bit(Primary clock is under software control) #pragma config FCMEN = OFF #pragma config IESO = OFF //Internal/External Oscillator Switchover bit(Oscillator Switchover mode disabled) #pragma config PWRTEN = OFF,BOREN = OFF,BORV = 19 #pragma config WDTEN = OFF,WDTPS = 1 #pragma config MCLRE = OFF //MCLR Pin Enable bit(RE3 input pin enabled; MCLR disabled) #pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF #pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF,WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF,EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF // //Define function // unsigned int chkSW(void); void mvLED(unsigned char *); void mvLED2(unsigned char *); void wat(unsigned int); // //Define status // #define PI_NORMAL 0 #define PI_RIGHT 1 #define PI_LEFT 2 //****************************************************************************************** // Program : // Title : // Input : // Output : //****************************************************************************************** void main(void) { unsigned char LEDlst = 0x01; unsigned int status; // ***************************** // 1.Initialized Segment // ***************************** ANSELH = 0; //ALL Degital PORT (PORTB)@ TRISA = 0xFF; TRISB = 0xFF; TRISC = 0x00; LATC = 0x00; PORTC = 0x00; // ***************************** // 2.Program main // ***************************** while(1){ status = chkSW(); if (status== PI_RIGHT){ mvLED(&LEDlst); }else if (status== PI_LEFT){ mvLED2(&LEDlst); } PORTC = LEDlst; wat(10); }; } //****************************************************************************************** // Function Name : mvLED // Title : Move LED function // Input : // Output : //****************************************************************************************** void mvLED(unsigned char *LEDlst) { if(*LEDlst == 1){ *LEDlst = 2; }else if(*LEDlst > 127){ *LEDlst = 1; }else{ *LEDlst = *LEDlst * 2; } return; } //****************************************************************************************** // Function Name : mvLED2 // Title : Move LED function // Input : // Output : //****************************************************************************************** void mvLED2(unsigned char *LEDlst) { if(*LEDlst <= 1){ *LEDlst = 0x80; }else{ *LEDlst = *LEDlst / 2; } return; } //****************************************************************************************** // Function Name : chkSW // Title : Check Degital Switch // Input : // Output : //****************************************************************************************** unsigned int chkSW(void) { if((PORTB & 0b10000000) == 0){ while((PORTB & 0b10100000) != 0b10100000 ); return(PI_LEFT); }else if((PORTB & 0b00100000) == 0){ while((PORTB & 0b10100000) != 0b10100000 ); return(PI_RIGHT); }else{ return(PI_NORMAL); } } //****************************************************************************************** // Function Name : wat // Title : wait function // Input : // Output : //****************************************************************************************** void wat(unsigned int tim) { unsigned int lp,lp1 ; for(lp=0 ; lp < tim ; lp++ ) for(lp1=0 ; lp1 < 100 ; lp1++ ) ; return; }