main (1)

Download main (1)

If you can't read please download the document

Upload: nguyen-van-thuan

Post on 08-Nov-2015

216 views

Category:

Documents


3 download

DESCRIPTION

code MSP430

TRANSCRIPT

#include#include#include#include#include#define length 256#define maxAttempt 16#define true 1#define false 0#define Tprop_us 5 #define Tw 10 // v = 2.10^8 m/s// d = 1000 m// Tprop = d/v = 5 us // Tw = 2*Tpropint isBusy=false;int hasCollision=false; int x=0;FILE *f;void node(char *_name){int name;int count = length;int attempt=1;int seq1,seq2;int wait;name = atoi(_name);f=fopen("result.txt","wt");//kiem tra lien tuc kenh truyen khi ma ben phat dang co du lieuwhile(count){if(isBusy==false){seq1 = seq2 = x;//bat dau thuc hien truyen tung bit khi kenh khong banwhile(count){if(hasCollision==true) {count = length;hasCollision = false;printf("Station %d collision occured\n",name);fprintf(f,"Station %d collision occured\n",name);}//kiem tra kenh co bi xung dot hay khongif(seq1==seq2){//khong xung dotx++;seq1=x;count--;printf("Station %d transmitting bit %d of frame \n",name,length-count);fprintf(f,"Station %d transmitting bit %d of frame \n",name,length-count);usleep(Tprop_us);isBusy=true;seq2=x;}else{//xay ra xung dot trong qua trinh truyen//giai phong kenh truyenprintf("Station %d collision occured\n",name);fprintf(f,"Station %d collision occured\n",name);hasCollision=true;isBusy=false;//kiem tra dieu kien xem bien attempt co nho hon hoac bang maxAttempt hay khongif(attempt