Difference between revisions of "CSC270 Final Exam 2012"

From dftwiki3
Jump to: navigation, search
Line 118: Line 118:
  
 
The Arduino must be connected to a computer via the USB cable to get the appropriate power.  The computer does not need to have the arduino software installed for the Arduino to work.  The Arduino keeps its program in memory when it is powered down.
 
The Arduino must be connected to a computer via the USB cable to get the appropriate power.  The computer does not need to have the arduino software installed for the Arduino to work.  The Arduino keeps its program in memory when it is powered down.
 +
 +
=Misc.=
 +
 +
For completeness, the code for the receiving Arduino is shown below.
 +
 +
<br /><source lang="C">
 +
/*
 +
  Blink
 +
  Turns on an LED on for one second, then off for one second, repeatedly.
 +
 +
  This example code is in the public domain.
 +
*/
 +
 +
int STB    = 3;
 +
int ACK    = 4;
 +
int D0    = 5;
 +
int D1    = 6;
 +
int D2    = 7;
 +
int D3    = 8;
 +
int D4    = 9;
 +
int state  = -1;
 +
int  READY_TO_START = 1;
 +
 +
// takes the character ch and puts its 5 LSBs on
 +
// pins D0, D1, D2, D3, and D4.
 +
char receiveChar( ) {
 +
  char ch = 0x40;
 +
  int  pin = D0;
 +
  byte mask = 1;
 +
 +
  for ( int i=0; i<5; i++ ) {
 +
    if ( digitalRead( pin ) == HIGH ) 
 +
      ch = ch + mask;
 +
    mask  <<= 1;
 +
    pin += 1;
 +
  }
 +
 +
  return ch; 
 +
}
 +
 +
void setup() {               
 +
  // initialize the digital pin as an output.
 +
  // Pin 13 has an LED connected on most Arduino boards:
 +
  pinMode(13, OUTPUT); 
 +
  pinMode( ACK, OUTPUT ); // ack to kit
 +
  pinMode( STB, INPUT);  // strobe from kit 
 +
  pinMode( D0, INPUT );
 +
  pinMode( D1, INPUT );
 +
  pinMode( D2, INPUT );
 +
  pinMode( D3, INPUT );
 +
  pinMode( D4, INPUT );
 +
 +
  // set data communication speed
 +
  Serial.begin( 115200 );
 +
 
 +
  // set STB high
 +
  digitalWrite( ACK, HIGH );
 +
  state = READY_TO_START;
 +
 +
}
 +
 +
void loop() {
 +
  char ch;
 +
 
 +
  // if we haven't been initialized, wait...
 +
  if ( state != READY_TO_START )
 +
    return;
 +
 +
  // wait till STB == 0
 +
  while ( digitalRead( STB ) == HIGH )   
 +
    delay( 10 );  // wait a bit second
 +
 
 +
  // STB is low.  Read the data
 +
  ch = receiveChar();
 +
  //Serial.println( ch );
 +
 +
  // set ACK LOW
 +
  if ( ch=='A' )
 +
    digitalWrite( 13, HIGH ); // turn LED OFF
 +
  else
 +
    digitalWrite( 13, LOW );
 +
   
 +
  // set ACK low
 +
  digitalWrite( ACK, LOW );
 +
 
 +
  // wait for STOB to go high
 +
  while ( digitalRead( STB ) == LOW )
 +
    delay( 10 ); // wait
 +
   
 +
  // bring ACK back up
 +
  digitalWrite( ACK, HIGH );
 +
 +
  state = READY_TO_START;
 +
}
 +
 +
</source><br />
  
 
</onlydft>
 
</onlydft>

Revision as of 07:23, 26 April 2012

--D. Thiebaut 23:04, 25 April 2012 (EDT)



...