首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用模拟值控制Mcp41010雨刷位置

如何用模拟值控制Mcp41010雨刷位置
EN

Stack Overflow用户
提问于 2016-05-01 04:34:41
回答 1查看 576关注 0票数 0

我使用的是一个Mcp41010数字芯片,我想知道如何用模拟输入电压来调整芯片的雨刷位置,我需要一种降低(-)雨刷位置的方法,如果电压超过某个点,增加(++)芯片的雨刷位置回到正常位置--这是我发现的一些代码,它会使雨刷位置上下衰减,我需要一种控制它的方法。如果我的解释足够清楚的话,我对arduino还是很陌生的。

代码语言:javascript
复制
int CS_signal = 12;                      // Chip Select signal onsul pin 2 of Arduino
int CLK_signal = 52;                     // Clock signal on pin 4 of Arduino
int MOSI_signal = 51;                    // MOSI signal on pin 5 of Arduino
byte cmd_byte2 = B00010001 ;            // Command byte
int initial_value = 100;                // Setting up the initial value

void initialize() {                     // send the command byte of value 100 (initial value)
spi_out(CS_signal, cmd_byte2, initial_value);
}

void spi_out(int CS, byte cmd_byte, byte data_byte){                         // we need this function to send command byte and data byte to the chip

digitalWrite (CS, LOW);                                                 //  to start the transmission, the chip select must be low
spi_transfer(cmd_byte); // invio il COMMAND BYTE
delay(2);
spi_transfer(data_byte); // invio il DATA BYTE
delay(2);
digitalWrite(CS, HIGH);                                                 //     to stop the transmission, the chip select must be high
}

void spi_transfer(byte working) {
for(int i = 1; i <= 8; i++) {                                           //      Set up a loop of 8 iterations (8 bits in a byte)
if (working > 127) { 
digitalWrite (MOSI_signal,HIGH) ;                                    // If the MSB is a 1 then set MOSI high
} else { 
digitalWrite (MOSI_signal, LOW) ; }                                  // If the MSB is a 0 then set MOSI low                                           

digitalWrite (CLK_signal,HIGH) ;                                        //  Pulse the CLK_signal high
working = working << 1 ;                                                // Bit-shift the working byte
digitalWrite(CLK_signal,LOW) ;                                          // Pulse the CLK_signal low
}
}

void setup() {
pinMode (CS_signal, OUTPUT);
pinMode (CLK_signal, OUTPUT);
pinMode (MOSI_signal, OUTPUT);

initialize();

Serial.begin(9600);                                                     // setting the serial speed
Serial.println("ready!");
}

void loop() {
for (int i = 0; i < 255; i++) {
spi_out(CS_signal, cmd_byte2, i); 
Serial.println(i); delay(10); 
}
for (int i = 255; i > 0; --i) {
spi_out(CS_signal, cmd_byte2, i);
Serial.println(i);
delay(10);
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-01 07:17:55

代码语言:javascript
复制
int CS_signal = 12;                      // Chip Select signal onsul pin 2     of Arduino
int CLK_signal = 52;                     // Clock signal on pin 4 of Arduino
int MOSI_signal = 51;                    // MOSI signal on pin 5 of Arduino
byte cmd_byte2 = B00010001 ;            // Command byte
int initial_value = 100;                // Setting up the initial value

void initialize() {                     // send the command byte of value 100 (initial value)
spi_out(CS_signal, cmd_byte2, initial_value);

}

代码语言:javascript
复制
void spi_out(int CS, byte cmd_byte, byte data_byte){                        // we need this function to send command byte and data byte to the chip

digitalWrite (CS, LOW);                                                 // to start the transmission, the chip select must be low
spi_transfer(cmd_byte); // invio il COMMAND BYTE
delay(2);
spi_transfer(data_byte); // invio il DATA BYTE
delay(2);
digitalWrite(CS, HIGH);                                                 // to stop the transmission, the chip select must be high
}

void spi_transfer(byte working) {
for(int i = 1; i <= 8; i++) {                                           // Set up a loop of 8 iterations (8 bits in a byte)
 if (working > 127) { 
   digitalWrite (MOSI_signal,HIGH) ;                                    //  If the MSB is a 1 then set MOSI high
 } else { 
   digitalWrite (MOSI_signal, LOW) ; }                                  // If the MSB is a 0 then set MOSI low                                           

digitalWrite (CLK_signal,HIGH) ;                                        // Pulse the CLK_signal high
working = working << 1 ;                                                // Bit-shift the working byte
digitalWrite(CLK_signal,LOW) ;                                          // Pulse the CLK_signal low
}
}

void setup() {
pinMode (CS_signal, OUTPUT);
pinMode (CLK_signal, OUTPUT);
pinMode (MOSI_signal, OUTPUT);

initialize();

Serial.begin(9600);                                                     // setting the serial speed
Serial.println("ready!");
}

  void loop() {
  //  read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  if(sensorValue <= 200){
  //  Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
   float voltage = sensorValue * (5.0 / 1023.0);
   //   print out the value you read:
   Serial.println(voltage);

    int i = sensorValue;{
    spi_out(CS_signal, cmd_byte2, i); 
    Serial.println(i); 

   }
   }

   }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36963088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档