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

📄 f34x_uart_stdio.c

📁 c8051f340 串口 程序This program demonstrates how to configure the C8051F340 to use routines in STDIO.h
💻 C
字号:
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <C8051F340.h>                 // SFR declarations
#include <intrins.h>
#include <stdio.h>
#include <CH423.h>
#include <lcd.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F34x
//-----------------------------------------------------------------------------

sfr16 SBRL1 = 0xB4;

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSCLK       48000000          // SYSCLK frequency in Hz

#define BAUDRATE1     115200           // Baud rate of UART1 in bps


//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void SYSTEMCLOCK_Init (void);
void PORT_Init (void);
void UART1_Init (void);
void Delay (void);

//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------



//-----------------------------------------------------------------------------
// SYSTEMCLOCK_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None

// This routine initializes the system clock to use the internal system clock
// routed through the clock multiplier as its clock source.
//
//-----------------------------------------------------------------------------

void SYSTEMCLOCK_Init (void)
{
   OSCICN |= 0x03;                     // Configure internal oscillator for
                                       // its maximum frequency and enable
                                       // missing clock detector

   CLKMUL  = 0x00;                     // Select internal oscillator as
                                       // input to clock multiplier

   CLKMUL |= 0x80;                     // Enable clock multiplier
   Delay();                            // Delay for clock multiplier to begin
   CLKMUL |= 0xC0;                     // Initialize the clock multiplier
   Delay();                            // Delay for clock multiplier to begin

   while(!(CLKMUL & 0x20));            // Wait for multiplier to lock
   CLKSEL  = 0x03;                     // Select system clock
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------

void PORT_Init (void)
{  
   
   P0MDIN |= 0xFF;                     // Lower four pins on P0 are digital
   P0MDOUT = 0xFF;					   //pull-push
   
   P1MDIN |= 0xFF;   				  
   P1MDOUT = 0xFF;                     
   
   P2MDOUT =0x01;

   P3MDIN |= 0xFF;                    
   P3MDOUT = 0xFF;  
                                     
   P0    = 0xFF;
   P1    = 0xFF;
   P3    = 0xff;
   P0SKIP    = 0xFF;
   P1SKIP    = 0xFF;
                    
   XBR0  = 0x00;                              
   XBR1  = 0x40;                     // Enable crossbar and enable
   XBR2  = 0x01;                                    // weak pull-ups
}

//-----------------------------------------------------------------------------
// UART1_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure UART1 for baudrate1 and 8-N-1.
//
//-----------------------------------------------------------------------------

void UART1_Init (void)
{
   SMOD1 = 0x0C;                       // set to disable parity, 8-data bit,
                                       // disable extra bit, 
                                       // stop bit 1 bit wide

   SCON1 = 0x10;                       // SCON1: 8-bit variable bit rate
                                       //        level of STOP bit is ignored
                                       //        RX enabled
                                       //        ninth bits are zeros
                                       //        clear RI0 and TI0 bits

   if (SYSCLK/BAUDRATE1/2/0xFFFF < 1) {
      SBRL1 = -(SYSCLK/BAUDRATE1/2);
      SBCON1 |= 0x03;                  // set prescaler to 1
   } else if (SYSCLK/BAUDRATE1/2/0xFFFF < 4) {
      SBRL1 = -(SYSCLK/BAUDRATE1/2/4);
      SBCON1 &= ~0x03;
      SBCON1 |= 0x01;                  // set prescaler to 4

   } else if (SYSCLK/BAUDRATE1/2/0xFFFF < 12) {
      SBRL1 = -(SYSCLK/BAUDRATE1/2/12);
      SBCON1 &= ~0x03;                 // set prescaler to 12
   } else {
      SBRL1 = -(SYSCLK/BAUDRATE1/2/48);
      SBCON1 &= ~0x03;
      SBCON1 |= 0x02;                  // set prescaler to 4
   }

   SCON1 |= 0x02;                      // indicate ready for TX
   SBCON1 |= 0x40;                     // enable baud rate generator
}


//-----------------------------------------------------------------------------
// putchar
//-----------------------------------------------------------------------------
//
// Return Value : UART0/1 buffer value
// Parameters   : character to be transmitted across UART0/1
//
// This is an overloaded fuction found in the stdio library.  When the
// function putchar is called, either by user code or through calls to stdio
// routines such as printf, the following routine will be executed instead 
// of the function located in the stdio library.
//
// The function checks the UART global variable to determine which UART to 
// use to receive a character.
//
// The routine expands '\n' to include a carriage return as well as a 
// new line character by first checking to see whether the character  
// passed into the routine equals '\n'.  If it is, the routine waits for 
// TI0/TI1 to be set, indicating that UART 0/1 is ready to transmit another 
// byte.  The routine then clears TI0/TI1 bit, and sets the UART0/1 output 
// buffer to '0x0d', which is the ASCII character for carriage return.
//
// The routine the waits for TI0/TI1 to be set, clears TI0/TI1, and sets
// the UART output buffer to <c>.  
//
//-----------------------------------------------------------------------------

char putchar (char c)  
{
     if (c == '\n')  
	 {                                  // check for newline character
         while (!(SCON1 & 0x02));      // wait until UART1 is ready to transmit
         SCON1 &= ~0x02;               // clear TI1 interrupt flag
         SBUF1 = 0x0d;                 // output carriage return
      }
      while (!(SCON1 & 0x02));         // wait until UART1 is ready to transmit
      SCON1 &= ~0x02;                  // clear TI1 interrupt flag
      return (SBUF1 = c);              // output <c> using UART 1
   
}

//-----------------------------------------------------------------------------
// _getkey
//-----------------------------------------------------------------------------
//
// Return Value : byte received from UART0/1
// Parameters   : none

// This is an overloaded fuction found in the stdio library.  When the
// function _getkey is called, either by user code or through calls to stdio
// routines such as scanf, the following routine will be executed instead 
// of the function located in the stdio library.
//
// The function checks the UART global variable to determine which UART to 
// use to receive a character.
//
// The routine waits for RI0/RI1 to be set, indicating that a byte has
// been received across the UART0/UART1 RX line.  The routine saves the 
// received character into a local variable, clears the RI0/RI1 interrupt
// flag, and returns the received character value.
//
//-----------------------------------------------------------------------------
char _getkey ()  {
  char c;
  while (!(SCON1 & 0x01));           // wait until UART1 receives a character
    c = SBUF1;                         // save character to local variable
    SCON1 &= ~0x01;                    // clear UART1 receive interrupt flag
    return (c);                        // return value received through UART1
  
}

//-----------------------------------------------------------------------------
// Delay
//-----------------------------------------------------------------------------
//
// Return Value : none
// Parameters   : none
//
// Used for a small pause of approximately 40 us.
//
//-----------------------------------------------------------------------------

void Delay(void)
{
   int x;
   for(x = 0;x < 500;x)
      x++;
}


//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void) {
   char input_char;

   PCA0MD &= ~0x40;                    // Disable Watchdog timer

   SYSTEMCLOCK_Init ();                // initialize oscillator
   PORT_Init ();                       // initialize crossbar and GPIO
   UART1_Init ();                      // initialize UART1
   
   CH423_Set_H(0xff);
   CH423_Set_L(0xff);
   
   // transmit example UART1
   printf ("Hello, from UART1!\n");
                      
   while (1) 
   {
      input_char = getchar();
	  printf (" '%c', 0x%02x\n", (unsigned char) input_char, 
                                 (unsigned) input_char);
      switch (input_char) 
	  {
             case  0x01:
             Main_LCD_Show_Black();        // 
             break;
             case  0x02:
             Main_LCD_Show_White();           //
             break;
             case  0x03:
             Main_LCD_Show_Red();    // 
             break;
             case  0x04:                     // 
             Main_LCD_Show_Green();
             break;
			 case  0x05:
             Main_LCD_Show_Blue();        // 
             break;
             case  0x06:
             Main_LCD_Grey_Scale_Test();           //
             break;
             case  0x07:
             Sub_LCD_Show_Black();         // 
             break;
             case  0x08:                  // 
             Sub_LCD_Show_White();
			 break;
			 case  0x09:
             Sub_LCD_Show_Red();           // 
             break;
             case  0x0a:                  // 
             Sub_LCD_Show_Green();
             break;
			 case  0x0b:
             Sub_LCD_Show_Blue();           // 
             break;
             case  0x0c:                  // 
             Sub_LCD_Grey_Scale_Test();
			 break;
			 case  0x0d:                  // 
             LCD_Off();
             break;
             case  0x10:                  // 
             CH423_Set_H(0xff);
             break;
             case  0x11:                  // 
             CH423_Set_L(0x7f); 
             break;
             case  0x12:                  // 
             CH423_Set_L(0xbf); 
             break;
             case  0x13:                  // 
             CH423_Set_L(0xdf); 
             break;
             case  0x14:                  // 
             CH423_Set_L(0xef); 
             break;
             case  0x15:                  // 
             CH423_Set_L(0xf7); 
             break;
             case  0x16:                  // 
             CH423_Set_L(0xfb); 
             break;
             case  0x17:                  // 
             CH423_Set_L(0xfd); 
             break;
             case  0x18:                  // 
             CH423_Set_L(0xfe); 
             break;
             case  0x20:                  // 
             CH423_Set_H(0x00);
             CH423_Set_L(0xff);
             break;
             default:
             break;
		 }
		 input_char=0x00;
      

   }

}

⌨️ 快捷键说明

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