|
- //*************************************************************************
- // 串口通信
- //环境:C8051F120
- //
- //说明:
- //
- // 本程序实现串口打印信息
- //
- // 使用外部晶振 22.1184MHz ,通过PLL倍频达到49.76MHz作为系统时钟
- //
- // 使用printStr(unsigned char *Str)函数输出字符串:
- // 使用printHex(unsigned char *Str,unsigned int length)函数输出字符串
- //
- //**************************************************************************/
- #include // Device-specific SFR Definitions
- #include
- //-----------------------------------------------------------------------------
- // 16-bit SFR Definitions for 'F12x
- //-----------------------------------------------------------------------------
- sfr16 RCAP2 = 0xca; // Timer2 capture/reload 在51中只有T0和T1
- // 口诀:定时晶振分溢出,T模门方两重载(51)
- // 而在c51中则常用的是T2 ,使用的自动重载波特率发生方式(方式2)
- sfr16 TMR2 = 0xcc; // Timer2
- //------------------------------------------------------------------------------
- // Function Prototypes
- //------------------------------------------------------------------------------
- // Initialization Routines
- void PORT_Init (void);
- void SYSCLK_Init (void);
- //用于串口通信初始化
- void UART0_Init (void);
- void printHex(unsigned char *Str,unsigned int length);//输出16进制
- void printStr(unsigned char *Str);//输出字符串
- void delay(unsigned int m)
- {
- unsigned int n;
- n=0;
- while(n < m)
- {n++;}
- return;
- }
- //-----------------------------------------------------------------------------
- // Global Variables
- //-----------------------------------------------------------------------------
- unsigned int i, j;
- //打印信息字母必须使用大写,数字定义为unsigned char
- //static byte C8051F_FLASH_BUFFER[]="DEFINE AS UNSIGNED CHAR AND USE BIG LETTERS AND U CAN PRINTF CORRECTLY";
- unsigned char C8051F_FLASH_BUFFER[8]="";
- //unsigned char rebuffer[1316];
- unsigned char test_str1[] = "we are";
- // Application Functions
- void UDP(void);
- //-----------------------------------------------------------------------------
- // Constant Definitions
- //-----------------------------------------------------------------------------
- #define SERVER_MODE 1
- #define DATA_BUFF_LEN 42
- //-----------------------------------------------------------------------------
- // Global Constants
- //-----------------------------------------------------------------------------
- #define EXTCLK 22118400 // External oscillator frequency in Hz 外部晶振的频率为22118400
- #define SYSCLK 49760000 // Output of PLL derived from
- // (EXTCLK*9/4)
- #define BAUDRATE 115200 // Baud rate of UART in bps
- // Note: The minimum standard baud rate
- // supported by the UART0_Init routine
- // in this file is 19,200 bps when
- // SYSCLK = 49.76MHz.
- // SYSTEMCLOCK = System clock frequency in Hz
- #define SYSTEMCLOCK 22118400//L( //* 9 / 4)
- //-----------------------------------------------------------------------------
- // Main Routine
- //-----------------------------------------------------------------------------
- void main(void)
- {
- // Disable watchdog timer
- WDTCN = 0xde;
- WDTCN = 0xad;//先写入0xDE再下入0xAD:禁止看门狗
- // Initialize the MCU
- PORT_Init();
- SYSCLK_Init();
- UART0_Init (); // initialize UART0
- SFRPAGE = UART0_PAGE; // Direct printf output to UART0
- EA=1;
- /*
- for(i= 0; i<1316;i++)
- rebuffer[i] = 0x14;
-
- delay(100);
- //打印16进制
- for(i=0;i<1;i++)
- {
- printHex(rebuffer,1316);
- delay(100);
- }
- */
- //打印字符串
- for(i=0;i<1;i++)
- {
- printStr(test_str1);
- delay(100);
- }
- while(1);
- }
- //-----------------------------------------------------------------------------
- // Initialization Routines
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- // PORT_Init
- //-----------------------------------------------------------------------------
- //
- // Configure the Interrupts, Crossbar and GPIO ports
- //
- void PORT_Init (void)
- {
- char SFRPAGE_SAVE = SFRPAGE; // 保存当前SFR页
-
- SFRPAGE = CONFIG_PAGE; // 设置SFR页
- P1MDOUT |= 0x40; // 设置 P1.6(TB_LED) 为推挽输出方式
- P2MDOUT |= 0x08; // 设置 P2.2(AB4_LED1) 为推挽输出方式
- // all pins used by the external memory interface are in push-pull mode
- //端口输出方式寄存器,各位设置为1时为推挽式输出,0时输出为漏极开路方式
- P4MDOUT = 0xDF;
- P5MDOUT = 0xFF;
- P6MDOUT = 0xFF;
- P7MDOUT = 0xFF;
- P4 = 0xC0; //1100 0000 //P4为控制总线 /WR(P4.7), /RD(P4.6)设置为逻辑高电平, RESET 为低电平
- P5 = 0xFF;
- P6 = 0xFF; // P5, P6 为地址总线
- P7 = 0xFF; // P7 为数据总线
- TCON &= ~0x01; // 设置 /INT0 位为0,使外部中断0为电平触发
- // Enable UART0, CP0, and /INT0. This puts /INT0 on P0.3.
- XBR0 = 0x84; // 端口I/O 交叉开关控制0 1000 0100 设置CPOE 位和 UART0EN 位 为1,
- XBR1 = 0x04; // 端口I/O 交叉开关控制1
- XBR2 = 0x40; // 端口I/O 交叉开关控制2
- P0MDOUT |= 0x01; // Set TX pin to push-pull
- SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
- }
- //-----------------------------------------------------------------------------
- // SYSCLK_Init
- //-----------------------------------------------------------------------------
- //
- // This routine initializes the system clock.
- //
- void SYSCLK_Init (void)
- {
- int i; // delay counter
- char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
- SFRPAGE = CONFIG_PAGE; // set SFR page
- OSCXCN = 0x67; // start external oscillator with22.1184MHz crystal
- for (i=0; i < 256; i++) ; // Wait for osc. to start up
- while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
- CLKSEL = 0x01; // Select the external osc. as
- // the SYSCLK source
- OSCICN = 0x00; // Disable the internal osc.
- //Turn on the PLL and increase the system clock by a factor of M/N = 9/4
- SFRPAGE = CONFIG_PAGE;
- PLL0CN = 0x04; // Set PLL source as external osc.
- SFRPAGE = LEGACY_PAGE;
- FLSCL = 0x10; // Set FLASH read time for 50MHz clk
- // or less
- SFRPAGE = CONFIG_PAGE;
- PLL0CN |= 0x01; // Enable Power to PLL,之后使能PLL时就不须再使能Power to PLL了
- PLL0DIV = 0x04; // Set Pre-divide value to N (N = 4)
- PLL0FLT = 0x01; // Set the PLL filter register for
- // a reference clock from 19 - 30 MHz
- // and an output clock from 65-100 MHz
- PLL0MUL = 0x09; // Multiply SYSCLK by M (M = 9)
- for (i=0; i < 256; i++) ; // Wait at least 5us
- PLL0CN |= 0x02; // Enable the PLL
- while(!(PLL0CN & 0x10)); // Wait until PLL frequency is locked
- CLKSEL = 0x02; // Select PLL as SYSCLK source
- SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
- }
- void UART0_Init (void)
- {
- char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
- SFRPAGE = UART0_PAGE;
- SCON0 = 0x50; // SCON0: URAT0 WORK mode 0, 8-bit UART, enable RX
- SSTA0 = 0x10; // Timer 1 generates UART0 baud rate and
- // UART0 baud rate divide by two disabled
- SFRPAGE = TIMER01_PAGE;
- TMOD &= ~0xF0;
- TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
- TH1 = -(SYSCLK/BAUDRATE/16); //即 TH1 = 0xFF- (SYSCLK/BAUDRATE/16));
- // Set the Timer1 reload value
- // When using a low baud rate, this equation
- // should be checked to ensure that the
- // reload value will fit in 8-bits.
- CKCON |= 0x10; // T1M = 1; SCA1:0 = xx
- TL1 = TH1; // initialize Timer1
- TR1 = 1; // start Timer1
- SFRPAGE = UART0_PAGE;
- TI0 = 1; // Indicate TX0 ready
- SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
- ES0=1;
- }
- /*//可软件查询,可省略中断处理
- void UART0_ISR (void) interrupt 4
- {
- // if(TI0)
- // {TI0=0;}
- }
- */
- /**********************************************************
- * printHex函数用法:打印16进制数据,例如:
- * for(i= 0; i<1316;i++)
- * rebuffer[i] = 0x14;
- *
- * delay(100);
- *
- * //打印16进制
- * for(i=0;i<1;i++)
- * {
- * printHex(rebuffer,1316);
- * delay(100);
- * }
- *
- ***********************************************************/
- void printHex(unsigned char *Str,unsigned int length)
- {
- j = 0;
- while(j <= length ) //判断是否到了字符串的尾部0x00
- {
- SBUF0 = *Str; //输出字符
- delay(100);
- while(!TI0); //空语句判断字符是否发完
- TI0 = 0; //清TI
- Str++; //字符串指针加一
- j++;
- }
- }
- /****************************************************
- * printStr函数用法:打印字符串或字符数组例如:
- * 1. unsigned char str[] = "hello my world!"
- *
- * printStr(str);
- *
- * 即可在串口助手中看见hello my world!打印出来,
- *
- ****************************************************/
- void printStr(unsigned char *Str)
- {
- while(*Str != '\0' ) //判断是否到了字符串的尾部0x00
- {
- SBUF0 = *Str; //输出字符
- delay(200);
- while(!TI0); //空语句判断字符是否发完
- TI0 = 0; //清TI
- Str++; //字符串指针加一
- }
- }
复制代码
代码如上,我想问下,
1.以下的这些端口初始化有什么作用?之前我删除了这些口的初始化,打印出了乱码。
//端口输出方式寄存器,各位设置为1时为推挽式输出,0时输出为漏极开路方式
P4MDOUT = 0xDF;
P5MDOUT = 0xFF;
P6MDOUT = 0xFF;
P7MDOUT = 0xFF;
P4 = 0xC0; //1100 0000 //P4为控制总线 /WR(P4.7), /RD(P4.6)设置为逻辑高电平, RESET 为低电平
P5 = 0xFF;
P6 = 0xFF; // P5, P6 为地址总线
P7 = 0xFF; // P7 为数据总线
2.我用printStr(unsigned char *Str)函数打印一字符串,第二个字符总打印不出来,比如unsigned char test_str1[] = "we are";这是我打印的字符串,在main()函数中 //打印字符串
for(i=0;i<1;i++)
{
printStr(test_str1);
delay(100);
}
while(1);
然后在超级终端中输出是 w are。正常应该是we are才对的,是不是哪里设置错了呢?
|
|