⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xtest14_8.c

📁 AT91sam7s 256 B/D example
💻 C
字号:
/* ========================================================================== */
/*	    Xtest14_8.c : RS-232C Communication by USART0 Polling(1)	      */
/* ========================================================================== */
/*			  Designed and programmed by Duck-Yong Yoon in 2007.  */

#include "AT91SAM7S256.h"
#include "lib_AT91SAM7S256.h"
#include "OK7S256ads.h"

unsigned int USART0_RX_char_nowait(void)	/* receive a character by USART0 */
{
  if(*AT91C_US0_CSR & 0x0001)			// if RXRDY=1, return a character
    return *AT91C_US0_RHR;
  else						// if RXRDY=0, return 0
    return 0;
}

unsigned int USART0_RX_char(void)		/* receive a character by USART0 */
{
  while(!(*AT91C_US0_CSR & 0x0001));		// wait until RXRDY=1

  return *AT91C_US0_RHR;			// receive a character
}

void USART0_TX_char(unsigned int data)		/* transmit a character by USART0 */
{
  while(!(*AT91C_US0_CSR & 0x0002));		// wait until TXRDY=1

  *AT91C_US0_THR = data;			// transmit a character
}

void USART0_string(char *string)		/* transmit a string by USART0 */
{
  while(*string != '\0')
    { USART0_TX_char(*string);
       string++;
    }
}

int main(void)
{
  unsigned int cursor, RxD;

  MCU_initialize();				// initialize AT91SAM7S256 & kit
  Delay_ms(50);					// wait for system stabilization
  LCD_initialize();				// initialize text LCD

  LCD_string(0x80,"RS-232C (USART0)");		// display title
  LCD_string(0xC0,"                ");
  Beep();

  LCD_command(0x0F);                            // cursor ON
  LCD_command(0xC0);                            // initial cursor position
  cursor = 0;

  AT91F_US0_CfgPMC();				// enable clock of USART0
  AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA,AT91C_PA6_TXD0|AT91C_PA5_RXD0,0);
 						// TXD0, RXD0 by Peripheral A
 
  *AT91C_US0_MR = 0x100008C0;			// normal mode, no parity
						// 8 data, 1 stop, LSB first, 16x
  *AT91C_US0_BRGR = 26;				// MCK/16/26 = 115200 bps
  *AT91C_US0_CR = 0x00000150;			// TX, RX enable

  while(1)
    { switch(Key_input())                       // key input
        { case 1 : LED_on(LED1); LED_off(LED2);	// KEY1 ?
                   USART0_string("  KEY1 is OK !  ");
                   USART0_TX_char(0x0D);
                   USART0_TX_char(0x0A);
                   break;
          case 2 : LED_on(LED2); LED_off(LED1);	// KEY2 ?
                   USART0_string("  KEY2 is OK !  ");
                   USART0_TX_char(0x0D);
                   USART0_TX_char(0x0A);
                   break;
          default: break;
        }

      RxD = USART0_RX_char_nowait();		// receive a character if any
      if(RxD != 0x00)
        { LCD_data(RxD);                        // display a received character
          cursor++;                             // 16 character OK ?
          if(cursor == 16)
            { LCD_command(0xC0);                // if yes, go home
              cursor = 0;
              Beep();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -