Compare commits

...

6 Commits

Author SHA1 Message Date
EmaMaker 52d3fac821
Add files via upload 2019-01-27 21:57:31 +01:00
EmaMaker 143da9ed0a
Add files via upload 2019-01-01 16:32:15 +01:00
EmaMaker 28819f62d0
Update README.md 2018-12-28 14:22:39 +01:00
EmaMaker 15658c9d3c
Created arduino branch
Added interrupt test, spi example
2018-12-28 14:21:07 +01:00
EmaMaker b115c0dfd5
Update README.md 2018-12-28 14:18:15 +01:00
EmaMaker a8323fde5b A 2018-12-27 21:55:23 +01:00
9 changed files with 309 additions and 54 deletions

View File

@ -1,4 +1 @@
Code Samples
Code in this branch are useful in every language and can be easily translated in other languages.
Just read the concept, not the code
Codes here are only used in Arduino-C language, with arduino specific codes (interrupts, spi, etc...)

View File

@ -0,0 +1,43 @@
// Written by Nick Gammon
// February 2011
#include <SPI.h>
void setup (void)
{
//10 is the SS pin. Not the Gestapo one
Serial.begin(9600);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH); // ensure 10 stays high for now
// Put SCK, MOSI, 10 pins into output mode
// also put SCK, MOSI into LOW state, and 10 into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin ();
// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);
} // end of setup
void loop (void)
{
char c;
// enable Slave Select
digitalWrite(10, LOW); // 10 is pin 10
// send test string
for (const char * p = "Hello, world!\n" ; c = *p; p++){
Serial.print(c);
SPI.transfer (c);
}
// disable Slave Select
digitalWrite(10, HIGH);
delay (1000); // 1 seconds delay
} // end of loop

57
SPI example/spi_slave.ino Normal file
View File

@ -0,0 +1,57 @@
// Written by Nick Gammon
// February 2011
#include <SPI.h>
char buf [100];
volatile byte pos;
volatile boolean process_it;
void setup (void)
{
Serial.begin (115200); // debugging
// turn on SPI in slave mode
SPCR |= bit (SPE);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// get ready for an interrupt
pos = 0; // buffer empty
process_it = false;
// now turn on interrupts
SPI.attachInterrupt();
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR; // grab byte from SPI Data Register
// add to buffer if room
if (pos < (sizeof (buf) - 1))
buf [pos++] = c;
// example: newline means time to process buffer
if (c == '\n')
process_it = true;
} // end of interrupt routine SPI_STC_vect
// main loop - wait for flag set in interrupt routine
void loop (void)
{
if (process_it)
{
buf [pos] = 0;
Serial.println (buf);
pos = 0;
process_it = false;
} // end of flag set
} // end of loop

View File

@ -0,0 +1,22 @@
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); // RX, TX
/**THIS EXAMPLE HAS BEEN SET UP AS MASTER-SLAVE COMMUNICATION, BUT USING mySerial.listen() and mySerial.send() BOTH ARDUINO'S CAN SEND DATA TO THE OTHER**/
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
Serial.println("Master arduino ready to send!");
// set the data rate for the SoftwareSerial port
mySerial.begin(57600);
}
void loop() {
mySerial.write('A'); //Write the serial data: A is corrispondent for 65 (ASCII)
delay(1); //bit of delay for the data to be processed
}

View File

@ -0,0 +1,22 @@
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); // RX, TX
/**THIS EXAMPLE HAS BEEN SET UP AS MASTER-SLAVE COMMUNICATION, BUT USING mySerial.listen() and mySerial.send() BOTH ARDUINO'S CAN SEND DATA TO THE OTHER**/
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
Serial.println("Slave Arduino ready to receive!");
// set the data rate for the SoftwareSerial port
mySerial.begin(57600);
}
void loop() {
//prints what the master sent, if data is available
if(mySerial.available() > 0){
Serial.println(mySerial.read());
}
}

View File

@ -1,50 +0,0 @@
'''
PYTHON CODE BRUTEFORCES WORDS FROM GIVEN CHAR LIST.
ENDLESS WORD LIST CAN BE GENERATED, UNTIL PROGRAM STOPPED
EVERY 100 WORDS GENERATED THEY ARE SAVED ON FILE
'''
lPossibleChars = ("a", "b", "c", "d", "e", "f")
sWord = ""
sFileName = "words.txt"
lGenWords = []
dWordsForSave = 100
def bruteforce(index):
global sWord, lPossibleChars
#generates only if the index to change is less than currrent word length
if index < len(sWord):
for i in range(len(lPossibleChars)):
#makes the word a list
lWord = list(sWord)
#changes the index
lWord[index] = lPossibleChars[i]
#makes the list back a word
sWord = ''.join(lWord)
print(sWord)
#adds to the list
lGenWords.append(sWord)
#goes on for the next index
bruteforce(index + 1)
while True:
#saves word on file (append mode) and cleares the list
if len(lGenWords) >= dWordsForSave:
f = open(sFileName, "a")
for s in lGenWords:
f.write(s + "\n")
lGenWords[:] = []
f.close()
#starts bruteforcing the word and adds a new character when ended
if len(sWord) > 0:
bruteforce(0)
sWord += lPossibleChars[0]

View File

@ -0,0 +1,89 @@
// Written by Nick Gammon
// February 2011
#include <SPI.h>
#define DISTANCE 0b00001111
#define DEGREES 0b00001010
#define SPI_DELAY 150
#define SS 10
SPISettings settings(100000, MSBFIRST, SPI_MODE0);
byte mess;
void setup (void)
{
//10 is the SS pin. Not the Gestapo one
Serial.begin(9600);
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH); // ensure 10 stays high for now
// Put SCK, MOSI, 10 pins into output mode
// also put SCK, MOSI into LOW state, and 10 into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin ();
// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);
} // end of setup
void loop (void) {
/**THIS PART COULD BE REFACTORED INTO A NICER THING, BUT IT'S JUST FOR EXAMPLE**/
Serial.println();
Serial.print("Sending byte: ");
Serial.println(DISTANCE);
//Sends a byte to the slave. The slave now prepares the response byte (The slave knows what to do)
SPI.beginTransaction(settings);
digitalWrite(SS, LOW);
SPI.transfer(DISTANCE);
digitalWrite(SS, HIGH);
SPI.endTransaction();
delay(SPI_DELAY);
//Sends a byte to get the response that the slave has prepared
Serial.print("Now getting response for previously asked byte: ");
SPI.beginTransaction(settings);
digitalWrite(SS, LOW);
mess = SPI.transfer(0);
digitalWrite(SS, HIGH);
SPI.endTransaction();
Serial.println(mess);
delay(SPI_DELAY);
//Does the same thing but with different bytes
Serial.println();
Serial.print("Sending byte: ");
Serial.println(DEGREES);
SPI.beginTransaction(settings);
digitalWrite(SS, LOW);
SPI.transfer(DEGREES);
digitalWrite(SS, HIGH);
SPI.endTransaction();
delay(SPI_DELAY);
Serial.print("Now getting response for previously asked byte: ");
SPI.beginTransaction(settings);
digitalWrite(SS, LOW);
mess = SPI.transfer(0);
digitalWrite(SS, HIGH);
SPI.endTransaction();
Serial.println(mess);
delay(SPI_DELAY);
}

View File

@ -0,0 +1,56 @@
#include <SPI.h>
#define DISTANCE 0b00001111
#define DEGREES 0b00001010
byte c;
bool bSending = false;
byte bSendingByte;
void setup() {
Serial.begin(9600);
SPCR |= bit (SPE);
pinMode(MISO, OUTPUT);
SPI.attachInterrupt();
}
ISR(SPI_STC_vect ) {
//the bool defines whether the slave is sending or not.
//It's assumed by both master and slave that the first communication is from master to slave
if(bSending){
//sends the previously prepared byte to the master
SPCR = 64 & 127; // disble interrupt
SPDR= bSendingByte;
SPCR = 64 |128; // enable interrupt
Serial.print("Sending byte: ");
Serial.println(bSendingByte);
}else{
//prepares output for next time it's asked, based on the received byte
switch(SPDR){
case DISTANCE:
bSendingByte = 0b00111111;
break;
case DEGREES:
bSendingByte = 0b11100011;
break;
}
Serial.println();
Serial.print("Received byte: ");
Serial.print(SPDR);
Serial.println(" - Preparing response byte");
//sets SPDR (SPI byte) to be send next time
SPDR = bSendingByte;
}
//Negates bSesnding. If the slave has received, next time he will send and viceversa
bSending = !bSending;
}
void loop() {
}

19
test_interrupt.ino Normal file
View File

@ -0,0 +1,19 @@
#define BTN 5
void setup() {
attachInterrupt(BTN, testInterrupt, RISING);
Serial.begin(9600);
}
void loop() {
//Serial.println(digitalRead(5));
Serial.println("ciao");
delay(1000);
}
void testInterrupt(){
for(int i = 0; i < 10; i++){
Serial.println("TEST INTERRUPT");
}
}