Show Posts
Pages: [1]
1  International / Software / Re: Comunicazione arduino pic tramite seriale on: July 20, 2012, 03:15:13 am
grazie mille per le risposte smiley....mi potreste x favore fare un esempio di codice che implementa il protocollo con i caratteri di sincronizzazione...ho difficoltà nell'implementare quanto mi avete detto...grazie ancora
2  International / Software / Re: Comunicazione arduino pic tramite seriale on: July 19, 2012, 02:39:02 am
figurati grazie lo stesso smiley...anche il baud rate per il Pic è fissato a 9600..scambiano correttamente informazioni ma non so ad esempio come far aspettare il pic...deve leggere i dati dopo che l'arduino li ha scritti
3  International / Software / Comunicazione arduino pic tramite seriale on: July 18, 2012, 03:16:24 pm
Salve a tutti, ho collegato un PIC16f887 ad un Arduino mega 2560 tramite seriale (uart),quindi usando solo i piedini Rx e Tx.Ho problemi con lo scambio d'informazioni tra i due; ho scritto il codice ma ho problemi con la sincronizzazione.
Le operazioni sono le seguenti:

1)Arduino......  invia 3 dati
2)Pic.............leggi i 3 dati
3)Pic.............invia 4 dati
4)Arduino......  ricevi 4 dati

ho provato in tanti modi mettendo istruzioni di controllo(if while) ma niente.....ho bisogno necessariamente di qualche segnale aggiuntivo?
Di seguito è postato il codice con qualche piccolo commento.
Ringrazio anticipatamente chi volesse aiutarmi, dandomi qualche suggerimento.

CODICE ARDUINO:
Code:
int id=3;
int letto=0;
unsigned int myInt[2]={4,2};; // 1 int 2 bytes
unsigned int k [4] = {0,2,3,4};
int buffer;
int volte=0;
byte LSB,MSB,LSB1,MSB1;
byte b[4]={0,0,0,0};
int c=0;
char buf[3];
void setup()
{
    Serial.begin(9600);
}


void encrypt (unsigned int* v, unsigned int* k) {
    unsigned int v0,v1, sum, i;
    unsigned int delta;
    unsigned int k0, k1, k2, k3;   /* chiave */
    delta=3;
    v0=v[0];
    v1=v[1];
    sum=0;
    k0=k[0];
    k1=k[1];
    k2=k[2];
    k3=k[3];
    for (i=0; i < 32; i++) {
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    }
    v[0]=v0;
    v[1]=v1;
}

void loop()
{
  //MANDO I 3 DATI AL PIC CIOE' id,myInt[0],myInt[1]
        Serial.println(id,DEC);
         delay(1000);
        Serial.println(myInt[0],DEC);
         delay(1000);
        Serial.println(myInt[1],DEC);
         encrypt(myInt,k);
          LSB = myInt[0] & 0xff;
          MSB = (myInt[0] >> 8) & 0xff;
          LSB1 = myInt[1] & 0xff;
          MSB1 = (myInt[1] >> 8) & 0xff;
         
       
//leggo 4 dati dal PIC         
 if (Serial.available())
    {     
               b[0]=Serial.read();
           delay(100);
                b[1]=Serial.read();
          delay(100);
              b[2]=Serial.read();
           delay(100);
             b[3]=Serial.read();
                delay(100);
          //se le codifiche sono uguali fai suonare un cicalino
          if ((LSB==b[0])&&(MSB==b[1])&& (LSB1==b[2])&&(MSB1==b[3]))
           {
         for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=2)
        {
         analogWrite(9, fadeValue);         
          delay(15);
        }               
           }
           }
}


CODICE PIC:

Code:
#include "built_in.h"
unsigned int myInt[2];
unsigned buffer[4];
unsigned int k [4] = {0,2,3,4};
int ident=0;

void encrypt (unsigned int* v, unsigned int* k) {
    unsigned int v0,v1, sum, i;
    unsigned int k0, k1, k2,k3;
    unsigned int delta;
    v0=v[0]; v1=v[1]; sum=0;
    delta=3;
    k0=k[0]; k1=k[1]; k2=k[2]; k3=k[3];
    for (i=0; i < 8; i++) {
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    }
    v[0]=v0;
    v[1]=v1;
}

void main()
{
      //LEGGE identificativo e dati mandati dall'arduino
     ident=UART1_Read();
     Delay_ms(100);
     myInt[0]=UART1_Read();
     Delay_ms(100);
     myInt[1]=UART1_Read();
     Delay_ms(100);

encrypt(myInt,k);
buffer[0] = Lo(myint[0]);
buffer[1] = Hi(myint[0]);
buffer[2] = Lo(myint[1]);
buffer[3] = Hi(myint[1]);
//mando 4 dati all'arduino
UART1_Write(buffer[0]);
Delay_ms(100);
UART1_Write(buffer[1]);
Delay_ms(100);
UART1_Write(buffer[2]);
Delay_ms(100);
UART1_Write(buffer[3]);
}
4  Using Arduino / Programming Questions / Re: Difference code arduino-Pic on: July 16, 2012, 09:02:26 am
I have write the following code for PIC:

Code:
unsigned long la;
void main()
{
  ANSEL  = 0;                     // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;                   // Disable comparators
  C2ON_bit = 0;
  la=123;
UART1_Init(9600);
delay_ms(100);
UART1_Write(la);
delay_ms(100);
}

I have tried to print on serial a number with three digits (la=123) and it works...for number with four digits (example la=1234) it doesn't work!!!
The problem is that the serial read and write only one byte to the time....how can i resolve this problem??thanks to all
5  Using Arduino / Programming Questions / Re: Difference code arduino-Pic on: July 16, 2012, 06:09:26 am
I have modified the code how you say paul but the result are still different smiley-sad

Arduino:
Code:
unsigned long myInt[2];
unsigned long k [4] = {0,2,3,4};

void setup()
{
    Serial.begin(9600);
}


void encrypt (unsigned long* v, unsigned long* k) {
    unsigned long v0,v1, sum, i;
    unsigned long delta;
    unsigned long k0, k1, k2, k3;   /* chiave */
    delta=0x9e3779b9;
    v0=v[0];
    v1=v[1];
    sum=0;
    k0=k[0];
    k1=k[1];
    k2=k[2];
    k3=k[3];
    for (i=0; i < 32; i++) {
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    }
    v[0]=v0;
    v[1]=v1;
}

void loop()
{
         
         myInt[0]=4;
         myInt[1]=1;
         Serial.println(myInt[0],DEC);
         Serial.println(myInt[1],DEC);
         encrypt(myInt,k);
         Serial.println(myInt[0],DEC);
         Serial.println(myInt[1],DEC);
         delay(1000);     
}
 


Pic 16f887:
Code:
unsigned long myInt[2];
unsigned long k [4] = {0,2,3,4};


void encrypt (unsigned long* v, unsigned long* k) {
    unsigned long v0,v1, sum, i;
    unsigned long k0, k1, k2,k3;
    unsigned long delta;
    v0=v[0]; v1=v[1]; sum=0;

    delta=0x9e3779b9;;
    k0=k[0]; k1=k[1]; k2=k[2]; k3=k[3];
    for (i=0; i < 32; i++) {
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    }
    v[0]=v0;
    v[1]=v1;
}

void main()
{
UART1_Init(9600);
myInt[0]=4;
myInt[1]=1;
UART1_Write(myInt[0]);
UART1_Write(myInt[1]);
encrypt(myInt,k);
UART1_Write(myInt[0]);
UART1_Write(myInt[1]);
delay_ms(1000);
}
6  Using Arduino / Programming Questions / Re: Difference code arduino-Pic on: July 16, 2012, 05:57:01 am
sorry you are right!!i have modified delta..now are the same

Thanks paul i will try with an array of two elements.
7  Using Arduino / Programming Questions / Re: Difference code arduino-Pic on: July 16, 2012, 05:37:21 am
sorry, the last version of codes are:

code for arduino:
Code:
unsigned long myInt[1];
unsigned long k [4] = {0,2,3,4};

void setup()
{
    Serial.begin(9600);
}


void encrypt (unsigned long* v, unsigned long* k) {
    unsigned long v0,v1, sum, i;
    unsigned long delta;
    unsigned long k0, k1, k2, k3;   /* chiave */
    delta=0x9e3779b9;
    v0=v[0];
    v1=v[1];
    sum=0;
    k0=k[0];
    k1=k[1];
    k2=k[2];
    k3=k[3];
    for (i=0; i < 32; i++) {
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    }
    v[0]=v0;
    v[1]=v1;
}

void loop()
{
          
         myInt[0]=4;
         Serial.println(myInt[0],DEC);
    
         encrypt(myInt,k);
         Serial.println(myInt[0],DEC);
         delay(1000);    
}



code for PIc
Code:
unsigned long myInt[1];
unsigned long k [4] = {0,2,3,4};


void encrypt (unsigned long* v, unsigned long* k) {
    unsigned long v0,v1, sum, i;
    unsigned long k0, k1, k2,k3;
    unsigned long delta;
    v0=v[0]; v1=v[1]; sum=0;

    delta=0x9e3779b9;;
    k0=k[0]; k1=k[1]; k2=k[2]; k3=k[3];
    for (i=0; i < 32; i++) {
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    }
    v[0]=v0;
    v[1]=v1;
}

void main()
{
UART1_Init(9600);
myInt[0]=4;
UART1_Write(myInt[0]);
encrypt(myInt,k);
UART1_Write(myInt[0]);
delay_ms(1000);
}
8  Using Arduino / Programming Questions / Re: Difference code arduino-Pic on: July 16, 2012, 05:31:30 am
thanks paul, but the pic now contains:
Code:
v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);

without //
9  Using Arduino / Programming Questions / Re: Difference code arduino-Pic on: July 16, 2012, 05:22:29 am
Thanks all for answers…you are very kind!
Sorry the algorithm is described here:  http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
I have defined delta and all other variables as unsigned long…but my outputs are still different
The serial output are:

Arduino:
4
1655960582

Mikroc:
4
218
10  Using Arduino / Programming Questions / Difference code arduino-Pic on: July 16, 2012, 04:39:22 am
Hi,I'm a problem with the code of TEA algorithm. I have implemented  the algorithm for arduino mega 2560 and for PIC16f887.

This my code for arduino,It is write with ide Arduino 1.0:

Code:
unsigned int myInt[1];
unsigned int k [4] = {0,2,3,4};

void setup()
{
    Serial.begin(9600);
}


void encrypt (unsigned int* v, unsigned int* k) {
    unsigned int v0,v1, sum, i;
    unsigned int delta;
    unsigned int k0, k1, k2, k3;   /* chiave */
    delta=0x9e3779b9;
    v0=v[0];
    v1=v[1];
    sum=0;
    k0=k[0];
    k1=k[1];
    k2=k[2];
    k3=k[3];
    for (i=0; i < 32; i++) {
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    }
    v[0]=v0;
    v[1]=v1;
}

void loop()
{
          
         myInt[0]=4;
         Serial.println(myInt[0],DEC);
    
         encrypt(myInt,k);
         Serial.println(myInt[0],DEC);
         delay(1000);    
}



This my code for PIC16F887,It is write with Mikro C pro for pic (version 1.65):
Code:
unsigned int myInt[1];
unsigned int k [4] = {0,2,3,4};


void encrypt (unsigned int* v, unsigned int* k) {
    unsigned int v0,v1, sum, i;
    unsigned int k0, k1, k2,k3;
    unsigned int delta;
    v0=v[0]; v1=v[1]; sum=0;

     delta=0x9e3779b9;
    k0=k[0]; k1=k[1]; k2=k[2]; k3=k[3];
    for (i=0; i < 32; i++) {
        sum += delta;
        v0 += ((v1<<4) + k0);// ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2);// ^ (v0 + sum) ^ ((v0>>5) + k3);
    }
    v[0]=v0;
    v[1]=v1;
}

void main()
{
UART1_Init(9600);
myInt[0]=4;
UART1_Write(myInt[0]);
encrypt(myInt,k);
UART1_Write(myInt[0]);
delay_ms(1000);
}


When i execute the codes i have two different coding of the same number...what is the difference?
Please can someone help me.... thanks a lot
Pages: [1]