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 build1903 core i3 LGA1150 * Strage NAS2ドライブの"\2019\pic_data\project" ただしexFAT * Created on 2019/09/28, 16:13 */ #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 sw_cnt; sw_cnt = 1; //下のwhile(1)の内側に入れるべき? int sw_mae; int sw_ato; while(1){ // 無限ループ sw_mae = 0; sw_ato = 0; while(RA0 == 1){ //ボタンが押されるまで何もしない NOP(); //この行は不要かも } //RA0は10Kohmでプルアップ。スイッチが押されるとRA0は0に if (RA0 == 0) { //ボタンが押されたら(els文は3行前にあるので省略可?) sw_mae = 1; //状態遷移前のフラグを立てる __delay_ms(20); //20ms待つ if (RA0 == 0){ //やはりボタンが押された状態なら sw_ato = 1;} //状態遷移後のフラグを立てる else { //もしそうでないなら・・・ sw_ato = 0; //このelse文はひつようなのか? } if (sw_mae == 1 || sw_ato == 1){ //前後の状態が同じ1なら sw_cnt ++; //スイッチカウントを一つ進める } if (sw_cnt == 5) { //スイッチカウントが5になったら sw_cnt = 1; //1にもどす } } if (sw_cnt == 1) { //以下スイッチカウントに応じたport出力 PORTB = 0b10001000; } if (sw_cnt == 2) { PORTB = 0b01000100; } if (sw_cnt == 3) { PORTB = 0b00100010; } if (sw_cnt == 4) { PORTB = 0b00010001; } __delay_ms(1000); //1秒以上押し続けるとスイッチカウントがアップ } }
Private
[
?
]
Run code
Submit