Paso 3: Código de prueba
#include < SPI.h >#include < string.h >
#include < stdio.h >
#define STATUS_PIN 9
#define CS_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13
#define CMD_SET_ON_OFF 0 x 01
#define CMD_SET_POLARITY 0 x 02
#define CMD_SET_FREQ 0 x 03
#define CMD_SET_DUTY 0 x 04
#define USE_HARDWARE_SPI 1
#define SPI_MODE SPI_MODE0
#define MIN_DUTY 250
#define MAX_DUTY 1250
en ciclo corto sin signo [27];
gIndex unsigned char;
Grupo char sin signo;
#if(! USE_HARDWARE_SPI)
unsigned char SoftSpiTransfer (datos char sin signo)
{
unsigned char cnt;
unsigned char rst;
RST = 0;
para (cnt = 0; < 8 cnt cnt ++)
{
RST << = 1;
Si (datos y 0x80)digitalWrite(MOSI_PIN,HIGH);
digitalWrite(MOSI_PIN,LOW) otra cosa;
digitalWrite(SCK_PIN,HIGH);
digitalWrite(SCK_PIN,LOW);
RST | = digitalRead(MISO_PIN);
datos << = 1;
}
volver primera;
}
#define SpiExchangeByte(x) SoftSpiTransfer(x)
#elif define PCDUINO_IDE
#define SpiExchangeByte(x) SPI.transfer(x,SPI_CONTINUE)
#else
#define SpiExchangeByte(x) SPI.transfer(x)
#endif
void DeviceSynchronize(void)
{
SpiExchangeByte(0);
SpiExchangeByte(0);
SpiExchangeByte(0);
SpiExchangeByte(0);
SpiExchangeByte(0);
SpiExchangeByte(0);
SpiExchangeByte(0);
SpiExchangeByte(0);
}
SendCmd vacío (cmd unsigned char, unsigned char índice, valor corto sin signo)
{
unsigned char CheckSum;
unsigned char valueL;
unsigned char valueH;
valueL = valor & 0xff;
valueH = ((valor >> 8) & 0xff);
Suma de comprobación = 0x87;
SpiExchangeByte(0x87);
SpiExchangeByte(cmd);
Suma += cmd;
SpiExchangeByte (índice), //index
Índice de suma +=;
SpiExchangeByte(valueL);
Suma += valueL;
SpiExchangeByte(valueH);
Suma += valueH;
SpiExchangeByte (CheckSum); //CheckSum
}
void SetPwmOnOff (índice de unsigned char, boolean en)
{
SendCmd(CMD_SET_ON_OFF,index,on);
}
void SetPwmDuty (unsigned char índice, en ciclo corto sin signo)
{
SendCmd(CMD_SET_DUTY,index,duty);
}
void SetPwmFrequency (unsigned char índice, frecuencia de corto sin signo)
{
SendCmd(CMD_SET_FREQ,index,frequency);
}
void setup() {}
Pon tu código de instalación, para ejecutar una vez:
#if(USE_HARDWARE_SPI)
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE);
SPI.setClockDivider(SPI_CLOCK_DIV2);
#endif
Grupo = 0;
gIndex = 0;
deber [0] = 970;
deber [1] = 860;
deber [2] = 480;
deber [3] = 470;
deber [4] = 690;
deber [5] = 730;
deber [6] = MIN_DUTY;
deber [7] = MIN_DUTY;
deber [8] = MIN_DUTY;
deber [9] = MIN_DUTY;
deber [10] = MIN_DUTY;
deber [11] = MIN_DUTY;
deber [12] = MIN_DUTY;
deber [13] = MIN_DUTY;
deber [14] = MIN_DUTY;
deber [15] = MIN_DUTY;
deber [16] = MIN_DUTY;
deber [17] = MIN_DUTY;
deber [18] = MIN_DUTY;
deber [19] = MIN_DUTY;
deber [20] = MIN_DUTY;
deber [21] = MIN_DUTY;
deber [22] = MIN_DUTY;
deber [23] = MIN_DUTY;
deber [24] = MIN_DUTY;
deber [25] = MIN_DUTY;
deber [26] = MIN_DUTY;
DeviceSynchronize();
SetPwmDuty(0,970);
SetPwmDuty(1,860);
SetPwmDuty(2,480);
SetPwmDuty(3,470);
SetPwmDuty(4,690);
SetPwmDuty(5,730);
}
void loop() {}
Pon tu código principal, para ejecutar varias veces:
char tmpChar;
scanf ("%c" & tmpChar);
Si ((tmpChar < '8') & & (tmpChar > = '0'))
{
gIndex = tmpChar - '0';
SetPwmOnOff(gIndex+group,1);
}
else if (tmpChar == 'z')
{
Grupo = 0;
}
else if (tmpChar == 'x')
{
Grupo = 8;
}
else if (tmpChar == 'c')
{
Grupo = 16;
}
else if (tmpChar == 'v')
{
Grupo = 24;
}
else if (tmpChar == 'w')
{
Si (duty [4] < MAX_DUTY) deber [4] += 10;
SetPwmDuty(4,duty[4]);
}
else if (tmpChar == de ')
{
Si (duty [4] > MIN_DUTY) deber [4]-= 10;
SetPwmDuty(4,duty[4]);
}
else if (tmpChar == 'a')
{
Si (deber [5] < MAX_DUTY) deber [5] += 10;
SetPwmDuty(5,duty[5]);
}
else if (tmpChar == había ')
{
Si (deber [5] > MIN_DUTY) deber [5]-= 10;
SetPwmDuty(5,duty[5]);
}
else if (tmpChar == 'i')
{
Si (deber [3] < MAX_DUTY) deber [3] += 10;
SetPwmDuty(3,duty[3]);
}
else if (tmpChar == 'k')
{
Si (deber [3] > MIN_DUTY) deber [3]-= 10;
SetPwmDuty(3,duty[3]);
}
else if (tmpChar == ' o ')
{
Si (deber [2] < MAX_DUTY) deber [2] += 10;
SetPwmDuty(2,duty[2]);
}
else if (tmpChar == 'l')
{
Si (deber [2] > MIN_DUTY) deber [2]-= 10;
SetPwmDuty(2,duty[2]);
}
else if (tmpChar == 'f')
{
Si (deber [1] < MAX_DUTY) deber [1] += 10;
SetPwmDuty(1,duty[1]);
}
else if (tmpChar == 'h')
{
Si (deber [1] > MIN_DUTY) deber [1]-= 10;
SetPwmDuty(1,duty[1]);
}
else if (tmpChar == ' t ')
{
Si (deber [0] < MAX_DUTY) deber [0] += 10;
SetPwmDuty(0,duty[0]);
}
else if (tmpChar == 'g')
{
Si (deber [0] > MIN_DUTY) deber [0]-= 10;
SetPwmDuty(0,duty[0]);
}
Si (tmpChar > = ' 0')printf("D0:%d-D1:%d-D2:%d-D3:%d-D4:%d-D5:%d-\n",duty[0],duty[1],duty[2],duty[3],duty[4],duty[5]);
}