| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3074 人关注过本帖
标题:单片机控制
只看楼主 加入收藏
qiushizhang
该用户已被删除
收藏
 问题点数:0 回复次数:10 
单片机控制
提示: 作者被禁止或删除 内容自动屏蔽
搜索更多相关主题的帖子: 单片机 交通灯 
2008-06-03 18:32
扫门星
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2008-8-7
收藏
得分:0 
#include <reg52.h>                    /* special function register 8052       */
#include <rtx51tny.h>                 /* RTX-51 tiny functions & defines      */

#define  OLEN  8                      /* size of serial transmission buffer   */
unsigned char  ostart;                /* transmission buffer start index      */
unsigned char  oend;                  /* transmission buffer end index        */
idata    char  outbuf[OLEN];          /* storage for transmission buffer      */
unsigned char  otask = 0xff;          /* task number of output task           */

#define  ILEN  8                      /* size of serial receiving buffer      */
unsigned char  istart;                /* receiving buffer start index         */
unsigned char  iend;                  /* receiving buffer end index           */
idata    char  inbuf[ILEN];           /* storage for receiving buffer         */
unsigned char  itask = 0xff;          /* task number of output task           */

#define   CTRL_Q  0x11                /* Control+Q character code             */
#define   CTRL_S  0x13                /* Control+S character code             */

bit   sendfull;                       /* flag: marks transmit buffer full     */
bit   sendactive;                     /* flag: marks transmitter active       */
bit   sendstop;                       /* flag: marks XOFF character           */

/******************************************************************************/
/*       putbuf:  write a character to SBUF or transmission buffer            */
/******************************************************************************/
void putbuf (char c)  {
  if (!sendfull)  {                   /* transmit only if buffer not full     */
    if (!sendactive && !sendstop)  {  /* if transmitter not active:           */
      sendactive = 1;                 /* transfer the first character direct  */
      SBUF = c;                       /* to SBUF to start transmission        */
    }
    else  {                           /* otherwize:                           */
      outbuf[oend++ & (OLEN-1)] = c;  /* transfer char to transmission buffer */
      if (((oend ^ ostart) & (OLEN-1)) == 0)  sendfull = 1;
    }                                 /* set flag if buffer is full           */
  }
}


/******************************************************************************/
/*       putchar:  interrupt controlled putchar function                      */
/******************************************************************************/
char putchar (char c)  {
  if (c == '\n')  {                   /* expand new line character:           */
    while (sendfull)  {               /* wait for transmission buffer empty   */
      otask = os_running_task_id ();  /* set output task number               */
      os_wait (K_SIG, 0, 0);          /* RTX-51 call: wait for signal         */
      otask = 0xff;                   /* clear output task number             */
    }
    putbuf (0x0D);                    /* send CR before LF for <new line>     */
  }
  while (sendfull)  {                 /* wait for transmission buffer empty   */
    otask = os_running_task_id ();    /* set output task number               */
    os_wait (K_SIG, 0, 0);            /* RTX-51 call: wait for signal         */
    otask = 0xff;                     /* clear output task number             */
  }
  putbuf (c);                         /* send character                       */
  return (c);                         /* return character: ANSI requirement   */
}


/******************************************************************************/
/*       _getkey:  interrupt controlled _getkey                               */
/******************************************************************************/
char _getkey (void)  {
  while  (iend == istart)  {
    itask = os_running_task_id ();    /* set input task number                */
    os_wait (K_SIG, 0, 0);            /* RTX-51 call: wait for signal         */
    itask = 0xff;                     /* clear input task number              */
  }
  return (inbuf[istart++ & (ILEN-1)]);
}


/******************************************************************************/
/*       serial:  serial receiver / transmitter interrupt                     */
/******************************************************************************/
void serial (void) interrupt 4 using 2  { /* use registerbank 2 for interrupt */
  unsigned char c;
  bit   start_trans = 0;

  if (RI)  {                         /* if receiver interrupt                 */
    c = SBUF;                        /* read character                        */
    RI = 0;                          /* clear interrupt request flag          */
    switch (c)  {                    /* process character                     */
      case CTRL_S:
        sendstop = 1;                /* if Control+S stop transmission        */
        break;

      case CTRL_Q:
        start_trans = sendstop;      /* if Control+Q start transmission       */
        sendstop = 0;
        break;

      default:                       /* read all other characters into inbuf  */
        if (istart + ILEN != iend)  {
          inbuf[iend++ & (ILEN-1)] = c;
        }
                                     /* if task waiting: signal ready         */
        if (itask != 0xFF) isr_send_signal (itask);
        break;
    }
  }

  if (TI || start_trans)  {          /* if transmitter interrupt              */
    TI = 0;                          /* clear interrupt request flag          */
    if (ostart != oend)  {           /* if characters in buffer and           */
      if (!sendstop)  {              /* if not Control+S received             */
        SBUF = outbuf[ostart++ & (OLEN-1)];      /* transmit character        */
        sendfull = 0;                /* clear 'sendfull' flag                 */
                                     /* if task waiting: signal ready         */
        if (otask != 0xFF)  isr_send_signal (otask);
      }
    }
    else sendactive = 0;             /* if all transmitted clear 'sendactive' */
  }

}


/******************************************************************************/
/*       serial_init: initialize serial interface                             */
/******************************************************************************/
void serial_init (void)  {
  SCON  = 0x50;                      /* mode 1: 8-bit UART, enable receiver   */
  TMOD |= 0x20;                      /* timer 1 mode 2: 8-Bit reload          */
  TH1   = 0xf3;                      /* reload value 2400 baud                */
  TR1   = 1;                         /* timer 1 run                           */
  ES    = 1;                         /* enable serial port interrupt          */
}
2008-08-07 15:27
jy02163807
Rank: 1
等 级:新手上路
帖 子:52
专家分:3
注 册:2007-12-20
收藏
得分:0 
原来这就是汇编!

力拔山兮气盖世
2008-09-15 13:05
你们都要疼我哦
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:火星
等 级:贵宾
威 望:49
帖 子:1296
专家分:2746
注 册:2008-7-13
收藏
得分:0 
  汇编啥时候跟C长的一个模样了.

小妹,哥哥看你骨骼清奇,绝非凡人,将来必成大业,不如这样,你先把裤裤脱了,待哥哥为你开启灵窍,然后我们一起努力钻研如何
2008-09-17 20:42
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
建议楼主先学学基础 呵呵
2008-09-18 18:12
I喜欢c
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:64
帖 子:1749
专家分:0
注 册:2007-3-2
收藏
得分:0 


真受不了...

 我是指针,却丢失了目标地址!          我是循环,却缺少了结束条件!      我是函数,却没有人来调用!   
2008-09-19 22:52
大禹治水
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-9-20
收藏
得分:0 
这不是C么
2008-09-20 01:18
随心
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:12
帖 子:2577
专家分:250
注 册:2007-8-12
收藏
得分:0 

天之道,利而不害。圣人之道,为而不争。信言不美,美言不信。善者不辩,辩者不善。知者不博,博者不知。
2008-09-20 01:49
yunqiu
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-9-22
收藏
得分:0 
你用的是那种单片机,不同的单片机,汇编指令不同
2008-09-22 02:35
hb1122
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-3-28
收藏
得分:0 

偶很笨,但偶很好学,偶很菜,但偶很努力! 希望能得到好多好心的好人帮偶学习! 呵呵!!
2008-09-23 11:28
快速回复:单片机控制
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019158 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved