//#include #include #include #include #include "7seg.h" //#include //to use the CCP //#include //to use the CCP static int ad=1;//to use the Force sensor static int count_PWM=0;// PWM pulse seed /* * FileName : config.h * Created for : Dept.Elecon. */ #ifndef __HEADER_CONFIG__ #define __HEADER_CONFIG__j #pragma config OSC = HS // Oscillator Selection bits //#pragma config OSC = INTIO7 // 1MHz internal oscillator (Corrected) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit #pragma config IESO = OFF // Internal/External Oscillator Switchover bit #pragma config PWRT = OFF // Power-up Timer Enable bit #pragma config BOREN = OFF // Brown-out Reset Enable bits #pragma config BORV = 20 // Brown-out Reset Voltage bit #pragma config WDT = OFF // Watchdog Timer Enable bit #pragma config WDTPS = 1024 // Watchdog Timer Postscale Select bits #pragma config MCLRE = ON // MCLR Pin Enable bit #pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit #pragma config PBADEN = OFF // PORTB A/D Enable bit #pragma config CCP2MX = PORTBE // CCP2 MUX bit #pragma config STVREN = OFF // Stack Full/Underflow Reset Enable bit #pragma config LVP = OFF // Single-Supply ICSP Enable bit #pragma config XINST = OFF // Extended Instruction Set Enable bit #pragma config DEBUG = OFF // Background Debugger Enable bit ??????????? #pragma config CP0 = OFF // Code Protection bit Block 0 #pragma config CP1 = OFF // Code Protection bit Block 1 #pragma config CP2 = OFF // Code Protection bit Block 2 #pragma config CP3 = OFF // Code Protection bit Block 3 #pragma config CPB = OFF // Boot Block Code Protection bit #pragma config CPD = OFF // Data EEPROM Code Protection bit #pragma config WRT0 = OFF // Write Protection bit Block 0 #pragma config WRT1 = OFF // Write Protection bit Block 1 #pragma config WRT2 = OFF // Write Protection bit Block 2 #pragma config WRT3 = OFF // Write Protection bit Block 3 #pragma config WRTB = OFF // Boot Block Write Protection bit #pragma config WRTC = OFF // Configuration Register Write Protection bit #pragma config WRTD = OFF // Data EEPROM Write Protection bit #pragma config EBTR0 = OFF // Table Read Protection bit Block 0 #pragma config EBTR1 = OFF // Table Read Protection bit Block 1 #pragma config EBTR2 = OFF // Table Read Protection bit Block 2 #pragma config EBTR3 = OFF // Table Read Protection bit Block 3 #pragma config EBTRB = OFF // Boot Block Table Read Protection bit #endif // unsigned char Timer2Config;//PWM clock source //using pwm.h //_/_/_/_/_/_/_/ To use the input by Force sensor _/_/_/_/_/_/_/_/_/_/_/ //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ //*********************** Initialization of Interrupt *********************** void InitInterrupt(void) { IPEN = 1; // Enable priority levels /* Initialization for INT1, INT2 interrupt */ INT2IP = 0; // INT2 ext. int. priority is low priority INT1IP = 0; // INT1 ext. int. priority is low priority INTEDG2 = 0; // INT2 ext. int. edge is falling edge INTEDG1 = 0; // INT1 ext. int. edge is falling edge INT2IF = 0; // clear INT2 external itr. flag INT1IF = 0; // clear INT1 external itr. flag /* Initialization for CCP2 interrupt */ T3CCP1 = 0; T3CCP2 = 0; // CCP2-TMR1 CCP1-TMR1 (CCP1 not used) T1CONbits.T1CKPS=0b01; // TMR1 Prescale = 1:2 T1CONbits.TMR1CS=0; // TMR1 Clock source = Internal clock(Fosc/4) CCPR2H=0x61; CCPR2L=0xA8; //count 20ms ( CCPR = 25000*0.4us*2(PS) ) CCP2CONbits.CCP2M=0b1011; // CCP2 Compare Mode PIR2bits.CCP2IF=0; //clear flag CCP2 INT2IE = 1; // Enable INT2 ext. int. INT1IE = 1; // Enable INT1 ext. int. CCP2IE = 1; // Enable CCP2 int. T1CONbits.TMR1ON=1; // TMR1 work } void interrupt low_priority isrlow(void) { if(INT1IF){ // SW1 turns on (falling down) Delay10KTCYx(3); // Wait 12ms to avoid chatter if(!RB1 && count_PWM != 34) count_PWM++; // Level up INT1IF = 0; // Clear flag INT1IF } else if(INT2IF){ // SW2 turns on (falling down) Delay10KTCYx(3); // Wait 12ms to avoid chatter if(!RB2 && count_PWM) count_PWM--; // Level down INT2IF = 0; // Clear flag INT2IF } } void interrupt isrhigh(void) { if(PIR2bits.CCP2IF) // CCP2 int. { if(count_PWM){ LATB3 = 1; Delay1KTCYx(2); Delay100TCYx(count_PWM); LATB3 = 0; } PIR2bits.CCP2IF=0;//clear } } //********************************************************************* void main (void){ int num[10] = {NUM0,NUM1,NUM2,NUM3,NUM4,NUM5,NUM6,NUM7,NUM8,NUM9}; int ad=0, i; int tens=0, ones=0; // AD変換値0〜1023を7セグで0〜99の表示にするための十の位と一の位 TRISA = 0x01; TRISB = 0b00000110; TRISC = 0; TRISD = 0x01; TRISE = 0; LATA = 0; LATB = 0; LATC = 0; LATD = 0; LATE = 0; //***********************Control Servo Motor*********************** InitInterrupt(); GIE = 1; PEIE = 1; //******************************************************* //_/_/_/_/_/_/_/ To use the input by Force sensor _/_/_/_/_/_/_/_/_/_/_/ OpenADC(ADC_FOSC_16 & ADC_RIGHT_JUST&ADC_12_TAD, ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS,ADC_1ANA); //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ while (1){ //_/_/_/_/_/_/_/ Sample program to practice the using "Switch" _/_/_/_/_/_/_/_/_/_/_/ if(PORTDbits.RD1==0) LATBbits.LATB5=0; else LATBbits.LATB5=1; //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ //************ 7 SEG. LED ********************** ConvertADC(); while(BusyADC()); ad=ReadADC(); tens = ad / 100; if(tens > 9) tens = 9; ones = ad % 10; for(i=0; i<15; i++){ LATE=0; LATC=num[ones]; LATE=0x02; // RORT Eに0b00000010をセット = 7セグ2が点灯 Delay10KTCYx(1); // 残像効果 ディレイ 10MHzで動作=0.1μs ×4サイクル=0.4μs→10KT=0.4μ×10K=4ms LATE=0; // PORT Eに0をセット=7セグ消灯 LATC=num[tens]; LATE=0x01; // PORT Eに1をセット=7セグ1が点灯 Delay10KTCYx(1); // ディレイ } } }