Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------------------------------------------------------
- // Hello.c
- //------------------------------------------------------------------------------------
- //8051 Test program to demonstrate serial port I/O. This program writes a message on
- //the console using the printf() function, and reads characters using the getchar()
- //function. An ANSI escape sequence is used to clear the screen if a '2' is typed.
- //A '1' repeats the message and the program responds to other input characters with
- //an appropriate message.
- //
- //Any valid keystroke turns on the green LED on the board; invalid entries turn it off
- //------------------------------------------------------------------------------------
- // Includes
- //------------------------------------------------------------------------------------
- #include <c8051f120.h>
- #include <stdio.h>
- #include "putget.h"
- //------------------------------------------------------------------------------------
- // Global Constants
- //------------------------------------------------------------------------------------
- #define EXTCLK 22118400 // External oscillator frequency in Hz
- #define SYSCLK 49766400 // Output of PLL derived from (EXTCLK * 9/4)
- #define BAUDRATE 115200 // UART baud rate in bps
- //------------------------------------------------------------------------------------
- // Function Prototypes
- //------------------------------------------------------------------------------------
- void main(void);
- void SYSCLK_INIT(void);
- void PORT_INIT(void);
- void UART0_INIT(void);
- //------------------------------------------------------------------------------------
- // MAIN Routine
- //------------------------------------------------------------------------------------
- void main(void)
- {
- char c;
- WDTCN = 0xDE; // Disable the watchdog timer
- WDTCN = 0xAD;
- PORT_INIT(); // Initialize the Crossbar and GPIO
- SYSCLK_INIT(); // Initialize the oscillator
- UART0_INIT(); // Initialize UART0
- SFRPAGE = UART0_PAGE; // Direct output to UART0
- printf("\033[33;44m");
- printf("\033[2J"); // Erase screen & move cursor to home position
- printf("\033[2;25H"); // row 2 col 20
- printf("Type <ESC> to end the program\n");
- printf("\033[13;25r"); // scroll section 13-25
- c = getchar();
- printf("\033[13;1H"); // jump to begining of scroll section
- while(c!= 0x1B){
- if(c<' ' || c > '~'){
- printf("Character %02X is \033[4mnot printable\033[0;33;44m\n\r",c);// escape is for underline / reset
- }else{
- printf("\033[s");// save
- printf("\033[6;1H");// line 6
- printf("Keyboard character is \033[37m%c\033[33m.\n",c);// print key line, key is white
- printf("\033[u");// load
- }
- c = getchar();
- }
- }
- //------------------------------------------------------------------------------------
- // SYSCLK_Init
- //------------------------------------------------------------------------------------
- //
- // Initialize the system clock to use a 22.1184MHz crystal as its clock source
- //
- void SYSCLK_INIT(void)
- {
- int i;
- char SFRPAGE_SAVE;
- SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
- SFRPAGE = CONFIG_PAGE;
- OSCXCN = 0x67; // Start ext osc with 22.1184MHz crystal
- for(i=0; i < 256; i++); // Wait for the oscillator to start up
- while(!(OSCXCN & 0x80));
- CLKSEL = 0x01;
- OSCICN = 0x00;
- SFRPAGE = CONFIG_PAGE;
- PLL0CN = 0x04;
- SFRPAGE = LEGACY_PAGE;
- FLSCL = 0x10;
- SFRPAGE = CONFIG_PAGE;
- PLL0CN |= 0x01;
- PLL0DIV = 0x04;
- PLL0FLT = 0x01;
- PLL0MUL = 0x09;
- for(i=0; i < 256; i++);
- PLL0CN |= 0x02;
- while(!(PLL0CN & 0x10));
- CLKSEL = 0x02;
- SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
- }
- //------------------------------------------------------------------------------------
- // PORT_Init
- //------------------------------------------------------------------------------------
- //
- // Configure the Crossbar and GPIO ports
- //
- void PORT_INIT(void)
- {
- char SFRPAGE_SAVE;
- SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
- SFRPAGE = CONFIG_PAGE;
- XBR0 = 0x04; // Enable UART0
- XBR1 = 0x00;
- XBR2 = 0x40; // Enable Crossbar and weak pull-up
- P0MDOUT |= 0x01; // Set TX0 on P0.0 pin to push-pull
- P1MDOUT |= 0x40; // Set green LED output P1.6 to push-pull
- SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
- }
- //------------------------------------------------------------------------------------
- // UART0_Init
- //------------------------------------------------------------------------------------
- //
- // Configure the UART0 using Timer1, for <baudrate> and 8-N-1
- //
- void UART0_INIT(void)
- {
- char SFRPAGE_SAVE;
- SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
- SFRPAGE = TIMER01_PAGE;
- TMOD &= ~0xF0;
- TMOD |= 0x20; // Timer1, Mode 2, 8-bit reload
- TH1 = -(SYSCLK/BAUDRATE/16); // Set Timer1 reload baudrate value T1 Hi Byte
- CKCON |= 0x10; // Timer1 uses SYSCLK as time base
- TL1 = TH1;
- TR1 = 1; // Start Timer1
- SFRPAGE = UART0_PAGE;
- SCON0 = 0x50; // Mode 1, 8-bit UART, enable RX
- SSTA0 = 0x10; // SMOD0 = 1
- TI0 = 1; // Indicate TX0 ready
- SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement