/*copyright 2007,ShangHai HaoTun Electronic Technology co.,Ltd
*
*This sample is used for control light by infrared .
*
*write by Shifang
*
*V1.1
*/
//适用于TC9012的遥控器
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
////////////////////////////////////////////////
#define TURE 1
#define FALSE 0
////////////////////////////////////////////////
sbit IR=P3^2;//红外接口标志
sbit RS = P2^3;//Pin4
sbit RW = P2^2; //Pin5
sbit E = P2^4;//Pin6
#define Data P0 //数据端口
unsigned int hour,minute,second,count;
char code Tab[16]="0123456789ABCDEF";
char data TimeNum[]=" ";
char data Test1[]=" ";
////////////////////////////////////////////
unsigned char irtime;//红外用全局变量
bit irpro_ok,irok;
unsigned char IRcord[4];
unsigned char irdata[33];
void ShowString (unsigned char line,char *ptr);
//////////////////////////////////////////////
void Delay(unsigned char mS);
void Ir_work(void);
void Ircordpro(void);
////////////////////////////////////////////////////////////////
void Delay(unsigned char mS)//delay mS
{
unsigned char us,usn;
while(mS!=0)
{
usn = 4;
while(usn!=0)
{
us=0xf0;
while (us!=0) {us--;};
usn--;
}
mS--;
}
}
//////////////////////////////////////////////////////////////////
//************************************************************* //
//////////////////////////////////////////////////////////////////
void tim0_isr (void) interrupt 1 using 1//定时器0中断服务函数
{
irtime++;
}
///////////////////////////////////////////////////////////////////////
void ex0_isr (void) interrupt 0 using 0//外部中断0服务函数
{
static unsigned char i;
static bit startflag;
if(startflag)
{
if(irtime<63&&irtime>=33)//引导码 TC9012的头码
i=0;
irdata[i]=irtime;
irtime=0;
i++;
if(i==33)
{
irok=1;
i=0;
}
}
else
{irtime=0;startflag=1;}
}
////////////////////////////////////////////////////////////////////
void TIM0init(void)//定时器0初始化
{
TMOD=0x02;//定时器0工作方式2,TH0是重装值,TL0是初值
TH0=0x00;//reload value
TL0=0x00;//initial value
ET0=1;//开中断
TR0=1;
}
///////////////////////////////////////////////////////////////////
void EX0init(void)
{
IT0 = 1; // Configure interrupt 0 for falling edge on /INT0 (P3.2)
EX0 = 1; // Enable EX0 Interrupt
EA = 1;
}
/****************************************************************/
/******************************************************************/
void Ir_work(void)//红外键值散转程序
{
TimeNum[5] = Tab[IRcord[0]/16];
TimeNum[6] = Tab[IRcord[0]%16];
TimeNum[8] = Tab[IRcord[1]/16];
TimeNum[9] = Tab[IRcord[1]%16];
TimeNum[11] = Tab[IRcord[2]/16];
TimeNum[12] = Tab[IRcord[2]%16];
TimeNum[14] = Tab[IRcord[3]/16];
TimeNum[15] = Tab[IRcord[3]%16];
ShowString(1,TimeNum);
irpro_ok=0;
}
/*****************************************************************/
void Ircordpro(void)//红外码值处理函数
{
unsigned char i, j, k;
unsigned char cord,value;
k=1;
for(i=0;i<4;i++)//处理4个字节
{
for(j=1;j<=8;j++) //处理1个字节8位
{
cord=irdata[k];
if(cord>7)//大于某值为1
{
value=value|0x80;
}
else
{
value=value;
}
if(j<8)
{
value=value>>1;
}
k++;
}
IRcord[i]=value;
value=0;
} irpro_ok=1;//处理完毕标志位置1
}
/******************************************************************/
void DelayUs(unsigned char us)//delay us
{
unsigned char uscnt;
uscnt=us>>1;/* Crystal frequency in 12MHz*/
while(--uscnt);
}
/******************************************************************/
void DelayMs(unsigned char ms)//delay Ms
{
while(--ms)
{
DelayUs(250);
DelayUs(250);
DelayUs(250);
DelayUs(250);
}
}
void WriteCommand(unsigned char c)
{
DelayMs(5);//short delay before operation
E=0;
RS=0;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
}
/****************************************************************/
void WriteData(unsigned char c)
{
DelayMs(5); //short delay before operation
E=0;
RS=1;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
RS=0;
}
/*********************************************************************/
void ShowChar(unsigned char pos,unsigned char c)
{
unsigned char p;
if (pos>=0x10)
p=pos+0xb0; //是第二行则命令代码高4位为0xc
else
p=pos+0x80; //是第二行则命令代码高4位为0x8
WriteCommand (p);//write command
WriteData (c); //write data
}
/*************************************************************************/
void ShowString (unsigned char line,char *ptr)
{
unsigned char l,i;
l=line<<4;
for (i=0;i<16;i++)
ShowChar (l++,*(ptr+i));//循环显示16个字符
}
/*********************************************************************/
void InitLcd()
{
DelayMs(15);
WriteCommand(0x38); //display mode
WriteCommand(0x38); //display mode
WriteCommand(0x38); //display mode
WriteCommand(0x06); //显示光标移动位置
WriteCommand(0x0c); //显示开及光标设置
WriteCommand(0x01); //显示清屏
}
/////////////////////////////////////////////////////////////////
void main(void)
{
EX0init(); // Enable Global Interrupt Flag
TIM0init();
InitLcd();//
DelayMs(15);
sprintf(Test1," www.haotun.com ");//the first line
ShowString(0,Test1);
sprintf(TimeNum,"Code ");//the second line
ShowString(1,TimeNum);
while(1)//主循环
{
if(irok)
{
Ircordpro();
irok=0;
}
if(irpro_ok)//step press key
{
Ir_work();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
51C语言编程实例,51单片机的开发实例总共20个
共197个文件
uv2:22个
plg:21个
lst:21个
4星 · 超过85%的资源 需积分: 13 112 下载量 109 浏览量
2008-12-26
20:23:15
上传
评论 1
收藏 194KB RAR 举报
温馨提示
51C语言编程实例 有关STC单片机的开发实例总共20个
资源推荐
资源详情
资源评论
收起资源包目录
51C语言编程实例,51单片机的开发实例总共20个 (197个子文件)
1602 8KB
18b20 7KB
24c02 9KB
矩阵键盘_Uv2.Bak 2KB
数码管动态扫描_Uv2.Bak 2KB
流水灯_Uv2.Bak 2KB
数码管动态扫描_Opt.Bak 1KB
keypad_Opt.Bak 1024B
蜂鸣器_Opt.Bak 1021B
流水灯_Opt.Bak 1020B
中断_Opt.Bak 1015B
矩阵键盘_Opt.Bak 982B
红外解码.c 6KB
24c02_EEPROM读写.c 3KB
解码.c 3KB
18b20温度检测.c 3KB
液晶lcd1602.c 3KB
串口模拟发送.c 2KB
按键程序.c 2KB
串口通讯.c 2KB
矩阵键盘.c 2KB
红外测试.c 1KB
数码管移动.c 929B
脉宽调制.c 836B
输入输出口.c 789B
数码管动态扫描.c 734B
定时器0.c 610B
定时器1.c 604B
蜂鸣器.c 597B
看门狗.c 577B
流水灯.c 573B
继电器试验.c 353B
中断.c 231B
红外解码.hex 5KB
1602.hex 4KB
串口通信.hex 3KB
24c02.hex 2KB
18b20.hex 1KB
红外解码.hex 893B
矩阵键盘.hex 791B
keypad.hex 760B
数码管移动.hex 693B
数码管动态扫描.hex 640B
PWM.hex 368B
红外测试.hex 252B
蜂鸣器.hex 209B
WDT.hex 193B
流水灯.hex 192B
定时器1.hex 185B
定时器0.hex 185B
中断.hex 140B
继电器.hex 135B
IO.hex 100B
IO 834B
keypad 3KB
矩阵键盘.lnp 48B
红外解码.LST 11KB
24c02_EEPROM读写.LST 9KB
解码.LST 7KB
18b20温度检测.LST 6KB
液晶lcd1602.LST 5KB
按键程序.LST 5KB
串口通讯.LST 4KB
串口模拟发送.LST 3KB
矩阵键盘.LST 3KB
红外测试.LST 3KB
数码管移动.LST 3KB
脉宽调制.LST 3KB
数码管动态扫描.LST 2KB
定时器1.LST 2KB
定时器0.LST 2KB
输入输出口.LST 2KB
看门狗.LST 2KB
蜂鸣器.LST 2KB
流水灯.LST 2KB
继电器试验.LST 1KB
中断.LST 1KB
红外解码.M51 18KB
24c02.M51 12KB
1602.M51 11KB
18b20.M51 9KB
红外解码.M51 8KB
keypad.M51 6KB
串口通信.M51 6KB
矩阵键盘.M51 5KB
串口.M51 5KB
红外测试.M51 4KB
数码管移动.M51 4KB
PWM.M51 4KB
数码管动态扫描.M51 4KB
定时器0.M51 3KB
定时器1.M51 3KB
WDT.M51 3KB
蜂鸣器.M51 3KB
流水灯.M51 3KB
中断.M51 3KB
继电器.M51 2KB
IO.M51 2KB
红外解码.OBJ 13KB
24c02_EEPROM读写.OBJ 10KB
共 197 条
- 1
- 2
资源评论
- zaofenger2013-07-0751C语言编程实例,51单片机的开发实例 都是51实例
- wuguan_1112014-06-21非常的不错,对初学者的作用很大
- 色色的夜猫2014-04-24非常的不错,对初学者的作用很大
dumuzhou2000
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功