// CONFIG #pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled) #pragma config MCLRE = ON // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR) #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled) #pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB3/PGM pin has digital I/O function, HV on MCLR must be used for programming) #pragma config CPD = OFF // Data EE Memory Code Protection bit (Code protection off) #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off) #pragma config CCPMX = RB2 // CCP1 Pin Selection bit (CCP1 function on RB2) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include #define _XTAL_FREQ 8000000 // 8MHz #define TIMER_COUNT 200 #define AIN PORTAbits.RA0 #define VREF 5000 // LEDの点灯パターン int pattern[] = { 0b10000000, // 0 0b11110010, // 1 0b01001000, // 2 0b01100000, // 3 0b00110010, // 4 0b00100100, // 5 0b00000100, // 6 0b10110000, // 7 0b00000000, // 8 0b00100000, // 9 0b00010000, // A 0b00000110, // B 0b10001100, // C 0b01000010, // D 0b00001100, // E 0b00011100, // F }; static int no; static volatile int llseg = 0, lrseg = 0, rlseg = 0, rrseg = 0, sel = 0; unsigned int adin; float temper, sensv; int tm; // 割り込みエントリ void __interrupt() isr(void) { if (INTCONbits.TMR0IF) // TMR0 { switch(sel) { case 0: PORTAbits.RA7 = 0; PORTB = pattern[rrseg]; PORTAbits.RA1 = 1; break; case 1: PORTAbits.RA1 = 0; PORTB = pattern[rlseg]; PORTAbits.RA4 = 1; break; case 2: PORTAbits.RA4 = 0; PORTB = pattern[lrseg]; PORTAbits.RA6 = 1; break; case 3: PORTAbits.RA6 = 0; PORTB = pattern[llseg]; PORTAbits.RA7 = 1; break; } if(++sel == 4) sel = 0; INTCONbits.TMR0IF = 0; // 割り込みフラグクリア TMR0 = TIMER_COUNT; // タイマ設定 } } void htod(int hx, int* h, int* l) { *h = hx / 10; *l = hx - *h * 10; } void main(void) { OSCCON = 0b01110000; // 周波数8MHz ADCON0 = 0; ADCON1 = 0b11001110; // RA0のみアナログ TRISA = 0b00000001; // RA0入力 TRISB = 0b00000000; // 全て出力 PORTA = 0b00000000; // 全てLOWにする PORTB = 0b00000000; // 全てLOWにする OPTION_REGbits.T0CS = 0; // TMR0クロックソースは内部クロック OPTION_REGbits.PSA = 0; // プリスケーラをTMR0へ割り当て OPTION_REGbits.PS = 0b111; // プリスケーラ256 no = 0; TMR0 = TIMER_COUNT; INTCONbits.TMR0IE = 1; // TMR0割り込みを許可 INTCONbits.GIE = 1; // すべての割り込み許可 __delay_ms(100); while (1) { ADCON0 = 0b01000001; // RA0を選択 __delay_us(20); // 充電待ち //ADコンバート GO = 1; // AD変換開始 while(GO); // AD変換終了待ち adin = ADRESH; adin <<= 8; adin |= ADRESL; //温度に変換 sensv = (float)adin * VREF / 1024.0; temper = (sensv - 500.0) / 10.0; tm = temper; // 整数部 htod(tm, &llseg, &lrseg); tm = (temper - tm) * 100; // 少数2桁 htod(tm, &rlseg, &rrseg); __delay_ms(1000); } return; }