2471 views|7 replies

3255

Posts

0

Resources
The OP
 

I2C-24C04 and buzzer This program problem [Copy link]

I2C-24C04 and buzzer program problem
/* Name: I2C-24C04 and buzzer
Description: The program first writes a musical note to the 24C04 , then reads it and plays it.
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define NOP4() {_nop_();_nop_();_nop_();_nop_();}
sbit SCL=P1^0;
sbit SDA=P1^1;
sbit SPK=P3^0;
// Delay table corresponding to standard note frequency
uchar code HI_LIST[]={0,226,229,232,233,236,238,240,241,242,244,245,246,247,248};
uchar code LO_LIST[]={0,4,13,10,20,3,8,6,2,23,5,26,1,4,3};
// The note to be written into 24C04
uchar code Song_24C04[]={1,2,3,1,1,2,3,1,3,4,5,3,4,5};
uchar sidx; // Read the note index
// Delay
void DelayMS(uint ms)
{
flying i;
while(ms--) for(i=0;i<120;i++);
}
//IIC starts
void Start()
{
SDA=1;SCL=1;NOP4();SDA=0;NOP4();SCL=0;
}
//IIC stop
void Stop()
{
SDA=0;SCL=0;NOP4();SCL=1;NOP4();SDA=1;
}
// Read the response
void RACK()
{
SDA=1;NOP4();SCL=1;NOP4();SCL=0;
}
// Send a non-response signal
void NO_ACK()
{
SDA=1;SCL=1;NOP4();SCL=0;SDA=0;
}
// Write a byte of data to 24C04
void Write_A_Byte(uchar b)
{
flying i;
for(i=0;i<8;i++)
{
b<<=1;SDA=CY;_nop_();SCL=1;NOP4();SCL=0;
}
RACK();
}
// Write data to the specified address
void Write_IIC(uchar addr,uchar dat)
{
Start();
Write_A_Byte(0xa0);Write_A_Byte(addr);Write_A_Byte(dat);
Stop();
DelayMS(10);
}
// Read a byte of data from 24C04
uchar Read_A_Byte()
{
flying i,b;
for(i=0;i<8;i++)
{
SCL=1;b<<=1;b|=SDA;SCL=0;
}
return b;
}
// Read data from the current address
uchar Read_Current()
{
uchar d;
Start();
Write_A_Byte(0xa1);d=Read_A_Byte();NO_ACK();
Stop();
return d;
}
// Read data from any address
uchar Random_Read(uchar addr)
{
Start();
Write_A_Byte(0xa0);Write_A_Byte(addr);
Stop();
return Read_Current();
}
// Timer 0 interrupt
void T0_INT() interrupt 1
{
SPK=~SPK;
TH0=HI_LIST[sidx];
TL0=LO_LIST[sidx];
}
// Main program
void main()
{
uint i;
IE=0x82;
TMOD=0x00;
for(i=0;i<14;i++) // Write the note table to 24C04
{
Write_IIC(i,Song_24C04);
}
while(1) // repeatedly read and play notes
{
for(i=0;i<15;i++) // Read notes from 24C04
{
sidx=Random_Read(i); // Read from the specified address
TR0=1; // play
DelayMS(300);
}
}
}
question:
1.#define NOP4() {_nop_();_nop_();_nop_();_nop_();}//-------- I don't understand what this macro definition means
2. Write_IIC(i,Song_24C04 );//---------------- What does i,Song_24C04 mean? Why is there another [ ]
3.uchar code HI_LIST[]={0,226,229,232,233,236,238,240,241,242,244,245,246,247,248};
uchar code LO_LIST[]={0,4,13,10,20,3,8,6,2,23,5,26,1,4,3};----------------- What do the numbers in these brackets mean?
4. //IIC starts
void Start()
{
SDA=1;SCL=1;NOP4();SDA=0;NOP4();SCL=0;
}
-----------------NOP4();SDA=0;NOP4();SCL=0;How do you understand this?
5. // Write data to the specified address
void Write_IIC(uchar addr,uchar dat)
{
Start();
Write_A_Byte(0xa0);Write_A_Byte(addr);Write_A_Byte(dat);
Stop();
--------------- Why is there a hexadecimal number in the first function bracket, and what does addr and dat mean after that?

121047wl3h8ldvpplcm3qp.png (15.47 KB, downloads: 0)

121047wl3h8ldvpplcm3qp.png
This post is from 51mcu

Latest reply

If you don’t study for three days, you won’t be able to catch up with the captain.   Details Published on 2020-7-16 15:23
 

7462

Posts

2

Resources
2
 

4 no operations, no operations are assembly. [] Array, curly brackets are the static array initialization method after c99, I forgot the specific name @lcofjp . SCL, SDA correspond to two IOs, 1 is pulled high, 0 is set low, simulating the timing and data of iic. 0xa0 is fixed, just write it in.

This post is from 51mcu

Comments

I mean what do the values in the array mean?  Details Published on 2020-7-14 23:37
I mean what do the values in the array mean?  Details Published on 2020-7-14 15:43
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

3255

Posts

0

Resources
3
 
freebsder posted on 2020-7-14 14:32 4 empty operations, empty operations are assembly. [] Array, curly braces are the static array initialization method after c99, I forgot the specific name @lcofjp &nbs ...

I mean what do the values in the array mean?

This post is from 51mcu

Comments

//Delay table corresponding to standard note frequency  Details Published on 2020-7-14 16:37
 
 
 

7462

Posts

2

Resources
4
 
QWE4562009 posted on 2020-7-14 15:43 I mean what do the values in the array mean?

// Delay table corresponding to standard note frequency

This post is from 51mcu
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

6040

Posts

204

Resources
5
 
freebsder posted on 2020-7-14 14:32 4 empty operations, empty operations are assembly. [] Array, curly braces are the static array initialization method after c99, I forgot the specific name @lcofjp &nbs ...

For me who writes js, doesn't C language always support initialization in curly braces?

This post is from 51mcu

Comments

I have no impression, it seems that this method was only supported later.  Details Published on 2020-7-15 13:32
 
 
 

7462

Posts

2

Resources
6
 
lcofjp posted on 2020-7-14 23:37 For me who writes js, doesn't C language always support initialization in curly braces?

I have no impression, it seems that this method was only supported later.

This post is from 51mcu

Comments

I think you are confused with the structure.  Details Published on 2020-7-15 20:55
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

6040

Posts

204

Resources
7
 
freebsder posted on 2020-7-15 13:32 I don’t remember, it seems that this method was only supported later.

I think you are confusing it with the structure.

This post is from 51mcu

Comments

If you don’t study for three days, you won’t be able to catch up with the captain.  Details Published on 2020-7-16 15:23
 
 
 

7462

Posts

2

Resources
8
 
lcofjp posted on 2020-7-15 20:55 I suspect you are confused with the structure

If you don’t study for three days, you won’t be able to catch up with the captain.

This post is from 51mcu
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list