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

📄 uart_2.c

📁 基于cc1010的设计实例
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                    CHIPCON CC1010 EXAMPLE PROGRAM            *
 *      ***   + +   ***                         UART                         *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * Demonstrates the use of UART0.                                            *
 * - printf and scanf (high-level functions)                                 *
 * - UART0_SETUP and UART0_CLOSE, polled                                     *
 * - UART0_WAIT_AND_SEND and UART0_WAIT_AND_RECEIVE                          *
 *                                                                           *
 * A cable must be connected between UART0 on the evaluation board and COM2  *
 * on the PC. Set up HyperTerminal to use COM2 at 57600 bps.                 *
 *                                                                           *
 * The program use printf and scanf to run a menu-driven application.        *
 * The terminal window must be set up to not echo typed                      *
 * characters, all text that appears should be sent from the eval. board.    *
 *                                                                           *
 * The application also demonstrates file transfer. A menu choice selects    *
 * data file transfer from HyperTerminal. The application will read the text *
 * file until the first line feed and store it in the XRAM. When the user    *
 * selects the appropriate menu option the file is echoed back to terminal.  *
 *                                                                           *
 * The program is not suitable for use with the debugger.                    *
 *****************************************************************************
 * Author:              ARR                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 * 1.0  2002/04/16      First public release                                 *
 *                                                                           *
 * $Log: uart_2.c,v $
 * Revision 1.2  2002/11/19 15:50:10  kht
 * Added startup macros
 *
 * Revision 1.1  2002/10/14 11:29:11  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/

#include <chipcon/hal.h>
#include <chipcon/cc1010eb.h>
#include <stdio.h>
#include <string.h>


//Define the absolute address for the file buffer
#define FILEBUF_ADDRESS 0x0000

//Define ASCII codes / terminal commands
#define ASCII_LF   0x0A
#define ASCII_CR   0x0D
#define ASCII_NUL  0x00
#define ASCII_ESC  0x1B


//Function prototypes
int receiveFile(byte xdata *buffer);
void echoFile(byte xdata *buffer, int length);

//File RAM buffer, max 1024 bytes
byte xdata fileBuf[1024] _at_ FILEBUF_ADDRESS;


//Main function
int main(void){

    // Local variables
    bool name_added = 0;
    char name[20];
    char old_name[20];
    char user_request;
    bool file_received = 0;
    char menu_option;
    bool running = 1;
    int length = 0;

    //Turn off watchdog
    //(otherwise the CC1010 will reset after a predefined time)
    WDT_ENABLE(FALSE);

    // Set optimum settings for speed and low power consumption
    MEM_NO_WAIT_STATES();
    FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS);

    //All I/Os are inputs after reset. Make I/Os connected to LEDs outputs
    RLED=YLED=GLED=BLED=LED_OFF;
    RLED_OE(TRUE); YLED_OE(TRUE); GLED_OE(TRUE); BLED_OE(TRUE);

    //Setup UART0 with polled I/O
    UART0_SETUP(57600, CC1010EB_CLKFREQ, UART_NO_PARITY | UART_RX_TX | UART_POLLED);

	name[0] = 0;
	old_name[0] = 0;

    //Loop: process user requests
    while (running == 1){
        //Clear screen
        printf("%c[2J", ASCII_ESC);

        //Place cursor at upper left corner
        printf("%c[H", ASCII_ESC);

        //IF user has entered valid filename
        if (name_added == 1){
            //Display header for file menu
            printf("\n\nMenu for %s\n\n", name);

            //Display menu option for changing filename
            printf("1: New file\n");
        //ELSE (user has not entered valid filename)
        }else{
            //Display header for initial menu
            printf("\n\nMenu\n\n");

            //Display menu option for entering filename
            printf("1: Open file\n");
        }

        //Display menu option for file transmit
        printf("2: Transmit file [PC->CC1010EB]\n");

        //IF received file
        if (file_received == 1){
            //Display menu option for echoing file content
            printf("3: Receive file [CC1010EB->PC]\n");
        }

        //Display menu footer
        printf("9: End program\n");
        printf("\nSelect a menu option: ");

        //Get menu request from user
        menu_option = getchar();

        //IF request to enter new filename
        if (menu_option == '1'){
            //Display user instruction
            printf("\n\nName: ");

            //Get filename from user
            scanf("%s", name);
			getchar(); //empty CR from input stream

			//Reset menu if new file
			if(strcmp(name, old_name) != 0){
			    file_received = 0;
                strcpy(old_name, name);
			}

            //Indicate name has been entered
            name_added = 1;
        //ELSE IF request to receive file
        }else if (menu_option == '2'){
            //Display user instruction
            printf("\n\nTerminal program: choose 'Transfer' + 'Send Text File' to transmit.\n");

            //Receive file characters
            length = receiveFile(&fileBuf);

            //Display transfer status and user instruction
            printf("\nTransferred %d bytes [PC->CC1010EB], ok (y/n): ", length);

            //Get request from user
			do{
                user_request = getchar();
			}while((user_request != 'y') && (user_request != 'n'));

            //IF user accept file transfer
            if (user_request == 'y'){
                //Indicate file transfer success
                file_received = 1;
            //ELSE (user rejects file transfer)
            }else{
                //Indicate file transfer corrupted
                file_received = 0;
            }
        //ELSE IF request to echo content of received file
        }else if ((menu_option == '3') && (file_received == 1)){
            //Display file content in reverse/inverted video mode
            printf("\n\nFile contents:\n\n");
            printf("%c[7m", ASCII_ESC);
            echoFile(&fileBuf, length);

            //Reset to normal video and wait for user input
            printf("%c[0m", ASCII_ESC);
            printf("\n");
            getchar();
        //ELSE IF request to exit program
        }else if (menu_option == '9'){
            //Display exit information
            printf("\n\nExiting...\n\n");

            //Issue end of terminal communication
            running = 0;
        //ELSE (invalid entry)
        }else{
            //Display error information
            printf("\n\nInvalid choice: %c\n\nPress any key to continue", menu_option);
            getchar();
        }
    }

    //Close UART channel
    UART0_CLOSE();

    //Infinite loop
    while(1);
}


//Function that demonstrates the use of the UART HAL RX primitive
int receiveFile(byte xdata *buffer){
    int i = 0;
    byte b = '0';

    GLED=LED_ON;

    //Receive characters from file until end of line (line feed or nul character)
    for( i = 0, b = '0';
         (b != ASCII_CR) && (b != ASCII_LF) && (b != ASCII_NUL) && (i < 1024);
         buffer++, i++){
        //Store received character in buffer
        UART0_WAIT_AND_RECEIVE(b);
        *buffer = b;
    }

    GLED=LED_OFF;

    //Return number of received characters
    return i;
}


//Function that demonstrates the use of the UART HAL TX primitive
void echoFile(byte xdata *buffer, int length){
    int i;

    //Send all bytes previously received
    for (i = 0; i < length; i++, buffer++){
        UART0_WAIT_AND_SEND(*buffer);
    }
}



⌨️ 快捷键说明

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