//----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include <SI_C8051F300_Register_Enums.h> #include <stdio.h> //----------------------------------------------------------------------------- // Global CONSTANTS //----------------------------------------------------------------------------- #define SYSCLK 24500000 // SYSCLK frequency in Hz #define BAUDRATE 115200 // Baud rate of UART in bps SI_SBIT(DE_RE, SFR_P0, 6); //定義P0.6 控制名稱為DE_RE SI_SBIT(LED2, SFR_P0, 1); //定義P0.1 控制名稱為LED2 SI_SBIT(LED1, SFR_P0, 0); //定義P0.0 控制名稱為LED1 SI_SBIT(LED3, SFR_P0, 2); //定義P0.2 控制名稱為LED3 SI_SBIT(LED4, SFR_P0, 3); //定義P0.3 控制名稱為LED4 SI_SBIT(LED5, SFR_P0, 7); //定義P0.7 控制名稱為LED5 //----------------------------------------------------------------------------- // Function PROTOTYPES //----------------------------------------------------------------------------- SI_INTERRUPT_PROTO(UART0_Interrupt, 4); void SYSCLK_Init (void); void UART0_Init (void); void PORT_Init (void); void Timer2_Init (int16_t); void MyDelay (uint16_t Value); #define KEY_DOWN 1 #define KEY_UP 0 #define LED_ON 0 #define LED_OFF 1 //----------------------------------------------------------------------------- // Global Variables //----------------------------------------------------------------------------- #define UART_BUFFERSIZE 6 uint8_t UART_Buffer[UART_BUFFERSIZE]; uint8_t UART_Buffer_Size = 0; uint8_t UART_Input_First = 0; uint8_t UART_Output_First = 0; uint8_t TX_Ready =1; uint8_t RX_Ready =0; static char Byte; uint8_t SnapDown_TimeCnt =0; uint8_t fSnap = 0; uint8_t RecDown_TimeCnt =0; uint8_t fRec = 0; //----------------------------------------------------------------------------- // SiLabs_Startup() Routine // ---------------------------------------------------------------------------- // This function is called immediately after reset, before the initialization // code is run in SILABS_STARTUP.A51 (which runs before main() ). This is a // useful place to disable the watchdog timer, which is enable by default // and may trigger before main() in some instances. //----------------------------------------------------------------------------- void SiLabs_Startup (void) { PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer } //----------------------------------------------------------------------------- // MAIN Routine //----------------------------------------------------------------------------- void main (void) { // enable) PORT_Init(); // Initialize Port I/O SYSCLK_Init (); // Initialize Oscillator UART0_Init(); IE_EA = 1; DE_RE =0; Timer2_Init (SYSCLK / 12 / 1000); // Init Timer2 to generate // 每1ms 計時中斷 while(1) { if(RX_Ready) { RX_Ready = 0; UART_Buffer_Size =0; if(UART_Buffer[3]==0x01)//任一鍵按下 { if(UART_Buffer[4]==0x01)//REC { fRec = KEY_DOWN; RecDown_TimeCnt=0; } else if(UART_Buffer[4]==0x02)//SNAP { fSnap = KEY_DOWN; SnapDown_TimeCnt=0; } else if(UART_Buffer[4]==0x03)//LED UP LED3 = LED_ON; else if(UART_Buffer[4]==0x04)//LED DO LED4 = LED_ON; else if(UART_Buffer[4]==0x05)//Mirror LED5 = LED_ON; } if(UART_Buffer[3]==0x00)//任一鍵放開 { if(UART_Buffer[4]==0x01)//REC { fRec = KEY_UP; } else if(UART_Buffer[4]==0x02)//SNAP { fSnap = KEY_UP; } else if(UART_Buffer[4]==0x03)//LED UP LED3 = LED_OFF; else if(UART_Buffer[4]==0x04)//LED DO LED4 = LED_OFF; else if(UART_Buffer[4]==0x05)//Mirror LED5 = LED_OFF; } IE_ES0 =1; } if(SnapDown_TimeCnt >55 && fSnap == KEY_DOWN) { LED1 = LED_ON; } else if(SnapDown_TimeCnt >55 && fSnap == KEY_UP) { LED1 =LED_OFF; SnapDown_TimeCnt =0; } else if(SnapDown_TimeCnt >45 && SnapDown_TimeCnt <55 && fSnap == KEY_UP) { LED1 = LED_ON; LED2 = LED_ON; MyDelay(1100);//延遲約0.5秒 LED1 = LED_OFF; LED2 = LED_OFF; SnapDown_TimeCnt =0; } if(RecDown_TimeCnt >55 && fRec == KEY_DOWN) { LED2 = LED_ON; } else if(RecDown_TimeCnt >55 && fRec == KEY_UP) { LED2 = LED_OFF; RecDown_TimeCnt =0; } else if(RecDown_TimeCnt >45 && RecDown_TimeCnt <55 && fRec == KEY_UP) { LED4 = LED_ON; LED5 = LED_ON; MyDelay(1100);//延遲約0.5秒 LED4 = LED_OFF; LED5 = LED_OFF; RecDown_TimeCnt =0; } } } //----------------------------------------------------------------------------- // Initialization Subroutines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // PORT_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // Configure the Crossbar and GPIO ports. // // P0.4 digital push-pull UART TX // P0.5 digital open-drain UART RX // //----------------------------------------------------------------------------- void PORT_Init (void) { P0MDOUT |= 0x10; // set UART TX to push-pull output XBR1 = 0x03; // Enable UTX, URX as push-pull output XBR2 = 0x40; // Enable crossbar, weak pull-ups // disabled XBR0 = 0x40; P0MDOUT |= 0x40; } //----------------------------------------------------------------------------- // SYSCLK_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // This routine initializes the system clock to use the internal oscillator // at its maximum frequency. // Also enables the Missing Clock Detector. //----------------------------------------------------------------------------- void SYSCLK_Init (void) { OSCICN |= 0x03; // Configure internal oscillator for // its maximum frequency RSTSRC = 0x04; // Enable missing clock detector } //----------------------------------------------------------------------------- // UART0_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1. //----------------------------------------------------------------------------- void UART0_Init (void) { SCON0 = 0x10; // SCON0: 8-bit variable bit rate // level of STOP bit is ignored // RX enabled // ninth bits are zeros // clear SCON0_RI and SCON0_TI bits if (SYSCLK/BAUDRATE/2/256 < 1) { TH1 = -(SYSCLK/BAUDRATE/2); CKCON |= 0x10; // T1M = 1; SCA1:0 = xx } else if (SYSCLK/BAUDRATE/2/256 < 4) { TH1 = -(SYSCLK/BAUDRATE/2/4); CKCON |= 0x01; // T1M = 0; SCA1:0 = 01 CKCON &= ~0x12; } else if (SYSCLK/BAUDRATE/2/256 < 12) { TH1 = -(SYSCLK/BAUDRATE/2/12); CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00 } else { TH1 = -(SYSCLK/BAUDRATE/2/48); CKCON |= 0x02; // T1M = 0; SCA1:0 = 10 CKCON &= ~0x11; } TL1 = 0xff; // set Timer1 to overflow immediately TMOD |= 0x20; // TMOD: timer 1 in 8-bit autoreload TMOD &= ~0xD0; // mode TCON_TR1 = 1; // START Timer1 TX_Ready = 1; // Flag showing that UART can transmit IP |= 0x10; // Make UART high priority IE_ES0 = 1; // Enable UART0 interrupts } //----------------------------------------------------------------------------- // Interrupt Service Routines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // UART0_Interrupt //----------------------------------------------------------------------------- // // This routine is invoked whenever a character is entered or displayed on the // Hyperterminal. // //----------------------------------------------------------------------------- SI_INTERRUPT(UART0_Interrupt, 4) { if (SCON0_RI == 1) { if( UART_Buffer_Size == 0) { // If new word is entered UART_Input_First = 0; } SCON0_RI = 0; // Clear interrupt flag Byte = SBUF0; // Read a character from UART if (UART_Buffer_Size < UART_BUFFERSIZE) { UART_Buffer[UART_Input_First] = Byte; // Store in array UART_Buffer_Size++; // Update array's size UART_Input_First++; // Update counter } if(UART_Buffer_Size >= UART_BUFFERSIZE) { RX_Ready =1; UART_Buffer_Size =0; IE_ES0 = 0; } } if (SCON0_TI == 1) // Check if transmit flag is set { SCON0_TI = 0; // Clear interrupt flag if (UART_Buffer_Size != 1) // If buffer not empty { // If a new word is being output if ( UART_Buffer_Size == UART_Input_First ) { UART_Output_First = 0; } // Store a character in the variable byte Byte = UART_Buffer[UART_Output_First]; if ((Byte >= 0x61) && (Byte <= 0x7A)) { // If upper case letter Byte -= 32; } SBUF0 = Byte; // Transmit to Hyperterminal UART_Output_First++; // Update counter UART_Buffer_Size--; // Decrease array size } else { UART_Buffer_Size = 0; // Set the array size to 0 TX_Ready = 1; // Indicate transmission complete } } } //----------------------------------------------------------------------------- // Timer2_Init //----------------------------------------------------------------------------- // // Configure Timer2 to 16-bit auto-reload and generate an interrupt at // interval specified by <counts> using SYSCLK/12 as its time base. // void Timer2_Init (int16_t counts) { TMR2CN = 0x00; // Stop Timer2; Clear TF2; // use SYSCLK/12 as timebase CKCON &= ~(CKCON_T2MH__BMASK | CKCON_T2ML__BMASK ); // Timer2 clocked based on TMR2CN_T2XCLK TMR2RL = -counts; // Init reload values TMR2 = 0xffff; // set to reload immediately IE_ET2 = 1; // enable Timer2 interrupts TMR2CN_TR2 = 1; // start Timer2 } //----------------------------------------------------------------------------- // Timer2_ISR //----------------------------------------------------------------------------- // This routine changes the state of the LED whenever Timer2 overflows. // SI_INTERRUPT(Timer2_ISR, TIMER2_IRQn) { TMR2CN_TF2H = 0; // clear Timer2 interrupt flag if(fSnap == KEY_DOWN) { SnapDown_TimeCnt++; // change state of LED if(SnapDown_TimeCnt==255) SnapDown_TimeCnt = 254; } else if(fRec == KEY_DOWN) { RecDown_TimeCnt++; // change state of LED if(RecDown_TimeCnt==255) RecDown_TimeCnt = 254; } } void MyDelay(uint16_t Value) { uint16_t i,j; for(i=0;i<Value;i++) { for(j=0;j<1000;j++) _nop_(); } } //----------------------------------------------------------------------------- // End Of File //-----------------------------------------------------------------------------
时间: 2025-07-04 12:18:47 浏览: 6
### UART Communication and LED Control on C8051F300 Microcontroller
The C8051F300 microcontroller is a popular choice for embedded systems due to its integrated peripherals, including UART communication capabilities. Understanding how to implement UART communication and control LEDs can be essential for various applications, such as data logging, sensor interfacing, and user feedback mechanisms.
To establish UART communication, the microcontroller must be configured with appropriate settings for baud rate, data bits, parity, and stop bits. The following code snippet demonstrates how to initialize the UART module:
```c
#include <C8051F300.h>
void UART_Init(void) {
SCON = 0x50; // Set UART mode to 8-bit, variable baud rate
TMOD |= 0x20; // Timer 1 in mode 2 (8-bit auto-reload)
TH1 = 0xFD; // Baud rate setting for 9600 bps
TR1 = 1; // Start Timer 1
TI = 1; // Indicate that transmitter is ready
}
```
In this initialization function, `SCON` is set to configure the UART for 8-bit data format with variable baud rate. The `TMOD` register is configured to operate Timer 1 in mode 2, which allows for an 8-bit auto-reload feature necessary for generating accurate baud rates. The value loaded into `TH1` determines the baud rate, where `0xFD` corresponds to a baud rate of 9600 bps[^1].
For LED control, the microcontroller's GPIO pins can be manipulated to turn LEDs on or off based on specific conditions within the program. Typically, an LED might be connected to a port pin through a current-limiting resistor to ground. Here's an example of how to toggle an LED connected to P1.0:
```c
void Toggle_LED(void) {
P1 ^= 0x01; // Toggle P1.0
}
```
This function uses bitwise XOR operation to invert the state of P1.0, effectively toggling the LED connected to it.
Debugging issues related to UART communication often involves checking the configuration registers (`SCON`, `TMOD`, `TH1`) and ensuring that the correct baud rate is being used. Additionally, verifying the physical connections between devices and using oscilloscopes or logic analyzers can help identify timing issues or incorrect signal levels.
When dealing with LED control, common problems include incorrect pin configurations, faulty wiring, or misinterpretation of active-high versus active-low signals. It's crucial to double-check the microcontroller's datasheet to ensure proper configuration of the I/O ports and any necessary pull-up or pull-down resistors.
阅读全文
相关推荐


















