//**************************************************************************** // PIC12F683 Sample Program // Project : // Program Name : // Programer : // Create date : // Replace date : // History : // Language : XC8 C Laungage Version 2.31 // //**************************************************************************** // Description : // //**************************************************************************** #include #include #include #include #include #include #include //int at 0x2007 __CONFIG = _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF // & _CPD_OFF & _BOD_OFF & _IESO_OFF & _FCMEN_OFF ; #pragma config FOSC = INTOSCIO #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config MCLRE = OFF #pragma config CP = OFF #pragma config CPD = OFF #pragma config BOREN = OFF #pragma config IESO = OFF #pragma config FCMEN = OFF void Init_CCP(void); void Tim1s(void); int main(void) { // ************************* // 1.Initialized Segment // ************************* // (1)Set I/O Port // 1-1)Set I/O Port config ANSEL=0x00; //ALL Degital PORT // 1-2)Set OSC // OSCCON=0x70; //OSC:8MHz OSCCON=0x30; //OSC:500KHz // 1-3)Set I/O Port TRISIO=0xFB; GPIO=0x00; // (2)Initialize CCP-Module Init_CCP(); // ************************* // 2.Program main // ************************* while(1){ // (1)Move -60° CCPR1L = 7; Tim1s(); // (2)Move 0° CCPR1L = 12; Tim1s(); // (3)Move 60° CCPR1L = 16; Tim1s(); // (4)Move 0° CCPR1L = 12; Tim1s(); } } //**************************************************************************** // Function Name : Init_CCP // Title : CCPモジュールの初期化 // Input : // Output : // Description : //**************************************************************************** void Init_CCP(void) { // 1.Set CCP1CON Register // Bit7-6:00, Bit5-4:PMW mode, Bit3-0:CCP1 mode select bit // ※Bit5-4(00):Init value OSC,Bit3-0(11xx):PWM mode CCP1CON = 0x0c ; // B'00001100' // 2.Set PR2 Register // PWM Frequency // PR2 = 156 ; // 50Hz; 500000Hz(Int_OSC)/4/16(Prescaler)/50(Hz)==>156(DEC) // 3.Set CCPR1L Register // PWM Duty // CCPR1L = 0 ; // 4.Set T2CON Register // Bit6-3:00, Bit2:TIMER 2 ON, Bit1-0:Prescale(00;Prescaler=1,01;Prescaler=4,1X;Prescaler=16) T2CON = 0x06 ; //B'00000110' } //****************************************************************************************** // Function Name : TIM1S // Title : タイマー // Input : // Output : // Description : //****************************************************************************************** void Tim1s(void) { // 1.Set Register // (1)T1CON Register B'00010101' // Bit7(0) :None // Bit6(0)/T1RUN :0 = System clock is derived from another source // Bit5,4(01)/T1CKPS:1:2 Prescale Value // Bit3(0)/T1OSCEN :OSC is shut off // Bit2(1)/T1SYNC :ASyncronize // Bit1(0)/TMR1CS :Internal clock(Fosc/4) // Bit0(1)/TMR1ON :Enable Timer1 T1CON = 0x15 ; // (2)Set TMR1 Value // 500000Hz(Int_OSC)/4/2(Prescale) => 0d62500(0xF424) // 0xFFFF(-)0xF424 => 0d3035(0x0BDB) // TMR1H = 0x0b ; TMR1L = 0xdb ; // // 2.Timer // PIR1 = PIR1 & 0xfe; while((PIR1 & 0x01) == 0); }