标题: 51单片机printf函数的使用 [打印本页]

作者: lzhcccc    时间: 2021-2-16 14:48
标题: 51单片机printf函数的使用
为了参加蓝桥杯的比赛学习51单片机,突发奇想51单片机能不能使用printf函数,总结整理希望大家能用到
#include "STC15F2K60S2.H"
#include "stdio.h"
void delay();
void UartInit(void)                //9600bps@11.0592MHz
{
        SCON = 0x50;               
        AUXR |= 0x40;               
        AUXR &= 0xFE;               
        TMOD &= 0x0F;               
        TL1 = 0xE0;               
        TH1 = 0xFE;               
        ET1 = 0;               
        TR1 = 1;               
}
void main()
{
        UartInit();
        while(1)
        {
                TI=1;
          printf("hello world\r\n");
                delay();
        }
}
void delay()
{
        unsigned int i,j;
        for(i=0;i<2000;i++);
        for(j=0;j<3000;j++);
}


作者: 刀妹    时间: 2021-2-17 10:38
实测可行吗
作者: 5.1_phantom    时间: 2021-2-26 10:10
可以往显示屏上输出吗???
作者: lzhcccc    时间: 2021-3-4 15:38
刀妹 发表于 2021-2-17 10:38
实测可行吗

可行

作者: lzhcccc    时间: 2021-3-4 15:39
  5.1_phantom 发表于 2021-2-26 10:10
可以往显示屏上输出吗???

串口屏?
作者: x51hei666    时间: 2021-3-25 17:14
波特率是多少了?

作者: aking991    时间: 2021-3-26 08:15
51单片机可用printf函数啊,谁说不能用
作者: profile5    时间: 2021-3-26 09:43
求解,不做重映射 为什么就可以直接使用?
作者: liuwang2051    时间: 2021-3-26 09:58
        while(1)
        {
                TI=1;
          printf("hello world\r\n");
                delay();
        }

刚开始学C语言,请问这句“TI=1;”,是什么作用?
作者: 两仪式    时间: 2021-3-26 10:37
把pringf里面的putchar 换成 串口发送对吧
作者: rsx9583    时间: 2021-3-26 16:54
直接可以用的,不过这个PRINTF好占程序空间。
作者: 995879859    时间: 2021-3-26 17:14
讲的很基础,很容易懂,挺好的。
作者: jiahz2019    时间: 2021-10-5 09:22
我直接拷贝程序,编译报错是咋回事?
作者: csmyldl    时间: 2021-10-5 10:46
jiahz2019 发表于 2021-10-5 09:22
我直接拷贝程序,编译报错是咋回事?

因为你用到头文件stc15f2k60s2.h,而你未添加STC单片机的头文件库
作者: jjGGWW    时间: 2021-10-5 15:09
可以往显示屏上输出吗???
作者: 13205495918    时间: 2021-10-15 09:03
liuwang2051 发表于 2021-3-26 09:58
while(1)
        {
                TI=1;

应该是接受标志位置一
作者: 13205495918    时间: 2021-10-15 09:05
liuwang2051 发表于 2021-3-26 09:58
while(1)
        {
                TI=1;

我说错了,是发送标志位
作者: dingpeng1980    时间: 2021-10-16 00:11
可以,我正在用,要配置中断
作者: .小破孩    时间: 2021-10-20 15:52
liuwang2051 发表于 2021-3-26 09:58
while(1)
        {
                TI=1;

定时器标志位,数据手册有解释
作者: .小破孩    时间: 2021-10-20 15:54
printf到哪里了?
作者: vscos    时间: 2021-12-13 22:30
可以用呀!只是没电脑上开发EXE程序的C好用,有很多条件限制,
作者: datouyuan    时间: 2021-12-14 10:09
keil的安装目录中有例程,可以直接拿来用。并且keil的帮助文件有很详细的说明和更适合实际工程的。
  1. /*------------------------------------------------------------------------------
  2. HELLO.C

  3. Copyright 1995-2005 Keil Software, Inc.
  4. ------------------------------------------------------------------------------*/

  5. #include <REG52.H>                /* special function register declarations   */
  6.                                   /* for the intended 8051 derivative         */

  7. #include <stdio.h>                /* prototype declarations for I/O functions */


  8. #ifdef MONITOR51                         /* Debugging with Monitor-51 needs   */
  9. char code reserve [3] _at_ 0x23;         /* space for serial interrupt if     */
  10. #endif                                   /* Stop Exection with Serial Intr.   */
  11.                                          /* is enabled                        */


  12. /*------------------------------------------------
  13. The main C function.  Program execution starts
  14. here after stack initialization.
  15. ------------------------------------------------*/
  16. void main (void) {

  17. /*------------------------------------------------
  18. Setup the serial port for 1200 baud at 16MHz.
  19. ------------------------------------------------*/
  20. #ifndef MONITOR51
  21.     SCON  = 0x50;                        /* SCON: mode 1, 8-bit UART, enable rcvr      */
  22.     TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
  23.     TH1   = 221;                /* TH1:  reload value for 1200 baud @ 16MHz   */
  24.     TR1   = 1;                  /* TR1:  timer 1 run                          */
  25.     TI    = 1;                  /* TI:   set TI to send first char of UART    */
  26. #endif

  27. /*------------------------------------------------
  28. Note that an embedded program never exits (because
  29. there is no operating system to return to).  It
  30. must loop and execute forever.
  31. ------------------------------------------------*/
  32.   while (1) {
  33.     P1 ^= 0x01;                         /* Toggle P1.0 each time we print */
  34.     printf ("Hello World\n");   /* Print "Hello World" */
  35.   }
  36. }


复制代码
代码。
作者: datouyuan    时间: 2021-12-14 10:19

keil帮助文件中使用串口中断和缓存的代码。
作者: wps10025    时间: 2021-12-14 11:09
51单片机包含#include "stdio.h",printf打印到串口,经常用
作者: vscos    时间: 2021-12-15 23:21
liuwang2051 发表于 2021-3-26 09:58
while(1)
        {
                TI=1;

串口发送中断标致位,如果发送成功,TI位等于1
作者: zhang1314hong    时间: 2022-8-11 17:05
datouyuan 发表于 2021-12-14 10:09
keil的安装目录中有例程,可以直接拿来用。并且keil的帮助文件有很详细的说明和更适合实际工程的。代码。

真给力!
作者: vscos    时间: 2022-8-12 06:32
我只发现这函数只能用在串口上,而且没有电脑上的C程序好用
作者: lzzasd    时间: 2022-8-12 09:58
printf本来就可以用   输出到串口    但是用到接收函数就复杂很多了
作者: 1314love    时间: 2022-8-12 17:04
liuwang2051 发表于 2021-3-26 09:58
while(1)
        {
                TI=1;

发送标志位TI,当发送完成后TI==1




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1