Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在AVR C中实现arduino Uno shiftOut()?_C_Arduino_Avr - Fatal编程技术网

如何在AVR C中实现arduino Uno shiftOut()?

如何在AVR C中实现arduino Uno shiftOut()?,c,arduino,avr,C,Arduino,Avr,我试图使用移位寄存器74hc595控制8x8 led矩阵。在正常的Arduino代码中,我们使用shiftOut()函数来写入dataPin,但是我在AVR C中尝试同样的东西时遇到了困难。我遇到的唯一问题是如何只将数据发送到移位寄存器的dataPin,因为如果我使用PORTB |=data端口中的所有内容都会受到影响 这是到目前为止我的代码- #define F_CPU 20000000L #include <avr/io.h> #include <avr/delay.h&g

我试图使用移位寄存器74hc595控制8x8 led矩阵。在正常的Arduino代码中,我们使用
shiftOut()
函数来写入dataPin,但是我在AVR C中尝试同样的东西时遇到了困难。我遇到的唯一问题是如何只将数据发送到移位寄存器的dataPin,因为如果我使用
PORTB |=data
端口中的所有内容都会受到影响

这是到目前为止我的代码-

#define F_CPU 20000000L
#include <avr/io.h>
#include <avr/delay.h>

#define latchPinColumn 3
#define latchPinRow 2
#define clockPin 4
#define dataPin 5

//for looping
byte i, j;

//storing data
byte dataToSendColumn;
byte dataToSendRow;

int main()
{
  //enabling output pins
  DDRB |= (1 << dataPin);
  DDRB |= (1 << clockPin);
  DDRB |= (1 << latchPinColumn);
  DDRB |= (1 << latchPinRow);
  
  PORTB = 0x00; //setting everything to low
  
  while(1)
  {
    for(i=0; i<8; i++)
    {
      for(j=0; j<8; j++)
      {
        dataToSendColumn = (1<<i);
        dataToSendRow = (1<<j);
        
        PORTB = 0x00;   //setting everything to low
        shiftOut(dataPin, clockPin, dataToSendColumn); //controlling the column
        PORTB |= (1 << latchPinColumn); //setting latch pin for led column to high
        PORTB ^= (1 << latchPinColumn); //setting latch pin for led column to low
        shiftOut(dataPin, clockPin, dataToSendRow); //controlling the row
        PORTB |= (1 << latchPinRow); //setting latch pin for led row to high
        PORTB ^= (1 << latchPinRow); //setting latch pin for led column to low
        
      }
    }
  }
}

void shiftOut(int dataP, int clock, int val)
{
  //performing shiftOut in LSBFIRST method
  for(i=0; i<8; i++)
  {
    data = val & 0b00000001;
    PORTB |= data;
    PORTB ^= (1^=clock);
  }
}
#定义F#U CPU 20000000L
#包括
#包括
#定义锁紧柱3
#定义闭锁销行2
#定义时钟引脚4
#定义数据引脚5
//用于循环
字节i,j;
//存储数据
字节dataToSendColumn;
字节dataToSendRow;
int main()
{
//启用输出引脚
DDRB |=(1)arduino如何实现移位
像这样在主管道内使用

void main(){
   //code ...
   // init section 
    Pin data = {&PORTB,4};
    Pin clock= {&PORTB,5};
    // make it output
    DDRB |=1<<4;
    DDRB |=1<<5;        
    //code ...
    while(1){
        // code ..
        // use when you need
        shiftOut(data,clock,LSBFIRST,9); // this will shift out 9(0b00001001) low bit will go first
        // code ...
    }   
        
}
void main(){
//代码。。。
//初始段
引脚数据={&PORTB,4};
引脚时钟={&PORTB,5};
//让它输出

DDRB |=1你应该提供你真正尝试的
shiftOut
,它会影响一切。你甚至不会编译它。我已经将代码更新为我在提交之前编写的代码,正如你所看到的那样,我正在影响整个PORTB,我知道这是错误的。要设置低引脚,请使用(&=~(bitmask)not^=(bitmask)你在按位运算符中有一些语言错误。我强烈建议你先学习或修改
C语言
。你可以继续练习以提高自己faster@Delta_G我使用XOR是因为我想切换位
// helper macros to make pin HIgh or low
#define  SET_PIN(port,pinNumber)  ( (port) |= (1<<pinNumber))
#define  CLEAR_PIN(port,pinNumber) ( (port) &=~(1<<pinNumber) )
// defention for Bits order
#define LSBFIRST 0
#define MSBFIRST 1

// pin information
typedef struct {
    volatile unsigned char * Port;
    char NUmber;
}Pin;

// function implementaiton
void shiftOut(Pin dataPin, Pin clockPin, char bitOrder, char val)
{
    char i;

    for (i = 0; i < 8; i++)  {
        if (bitOrder == LSBFIRST){
            (val & (1 << i))?SET_PIN(*dataPin.Port,dataPin.NUmber):CLEAR_PIN(*dataPin.Port,dataPin.NUmber);
        }
        else{
            (val & (1 << (7 - i)))?SET_PIN(*dataPin.Port,dataPin.NUmber):CLEAR_PIN(*dataPin.Port,dataPin.NUmber);
        }

        // make a clockpin go from high to low
        SET_PIN(*clockPin.Port,clockPin.NUmber);
        CLEAR_PIN(*clockPin.Port,clockPin.NUmber);
    }
}
void main(){
   //code ...
   // init section 
    Pin data = {&PORTB,4};
    Pin clock= {&PORTB,5};
    // make it output
    DDRB |=1<<4;
    DDRB |=1<<5;        
    //code ...
    while(1){
        // code ..
        // use when you need
        shiftOut(data,clock,LSBFIRST,9); // this will shift out 9(0b00001001) low bit will go first
        // code ...
    }   
        
}