/***************标准源码***************/
#include<reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda = P2 ^ 1; //数据口定义
sbit scl = P2 ^ 0;
char code music[] = {//生日快乐谱
0x11,0x05,0x11,0x05,0x12,0x0a,0x11,0x0a,0x14,0x0a,0x13,0x14,
0x11,0x05,0x11,0x05,0x12,0x0a,0x11,0x0a,0x15,0x0a,0x14,0x14,
0x11,0x05,0x11,0x05,0x21,0x0a,0x16,0x0a,0x14,0x0a,0x13,0x0a,
0x22,0x0a,0x17,0x05,0x17,0x05,0x16,0x0a,0x14,0x0a,0x15,0x0a,
0x14,0x14,0x00,0x00
};
uchar data buffer[100];
void delay(uint m) ;
void nop();
void delay1(uint m);
void IIc_init();
void start();
void stop();
void writebyte(uchar jad);
uchar readbyte();
void ACK();
uchar read24c02(uchar spress) ;
void write24c02(uchar spress, uchar info);
void main()
{
uchar sp, i, j, m;
IIc_init();
P1 = 0X55;
while (sp != sizeof(music))
{
write24c02(0x00 + sp, music[sp]);
sp++;
if (sp % 4 == 0)
P1 = ~P1;
}
P1 = 0X00;
}
void delay(uint m)
{
uint n, p;
for (n = m; n>0; n--)
for (p = 125; p>0; p--);
}
void nop()
{
_nop_();
_nop_();
}
void delay1(uint m)
{
uint n;
for (n = 0; n<m; n++);
}
void IIc_init()
{
scl = 1;
nop();
sda = 1;
nop();
}
void start()
{
sda = 1;
nop();
scl = 1;
nop();
sda = 0;
nop();
scl = 0;
nop();
}
void stop() //停止I2C 总线
{
sda = 0;
nop();
scl = 1;
nop();
sda = 1;
nop();
}
void writebyte(uchar jad) //写一个字节
{
uchar i;
scl = 0;
for (i = 0; i<8; i++)
{
if(jad & 0x80)
{
sda = 1;
}
else sda = 0;
jad <<= 1;
nop();
scl = 1; //待sda 线上的数据稳定后 将scl 拉高
nop();
scl = 0; //待sda 线上的数据稳定后 将scl 拉高
nop();
}
sda = 1;
nop();
}
uchar readbyte() //读一个字节
{
uchar i, j, k ;
scl = 0;
nop();
sda = 1;
for (i = 0; i<8; i++)
{
nop();
scl = 1;
nop();
if (sda == 1)
j = 1;
else
j = 0;
k = (k << 1) | j;
scl = 0;
}
nop();
return k;
}
void ACK() //应答函数
{
uchar i = 0;
scl = 1;
nop();
while ((sda == 1) && (i<255))
i++;
scl = 0;
nop();
}
uchar read24c02(uchar spress)
{
uchar i;
start();
writebyte(0xa0);
ACK();
writebyte(spress);
ACK();
start();
writebyte(0xa1);
ACK();
i = readbyte();
stop();
delay1(100);
return(i);
}
void write24c02(uchar spress, uchar info)
{
start();
writebyte(0xa0);
ACK(); writebyte(spress);
ACK();
writebyte(info);
ACK();
stop();
delay1(5000);
}
24c04音乐读取与存储.rar
(8.64 KB, 下载次数: 34)
|