亲,帮看一下keil中C程序错误。百度了不知道怎么改呀。。。。
#include <reg52.h>#define uint unsigned int
#define uchar unsigned char
#define max_frequency 108000 //搜索最大频率
#define min_frequency 76000 //搜索最小频率
sbit sda=P2^6;
sbit scl=P2^7;
sbit key1=P3^2;
sbit key2=P3^3;
sbit dula=P2^6; //定义锁存器锁存端
sbit wela=P2^7;
sbit rs=P1^0; //定义1602液晶RS端
sbit lcden=P2^5; //定义1602液晶LCDEN端(使能端)
sbit rw=P1^1; //定义1602数据/命令选择端
/*-----------------------------------------------
全局变量
-----------------------------------------------*/
uint frequency;
uchar radio_write_data[5]={0xae,0x93,0x20,0x17,0x00};
uchar radio_read_data[5];
uint pll;
/*-----------------------------------------------
函数声明
-----------------------------------------------*/
void init();
void start();
void response();
void writebyte(uchar date);
void stop();
uchar readbyte();
void radio_write();
void radio_read();
void get_frequency();
void get_pll();
void search();
void auto_search();
void delay1(uint xms);
void delay();
void FM_display(); //液晶显示
void write_com(uchar com); //1602液晶写命令
void write_data(uchar data); //1602液晶读命令
/*-----------------------------------------------
延时函数
-----------------------------------------------*/
void delay1(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
/*------------------------------------------------------------------------------------
1602液晶显示
------------------------------------------------------------------------------------*/
void write_com(uchar com)
{
rs=0;
rw=0;
lcden=0;
P0=com;
delay1(5);
lcden=1;
delay1(5);
lcden=0;
}
void write_data(uchar data)
{
rs=1;
rw=0;
lcden=0;
P0=data;
delay1(5);
lcden=1;
delay1(5);
lcden=0;
}
/*----------------------------------------------------------------------------
I2C总线
----------------------------------------------------------------------------*/
void delay()
{;;}
/*-----------------------------------------------
I2C总线初始化
-----------------------------------------------*/
void init()
{
scl=1;
delay();
sda=1;
delay();
}
/*-----------------------------------------------
I2C总线起始函数
-----------------------------------------------*/
void start()
{
scl=1;
delay();
sda=1;
delay();
sda=0;
delay();
}
/*------------------------------------------------
I2C总线停止函数
------------------------------------------------*/
void stop()
{
scl=1;
delay();
sda=0;
delay();
sda=1;
delay();
}
/*-------------------------------------------------
I2C应答信号
-------------------------------------------------*/
void response()
{
uchar i=0;
scl=1;
delay();
while((sda==1)&&(i<256))
i++;
scl=0;
delay();
}
/*--------------------------------------------------
I2C总线写一个字节
--------------------------------------------------*/
void writebyte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
/*--------------------------------------------------
I2C总线读一个字节
--------------------------------------------------*/
uchar readbyte()
{
uchar i,k;
scl=0;
delay();
sda=1;
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
delay();
return k;
}
/*---------------------------------------------------
由频率获得pll值
---------------------------------------------------*/
void get_pll()
{
uchar hlsi;
hlsi=radio_write_data[2]&0x10;
if (hlsi)
pll=(unsigned int)((float)((frequency+225)*4)/(float)32.768); //频率单位:kHz
else
pll=(unsigned int)((float)((frequency-225)*4)/(float)32.768); //频率单位:kHz
}
/*----------------------------------------------------
由pll值获得频率 frequency
----------------------------------------------------*/
void get_frequency()
{
uchar hlsi;
uint npll=0;
npll=pll;
hlsi=radio_write_data[2]&0x10;
if (hlsi)
frequency=(unsigned long)((float)(npll)*(float)8.192-225); //频率单位:KHz
else
frequency=(unsigned long)((float)(npll)*(float)8.192+225); //频率单位:KHz
}
/*-----------------------------------------------------
手动搜台 mode=1,+0.1MHz; mode=0,-0.1MHz 不考虑MUTE,SM位
-----------------------------------------------------*/
void search(bit mode)
{
radio_read();
if(mode)
{
frequency+=100; //频率单位:KHz
if(frequency>max_frequency)
frequency=min_frequency;
}
else
{
frequency-=100; //频率单位:KHz
if(frequency<min_frequency)
frequency=max_frequency;
}
get_pll();
radio_write_data[0]=pll/256;
radio_write_data[1]=pll%256;
if(radio_read_data[2]&0x80)
radio_write_data[2]=0x20; //向下调
else
radio_write_data[2]=0x28; //从搜索到的信号读取是否是立体声
radio_write_data[3]=0x17;
radio_write_data[4]=0x00;
radio_write();
FM_display();
}
/*-----------------------------------------------------
1602显示频率函数
-----------------------------------------------------*/
void FM_display()
{
uint FM;
dula=0; //初始化,数码管关闭,防止液晶显示屏出现乱码
wela=0;
lcden=0;
write_com(0x38); //设置16x2显示,5x7点阵,8位数据接口
write_com(0x0c); //设置开显示,不显示光标
write_com(0x06); //写一个字符后地址指针加1
write_com(0x01); //显示清0,数据指针清0
FM=frequency;
write_com(0x80); //数据指针位置,1602显示屏第一行
write_data('F');
write_data('M');
write_data(' ');
write_com(0x80+0x04);
if(FM/100000)
write_data('0'+FM/100000);
else
write_data(' ');
write_com(0x80+0x05);
write_data('0'+(FM%100000)/10000);
write_com(0x80+0x06);
write_data('0'+(FM%10000)/1000);
write_com(0x80+0x07);
write_data('.');
write_com(0x80+0x08);
write_data('0'+(FM%1000)/100);
write_com(0x80+0x09);
write_data('0'+(FM%100)/10);
write_com(0x80+0x10);
write_data('0'+(FM%10)/1);
write_com(0x80+0x11);
write_data(' ');
write_com(0x80+0x12);
write_data('M');
write_com(0x80+0x13);
write_data('H');
write_com(0x80+0x14);
write_data('z');
}
/*-----------------------------------------------------
自动搜台 mode=1,+0.1MHz; mode=0,-0.1MHz 不考虑MUTE,SM位
-----------------------------------------------------*/
void auto_search(bit mode)
{
bit Blf; //Blf=1,搜索到波段极限;Blf=0,没有搜索到波段极限
radio_read();
if(Blf)
{
if(mode) //判断波段是否在76000----108000之间
frequency=min_frequency;
else
frequency=max_frequency;
}
else //即使是自动搜索,最好也添加下面的加减,防止搜索到同一个电台
{
if(mode)
frequency+=100;
else
frequency-=100;
}
get_pll();
if(mode)
radio_write_data[2]=0xa8;
else
radio_write_data[2]=0x28;
radio_write_data[0]=pll/256+0xc0;
radio_write_data[1]=pll%256;
radio_write_data[3]=0x17;
radio_write_data[4]=0x00;
radio_write();
radio_read();
while((!radio_read_data[0]&0x80)) //搜台成功标志
{
radio_read();
FM_display();
}
if((radio_read_data[0]&0x40)==0x40)
Blf=1;
else
Blf=0;
get_pll();
if(radio_read_data[2]&0x80)
radio_write_data[2]=0x20; //向下调
else
radio_write_data[2]=0x28; //从搜索到的信号读取是否是立体声
radio_write_data[3]=0x17;
radio_write_data[4]=0x00;
radio_write();
}
/*-----------------------------------------------------
向Tea5767芯片写一个数据
-----------------------------------------------------*/
void radio_write()
{
uint i;
qidong:start();
writebyte(0xc0);
if(response!=1)
goto qidong;
for(i=0;i<5;i++)
{
writebyte(radio_write_data[i]);
if(response!=1)
goto qidong;
}
stop();
}
/*------------------------------------------------------
读取Tea5767芯片中的一个数据
------------------------------------------------------*/
void radio_read()
{
uint i;
uchar temp_l,temp_h;
qidong:start();
writebyte(0xc1);
if(response!=1)
goto qidong;
for(i=0;i<5;i++)
{
radio_read_data[i]=readbyte();
if(response!=1)
goto qidong;
}
stop();
temp_h=radio_write_data[0];
temp_l=radio_write_data[1];
temp_h&=0x3f;
pll=256*temp_h+temp_l;
get_frequency();
}
/*------------------------------------------------------
主函数
------------------------------------------------------*/
void main()
{
init();
radio_write();
radio_read();
}