示例#1
0
void ByteUtil::SetBitForByteData(byte *byteData, int countBits, int indexBit, bool bitValue) {
	int countBytes = GetByteLenForDataLen(countBits);
	int indexByte = static_cast<int>( ceil(static_cast<double>( (indexBit + 1) / BYTE_BIT_LEN )) );
	int posBitInByte = indexBit % BYTE_BIT_LEN;
	byte &b = byteData[indexByte];
	SetBitValue(b, posBitInByte, bitValue);
};
void UARTTransmitHandler()
{
    switch(state)
    {
        case Idle:
        {
            SET_TX();
            break;
        }

        case Start:
        {
            CLEAR_TX();
            state   = Bit0;
            break;
        }

        case Bit0:
        {
            SetBitValue( byteToTransmit & 0x01 );
            state   = Bit1;
            break;
        }

        case Bit1:
        {
            SetBitValue( byteToTransmit & 0x02 );
            state   = Bit2;
            break;
        }

        case Bit2:
        {
            SetBitValue( byteToTransmit & 0x04 );
            state   = Bit3;
            break;
        }

        case Bit3:
        {
            SetBitValue( byteToTransmit & 0x08 );
            state   = Bit4;
            break;
        }

        case Bit4:
        {
            SetBitValue( byteToTransmit & 0x10 );
            state   = Bit5;
            break;
        }

        case Bit5:
        {
            SetBitValue( byteToTransmit & 0x20 );
            state   = Bit6;
            break;
        }

        case Bit6:
        {
            SetBitValue( byteToTransmit & 0x40 );
            state   = Bit7;
            break;
        }

        case Bit7:
        {
            SetBitValue( byteToTransmit & 0x80 );
            state   = Stop;
            break;
        }

        case Parity:
        {
            state   = Idle;
            SetBitValue( parityValue );
            break;
        }

        case Stop:
        {
            SET_TX();
            state   = Idle;
            break;
        }

        default:
        {
            PANIC();
            break;
        }
    }
}