Thursday, November 19, 2009

more serial code with some explanation about the arduino SPI register

/* Ports and Pins

Direct port access is much faster than digitalWrite.
You must match the correct port and pin as shown in the table below.

Arduino Pin Port Pin
13 (SCK) PORTB 5
12 (MISO) PORTB 4
11 (MOSI) PORTB 3
10 (SS) PORTB 2
9 PORTB 1
8 PORTB 0
7 PORTD 7
6 PORTD 6
5 PORTD 5
4 PORTD 4
3 PORTD 3
2 PORTD 2
1 (TX) PORTD 1
0 (RX) PORTD 0
A5 (Analog) PORTC 5
A4 (Analog) PORTC 4
A3 (Analog) PORTC 3
A2 (Analog) PORTC 2
A1 (Analog) PORTC 1
A0 (Analog) PORTC 0

*/

// Defines for use with Arduino functions
#define clockpin 13 // CL - clock IN
#define enablepin 10 // BL - enable IN
#define latchpin 9 // XL - latch IN
#define datapin 11 // SI - data IN

// Defines for direct port access
#define CLKPORT PORTB
#define ENAPORT PORTB
#define LATPORT PORTB
#define DATPORT PORTB
#define CLKPIN 5
#define ENAPIN 2
#define LATPIN 1
#define DATPIN 3

// Variables for communication
unsigned long SB_CommandPacket; //megabrite command packet
int SB_CommandMode; //megabrite command mode

int SB_BlueCommand; //megabrite blue command
int SB_RedCommand; //megabrite red command
int SB_GreenCommand; //megabrite green command

//megabrite - Pd to Arduino serial data helper variables
int enableState;
int enaState;

//serial input for LED addresses
int LED;

//serial string input
int pd_String[15];

// Define number of MegaBrite modules
#define NumLEDs 3 //50

// Create LED value storage array [no. of leds][RGB channels=3]
int LEDChannels[NumLEDs][3] = {0};

// Set pins to outputs and initial states
void setup() {
//set data, clock, latch, enable pins for digital output
pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);

//digital 0 (for latchpin) & digital 0 (for enablepin)
digitalWrite(latchpin, 0);
digitalWrite(enablepin, 0);

//SPCR is the Arduino SPI (Serial Peripheral Interface) Control Register
/*
The SPI control register (SPCR) has 8 bits, each of which control a particular SPI setting.

SPCR
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| SPIE | SPE | DORD | MSTR | CPOL | CPHA | SPR1 | SPR0 |

SPIE - Enables the SPI interrupt when 1
SPE - Enables the SPI when 1
DORD - Sends data least Significant Bit First when 1, most Significant Bit first when 0
MSTR - Sets the Arduino in master mode when 1, slave mode when 0
CPOL - Sets the data clock to be idle when high if set to 1, idle when low if set to 0
CPHA - Samples data on the falling edge of the data clock when 1, rising edge when 0
SPR1 and SPR0 - Sets the SPI speed, 00 is fastest (4MHz) 11 is slowest (250KHz)
*/

//so here, we first enable the arduino SPI by setting SPE=1
//second, arduino is set to be the master
//third - setting SPI speeds to be fastest
SPCR = (1<<<< 2 bits free ===> shifted to next LED in chain
|||||||||| |||||||||| |||||||||| || = 32 bits

<<>> bitwise right shift
& bitwise AND
| bitwise OR
*/

SPDR = SB_CommandMode<<6>>4;
while(!(SPSR & (1<>6;
while(!(SPSR & (1<>8;
while(!(SPSR & (1<<<< sb_commandmode =" B00;" i =" 0;" sb_redcommand="LEDChannels[i][0]" sb_greencommand="LEDChannels[i][1]" sb_bluecommand="LEDChannels[i][2]" sb_commandmode="B01;" write="" to="" current="" control="" registers="" for="" int="" z="0;">< i =" 0;" j1 =" 0;" enablestate =" digitalRead(enablepin);"> 0)
{
enaState = Serial.read(); // receive data from Pd
Serial.println(enaState);
//digitalWrite(enablepin, enaState); // turn the leds on/off

//parse incoming string
//parseString();

/*for(int i=0; i<9; i++)
{
pd_String[i] = Serial.read();
Serial.print(pd_String[i], BYTE);
}*/

}

setLedColor(0, enaState, 1023, 1023);
//setLedColor(0, 1023, 1023, 1023);
setLedColor(1, 0, 1023, 1023);
setLedColor(2, 300, 0, 1023);

}


No comments:

Post a Comment