📄 write_bitfield.m
字号:
function dummy = write_bitfield(nn,index,data)
% Private. Bitfield write.
% Copyright 2002 The MathWorks, Inc.
% $Revision: 1.4 $ $Date: 2002/05/14 19:46:57 $
error(nargchk(2,3,nargin));
if ~ishandle(nn),
error('First Parameter must be a BITFIELD handle.');
end
if nargin==2,
data = index;
end;
% Is data a valid input
if ~isnumeric(data),
error('DATA must be a numeric array or hexidecimal strings (cell array) ');
end
% Reshape value according to ARRAYORDER property
if strcmp(nn.arrayorder,'row-major'),
data = reshape(permute(data,[2 1]),1,[]);
else
data = reshape(data,1,[]);
end
% Data is numeric format
% Do we have sufficent values ?
nvalues = numel(data);
nvaldef = prod(nn.size);
if nvalues < nvaldef,
% warning('DATA has less elements than specified numeric array, DATA will only be applied to beginning of memory area');
elseif nvalues > nvaldef,
% warning('DATA has more elements than specified numeric array, DATA will be be limited to defined memory area');
data = data(1:nvaldef); % Truncate!
end
% Convert numeric values into array of AUs
% where each column is the the AUs necessary for one numeric value
% The data will be little-endian (swapped later if necessary)
% thus cc.write([1 256]), where storageunitspervalue = 3, represent = 'unsigned', bitsperstorageunit = 8
% fdata =
% 1 0
% 0 1
% 0 0
switch (nn.represent)
case {'unsigned','ufract'},
if strcmp(nn.represent,'ufract'), % Adjust binary point
data = double(data).*2^(nn.binarypt);
else
data = round(double(data));
end
maxval = 2^nn.length-1;
minval = 0;
if any( maxval < data),
warning('Overflow: Saturation was required to fit into the specified numeric range');
[maxdat,inx] = max(data);
while maxdat > maxval,
data(inx) = maxval;
[maxdat,inx] = max(data);
end
end
if any(minval > data),
warning('Undeflow: Saturation was required to fit into the specified numeric range');
[mindat,inx] = min(data);
while mindat < minval,
data(inx) = minval;
[mindat,inx] = min(data);
end
end
data = data*2^(nn.prepad + nn.offset); % scale data by prepend to shift bits within word
bscaler = nn.bitsperstorageunit*(0:nn.storageunitspervalue-1);
uidata = [];
for val = data, % Do conversion
uidata = horzcat(uidata,mod(fix(val./2.^bscaler),2^nn.bitsperstorageunit)');
end
if strcmp( nn.endianness,'big') & (nn.storageunitspervalue > 1),
uidata = flipud(uidata);
end
case {'signed','fract'}, % 2's Complement
if strcmp(nn.represent,'fract'), % Adjust binary point
data = double(data).*2^(nn.binarypt);
else
data = round(double(data));
end
minval = -1*2^(nn.length-1);
maxval = abs(minval)-1;
if any( maxval < data),
warning('Overflow: Saturation was required to fit into the specified numeric range');
[maxdat,inx] = max(data);
while maxdat > maxval,
data(inx) = maxval;
[maxdat,inx] = max(data);
end
end
if any(minval > data),
warning('Undeflow: Saturation was required to fit into the specified numeric range');
[mindat,inx] = min(data);
while mindat < minval,
data(inx) = minval;
[mindat,inx] = min(data);
end
end
negEs = (0 > data)*2^nn.length;
data = data + negEs;
data = data*2^(nn.prepad+nn.offset); % scale data by prepend to shift bits within word
bscaler = nn.bitsperstorageunit*(0:nn.storageunitspervalue-1);
uidata = [];
for val = data, % Do conversion
uidata = horzcat(uidata,mod(fix(val./2.^bscaler),2^nn.bitsperstorageunit)');
end
% Endianness swap (if necessary), Big endian only
if strcmp( nn.endianness,'big') & (nn.storageunitspervalue > 1),
uidata = flipud(uidata);
end
case 'float',
error(['A bitfield cannot be a represented as a float.']);
otherwise
error(['Unexpected numeric representation: ''' nn.represent]);
end
% Call base class (memoryobj) to get unformatted data
memdata = read_memoryobj(nn);
memdata = reshape(double(memdata),nn.storageunitspervalue,[]);
% temp = dec2bin(memdata);
base = dec2bin( bitshift((2^nn.length-1), ...
nn.prepad+nn.offset,nn.prepad+nn.offset+nn.length), nn.wordsize);
baseinv = dec2bin( (2^nn.wordsize-1) - bin2dec(base) );
% base = bin2dec(flipud(reshape( base,[],nn.storageunitspervalue)'));
baseinv = bin2dec(flipud(reshape(baseinv,[],nn.storageunitspervalue)'));
if strcmp( nn.endianness,'big') & (nn.storageunitspervalue > 1),
base = flipud(base);
baseinv = flipud(baseinv);
end
memdata = bitand(memdata,baseinv); % zero out bit field only
uidata = bitor(memdata,uidata);
% Write raw values to memory
if nargin == 2,
write_memoryobj(nn,uidata);
else % timeout % index version - one value only!
write_memoryobj(nn,(index-1).*nn.storageunitspervalue+1,uidata);
end
% [EOF] write_numeric.m
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -