%Scott Mueller
%3/10/09
%Tx.m
%Description:  This is passed a single argument PWM, a us servo pulsewidth
%Which is outputted as 2 eight bit numbers to the serial port
%response time can be improved by passing the serial port object as an
%arguement to the function and opening it only once at the beginning of the
%main program.  In this case fclose() and delete() would then be called at
%the end of the main program.

function void = Tx(PWM)

     delete(instrfind); 
     s = serial('COM4','BAUD',9600); % Create serial object (PORT Dependent)
     fopen(s);  % Open the serial port for r/w
     TopBits = bitshift (PWM, -8); %top 8 bits of PWM
     BottomBits = bitand(PWM, 255); %bottom 8 bits of PWM

     fwrite(s,TopBits);
     %fprintf(fscanf(s))                      % Read Data back from PIC
     fwrite(s,BottomBits);
     %fprintf(fscanf(s))                      % Read Data back from PIC

     fclose(s);                                  % Close the serial port
     delete(s);                                  % Delete the serial object

end