|
楼主 |
发表于 2007-5-8 20:34:14
|
显示全部楼层
这是我现在整理后的程序,希望fhqmc 有空的话看看,看看我这样来读电脑发过来的数据对吗?我这样发送数据给电脑对吗?我自己做的键盘不是和电脑一起启动的,是电脑和另一个键盘启动后,我拔掉它插上去调试的,这样可以吗?
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/signal.h>
#include <stdio.h>
//#define 时钟线 PB0
//#define 数据线 PB7
unsigned char del_1,key_data,key_down,got,se_data,key,key_;
void send_0(void) //发送启事位
{
DDRB|=_BV(PB7);
DDRB|=_BV(PB0);
PORTB&=~_BV(PB7);
_delay_us(20);
PORTB&=~_BV(PB0);
_delay_us(40);
PORTB|=_BV(PB0);
_delay_us(20);
DDRB&=~_BV(PB7);
if((PINB&0x80)==0x00) //发完一个位后判断数据线是否为底,否--继续发送
got=1;
}
void send_1(void) //发送结束位
{
DDRB|=_BV(PB7);
DDRB|=_BV(PB0);
PORTB|=_BV(PB7);
_delay_us(20);
PORTB&=~_BV(PB0);
_delay_us(40);
PORTB|=_BV(PB0);
_delay_us(20);
DDRB&=~_BV(PB7);
if((PINB&0x80)==0x00) //发完一个位后判断数据线是否为底,否--继续发送
got=1;
}
void send(unsigned char dat_sent)
{
char fg_i;
for(fg_i=0;fg_i<8;fg_i++)
{
DDRB|=_BV(PB7);
DDRB|=_BV(PB0);
if(dat_sent&0x80)
PORTB|=_BV(PB7);
else
PORTB&=~_BV(PB7);
_delay_us(20);
PORTB&=~_BV(PB0);
_delay_us(40);
PORTB|=_BV(PB0);
_delay_us(20);
dat_sent<<=1;
DDRB&=~_BV(PB7);
if((PINB&0x80)==0x00) //发完一个位后判断数据线是否为底,否--继续发送
{
got=1; //发完一个位后判断数据线是否为低标志位
fg_i=9;
}
}
}
void on_of(unsigned char jiou) //发送奇偶位
{
unsigned char fh,fg;
for(fh=0;fh<8;fh++)
{
if(jiou&0x80)
fg++;
jiou<<=1;
}
if(fg%2==1) send_0();
else send_1();
}
void send_yda(unsigned char dat_fa) //应答电脑回复0xfa数
{
char fg_i;
for(fg_i=0;fg_i<8;fg_i++)
{
DDRB|=_BV(PB7);
DDRB|=_BV(PB0);
if(dat_fa&0x80)
PORTB|=_BV(PB7);
else
PORTB&=~_BV(PB7);
_delay_us(20);
PORTB&=~_BV(PB0);
_delay_us(40);
PORTB|=_BV(PB0);
_delay_us(20);
dat_fa<<=1;
DDRB&=~_BV(PB7);
if((PINB&0x80)==0x00) //发完一个位后判断数据线是否为低,否--继续发送
{
got=1; //发完一个位后判断数据线是否为低标志位
fg_i=9;
}
}
}
void read_data(void) //接收电脑发过来的数据,数据忽略
{
unsigned char ten;
for(ten=0;ten<10;ten++)
{
_delay_us(20);
DDRB|=_BV(PB0);
PORTB&=~_BV(PB0);
_delay_us(40);
PORTB|=_BV(PB0);
_delay_us(20);
}
}
SIGNAL(SIG_OVERFLOW0)
{
TCNT0=0X00;
if(((PINC&0x80)==0x00)&&(key_==0))
{
key_down=1;
key_=1;
se_data=0x1c;
}
if(((PINC&0x80)==0x80)&&(key_==1))
{
key_down=2;
key_=0;
}
if(((PINC&0x40)==0x00)&&(key_==0))
{
key_down=1;
key_=2;
se_data=0x32;
}
if(((PINC&0x40)==0x40)&&(key_==2))
{
key_down=2;
key_=0;
}
if(((PINC&0x20)==0x00)&&(key_==0))
{
key_down=1;
key_=3;
se_data=0x21;
}
if(((PINC&0x20)==0x20)&&(key_==3))
{
key_down=2;
key_=0;
}
if(((PINC&0x10)==0x00)&&(key_==0))
{
key_down=1;
key_=4;
se_data=0x23;
}
if(((PINC&0x10)==0x10)&&(key_==4))
{
key_down=2;
key_=0;
}
}
//本程序想在电脑上显示a字符,发送流程是先发送a的通码,再发0xff,接着发a的断码,请问我这样做对吗?PB0接clk PB7接data
int main(void)
{
DDRB=0x00;
PORTB=0XFF;
DDRC=0x00; //pc in
PORTC=0xff;
TIMSK=0x02;//允许溢出中断
TCNT0=0X00;//初值为0
TCCR0=2;//使用64分频
SREG=0x80;//开全中断
while(1)
{
while(1)
{
recheck:
if(key_down==1)
{
DDRB&=~_BV(PB0);
while((PINB&0x01)!=0x00); //判断clk是否为low
_delay_us(50);
while((PINB&0x01)!=0x01) //判断clk是否为high 否--等待
goto recheck;
DDRB&=~_BV(PB7);
if((PINB&0x80)!=0x80) goto read; //判断data是否为high
_delay_us(20); //0
send_0(); //启示位
if(got==1)
{
got=0;
goto read;
}
// se_data=0x1c;
send(se_data); //发a字符
if(got==1)
{
got=0;
goto read;
}
on_of(se_data); //奇偶位
if(got==1)
{
got=0;
goto read;
}
send_1(); //结束位
if(got==1)
{
got=0;
goto read;
}
}
else if(key_down==2)
{
//se_data=0xf0;
_delay_ms(30);
recheck1:
DDRB&=~_BV(PB0);
while((PINB&0x01)!=0x00); //判断clk是否为low
_delay_us(50);
while((PINB&0x01)!=0x01)//判断clk是否为high 否--等待
goto recheck1;
DDRB&=~_BV(PB7);
if((PINB&0x80)!=0x80) goto read; //判断data是否为high
goto read;
_delay_us(20); //0
send_0(); //启示位
if(got==1)
{
got=0;
goto read;
}
send(0xf0); //发a字符
if(got==1)
{
got=0;
goto read;
}
on_of(se_data); //奇偶位
if(got==1)
{
got=0;
goto read;
}
send_1(); //结束位
if(got==1)
{
got=0;
goto read;
}
//se_data=0x1c; //发送A的断码
_delay_ms(30);
recheck2:
DDRB&=~_BV(PB0);
while((PINB&0x01)!=0x00); //判断clk是否为low
_delay_us(50);
while((PINB&0x01)!=0x01)
goto recheck2;
DDRB&=~_BV(PB7);
if((PINB&0x80)!=0x80) goto read; //判断data是否为high
goto read;
_delay_us(20); //0
send_0(); //启示位
if(got==1)
{
got=0;
goto read;
}
send(se_data); //发a字符
if(got==1)
{
got=0;
goto read;
}
on_of(se_data); //奇偶位
if(got==1)
{
got=0;
goto read;
}
send_1(); //结束位
if(got==1)
{
got=0;
}
_delay_ms(30);
goto recheck;
}
read: //键盘读数据
DDRB&=~_BV(PB0);
while((PINB&0x01)!=0x01);//判断clk是否为high 否--等待
read_data(); //读十位数据
DDRB&=~_BV(PB7);
while((PINB&0x80)!=0x80) //判断数据线是否还为低,保持时钟信号
{
DDRB=0XFF;
_delay_us(20);
PORTB&=~_BV(PB0);
_delay_us(40);
PORTB|=_BV(PB0);
_delay_us(20);
}
DDRB&=~_BV(PB0);
while((PINB&0x01)!=0x00); //判断clk是否为low
_delay_us(50);
while((PINB&0x01)!=0x01);//判断clk是否为high 否--等待
DDRB&=~_BV(PB7);
if((PINB&0x80)!=0x80)
if(got==1)
{
got=0;
goto read;
}
se_data=0xfa;
_delay_us(20); //0
send_0(); //启示位
if(got==1)
{
got=0;
goto read;
}
send(se_data); //发a字符
if(got==1)
{
got=0;
goto read;
}
on_of(se_data); //奇偶位
if(got==1)
{
got=0;
goto read;
}
send_1(); //结束位
if(got==1)
{
key=0;
goto recheck;
}
}
}
} |
|