BitOperation.cs

本文介绍了一个C#中的位操作类,提供了设置和获取字节中特定比特位的方法。通过具体的位掩码操作实现了对字节内比特位的精确控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Text;

namespace Bit
{
    /*class methods for bits operation*/
    public class BitOperation
    {
        public Byte setBit(Byte targetByte, int targetPos, int value)
        {
            if ((value == 0 || value == 1) && (targetPos >= 0 && targetPos < 8))
            {
                int targetPosValue = -1;
                switch (targetPos)
                {
                    case 0:
                        targetPosValue = (targetByte >> 7) & 0x01;
                        if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                        {
                            break;
                        }
                        else
                        {
                            if (value == 1)
                            {
                                targetByte = Convert.ToByte(targetByte | 0x80);
                            }
                            else if (value == 0)
                            {
                                targetByte = Convert.ToByte(targetByte & 0x7f);
                            }
                            break;
                        }
                    case 1:
                        targetPosValue = (targetByte >> 6) & 0x01;
                        if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                        {
                            break;
                        }
                        else
                        {
                            if (value == 1)
                            {
                                targetByte = Convert.ToByte(targetByte | 0x40);
                            }
                            else if (value == 0)
                            {
                                targetByte = Convert.ToByte(targetByte & 0xbf);
                            }
                            break;
                        }
                    case 2:
                        targetPosValue = (targetByte >> 5) & 0x01;
                        if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                        {
                            break;
                        }
                        else
                        {
                            if (value == 1)
                            {
                                targetByte = Convert.ToByte(targetByte | 0x20);
                            }
                            else if (value == 0)
                            {
                                targetByte = Convert.ToByte(targetByte & 0xdf);
                            }
                            break;
                        }
                    case 3:
                        targetPosValue = (targetByte >> 4) & 0x01;
                        if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                        {
                            break;
                        }
                        else
                        {
                            if (value == 1)
                            {
                                targetByte = Convert.ToByte(targetByte | 0x10);
                            }
                            else if (value == 0)
                            {
                                targetByte = Convert.ToByte(targetByte & 0xef);
                            }
                            break;
                        }
                    case 4:
                        targetPosValue = (targetByte >> 3) & 0x01;
                        if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                        {
                            break;
                        }
                        else
                        {
                            if (value == 1)
                            {
                                targetByte = Convert.ToByte(targetByte | 0x08);
                            }
                            else if (value == 0)
                            {
                                targetByte = Convert.ToByte(targetByte & 0xf7);
                            }
                            break;
                        }
                    case 5:
                        targetPosValue = (targetByte >> 2) & 0x01;
                        if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                        {
                            break;
                        }
                        else
                        {
                            if (value == 1)
                            {
                                targetByte = Convert.ToByte(targetByte | 0x04);
                            }
                            else if (value == 0)
                            {
                                targetByte = Convert.ToByte(targetByte & 0xfb);
                            }
                            break;
                        }
                    case 6:
                        targetPosValue = (targetByte >> 1) & 0x01;
                        if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                        {
                            break;
                        }
                        else
                        {
                            if (value == 1)
                            {
                                targetByte = Convert.ToByte(targetByte | 0x02);
                            }
                            else if (value == 0)
                            {
                                targetByte = Convert.ToByte(targetByte & 0xfd);
                            }
                            break;
                        }
                    case 7:
                        targetPosValue = targetByte & 0x01;
                        if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                        {
                            break;
                        }
                        else
                        {
                            if (value == 1)
                            {
                                targetByte = Convert.ToByte(targetByte | 0x01);
                            }
                            else if (value == 0)
                            {
                                targetByte = Convert.ToByte(targetByte & 0xfe);
                            }
                            break;
                        }
                    default:
                        break;
                }
                return targetByte;
            }
            else
            {
                return 0;
            }
        }

        public int getBit(Byte targetByte, int targetPos)
        {
            int value = -1;

            switch (targetPos)
            {
                case 0:
                    value = (targetByte >> 7) & 0x01;
                    break;
                case 1:
                    value = (targetByte >> 6) & 0x01;
                    break;
                case 2:
                    value = (targetByte >> 5) & 0x01;
                    break;
                case 3:
                    value = (targetByte >> 4) & 0x01;
                    break;
                case 4:
                    value = (targetByte >> 3) & 0x01;
                    break;
                case 5:
                    value = (targetByte >> 2) & 0x01;
                    break;
                case 6:
                    value = (targetByte >> 1) & 0x01;
                    break;
                case 7:
                    value = targetByte & 0x01;
                    break;
                default:
                    break;
            }
            return value;
        }
    }
}
请翻译:The CS_ADC pin of the sensor selects the ADC for SPI communication. When CS_ADC is high, the ADC is in stand-by mode, and communications with the EEPROM are possible. When CS_ADC is low, the ADC is enabled. CS_EE and CS_ADC must never be simultaneously low. The ADC interface operates in SPI mode 1 where CPOL = 0 and CPHA = 1. The ADC has four configuration registers. Three registers are ‘reserved’ and must be set to the default values contained in EEPROM. These registers contain setup values that are specific to the pressure sense element, and should not be changed. Configuration register 1 toggles the ADC between pressure and temperature readings and controls the data rate of the ADC. To program a configuration register, the host sends a WREG command [0100 RRNN], where ‘RR’ is the register number and ‘NN’ is the number of bytes to be written –1. Example: To write the single byte default configuration to register 3, the command is [0100 1100]. It is possible to write the default values to all configuration registers with a single command by setting the address to 0 and the number of bytes to (4 -1) = 3, followed by all four configuration bytes in sequence. The command for this is [0100 0011]. The ADC is capable of full-duplex operation, which means commands are decoded at the same time that conversion data are read. Commands may be sent on any 8-bit data boundary during a data read operation. This allows for faster toggling between pressure and temperature modes. A WREG command can be sent without corrupting an ongoing read operation. Figure 3-1 shows an example of sending a WREG command while reading conversion data. Note that after the command is clocked in (after the 32nd SCLK falling edge), the sensor changes settings and starts converting using the new register settings. The WREG command can be sent on any of the 8-bit boundaries – the first, ninth, 17th or 25th SCLK rising edges as shown in Figure 3-1.
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值