本人是电子小白,之前接触过51单片机,现在才学avr ,求大神帮忙修改一段程序,将51的程序移植到atmega16上使用。改好必有重谢
程序如下:
#include<reg52.h>
#include<math.h>
#define uint unsigned int
#define uchar unsigned char
uchar m=0;
uchar u;
int flag0=0;
int flag1=0;
uchar n=0;
uchar v;
uint i;
uint x;
uint y;
uint c0,c1; //相位数据结果保存
float t0,f0,t1,f1;
uint a[]={70,61,48,48,48,48,48,46,48,48,72,90}; //频率数据结果保存
uint b[]={70,61,48,48,48,48,48,46,48,48,72,90}; //频率数据结果保存
//F,=,0,0,0,0,0,.,0,0,H,Z
sbit lcdrw=P2^1;
sbit lcdrs=P2^0;
sbit lcden=P2^2;
sbit Led=P2^3;
sbit LED_b=P1^0;
sbit LED_g=P1^1;
sbit LED_r=P1^2;
sbit K1=P1^3;
sbit K2=P1^4;
sbit K3=P1^5;
sbit K4=P1^6;
void Key_Judge();
//3Line_A 端口定义
sbit P_DATA_3A = P2^4; //定义数据传输端口
sbit CS_3A = P2^5; //定义片选信号端口
sbit CLK_3A = P2^6; //定义时钟端口
unsigned int ADJ_NUM ;
#define H 1 //定义1为高电平
#define L 0 //定义0为低电平
bit B_DATA; //传输数据一位
/////// //////////LCD1602的设置//////////////////////////////////
void delay(uint s)//延时1MS
{
uint r,g;
for(r=s;r>0;r--)
for(g=110;g>0;g--);
}
void Delay_10us(unsigned char count) //n*10US 延时子程序
{
unsigned char i;
unsigned char j;
for(i=count;i>0;i--)
for(j=5;j>0;j--)
;
}
void write_com(uchar com) // 写指令
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date) // 写数据
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void DisplayOneChar( unsigned char X, unsigned char Y, unsigned char in)
{
X &= 0x1; //限制X值为 0 和 1
Y &= 0xF; //限制Y值为 0 - 15
if (X)
{Y|=0x40;} //当要显示第二行时地址码+0x40;
Y |= 0x80; //得出列位置
write_com(Y); //写地址
write_data(in); //写数据
}
void initlcd()//初始化
{
lcdrw=0;
lcden=0;
write_com(0x01);//显示清屏
write_com(0x38);//显示模式设置
write_com(0x0c); //显示开,开游标,不闪烁
write_com(0x06); // 字符时整体不移动
}
////////////LCD1602设置///////////////
///////////定时器的设置//////////////
void CT_init() //定时计数器初始化
{ TMOD=0x99; //GATE=1,T1、T0工作在方式1,定时方式
TH0=0; //定时计数器初值清零
TL0=0;
TH1=0;
TL1=0;
TR0=1; //TR0,TR1置位,此时定时计数器的启动有INT0,INT1引脚电平决定
TR1=1;
ET0=1; //开中断
ET1=1;
}
void ITC0() interrupt 0 //外部中断0服务程序
{
u=m; //读定时计数器0溢出次数
m=0; //溢出次数清零
x=TH0*256+TL0; //读定时计数器0当前值
TH0=0; //定时计数器0清零
TL0=0;
}
void ITC1() interrupt 2
{
v=n;
n=0;
y=TH1*256+TL1;
TH1=0; //定时计数器1清零
TL1=0;
}
void TIME0() interrupt 1 //定时计数器0溢出中断
{
m++;
//溢出次数加1
}
void TIME1() interrupt 3
{
n++;
}
int pinlvfl0(float f0)
{
if((f0>0)&&(f0<200))
flag0=0;
else if((f0>200)&&(f0<400))
flag0=1;
else if((f0>400)&&(f0<600))
flag0=2;
return flag0;
}
int pinlvfl1(float f1)
{
if((f1>10)&&(f1<1000))
flag1=0;
else if((f1>1000)&&(f1<3000))
flag1=1;
else if((f1>3000)&&(f1<7000))
flag1=2;
return flag1;
}
/*--------------------------------------
;模块名称:Line_3A(void)
;功 能:实现三线单字节低位在前串口通信函数适用于WT588D
;入 参:无
;出 参:0 返回主函数
;-------------------------------------*/
void Line_3A(unsigned char dat)
{
unsigned char i,key_copy = 0X00;
P_DATA_3A = 1;
CLK_3A = H; //拉高
CS_3A= L; //拉低片选信号
delay(5); //5ms
B_DATA = dat&0X01;
for(i=0;i<8;i++)
{
CLK_3A = L; // 拉低
P_DATA_3A = B_DATA; //传输数据一位
Delay_10us(15); //延时150us
CLK_3A = H; //拉高
Delay_10us(15); //延时150us
dat = dat>>1;
B_DATA = dat&0X01;
}
P_DATA_3A = 1; //拉高电平
CS_3A = H;
CLK_3A = H;
}
void Key_Judge()
{
for(;;)
{
if((flag0==0)&&(flag1==0)) //假定两张卡片位红色
{
if(K1==0)
Line_3A(1); //读取对应语音
else if(K2==0)
Line_3A(2);
else if(K3==0)
Line_3A(3);
else if(K4==0)
Line_3A(4);
}
if((flag0==0)&&(flag1==1)) //假定两张卡片红蓝
{
if(K1==0)
Line_3A(5);
else if(K2==0)
Line_3A(6);
else if(K3==0)
Line_3A(7);
else if(K4==0)
Line_3A(8);
}
if((flag0==0)&&(flag1==2)) //假定两张卡片位红绿
{
if(K1==0)
Line_3A(9);
else if(K2==0)
Line_3A(10);
else if(K3==0)
Line_3A(11);
else if(K4==0)
Line_3A(12);
}
if((flag0==1)&&(flag1==0)) //假定两张卡片位蓝红
{
if(K1==0)
Line_3A(13);
else if(K2==0)
Line_3A(14);
else if(K3==0)
Line_3A(15);
else if(K4==0)
Line_3A(16);
}
if((flag0==1)&&(flag1==1)) //假定两张卡片位蓝蓝
{
if(K1==0)
Line_3A(17);
else if(K2==0)
Line_3A(18);
else if(K3==0)
Line_3A(19);
else if(K4==0)
Line_3A(20);
}
if((flag0==1)&&(flag1==2)) //假定两张卡片位蓝绿
{
if(K1==0)
Line_3A(21);
else if(K2==0)
Line_3A(22);
else if(K3==0)
Line_3A(23);
else if(K4==0)
Line_3A(24);
}
if((flag0==2)&&(flag1==0)) //假定两张卡片绿红
{
if(K1==0)
Line_3A(25);
else if(K2==0)
Line_3A(26);
else if(K3==0)
Line_3A(27);
else if(K4==0)
Line_3A(28);
}
if((flag0==2)&&(flag1==1)) //假定两张卡片位绿蓝
{
if(K1==0)
Line_3A(29);
else if(K2==0)
Line_3A(30);
else if(K3==0)
Line_3A(31);
else if(K4==0)
Line_3A(32);
}
if((flag0==2)&&(flag1==2)) //假定两张卡片位绿绿
{
if(K1==0)
Line_3A(33);
else if(K2==0)
Line_3A(34);
else if(K3==0)
Line_3A(35);
else if(K4==0)
Line_3A(36);
} break;
}
}
////////////定时器的设置//////////////
void main()
{
initlcd(); //液晶显示初始化
CT_init(); //定时计数器初始化
EA=1; //开总中断
EX0=1; //允许外部中断
EX1=1;
IT0=1; //设置外部中断方式为下降沿触发
IT1=1;
P3=0xff;
//P1=0x00;
while(1)
{
t0=u*65536+x; //计算脉冲时间宽度
f0=1000000/(2*t0); //计算频率
pinlvfl0(f0);
delay(10);
t1=v*65536+y; //计算脉冲时间宽度
f1=1000000/(2*t1); //计算频率
pinlvfl1(f1);
delay(10);
Key_Judge();
switch(flag0)
{
case 0:
{ DisplayOneChar(0, 0,'1');
DisplayOneChar(0, 2,'I');
DisplayOneChar(0, 3,'\'');
DisplayOneChar(0, 4,'m');
DisplayOneChar(0, 6,'r');
DisplayOneChar(0, 7,'e');
DisplayOneChar(0, 8,'d');
}
break;
case 1:
{ DisplayOneChar(0, 0,'1');
DisplayOneChar(0, 2,'I');
DisplayOneChar(0, 3,'\'');
DisplayOneChar(0, 4,'m');
DisplayOneChar(0, 6,'b');
DisplayOneChar(0, 7,'l');
DisplayOneChar(0, 8,'u');
DisplayOneChar(0, 9,'e');
}
break;
case 2:
{
DisplayOneChar(0, 0,'1');
DisplayOneChar(0, 2,'I');
DisplayOneChar(0, 3,'\'');
DisplayOneChar(0, 4,'m');
DisplayOneChar(0, 6,'g');
DisplayOneChar(0, 7,'r');
DisplayOneChar(0, 8,'e');
DisplayOneChar(0, 9,'e');
DisplayOneChar(0, 10,'n');
}
break;
default :
break;
}
Led=0;
delay(10);
Led=1;
//
switch(flag1)
{
case 0:
{ DisplayOneChar(1, 0,'2');
DisplayOneChar(1, 2,'I');
DisplayOneChar(1, 3,'\'');
DisplayOneChar(1, 4,'m');
DisplayOneChar(1, 6,'r');
DisplayOneChar(1, 7,'e');
DisplayOneChar(1, 8,'d');
}
break;
case 1:
{ DisplayOneChar(1, 0,'2');
DisplayOneChar(1, 2,'I');
DisplayOneChar(1, 3,'\'');
DisplayOneChar(1, 4,'m');
DisplayOneChar(1, 6,'b');
DisplayOneChar(1, 7,'l');
DisplayOneChar(1, 8,'u');
DisplayOneChar(1, 9,'e');
}
break;
case 2:
{
DisplayOneChar(1, 0,'2');
DisplayOneChar(1, 2,'I');
DisplayOneChar(1, 3,'\'');
DisplayOneChar(1, 4,'m');
DisplayOneChar(1, 6,'g');
DisplayOneChar(1, 7,'r');
DisplayOneChar(1, 8,'e');
DisplayOneChar(1, 9,'e');
DisplayOneChar(1, 10,'n');
}
break;
default :
break;
}
}
}
|