一往動いたので現状で固めておく。
#include "MicroBit.h" SPI spi(MOSI, MISO, SCK); MicroBitSerial serial(USBTX, USBRX); // Use P16 as !CS pin. MicroBitPin CS(MICROBIT_ID_IO_P16, MICROBIT_PIN_P16, PIN_CAPABILITY_DIGITAL); class MCP3008{ public: MCP3008(void); void init(void); uint16_t read(char CH); //private: }; MCP3008::MCP3008(void){ } void MCP3008::init(void){ CS.setDigitalValue(1); //spi.format(8, 0); // How many bits in one byte, and SPI mode0(0,0). //spi.frequency(1000000); } uint16_t MCP3008::read(char CH){ char mosi[] = {1, 0b10000000 | CH<<4, 0}; char miso[3]; CS.setDigitalValue(0); for(int i=0; i<3; i++){ miso[i] = spi.write(mosi[i]); } CS.setDigitalValue(1); return (miso[1]<<8 | miso[2]) & 1023; } int main(void){ MCP3008 mcp; // Instantiate the MCP3008 class. mcp.init(); // Initialize the instance. while(1){ uint16_t ADdata = mcp.read(0); // From which channel to read AD data. float volt = (float)ADdata * 3.23/1024.0; // 3.23 is the voltage measured at VREF pin. serial.printf("AD %d, %.2f(v)\n", ADdata, volt); wait(1.0); } release_fiber(); return 0; }
from microbit import * class MCP3008: def __init__(self, CSpin=pin16): self.CS = CSpin.write_digital self.CS(1) self.mosi = [bytearray([1, (1<<7)|(ch<<4), 0]) for ch in range(8)] self.miso = bytearray([0, 0, 0]) def read(self, CH): self.CH = CH self.CS(0) spi.write_readinto(self.mosi[CH], self.miso) self.CS(1) return (self.miso[1]<<8 | self.miso[2]) & 1023 ############## # how to use # ############## if __name__ == "__main__": # Initialize the microbit.spi module. # You can also specify any of the following options: # baudrate=1000000, bits=8, mode=0, sclk=pin13, mosi=pin15, and miso=pin14 spi.init() # Instantiate the MCP3008 class with !CS assigned to pinX. adc = MCP3008(CSpin=pin16) while True: # Get 10-bit data by converting the voltage applied to the channel X of MCP3008. ADdata = adc.read(CH=0) volt = ADdata * 3.23/1024.0 print("AD %d, %.2f(V)" % (ADdata, volt)) sleep(1000)
unsigned int read(unsigned char CH){ unsigned char mosi[36] = {3,4,6,4,6,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,1}; unsigned char ch[8][6] = {{0,2,0,2,0,2}, /*CH0*/ {0,2,0,2,4,6}, /*CH1*/ {0,2,4,6,0,2}, /*CH2*/ {0,2,4,6,4,6}, /*CH3*/ {4,6,0,2,0,2}, /*CH4*/ {4,6,0,2,4,6}, /*CH5*/ {4,6,4,6,0,2}, /*CH6*/ {4,6,4,6,4,6}}; /*CH7*/ unsigned char miso[10]; unsigned char i, k; unsigned int sum; /* Some bits of the digital data which are to be transferred to ADC are replaced according to the input channel you specify.*/ for(i=0; i<6; i++){ mosi[i+5] = ch[CH][i]; } /*The thirty-six of 3-bit digital data are transferred to ADC, and then the ADC will output each bit of a 10-bit digital code one at a time.*/ k = 9; for(i=0; i<36; i++){ miniput(mosi[i]); if((i>=16) && (i%2==0)){ miso[k] = miniget(); k--; } } /*Some shifting operations can generate a 10-bit digital code.*/ sum = 0; for(i=0; i<10; i++){ sum |= miso[i]<<i; } return sum; } main(){ unsigned int i; /*For PC-G850VS, busy wait does not need a "volatile" qualifier.*/ while(1){ printf("%4d, %4d, %4d, %4d\n", read(0), read(1), read(2), read(3)); for(i=0; i<500; i++){} /*busy wait*/ } }
GitHubはここ: