Hi all,
First of all my problem explanation is too long because i want to become clear. Sorry for that anyway. If you be patient and read untill the end of the problem i am very grateful.
I want to implement MIL-STD-1553 protocol with two Arduino Leonardo's for my graduation project. So, i completed transmitter.(There is much more slower than protocol requirements but this is not too important for now.) In receiver, it must be distinguish two signals (Command word and Status word) which they have beginning different synch bits. After that, it implement required algorithm to decode the manchester encoded data.
PS: Since transmitter's 1 bit is 200 usec (in my working condition), 1.5 bit = 1.5*200 = 300 usec(1.5 bit where first synch bit or 500 usec depend on the Command Word-Status Word and last bit of the previous signal).
Command Word: 1.5 bit high synch + 1.5 bit low synch+ 16 bit manchester encoded data + 1 manchester encoded odd parity bit
Status Word: 1.5 bit low synch+ 1.5 bit high synch+ 16 bit manchester encoded data + 1 manchester encoded odd parity bit
Transmitter algorithm;
#include <Manchester.h>
/*
Manchester Transmitter example
In this example transmitter will send one 16 bit number per transmittion
try different speeds using this constants, your maximum possible speed will
depend on various factors like transmitter type, distance, microcontroller speed, ...
MAN_300 0
MAN_600 1
MAN_1200 2
MAN_2400 3
MAN_4800 4
MAN_9600 5
MAN_19200 6
MAN_38400 7
*/
#define TX_PIN 3 //pin where your transmitter is connected
#define LED_PIN 13 //pin for blinking LED
uint8_t moo = 1; //last led status
uint16_t transmit_data = 0b1100010101000100;
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, moo);
//man.workAround1MhzTinyCore(); //add this in order for transmitter to work with 1Mhz Attiny85/84
man.setupTransmit(TX_PIN, MAN_4800);
}
void loop() {
man.transmit(transmit_data);
//transmit_data++;
moo++;
digitalWrite(LED_PIN, moo%2==0 ? LOW:HIGH);
}
Receiver algorithm;
#include <Manchester.h>
volatile int pwm_value_falling = 0;
volatile int pwm_value_rising = 0;
volatile int prev_time1 = 0;
volatile int prev_time2 = 0;
volatile int cworsw = 0;
volatile int f = 0;
volatile int u = 0;
volatile int z = 0;
volatile int r = 0;
volatile int i = 0;
volatile int m[17] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void setup() {
Serial.begin(4800);
// when pin D2 goes high, call the rising function
attachInterrupt(digitalPinToInterrupt(3), rising, RISING);
}
void loop() {
}
void rising() {
prev_time2 = micros();
pwm_value_falling = micros()-prev_time1;
if (pwm_value_falling<230){
pwm_value_falling = 200;
}else if(270<=pwm_value_falling && pwm_value_falling<340){
pwm_value_falling = 300;
}else if(340<=pwm_value_falling && pwm_value_falling<450){
pwm_value_falling = 400;
}else if(470<=pwm_value_falling){
pwm_value_falling = 500;
}
if ((pwm_value_falling == 500 || pwm_value_falling ==300)&&(r==1)){
f=1;
z = 0;
}else if ((pwm_value_falling == 500 || pwm_value_falling ==300)&&(z==1)){
u=1;
}
if ((f==1)&& (pwm_value_falling == 500)){
m[i] = 1;
i++;
}else if((f==1)&&(pwm_value_falling == 400)){
if (m[i-1]==0){
m[i] = 1;
i++;
}else if (m[i-1]==1){
m[i] = 0;
i++;}
}
if((z==1)&&(pwm_value_falling == 400)){
if (m[i-1]==0){
m[i] = 0;
i++;
}else if (m[i-1]==1){
m[i] = 1;
i++;}
}else if((z==1)&&(pwm_value_falling == 200)){
if (m[i-1]==0){
m[i] = 1;
i++;
}else if (m[i-1]==1){
m[i] = 0;
i++;}
}
//Serial.println(pwm_value_falling);
if(i == 17){
i = 0;
f = 0;
r = 0;
z =0;
u=0;
cworsw = 0;
for (int k = 0; k<17; k++){
Serial.print(m[k]);
}
Serial.println();
volatile int m[17] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
}
attachInterrupt(digitalPinToInterrupt(3), falling, FALLING);
}
void falling() {
prev_time1 = micros();
pwm_value_rising = micros()-prev_time2;
if (pwm_value_rising<230){
pwm_value_rising = 200;
}else if(270<=pwm_value_rising && pwm_value_rising<340){
pwm_value_rising = 300;
}else if(340<=pwm_value_rising && pwm_value_rising<450){
pwm_value_rising = 400;
}else if(470<=pwm_value_rising){
pwm_value_rising = 500;
}
if ((pwm_value_rising == 500 || pwm_value_rising ==300)&&(u==1)){
z=1;
f = 0;
}else if ((pwm_value_rising == 500 || pwm_value_rising ==300)&&(f==0)){
r=1;}
if((f==1)&&(pwm_value_rising == 400)){
if (m[i-1]==0){
m[i] = 1;
i++;
}else if (m[i-1]==1){
m[i] = 0;
i++;}
}else if((f==1)&&(pwm_value_rising == 200)){
if (m[i-1]==0){
m[i] = 0;
i++;
}else if (m[i-1]==1){
m[i] = 1;
i++;}
}
if ((z==1) && (pwm_value_rising == 500)){
m[i] = 0;
i++;
}else if((z==1)&&(pwm_value_rising == 400)){
if (m[i-1]==0){
m[i] = 0;
i++;
}else if (m[i-1]==1){
m[i] = 1;
i++;}
}
//Serial.println(pwm_value_rising);
if(i == 17){
i = 0;
f = 0;
r = 0;
z = 0;
u=0;
cworsw = 0;
for (int k = 0; k<17; k++){
Serial.print(m[k]);
}
Serial.println();
volatile int m[17] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
}
attachInterrupt(digitalPinToInterrupt(3), rising, RISING);
}
And also i used two Manchester library by GitHub (originals; GitHub - mchr3k/arduino-libs-manchester) (i change one of them for create and add the synch bits and parity bit and it works well.-i checked with using oscilloscope. Transmitted signal as same as like i write in beginning for Status Word and Command Word) Also one row was added to work this code on Arduino Leonardo. Anyway, this is the last version which i used;
PS: In order to send Status Word,
digitalWrite(TxPin,LOW);
delayMicroseconds(1.5*delay1);
digitalWrite(TxPin,HIGH);
delayMicroseconds(1.5*delay2);
To send Command Word, change two line in here (these lines are create synch bits)
digitalWrite(TxPin,HIGH);
delayMicroseconds(1.5*delay1);
digitalWrite(TxPin,LOW);
delayMicroseconds(1.5*delay2);
The problem is receiver can decode and print the Command Word data but it can not decode Status Word data. I want to decode both Command Word and Status Word. I can not solve this problem. Please help me. If anyone can solve this problem, i am very appreciate. Thank you a lot.
Best regards,
kycn