微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > AT89C2051利用中断产生PWM

AT89C2051利用中断产生PWM

时间:11-25 来源:互联网 点击:
原理图


#include "REG2051.H"
#include "intrins.h"
#define uint unsigned int
#define uchar unsigned char

const TH_600=0xfd;
const TL_600=0xd4;
const TH_1200=0xfb;
const TL_1200=0x7c;
const TH_2400=0xf6;
const TL_2400=0xa0;

sbit PWM=P3^7;
bit ld=0; //上一次发送的数据
uchar _pool[10]; //数据头
uchar _length; //当前数组长度
uchar _index; //当前索引
uchar _bit_index; //位索引

void onKeyDown(uchar,uchar);
void push(uchar);
void init();

void main(){
unsigned int r,c,prsd;
init();
while(1){
P1=0x0f;
if(P1!=0x0f){
r=~P1-0xf0;
P1=0xf0;
c=~(P1>>4)-0xf0;
if(!prsd){
onKeyDown(r,c);
prsd=1;
}
}else{
prsd=0;
}
}
}

void init(){
_length=0;

PWM=0;//启动时低电平
TMOD=0x01;
TH0=0x9E;
TL0=0x57;
ET0=1;
TF0=1;
TR0=1; //打开计时器
}

void push(uchar xcode){
_pool[_length]=xcode;
_length++;
}

void reset(){
_index=0;
_length=0;
_bit_index=0;
}

void onKeyDown(uchar row,uchar column){
unsigned char xcode;
reset();
push(0xaa); //压入地址码
xcode=row<4+column;
push(xcode); //压入键盘数据
EA=1;
while(EA);
}

void timer_pwm() interrupt 1{
uchar current,tmp;
if(PWM){
PWM=~PWM;
TH0=TH_600;
TL0=TL_600;
}else{
if(_index==_length){
EA=0;
}else{
PWM=~PWM;
_bit_index++;
current=_pool[_index];
tmp=current<_bit_index;
ld=CY;
if(ld){
TH0=TH_1200;
TL0=TL_1200;
}else{
TH0=TH_600;
TL0=TL_600;
}
}
if(_bit_index==8){
_bit_index=0;
_index++;
}
}
}

产生的波形

1200ms为1

600us为



根据NEC协议制作的波形图

#include "REG2051.H"

#define uint unsigned int
#define uchar unsigned char

sbit PWM=P3^7;
uchar code Pcode[]={0x16,0x0C,0x18,0x5E,0x08,0x1C,0x5A,0x42,0x52,0x4A,0x47,0x44,0x40};

void onKeyDown(uchar,uchar);
void write_data(uchar);
void start();
void delay_560(void);
void delay_1690(void);
void delay_4500(void);

void main(){
uint r,c;
PWM=0;
while(1){
P1=0x0f;
if(P1!=0x0f){
r=~P1-0xf0;
P1=0xf0;
c=~(P1>>4)-0xf0;
onKeyDown(r,c);
}
}
}

void onKeyDown(uchar row,uchar column){
uchar cmd,addr;
cmd=Pcode[row*4+column];
addr=0xaa;
start();
write_data(addr);
write_data(~addr);
write_data(cmd);
write_data(~cmd);
}

void start(){
PWM=1;
delay_4500();
delay_4500();
PWM=0;
delay_4500();
}

void write_data(uchar d){
uchar i;
for(i=0;i<8;i++){
d<=1;
if(CY){
PWM=1;
delay_560();
PWM=0;
delay_1690();
}else{
PWM=1;
delay_560();
PWM=0;
delay_560();
}
}
}

void delay_560(void)
{
unsigned char i,j,k;
for(i=31;i>0;i--)
for(j=3;j>0;j--)
for(k=1;k>0;k--);
}

void delay_1690(void)
{
unsigned char i,j,k;
for(i=211;i>0;i--)
for(j=1;j>0;j--)
for(k=1;k>0;k--);
}

void delay_4500(void)
{
unsigned char i,j,k;
for(i=26;i>0;i--)
for(j=34;j>0;j--)
for(k=1;k>0;k--);
}



Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top