Chinaunix首页 | 论坛 | 博客
  • 博客访问: 416573
  • 博文数量: 55
  • 博客积分: 167
  • 博客等级: 入伍新兵
  • 技术积分: 1167
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-28 10:20
个人简介

一个算是正常的中国码农!

文章分类

全部博文(55)

文章存档

2014年(1)

2013年(31)

2012年(23)

我的朋友

分类:

2012-12-14 16:11:00

程序功能:
遥控器按A、B、C、D四个按键,串口输出不同的信息
SC2272和单片机接口为D0、D1、D2、D3、VT和单片机P1.0-P1.5。
没有按遥控器时,5个输出口为低电平。
VT表示电平有效,例如:
按B键时,D0和VT输出均为高电平。()
程序如下:
#include "reg52.h"
#define DISTIME_DELAY 500000
#define LZ_WIRELESS_DEBUG 0
unsigned char in_buff;
char flag_send;
char timer0_count;
char code str[]=
{
"\r\n\
##########################################\r\n\
hardware is stc89c52\r\n\
software is keil c code\r\n\
SC2272-M4 TEST!\r\n\
write by:Mr Lee\r\n\
##########################################\r\n"
};
void uart_init()
{
   TMOD=0x20;     //TMOD=0
   TH1=0xf3;      //12MHZ   ,BPS:4800,N,8,1,0xf3=243
   TL1=0xf3;
   PCON=0x80;     //方式一,8位数据位,一位起始位和一位结束位
   TR1=1;         //
   SCON=0x40;     //串口通信控制寄存器  模式一
}
void timer0_init(void)
{
  //TMOD=0x01;//选定时器0,工作方式1,16位计数模式
  TMOD|=0x01;
  TH0=(65535-DISTIME_DELAY)/255;//设置初始值
  TL0=(65535-DISTIME_DELAY)%255;

  EA=1;//打开总中断
  ET0=1;//打开定时器0中断
  TR0=1;//定时器零开始计数
}
void uart_putchar(unsigned char dat)
{
SBUF=dat; //把数据送给sbuf缓存器中
while(TI!=1);//发送标志位 TI如果发送了为1,没发送为0,没发送等待,到了退出循环
TI=0;  //到了,TI清为0

}
void uart_printf(unsigned char *buff)
{

while(*buff)
        uart_putchar(*buff++);
}
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i
{
for(j=0;j<121;j++)
{;}
}
}

void main()
{
uart_init();
timer0_init();
uart_printf(str);
while(1)
{
if((P1&0x10)==0x10)
{
in_buff=(P1&0x0f);
if(flag_send==1)
{
flag_send=0;
switch(in_buff)
{
case 1:{uart_printf("key B is pressed\r\n");}break;
case 2:{uart_printf("key D is pressed\r\n");}break;
case 4:{uart_printf("key A is pressed\r\n");}break;
case 8:{uart_printf("key C is pressed\r\n");}break;
default:
{
uart_printf("wireless is error!\r\n");
}break;
}
#if LZ_WIRELESS_DEBUG==1
uart_putchar(in_buff);
uart_printf("\r\n");
#endif
}
}
else
{
if(flag_send==1)
{
flag_send=0;
uart_printf("no key is pressed\r\n");
}
}
}

}
void timer0() interrupt 1
{
TH0=(65535-DISTIME_DELAY)/255;
        TL0=(65535-DISTIME_DELAY)%255;
timer0_count++;
if(timer0_count>=20)
{
timer0_count=0;
flag_send=1;
}
}
图片如下:
阅读(966) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~