SPI вопрос
- Войдите на сайт для отправки комментариев
Пт, 29/12/2017 - 22:02
Здравствуйте! Можете обьеснить почему в этом коде работают 1 и 3 2 и 4 регистры одновременно а не 1 и 2 2 и 4 как должно.
#include <TimerOne.h>
#include <string.h>
#define AXIS_X 1
#define AXIS_Y 2
#define AXIS_Z 3
//--- Pin connected to ST_CP of 74HC595
int latchPin = 10;
//--- Pin connected to SH_CP of 74HC595
int clockPin = 13;
//--- Pin connected to DS of 74HC595
int dataPin = 11;
//--- Used for faster latching
int latchPinPORTB = latchPin - 8;
//holds value for all the pins, [x][y][z]
byte cube[16][16];
//Counts through the layers
int current_layer = 0;
//--- This process is run by the timer and does the PWM control
void iProcess(){
//last layer store
int oldLayerBit = current_layer + 2;
//increment layer count
current_layer++;
if(current_layer >= 16){
current_layer = 0;
}
//--- Run through all the shift register values and send them (last one first)
// latching in the process
latchOff();
for (int i = 0 ; i < 2 ; i++){
spi_transfer(cube[current_layer][i]);
}
//Hide the old layer
digitalWrite(oldLayerBit, LOW);
//New data on the pins
latchOn();
//new layer high
digitalWrite(current_layer + 2, HIGH);
}
//--- Direct port access latching
void latchOn(){
bitSet(PORTB,latchPinPORTB);
}
void latchOff(){
bitClear(PORTB,latchPinPORTB);
}
//--- Used to setup SPI based on current pin setup
// this is called in the setup routine;
void setupSPI(){
byte clr;
SPCR |= ( (1<<SPE) | (1<<MSTR) ); // enable SPI as master
SPCR &= ~( (1<<SPR1) | (1<<SPR0) ); // clear prescaler bits
clr=SPSR; // clear SPI status reg
clr=SPDR; // clear SPI data reg
SPSR |= (1<<SPI2X); // set prescaler bits
delay(10);
}
//--- The really fast SPI version of shiftOut
byte spi_transfer(byte data)
{
SPDR = ~data; // Start the transmission
loop_until_bit_is_set(SPSR, SPIF);
return SPDR;
}
void setup() {
Serial.begin(9600);
//layer pins
for(int i = 2; i < 10; i++)
{
pinMode(i, OUTPUT);
}
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
digitalWrite(latchPin,LOW);
digitalWrite(dataPin,LOW);
digitalWrite(clockPin,LOW);
//--- Setup to run SPI
setupSPI();
//--- Activate the PWM timer
Timer1.initialize(100); // Timer for updating pwm pins
Timer1.attachInterrupt(iProcess);
}
void loop(){
int i,x,y,z;
while (true)
{
effect_planboing(AXIS_Y, 400);
}
}
// ==========================================================================================
// Effect functions
// ==========================================================================================
// Draw a plane on one axis and send it back and forth once.
void effect_planboing (int plane, int speedd)
{
int i;
for (i=0;i<16;i++)
{
fill(0x00);
setplane(plane, i);
delay_ms(speedd);
}
}
// ==========================================================================================
// Draw functions
// =========================================================================================
void setplane_y (int y)
{
int z;
if (y>=0 && y<16)
{
for (z=0;z<16;z++)
cube[z][y] = 0xff;
}
}
void setplane (char axis, unsigned char i)
{
switch (axis)
{
case AXIS_Y:
setplane_y(i);
break;
}
}
// Fill a value into all 64 byts of the cube buffer
// Mostly used for clearing. fill(0x00)
// or setting all on. fill(0xff)
void fill (unsigned char pattern)
{
int z;
int y;
for (z=0;z<16;z++)
{
for (y=0;y<16;y++)
{
cube[z][y] = pattern;
}
}
}
// Delay loop.
// This is not calibrated to milliseconds,
// but we had allready made to many effects using this
// calibration when we figured it might be a good idea
// to calibrate it.
void delay_ms(uint16_t x)
{
uint8_t y, z;
for ( ; x > 0 ; x--){
for ( y = 0 ; y < 90 ; y++){
for ( z = 0 ; z < 20 ; z++){
asm volatile ("nop");
}
}
}
}
Можете обьеснить почему в этом коде работают 1 и 3 2 и 4 регистры одновременно а не 1 и 2 2 и 4 как должно.
А Вы можете "обьеснить" Вашу проблему так, чтобы кто-то кроме Вас ещё понядл?
Что такое "1 и 2 2 и 4" и кому оно что должно?
У меня 4 регистра а это порядок их установки.
У меня 4 регистра а это порядок их установки.
Что "это"????
???
Впрочем, не хотите объяснять только свою проблему - не надо. Если это Вам не нужно, то уж мне-то ...
Если вы не знаете что значит порядок установки и номер в этом порядке то это уже ваша проблема. Я полагаю что здесь есть люди которые в этом разбираются.
Понятно.
Одна ошибка у Вас - это не моя проблема, а Ваша. У меня всё работает как надо и проблем нет.
можно у автора скетча спросить
Проблема в том, что взят готовый проект (вот этот https://pastebin.com/75ityP4E) и бездумно повырезаны куски кода, поменяны константы и число итераций в циклах. Думаете кто-то захочет копаться в этом коде.