void usartSetup (void) {
// make sure the relevant pins are appropriately set up.
RCC_APB2ENR |= RCC_APB2ENR_IOPAEN; // enable clock for GPIOA
GPIOA_CRH |= (0x0BUL < < 4); // Tx (PA9) alt. out push-pull
GPIOA_CRH |= (0x04UL << 8); // Rx (PA10) in floating
RCC_APB2ENR |= RCC_APB2ENR_USART1EN; // enable clock for USART1
USART1_BRR = 64000000L/115200L; // set baudrate
USART1_CR1 |= (USART1_CR1_RE | USART1_CR1_TE); // RX, TX enable
USART1_CR1 |= USART1_CR1_UE; // USART enable
}
int SendChar (int ch) {
while (!(USART1_SR & USART1_SR_TXE));
USART1_DR = (ch & 0xFF);
return (ch);
}
int GetChar (void) {
while (!(USART1_SR & USART1_SR_RXNE));
return ((int)(USART1_DR & 0xFF));
void UART_init(void)
{
// USART3 init
RCC->APB1ENR |= RCC_APB1ENR_USART3EN; // такты на USART3
GPIOB->CRH &= (~(GPIO_CRH_CNF10_0)); //Настройка порта передатчика
GPIOB->CRH |= (GPIO_CRH_MODE10 | GPIO_CRH_CNF10_1); // AF Push-Pull out (TX)
// GPIOB->CRH &= (~(GPIO_CRH_CNF11)); // Настройка порта приемника
// GPIOB->CRH &= (~(GPIO_CRH_MODE11));
// GPIOB->CRH |= (GPIO_CRH_CNF11_1);
// GPIOB->BSRR |= GPIO_ODR_ODR11; // Input Pull-up (RX)
USART3->CR1 |= USART_CR1_UE; // Разрешить USART3
USART3->BRR = 0x0271; // 57600 baud
USART3->CR1 |= USART_CR1_TE; //Разрешить передатчик
}
Передача байта:
while ((USART3->SR & USART_SR_TXE) != USART_SR_TXE);
USART3->DR = 'D';
void setup()
{
RCC_APB2ENRbits.IOPAEN = 1; //Enable GPIOA
RCC_APB2ENRbits.USART1EN = 1; //Enable USART1
GPIOA_CRH = 0x4A0; //Set PA9 and PA10 as AFIO pins
USART1_BRR = 0×341; //Set value for required baud rate
USART1_CR1bits.UE = 1; //Set USART1
USART1_CR1bits.RE = 1; //Set USART1 RX to receive incoming data
USART1_CR1bits.TE = 1; //Set USART1 TX to send data
}
unsigned char USART1_receive()
{
while(USART1_SRbits.RXNE == 0); //wait until data is available for reading
return (0xFF & USART1_DR); //return read data
}
void USART1_send(unsigned char value)
{
USART1_DR = value; //transfer data to DR register
while(USART1_SRbits.TXE == 0); //wait until data transmission is complete
}
Комментариев нет:
Отправить комментарий