codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
/* * File: 4modeswitch.c * Author: hage no ossan dame ningen * DEVICE = 16F88 * TOOLS xc8 v2.10 MPLAB X IDE 5.25 PICKIT3 win10 core i3 LGA1150 1F DeskTopPC jitaku * Created on 2019/09/28, 16:13(本日未使用MACアドレスPCへの新規ダウンロードなのであと60日は最適化最大?) */ #include <xc.h> // CONFIG1 #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 = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital I/O, MCLR internally tied to VDD) #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled) #pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB3 is digital I/O, 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 = RB3 // CCP1 Pin Selection bit (CCP1 function on RB3) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off) // CONFIG2 #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled) #pragma config IESO = OFF // Internal External Switchover bit (Internal External Switchover mode disabled) #define _XTAL_FREQ 8000000 void main(void) { OSCCON = 0b01110000; // 内蔵クロックの周波数を8MHzに設定 PORTA = 0x00; // PORTAを初期化 PORTB = 0x00; // PORTBを初期化 TRISA = 0x01; // PORTA0(第17ピン)を入力に設定 TRISB = 0x00; // PORTBを全ピン(第6-13ピン)出力に設定 ANSEL = 0x00; // A/D変換を無効化 ADCON0 = 0x00; ADCON1 = 0x00; int i; for (i = 0; i < 6; i++) { PORTB = 0b00000001; //以下、プログラムがちゃんと動き出したことの確認 __delay_ms(50); //最初の4bitは2SC1815を経てメカニカルリレーへ。 PORTB = 0b00000010; //後ろの4bitは抵抗510オームを経てLED直結 __delay_ms(50); //リレーは倍率の違うOPアンプ回路の出力を一つだけ選択する PORTB = 0b00000100; //ロータリースイッチは接点がすぐだめになるので __delay_ms(50); PORTB = 0b00001000; __delay_ms(50); PORTB = 0b00000100; __delay_ms(50); PORTB = 0b00000010; __delay_ms(50); } PORTB = 0b00000001; __delay_ms(50); PORTB = 0b00001111; //合計80mAほど(<200mA) __delay_ms(500); PORTB = 0b10001000; // ===============以下修正済=============== int ButtonChangeTime = 0; int sw_cnt = 1; int ButtonPreviousState = 0; while(1){ __delay_ms(5); //←タイマーに置き換える if(timer){...} if (RA0 == 1) { //スイッチが押されたか? if (ButtonPreviousState == 0) { //OFFからONの判定 ButtonChangeTime = ButtonChangeTime + 1; if (ButtonChangeTime == 4) { // 20msチャタリング&ノイズ防止 ButtonChangeTime = 0; ButtonPreviousState = 1; sw_cnt = sw_cnt + 1; if (sw_cnt == 5) { sw_cnt = 1; } if (sw_cnt == 1) { PORTB = 0b00000010; } if (sw_cnt == 2) { PORTB = 0b00000100; } if (sw_cnt == 3) { PORTB = 0b00001000; } if (sw_cnt == 4) { PORTB = 0b00010000; } } } else { ButtonChangeTime = 0; } } else if (ButtonPreviousState == 1) {// ONからOFFの判定 ButtonChangeTime = ButtonChangeTime + 1; if (ButtonChangeTime == 20) { // 100msチャタリング&連続押し防止() ButtonChangeTime = 0; ButtonPreviousState = 0; } } else { ButtonChangeTime = 0; //(誤動作防止用) } } }
Private
[
?
]
Run code
Submit