#include "iic.h"
//#include "stc.h"
#include "intrins.h"
#include "tea5767.h"
void writeonebyte(unsigned char wrdata)
{
unsigned char i; //uchar i=1;
for(i=0;i<8;i++) //while(i)
{ //{
iic_scl=0; //iic_scl=0;
if(wrdata&0x80) iic_sda=1; //if(wrdata&i) iic_data=1;
else iic_sda=0; //else iic_data=0;
delay5us();
iic_scl=1;
delay5us();
iic_scl=0;
wrdata<<=1; //i<<=1;
// delay5us();
}
}
void write_tea5767(unsigned char wraddress,unsigned char *pchar,unsigned char num)
{
unsigned char i;
unsigned char *intchar;
do
{
iic_start();
writeonebyte(wraddress);
} while(!iic_testack());
intchar =pchar;
for(i=0;i<num;i++)
{
writeonebyte(*intchar);
iic_testack();
intchar++;
}
iic_stop();
}
unsigned char readonebyte(void)
{
unsigned char i, readvalue;
for(i=0;i<8;i++)
{
iic_scl=0;
readvalue<<=1;
// delay5us();
iic_sda=1;
delay5us();
iic_scl=1;
delay5us();
if(iic_sda) readvalue|=0x01;
iic_scl=0;
}
return(readvalue);
}
void read_tea5767(unsigned char readaddress,unsigned char *pchar,unsigned char num)
{
unsigned char i;
unsigned char *intchar;
do
{
iic_start();
writeonebyte(readaddress);
}while(!iic_testack());
intchar =pchar;
for(i=0;i<num;i++)
{
*intchar =readonebyte();
intchar++;
if(i==(num-1)) iic_nack();
else iic_mack();
}
iic_stop();
}
/*
unsigned int freqtopll()
{
unsigned int pll;
bit hlsi;
hlsi=send_data[2]&HLSI;
if(hlsi)
{pll=(unsigned int)((freq+225)>>13)*1000;}
else
{pll=(unsigned int)((freq-225)>>13)*1000;}
return(pll);
}
void set_pll(bit hlsi,unsigned long freq,unsigned char *pchar)
{
// unsigned char highpll,lowpll;
unsigned char *intchar =pchar;
union pllstr
{
unsigned int pllvalue;
unsigned char pllarray[2];
}pll;
// bit hlsi;
// hlsi=*(pchar+2)&HLSI;
if(hlsi)
{
pll.pllvalue=(unsigned int)((freq+200)*1000>>13); //previous 225
*(intchar+2)|=HLSI; //HLSI
}
else
{
pll.pllvalue=(unsigned int)((freq-200)*1000>>13); //previous 225
*(intchar+2)&=~HLSI;
}
// return(pll);
// highpll=(unsigned char)(pll>>8);
// lowpll=(unsigned char)(pll&0xff);
*intchar&=0xc0;
*intchar|=(pll.pllarray[0]&0x3f); //question
*(intchar+1)=pll.pllarray[1];
}
unsigned long get_freq(bit hlsi,unsigned char *pchar)
{
unsigned long freq;
// unsigned char highpll,lowpll;
unsigned char *intchar =pchar;
union pllstr
{
unsigned int pllvalue;
unsigned char pllarray[2];
}pll;
pll.pllarray[0]=*intchar&0x3f;
pll.pllarray[1]=*(intchar+1);
// pll=highpll<<8|lowpll;
if(hlsi)
freq=(unsigned long)((float)pll.pllvalue*(float)8.192)-200; //previous 225 //question occure here
else
freq=(unsigned long)((float)pll.pllvalue*(float)8.192)+200; //question occure here
return(freq);
}
*/
//#include "stc12c5206.h"
#include "iic.h"
#include "intrins.h"
//#define nop() _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
void delay5us(void) //operate @24MHz, delay 5us
{
unsigned char i =20;
while(i--);
}
void iic_start(void) //scl 高电平时,sda 从高往低跳
{
// unsigned char x=20;
iic_sda=1;
iic_scl=1;
delay5us();
iic_sda=0;
delay5us();
iic_scl=0;
delay5us();
}
void iic_stop(void) //scl 高电平时,sda 从低往跳高
{
// unsigned char x=20;
iic_sda=0;
iic_scl=1;
delay5us();
iic_sda=1;
delay5us();
}
void iic_mack(void) //应答信号,
{
// unsigned char x=20;
iic_sda=0;
delay5us();
iic_scl=1;
delay5us();
iic_scl=0;
}
void iic_nack(void)
{
// unsigned char x=20;
iic_sda=1;
delay5us();
iic_scl=1;
delay5us();
iic_scl=0;
}
bit iic_testack(void)
{
// unsigned char x=20;
bit flag =0;
iic_sda=1;
delay5us();
iic_scl=1;
delay5us();
if(!iic_sda)flag=1;
else flag=0;
iic_scl=0;
delay5us();
return(flag);
}
/************************************************************************************
MCU TYPE: stc11f02, osc frequence: 24MHz
if used uart, baud rate is 9600 bps
note iic time delay satisfy 5 us after data pin change
*************************************************************************************/
#include "stc.h"
#include "tea5767.h"
#include "uart.h"
sbit led = P1^2; //for test
#define test
#define USUROPE
#define keyuse 0x03
unsigned int i;
unsigned char j;
//unsigned char itest;
unsigned char keytemp;
unsigned char tempbuff;
unsigned char send_data[5]={0x33,0x27,0x08,0x3c,0x40};
unsigned char rece_data[5];
#define WRADDRESS 0XC0
#define RDADDRESS 0XC1
bit keyvalid;
bit read_enable;
void io_init(void)
{
P1M1 = 0XFF;
P1M0 = 0XFF;
}
void delay(void)
{
unsigned char k=200;
while(k--);
}
void int_serve(void) interrupt 2
{
EX1 =0;
PCON &=~0X02;
// return;
}
void uart_serve(void) interrupt 4
{
ES =0;
PCON &=~0X02;
}
void initfm(void)
{
write_tea5767(WRADDRESS,&send_data,5);
}
void main(void)
{
io_init();
initfm();
#ifdef test
Init_uart();
#endif
while(1)
{
if(RI)
{
RI =0;
i =0;
tempbuff =SBUF;
if(tempbuff ==0xEE)
{
for(j =0;j<5;j++)
{
send_data[j] =uart_rec();
}
write_tea5767(WRADDRESS,&send_data,5);
delay();
delay();
delay();
delay();
read_enable =1;
/*
read_tea5767(RDADDRESS,&rece_data,5);
uart_send(0xDF);
for(j=0;j<5;j++)
uart_send(rece_data[j]);
*/
}
}
if(read_enable ==1)
{
read_enable =0;
read_tea5767(RDADDRESS,&rece_data,5);
uart_send(0xDF);
for(j=0;j<5;j++)
uart_send(rece_data[j]);
}
keytemp = P1 & keyuse;
if(keytemp !=keyuse)
{
i =0;
delay();
if(keyvalid ==0)
{
keyvalid =1;
switch(keytemp)
{
/*
case 0x00: //all press
// if(mode ==0) mode =1;
// else mode =0;
break;
*/
case 0x01: //P1.1 press
send_data[0]=0x33;
send_data[1]=0x27;
write_tea5767(WRADDRESS,&send_data,5);
read_enable =1;
/*
read_tea5767(RDADDRESS,&rece_data,5);
for(j=0;j<5;j++)
uart_send(rece_data[j]);
*/
break;
case 0x02: //P1.0 press
send_data[0]=0x2f;
send_data[1]=0xfd;
write_tea5767(WRADDRESS,&send_data,5);
read_enable =1;
/*
read_tea5767(RDADDRESS,&rece_data,5);
for(j=0;j<5;j++)
uart_send(rece_data[j]);
*/
break;
default:
break;
}
}
// else keyvalid =1;
}
else
{
keyvalid = 0;
i++;
delay();
if(i ==20000)
{
IT1 =1; //open INT1
EX1 =1;
WAKE_CLKO |=(1<<6);
ES =1; //open serial port interrupt
EA =1;
PCON |=0X02;
_nop_();
_nop_();
_nop_();
_nop_();
i =0;
EX1 =0;
ES =0;
PCON &=~0X02;
}
}
}
}
#include "stc.h"
void Init_uart( void )
{
//#ifdef test
TMOD |= 0X20;
TH1 = 0XF3; //osc 24M, 4800 baud rate
TL1 = 0XF3;
SCON = 0X50;
PCON=0X80; //SMOD =1
TR1 = 1;
//#endif
}
void uart_send( unsigned char Buf )
{
TI = 0;
SBUF = Buf;
while(!TI);
TI = 0;
}
unsigned char uart_rec(void)
{
unsigned char temp;
while(!RI);
RI =0;
temp = SBUF;
return(temp);
}
外加几个头文件 ,不粘贴了