ひきつづきPICをぼちぼちやってます。PIC/Pythonのコードを書いてから端子を圧着するのが面倒で放置していたのですが、昨日やっと重い腰を上げて動かしました。一発でうまく動いてよかったです。PICのコードはビットシフトをほとんどノーコメントでつかっていて、自分でも何をやっているのか思い出せないです。DE10の教本を読みながらノリで書いたのですが。
これを先日作った鉄道模型コントローラなどと連携させたいです。心が折れないようにモチベーションを出すのが一番難しいです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
import serial | |
device = '/dev/ttyUSB0' | |
ser = serial.Serial(device, baudrate=9600) | |
mascon_level = –1 # init | |
static_mascon_level = –1 # init | |
buttons = –1; | |
same_count = 0; | |
while True: | |
r = ser.read() | |
mascon_new_level = ord(r) & 0b00001111 # 下位4bit | |
same_count += 1 | |
if mascon_new_level != mascon_level: | |
same_count = 0 | |
mascon_level = mascon_new_level | |
if same_count == 3 and static_mascon_level != mascon_level: | |
static_mascon_level = mascon_level | |
print(str(mascon_level) + ' ノッチ') | |
buttons_new = ord(r) >> 4 | |
if buttons_new != buttons: | |
buttons = buttons_new | |
#print('ボタン状態: ' + bytes(buttons)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PIC16F1579 Configuration Bit Settings | |
// CONFIG1 | |
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator; I/O function on CLKIN pin) | |
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) | |
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled) | |
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input) | |
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) | |
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) | |
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) | |
// CONFIG2 | |
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) | |
#pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCKED Bit Can Be Cleared & Set Once) | |
#pragma config PLLEN = OFF // PLL Disabled | |
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) | |
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) | |
#pragma config LPBOREN = OFF // Low Power Brown-out Reset enable bit (LPBOR is disabled) | |
#define _XTAL_FREQ 16000000 | |
#include <xc.h> | |
#include <stdio.h> | |
void putch(char write_char) | |
{ | |
while(!TXSTAbits.TRMT); | |
TXREG = write_char; | |
} | |
void main(void) | |
{ | |
OSCCON = 0b01111011; // 16MHz | |
ANSELC = 0b00000000; // All digital | |
TRISC = 0b11111111; // PORTC all input | |
PORTC = 0b00000000; // PORTC init | |
ANSELB = 0b00000000; | |
TRISB = 0b00010000; // RB4 input | |
PORTB = 0b00000000; | |
TRISA = 0b00000000; | |
RA0PPS = 0b00001001; // RA0 UART TX | |
TXSTA = 0b00100000; // 8bit UART | |
RCSTA = 0b10010000; | |
SPBRG = 25; // 16MHz 9600bps | |
INTCONbits.GIE = 1; // accept interrupt | |
PIE1bits.TXIE = 1; // accept TX interrupt | |
unsigned char mascon = 0; | |
unsigned char txbyte; | |
while (1) { | |
if (PORTCbits.RC3 == 1 && (PORTC & 0b00010111)) { // 18????ON | |
mascon = (PORTCbits.RC4 << 3) + (PORTC & 0b00000111) – 1; | |
} else { | |
mascon = 0; | |
} | |
txbyte = (PORTC & 0b11100000) + (PORTBbits.RB4 << 4) + mascon; | |
putch("%c", txbyte); | |
while(!TXSTAbits.TRMT); | |
__delay_ms(100); | |
} | |
return; | |
} |