Stap 3: Testen van Code
#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 0x01
#define CMD_SET_POLARITY 0x02
#define CMD_SET_FREQ 0x03
#define CMD_SET_DUTY 0x04
#define USE_HARDWARE_SPI 1
#define SPI_MODE SPI_MODE0
#define MIN_DUTY 250
#define MAX_DUTY 1250
niet-ondertekende korte plicht [27];
unsigned char gIndex;
unsigned char groep;
#if(! USE_HARDWARE_SPI)
unsigned char SoftSpiTransfer (unsigned char gegevens)
{
unsigned char cnt;
unsigned char rst;
RST = 0;
voor (cnt = 0; cnt < 8; cnt ++)
{
eerste << = 1;
Als (data & 0x80)digitalWrite(MOSI_PIN,HIGH);
anders digitalWrite(MOSI_PIN,LOW);
digitalWrite(SCK_PIN,HIGH);
digitalWrite(SCK_PIN,LOW);
RST | = digitalRead(MISO_PIN);
gegevens << = 1;
}
retourneren van eerste;
}
#define SpiExchangeByte(x) SoftSpiTransfer(x)
#elif gedefinieerd 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);
}
VOID SendCmd (unsigned char cmd, unsigned char index, unsigned short waarde)
{
unsigned char CheckSum;
unsigned char valueL;
unsigned char valueH;
valueL = waarde & 0xff;
valueH = ((waarde >> 8) & 0xff);
CheckSum = 0x87;
SpiExchangeByte(0x87);
SpiExchangeByte(cmd);
CheckSum += cmd;
SpiExchangeByte (index), //index
CheckSum += index;
SpiExchangeByte(valueL);
CheckSum += valueL;
SpiExchangeByte(valueH);
CheckSum += valueH;
SpiExchangeByte (controlesom); //CheckSum
}
VOID SetPwmOnOff (unsigned char index, boolean op)
{
SendCmd(CMD_SET_ON_OFF,index,on);
}
VOID SetPwmDuty (unsigned char index, niet-ondertekende korte plicht)
{
SendCmd(CMD_SET_DUTY,index,duty);
}
VOID SetPwmFrequency (unsigned char index, niet-ondertekende korte frequentie)
{
SendCmd(CMD_SET_FREQ,index,frequency);
}
VOID Setup {}
Zet uw setup-code hier, om het eenmalig uit te voeren:
#if(USE_HARDWARE_SPI)
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE);
SPI.setClockDivider(SPI_CLOCK_DIV2);
#endif
groep = 0;
gIndex = 0;
plicht [0] = 970;
plicht [1] = 860;
plicht [2] = 480;
plicht [3] = 470;
plicht [4] = 690;
plicht [5] = 730;
plicht [6] = MIN_DUTY;
plicht [7] = MIN_DUTY;
plicht [8] = MIN_DUTY;
plicht [9] = MIN_DUTY;
recht [10] = MIN_DUTY;
plicht [11] = MIN_DUTY;
plicht [12] = MIN_DUTY;
recht [13] = MIN_DUTY;
plicht [14] = MIN_DUTY;
plicht [15] = MIN_DUTY;
plicht [16] = MIN_DUTY;
plicht [17] = MIN_DUTY;
plicht [18] = MIN_DUTY;
plicht [19] = MIN_DUTY;
plicht [20] = MIN_DUTY;
plicht [21] = MIN_DUTY;
recht [22] = MIN_DUTY;
plicht [23] = MIN_DUTY;
plicht [24] = MIN_DUTY;
plicht [25] = MIN_DUTY;
plicht [26] = MIN_DUTY;
DeviceSynchronize();
SetPwmDuty(0,970);
SetPwmDuty(1,860);
SetPwmDuty(2,480);
SetPwmDuty(3,470);
SetPwmDuty(4,690);
SetPwmDuty(5,730);
}
void loop {}
Zet uw belangrijkste code hier, om het herhaaldelijk uitvoeren:
char tmpChar;
scanf ("%c", & tmpChar);
Als ((tmpChar < '8') & & (tmpChar > = '0'))
{
gIndex = tmpChar - '0';
SetPwmOnOff(gIndex+group,1);
}
else if (tmpChar == 'z')
{
groep = 0;
}
else if (tmpChar == 'x')
{
groep = 8;
}
else if (tmpChar == 'c')
{
groep = 16;
}
else if (tmpChar == 'v')
{
Groep = 24;
}
else if (tmpChar == 'w')
{
Als (plicht [4] < MAX_DUTY) plicht [4] += 10;
SetPwmDuty(4,duty[4]);
}
else if (tmpChar == de ')
{
Als (plicht [4] > MIN_DUTY) plicht [4]-= 10;
SetPwmDuty(4,duty[4]);
}
else if (tmpChar == 'a')
{
Als (plicht [5] < MAX_DUTY) plicht [5] += 10;
SetPwmDuty(5,duty[5]);
}
else if (tmpChar == had')
{
Als (plicht [5] > MIN_DUTY) plicht [5]-= 10;
SetPwmDuty(5,duty[5]);
}
else if (tmpChar == 'i')
{
Als (plicht [3] < MAX_DUTY) plicht [3] += 10;
SetPwmDuty(3,duty[3]);
}
else if (tmpChar == 'k')
{
Als (plicht [3] > MIN_DUTY) plicht [3]-= 10;
SetPwmDuty(3,duty[3]);
}
else if (tmpChar == ' o ')
{
Als (plicht [2] < MAX_DUTY) plicht [2] += 10;
SetPwmDuty(2,duty[2]);
}
else if (tmpChar == 'l')
{
Als (plicht [2] > MIN_DUTY) plicht [2]-= 10;
SetPwmDuty(2,duty[2]);
}
else if (tmpChar == 'f')
{
Als (plicht [1] < MAX_DUTY) plicht [1] += 10;
SetPwmDuty(1,duty[1]);
}
else if (tmpChar == 'h')
{
Als (plicht [1] > MIN_DUTY) plicht [1]-= 10;
SetPwmDuty(1,duty[1]);
}
else if (tmpChar == 't')
{
Als (plicht [0] < MAX_DUTY) plicht [0] += 10;
SetPwmDuty(0,duty[0]);
}
else if (tmpChar == 'g')
{
Als (plicht [0] > MIN_DUTY) plicht [0]-= 10;
SetPwmDuty(0,duty[0]);
}
Als (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]);
}