Étape 3: Code d’essai
#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
unsigned short devoir [27] ;
unsigned char gIndex ;
Groupe d’unsigned char ;
#if(! USE_HARDWARE_SPI)
unsigned char SoftSpiTransfer (données unsigned char)
{
unsigned char cnt ;
unsigned char TVD ;
RST = 0 ;
pour (cnt = 0; cnt < 8; cnt ++)
{
RST << = 1 ;
Si (données & 0x80)digitalWrite(MOSI_PIN,HIGH) ;
d’autre digitalWrite(MOSI_PIN,LOW) ;
digitalWrite(SCK_PIN,HIGH) ;
digitalWrite(SCK_PIN,LOW) ;
la TVD | = digitalRead(MISO_PIN) ;
données << = 1 ;
}
retourner la TVD ;
}
#define SpiExchangeByte(x) SoftSpiTransfer(x)
#elif défini PCDUINO_IDE
#define SpiExchangeByte(x) SPI.transfer(x,SPI_CONTINUE)
#else
#define SpiExchangeByte(x) SPI.transfer(x)
#endif
Sub DeviceSynchronize(void)
{
SpiExchangeByte(0) ;
SpiExchangeByte(0) ;
SpiExchangeByte(0) ;
SpiExchangeByte(0) ;
SpiExchangeByte(0) ;
SpiExchangeByte(0) ;
SpiExchangeByte(0) ;
SpiExchangeByte(0) ;
}
void SendCmd (unsigned char cmd, indice d’unsigned char, unsigned short valeur)
{
unsigned char CheckSum ;
unsigned char valueL ;
unsigned char valueH ;
valueL = valeur & 0xff ;
valueH = ((valeur >> 8) & 0xff) ;
CheckSum = 0 x 87 ;
SpiExchangeByte(0x87) ;
SpiExchangeByte(cmd) ;
CheckSum = cmd ;
SpiExchangeByte (index); //index
Indice de checkSum de += ;
SpiExchangeByte(valueL) ;
Somme de contrôle += valueL ;
SpiExchangeByte(valueH) ;
Somme de contrôle += valueH ;
SpiExchangeByte (CheckSum); //CheckSum
}
void SetPwmOnOff (indice d’unsigned char, boolean sur)
{
SendCmd(CMD_SET_ON_OFF,index,on) ;
}
void SetPwmDuty (unsigned char index, devoir court non signé)
{
SendCmd(CMD_SET_DUTY,index,duty) ;
}
void SetPwmFrequency (unsigned char index, fréquence court non signé)
{
SendCmd(CMD_SET_FREQ,index,frequency) ;
}
void setup() {}
Mettez votre code de programme d’installation ici, pour exécuter une fois :
#if(USE_HARDWARE_SPI)
SPI.begin() ;
SPI.setBitOrder(MSBFIRST) ;
SPI.setDataMode(SPI_MODE) ;
SPI.setClockDivider(SPI_CLOCK_DIV2) ;
#endif
Groupe = 0 ;
gIndex = 0 ;
obligation [0] = 970 ;
obligation [1] = 860 ;
obligation [2] = 480 ;
obligation [3] = 470 ;
obligation [4] = 690 ;
obligation [5] = 730 ;
obligation [6] = MIN_DUTY ;
douane [7] = MIN_DUTY ;
obligation [8] = MIN_DUTY ;
obligation [9] = MIN_DUTY ;
obligation [10] = MIN_DUTY ;
obligation [11] = MIN_DUTY ;
obligation [12] = MIN_DUTY ;
obligation [13] = MIN_DUTY ;
obligation [14] = MIN_DUTY ;
obligation [15] = MIN_DUTY ;
obligation [16] = MIN_DUTY ;
obligation [17] = MIN_DUTY ;
obligation [18] = MIN_DUTY ;
obligation [19] = MIN_DUTY ;
obligation [20] = MIN_DUTY ;
obligation [21] = MIN_DUTY ;
obligation [22] = MIN_DUTY ;
obligation [23] = MIN_DUTY ;
service [24] = MIN_DUTY ;
obligation [25] = MIN_DUTY ;
obligation [26] = MIN_DUTY ;
DeviceSynchronize() ;
SetPwmDuty(0,970) ;
SetPwmDuty(1,860) ;
SetPwmDuty(2,480) ;
SetPwmDuty(3,470) ;
SetPwmDuty(4,690) ;
SetPwmDuty(5,730) ;
}
void loop() {}
Mettez votre code principal ici, pour exécuter à plusieurs reprises :
tmpChar char ;
scanf (« %c », & tmpChar) ;
Si ((tmpChar < « 8 ») & & (tmpChar > = « 0 »))
{
gIndex = tmpChar - « 0 » ;
SetPwmOnOff(gIndex+group,1) ;
}
ElseIf (tmpChar == « z »)
{
Groupe = 0 ;
}
ElseIf (tmpChar == « x »)
{
Groupe = 8 ;
}
ElseIf (tmpChar == « c »)
{
groupe = 16 ;
}
ElseIf (tmpChar == « v »)
{
Groupe = 24 ;
}
ElseIf (tmpChar == « w »)
{
Si (obligation [4] < MAX_DUTY) obligation [4] += 10 ;
SetPwmDuty(4,duty[4]) ;
}
ElseIf (tmpChar == de ')
{
Si (obligation [4] > MIN_DUTY) obligation [4]-= 10 ;
SetPwmDuty(4,duty[4]) ;
}
ElseIf (tmpChar == « a »)
{
Si (obligation [5] < MAX_DUTY) devoir [5] += 10 ;
SetPwmDuty(5,duty[5]) ;
}
ElseIf (tmpChar == a ')
{
Si (obligation [5] > MIN_DUTY) devoir [5]-= 10 ;
SetPwmDuty(5,duty[5]) ;
}
ElseIf (tmpChar == « i »)
{
Si (exonération [3] < MAX_DUTY) devoir [3] += 10 ;
SetPwmDuty(3,duty[3]) ;
}
ElseIf (tmpChar == « k »)
{
Si (exonération [3] > MIN_DUTY) devoir [3]-= 10 ;
SetPwmDuty(3,duty[3]) ;
}
ElseIf (tmpChar == ' o ')
{
Si (obligation [2] < MAX_DUTY) devoir [2] += 10 ;
SetPwmDuty(2,duty[2]) ;
}
ElseIf (tmpChar == « l »)
{
Si (obligation [2] > MIN_DUTY) devoir [2]-= 10 ;
SetPwmDuty(2,duty[2]) ;
}
ElseIf (tmpChar == « f »)
{
Si (obligation [1] < MAX_DUTY) devoir [1] += 10 ;
SetPwmDuty(1,duty[1]) ;
}
ElseIf (tmpChar == « h »)
{
Si (obligation [1] > MIN_DUTY) devoir [1]-= 10 ;
SetPwmDuty(1,duty[1]) ;
}
ElseIf (tmpChar ' t ==')
{
Si (obligation [0] < MAX_DUTY) devoir [0] += 10 ;
SetPwmDuty(0,duty[0]) ;
}
ElseIf (tmpChar == « g »)
{
Si (obligation [0] > MIN_DUTY) devoir [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]) ;
}