- okok云
-
兄弟,参加电子设计大赛吧??b题?
这个最重要的是看芯片资料,芯片资料内对通信方式,协议,时序,都有的,可以利用单片机自带的通信接口,即端口第二功能。也可以自己制定时序。
这是24c02的程序。用的ATmega16;
最后祝你有个好成绩
#include <iom16v.h>
#include <macros.h>
#include "delay.h"
#include "lcd.h"
#define uchar unsigned char
#define uint unsigned int
/**************************************************************************
* 函数名称: ERROR();
* 功 能: 蜂鸣器报错
* 参 数:无
* 描 述:PA0口置低可以鸣响蜂鸣器
**************************************************************************/
void ERROR()
{
uint i;
DDRA |= (1 << PA0);
PORTA &= ~(1 << PA0);
for( i = 0 ; i < 10000 ; i ++);
PORTA |= (1 << PA0);
}
//********************************************************************8
//*函数 向24c02发送一个字节
//*名称 wtwib();
//*参数 地址,数据
//*********************************************************************/
void wtwib(uchar ad,uchar da)
{ TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//发送start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA0; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
TWDR =ad; //adress of the data
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
TWDR =da; //send the data to the bus
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send the stop signal
}
//=================================================================
//=======read a byte from 24c02
//========rtwib()
//==========need address
//================================================================
uchar rtwib(uchar ad)
{ uchar da;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send the signal of start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA1; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
TWDR =ad; //adress of the data
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send the signal of start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA1; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x58)
ERROR();
da=TWDR;
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send the stop signal
return da;
}
//========================================================
//====== send a page to the 24c02
//====== wtwis();
//=======need address and data and number;
//=========================================================
void wtwip(uchar ad,uchar n,uchar *da)
{ uchar i;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//发送start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA0; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
TWDR =ad; //adress of the data
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
for(i=0;i<n;i++)
{ TWDR =da[i]; //send the data to the bus
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
}
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send the stop signal
}
//====================================================
//========read a string from the 24C02
//========rtwis();
//========need address,number
//=====================================================
void rtwis(uchar ad,uchar *p,uchar n)
{ uchar i;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send the signal of start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA1; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
TWDR =ad; //adress of the data
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send the signal of start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA1; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
for(i=0;i<n;i++)
{ TWCR = (1<<TWINT) | (1<<TWEN)|(1<<TWEA);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x50)
ERROR();
p[i]=TWDR;
}
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send the stop signal
}
void main()
{ uchar a[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},i=0;
init_lcd();
TWBR = 0x1F;
TWSR = 0x02;
for(i=0;i<16;i++)
wtwib( 0x00+i,i);
for(i=0;i<16;i++)
rtwib( 0x00+i);
for(i=0;i<16;i++)
a[i]=0;
for(i=0;i<16;i++)
send_d(a[i]+48);
}
- clou
-
* 回复内容中包含的链接未经审核,可能存在风险,暂不予完整展示!看芯片的PDF资料啊 资料肯定介绍的很详细的 遇到一款新的芯片 使用之前必须详细了解它的参数 去21IC网http://www.2**.com/ 网站下载资料 然后看片选引脚 各个引脚的功能 就ok了
- cloudcone
-
看这个芯片的时序,用单片机的IO口实现这个时序对芯片的控制;
- nicehost
-
兄弟,参加电子设计大赛吧??b题?
这个最重要的是看芯片资料,芯片资料内对通信方式,协议,时序,都有的,可以利用单片机自带的通信接口,即端口第二功能。也可以自己制定时序。
这是24c02的程序。用的ATmega16;
最后祝你有个好成绩
#include
<iom16v.h>
#include
<macros.h>
#include
"delay.h"
#include
"lcd.h"
#define
uchar
unsigned
char
#define
uint
unsigned
int
/**************************************************************************
*
函数名称:
ERROR();
*
功
能:
蜂鸣器报错
*
参
数:无
*
描
述:PA0口置低可以鸣响蜂鸣器
**************************************************************************/
void
ERROR()
{
uint
i;
DDRA
|=
(1
<<
PA0);
PORTA
&=
~(1
<<
PA0);
for(
i
=
0
;
i
<
10000
;
i
++);
PORTA
|=
(1
<<
PA0);
}
//********************************************************************8
//*函数
向24c02发送一个字节
//*名称
wtwib();
//*参数
地址,数据
//*********************************************************************/
void
wtwib(uchar
ad,uchar
da)
{
TWCR
=
(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//发送start
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x08)
ERROR();
TWDR
=0xA0;
//adress
of
e2prom
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x18)
ERROR();
TWDR
=ad;
//adress
of
the
data
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x28)
ERROR();
TWDR
=da;
//send
the
data
to
the
bus
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x28)
ERROR();
TWCR
=
(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send
the
stop
signal
}
//=================================================================
//=======read
a
byte
from
24c02
//========rtwib()
//==========need
address
//================================================================
uchar
rtwib(uchar
ad)
{
uchar
da;
TWCR
=
(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send
the
signal
of
start
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x08)
ERROR();
TWDR
=0xA1;
//adress
of
e2prom
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x18)
ERROR();
TWDR
=ad;
//adress
of
the
data
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x28)
ERROR();
TWCR
=
(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send
the
signal
of
start
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x08)
ERROR();
TWDR
=0xA1;
//adress
of
e2prom
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x18)
ERROR();
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x58)
ERROR();
da=TWDR;
TWCR
=
(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send
the
stop
signal
return
da;
}
//========================================================
//======
send
a
page
to
the
24c02
//======
wtwis();
//=======need
address
and
data
and
number;
//=========================================================
void
wtwip(uchar
ad,uchar
n,uchar
*da)
{
uchar
i;
TWCR
=
(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//发送start
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x08)
ERROR();
TWDR
=0xA0;
//adress
of
e2prom
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x18)
ERROR();
TWDR
=ad;
//adress
of
the
data
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x28)
ERROR();
for(i=0;i<n;i++)
{
TWDR
=da[i];
//send
the
data
to
the
bus
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x28)
ERROR();
}
TWCR
=
(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send
the
stop
signal
}
//====================================================
//========read
a
string
from
the
24C02
//========rtwis();
//========need
address,number
//=====================================================
void
rtwis(uchar
ad,uchar
*p,uchar
n)
{
uchar
i;
TWCR
=
(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send
the
signal
of
start
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x08)
ERROR();
TWDR
=0xA1;
//adress
of
e2prom
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x18)
ERROR();
TWDR
=ad;
//adress
of
the
data
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x28)
ERROR();
TWCR
=
(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send
the
signal
of
start
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x08)
ERROR();
TWDR
=0xA1;
//adress
of
e2prom
TWCR
=
(1<<TWINT)
|
(1<<TWEN);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x18)
ERROR();
for(i=0;i<n;i++)
{
TWCR
=
(1<<TWINT)
|
(1<<TWEN)|(1<<TWEA);
while
(!(TWCR
&
(1<<TWINT)));
if
((TWSR
&
0xF8)
!=
0x50)
ERROR();
p[i]=TWDR;
}
TWCR
=
(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send
the
stop
signal
}
void
main()
{
uchar
a[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},i=0;
init_lcd();
TWBR
=
0x1F;
TWSR
=
0x02;
for(i=0;i<16;i++)
wtwib(
0x00+i,i);
for(i=0;i<16;i++)
rtwib(
0x00+i);
for(i=0;i<16;i++)
a[i]=0;
for(i=0;i<16;i++)
send_d(a[i]+48);
}