/************************************************************************* Function Name : UartInit Arguments : baud: baud rate(2400-1M bps) charSize: character size(5-8) parity: parity mode(0-None, 1-Reserved, 2-Even£¬3-Odd) stopbit: size of stop bits (1-2) frmSpace: setting value of min space time between frame unit: 0.1ms Return : TRUE-Initialization success FALSE-Initialization failed (invalid input) Description : UART initialization **************************************************************************/ uint8 UartInit(uint32 baud, uint8 charSize, uint8 parity, uint8 stopbit, uint16 frmSpace) { uint16 ubrrValue; //parameter check if ((baud < BAUD_MIN) || (baud > BAUD_MAX) || (charSize < 5) || (charSize > 8) || (parity > 3) || (stopbit < 1) || (stopbit > 2)) { return FALSE; } UartIOSet(); /* UBRR = Fosc / (16 * BAUD) - 1 Note: MSB of ubrrValue must be 0, because UBRRH Register shares the same I/O location as the UCSRC Register. So, the URSEL bit must used to distinguish them. *///ubrrValue = (F_CPU / 16 / baud) - 1; ubrrValue = RoundDiv(F_CPU/16, baud) - 1; UBRR0H = ubrrValue >> 8; UBRR0L = ubrrValue; /* Update USART Control & Status Reg C "UCSRC" URSEL - 1 (UCSRC Access) UMSEL - 0 (Asynchronous Mode) UPM1 - 1 (Even Parity) UPM0 - 0 (Even Parity) USBS - 0 (1 Stop bit) UCSZ1 - 1 (8 bit Character Size) UCSZ0 - 1 (8 bit Character Size) UCPOL - 0 (clock polarity) */ /* Note: (1<<URSEL) must be in forefront */ UCSR0C = ((parity<<UPM0) | ((stopbit-1)<<USBS) | ((charSize-5)<<UCSZ0)); /* Update USART Control & Status Reg B "UCSRB" RXCIE - 1 (RX Complete Interrupt Enable) TXCIE - 0 (TX Complete Interrupt Enable) UDRIE - 0 (UDR Empty Interrupt Enable) RXEN - 1 (Enable Reciver) TXEN - 1 (Enable Transmitter) UCSZ2 - 0 (8 Character Size) RXB8 - 0 (Receive Data Bit 8) TXB8 - 0 (Transmit Data Bit 8) */ UCSR0B = ((1<<RXEN) | (1<<TXEN) | (1<<RXCIE)); UartFlush(); UartSwitchToRx(); if (UartRxBufSet(buf1, BUF_SIZE1) == FALSE) return FALSE; if (UartTxBufSet(buf2, BUF_SIZE2) == FALSE) return FALSE; txCtrl.frmSpaceSet = frmSpace; return TRUE; }
std::string DecimalStr(const BigRat& q, const MachineInt& DecimalPlaces) { if (IsNegative(DecimalPlaces) || !IsSignedLong(DecimalPlaces) || IsZero(DecimalPlaces)) CoCoA_ERROR(ERR::BadArg, "FixedStr"); if (IsOneDen(q)) return DecimalStr(num(q), DecimalPlaces); const long DigitsAfterPoint = AsSignedLong(DecimalPlaces); const BigInt N = RoundDiv(abs(num(q))*power(10,DigitsAfterPoint),den(q)); string digits = ToString(N); if (len(digits) < 1+DigitsAfterPoint) { digits = string(DigitsAfterPoint+1-len(digits), '0') + digits; } string ans; if (q < 0) ans = '-'; const long IntegerPart = len(digits) - DigitsAfterPoint; ans.insert(ans.end(), &digits[0], &digits[IntegerPart]); ans += '.'; ans.insert(ans.end(), &digits[IntegerPart], &digits[IntegerPart+DigitsAfterPoint]); return ans; }