Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. //------------------------------------------------------------------------------------
  2. // Hello.c
  3. //------------------------------------------------------------------------------------
  4. //8051 Test program to demonstrate serial port I/O. This program writes a message on
  5. //the console using the printf() function, and reads characters using the getchar()
  6. //function. An ANSI escape sequence is used to clear the screen if a '2' is typed.
  7. //A '1' repeats the message and the program responds to other input characters with
  8. //an appropriate message.
  9. //
  10. //Any valid keystroke turns on the green LED on the board; invalid entries turn it off
  11. //------------------------------------------------------------------------------------
  12. // Includes
  13. //------------------------------------------------------------------------------------
  14. #include <c8051f120.h>
  15. #include <stdio.h>
  16. #include "putget.h"
  17.  
  18. //------------------------------------------------------------------------------------
  19. // Global Constants
  20. //------------------------------------------------------------------------------------
  21. #define EXTCLK 22118400 // External oscillator frequency in Hz
  22. #define SYSCLK 49766400 // Output of PLL derived from (EXTCLK * 9/4)
  23. #define BAUDRATE 115200 // UART baud rate in bps
  24.  
  25. //------------------------------------------------------------------------------------
  26. // Function Prototypes
  27. //------------------------------------------------------------------------------------
  28. void main(void);
  29. void SYSCLK_INIT(void);
  30. void PORT_INIT(void);
  31. void UART0_INIT(void);
  32.  
  33. //------------------------------------------------------------------------------------
  34. // MAIN Routine
  35. //------------------------------------------------------------------------------------
  36. void main(void)
  37. {
  38. char c;
  39.  
  40. WDTCN = 0xDE; // Disable the watchdog timer
  41. WDTCN = 0xAD;
  42.  
  43. PORT_INIT(); // Initialize the Crossbar and GPIO
  44. SYSCLK_INIT(); // Initialize the oscillator
  45. UART0_INIT(); // Initialize UART0
  46.  
  47. SFRPAGE = UART0_PAGE; // Direct output to UART0
  48.  
  49. printf("\033[33;44m");
  50. printf("\033[2J"); // Erase screen & move cursor to home position
  51. printf("\033[2;25H"); // row 2 col 20
  52. printf("Type <ESC> to end the program\n");
  53. printf("\033[13;25r"); // scroll section 13-25
  54.  
  55.  
  56.  
  57. c = getchar();
  58. printf("\033[13;1H"); // jump to begining of scroll section
  59. while(c!= 0x1B){
  60.  
  61.  
  62.  
  63. if(c<' ' || c > '~'){
  64. printf("Character %02X is \033[4mnot printable\033[0;33;44m\n\r",c);// escape is for underline / reset
  65.  
  66.  
  67. }else{
  68. printf("\033[s");// save
  69. printf("\033[6;1H");// line 6
  70. printf("Keyboard character is \033[37m%c\033[33m.\n",c);// print key line, key is white
  71.  
  72. printf("\033[u");// load
  73. }
  74.  
  75. c = getchar();
  76.  
  77. }
  78.  
  79. }
  80.  
  81. //------------------------------------------------------------------------------------
  82. // SYSCLK_Init
  83. //------------------------------------------------------------------------------------
  84. //
  85. // Initialize the system clock to use a 22.1184MHz crystal as its clock source
  86. //
  87. void SYSCLK_INIT(void)
  88. {
  89. int i;
  90. char SFRPAGE_SAVE;
  91.  
  92. SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
  93.  
  94. SFRPAGE = CONFIG_PAGE;
  95. OSCXCN = 0x67; // Start ext osc with 22.1184MHz crystal
  96. for(i=0; i < 256; i++); // Wait for the oscillator to start up
  97. while(!(OSCXCN & 0x80));
  98. CLKSEL = 0x01;
  99. OSCICN = 0x00;
  100.  
  101. SFRPAGE = CONFIG_PAGE;
  102. PLL0CN = 0x04;
  103. SFRPAGE = LEGACY_PAGE;
  104. FLSCL = 0x10;
  105. SFRPAGE = CONFIG_PAGE;
  106. PLL0CN |= 0x01;
  107. PLL0DIV = 0x04;
  108. PLL0FLT = 0x01;
  109. PLL0MUL = 0x09;
  110. for(i=0; i < 256; i++);
  111. PLL0CN |= 0x02;
  112. while(!(PLL0CN & 0x10));
  113. CLKSEL = 0x02;
  114.  
  115. SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
  116. }
  117.  
  118. //------------------------------------------------------------------------------------
  119. // PORT_Init
  120. //------------------------------------------------------------------------------------
  121. //
  122. // Configure the Crossbar and GPIO ports
  123. //
  124. void PORT_INIT(void)
  125. {
  126. char SFRPAGE_SAVE;
  127.  
  128. SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
  129.  
  130. SFRPAGE = CONFIG_PAGE;
  131. XBR0 = 0x04; // Enable UART0
  132. XBR1 = 0x00;
  133. XBR2 = 0x40; // Enable Crossbar and weak pull-up
  134. P0MDOUT |= 0x01; // Set TX0 on P0.0 pin to push-pull
  135. P1MDOUT |= 0x40; // Set green LED output P1.6 to push-pull
  136.  
  137. SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
  138. }
  139.  
  140. //------------------------------------------------------------------------------------
  141. // UART0_Init
  142. //------------------------------------------------------------------------------------
  143. //
  144. // Configure the UART0 using Timer1, for <baudrate> and 8-N-1
  145. //
  146. void UART0_INIT(void)
  147. {
  148. char SFRPAGE_SAVE;
  149.  
  150. SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
  151.  
  152. SFRPAGE = TIMER01_PAGE;
  153. TMOD &= ~0xF0;
  154. TMOD |= 0x20; // Timer1, Mode 2, 8-bit reload
  155. TH1 = -(SYSCLK/BAUDRATE/16); // Set Timer1 reload baudrate value T1 Hi Byte
  156. CKCON |= 0x10; // Timer1 uses SYSCLK as time base
  157. TL1 = TH1;
  158. TR1 = 1; // Start Timer1
  159.  
  160. SFRPAGE = UART0_PAGE;
  161. SCON0 = 0x50; // Mode 1, 8-bit UART, enable RX
  162. SSTA0 = 0x10; // SMOD0 = 1
  163. TI0 = 1; // Indicate TX0 ready
  164.  
  165. SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement