新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)—AT24C1024(帶程序解析!)—07

51單片機(jī)—AT24C1024(帶程序解析!)—07

作者: 時(shí)間:2016-11-25 來(lái)源:網(wǎng)絡(luò) 收藏



然后就是程序了:

main.c(先從主程序開(kāi)始嗎~~后邊還有很多的

//------------------------------------------------------------------------------
//main.c~~~主文件
#include"reg52.h"
#include"MacroAndCOnst.h"
#include"at24c1024.h"
#include"delay.h"
#include"lcdinit.h"
//------------------------------------------------------------------------------
uchar code title[]="AT24C1024 Text: ";
uchar send_data[16]={0,1,2,3,4,5,6,7,8,9};//要寫入到AT24C1024的數(shù)據(jù)
uchar receive_data[10]; //存儲(chǔ)懂AT24C1024讀出的數(shù)據(jù)
//------------------------------------------------------------------------------
//main
void main()
{
lcd_init();
lcd_display_string(0x80,title,16);
//delay(100);//不知道為什么,我第一做的時(shí)候延時(shí)時(shí)間長(zhǎng)就錯(cuò)誤,很明顯是寄存器公用問(wèn)題
//可是當(dāng)我整理的時(shí)候卻又能用了~我去列~留著~
while(!mcu_send_string(0xa0,0x00,0x00,send_data,10));
delay(10);//這里一定要有一定的延時(shí)~~你懂的~~不過(guò)我用while循環(huán)送~~也可以不用延時(shí)
//如果沒(méi)有while語(yǔ)句,那就必須要有個(gè)一直延時(shí)的~~
while(!mcu_receive_string(0xa0,0x00,0x00,receive_data,10));
lcd_display_string(0x80+0x40,receive_data,10);
while(1);
}

LcdInit.c

//------------------------------------------------------------------------------
//LCD1602的初始化程序—可調(diào)用函數(shù)—C51
#include"reg52.h"
#include"MacroAndConst.h"
#include"Delay.h"
#include"Foot.h"
//------------------------------------------------------------------------------
#definedata_port P2
sbit lcd_en=P3^4;//定義lcd_en使能引腳
sbit lcd_rs=P3^5;//定義鎖存引腳
sbit lcd_rw=P3^6;//定義讀寫引腳
//------------------------------------------------------------------------------
//LCD寫指令函數(shù)
void lcd_write_command(uchar com)
{
lcd_rs=0;
lcd_rw=0;
data_port=com;
delay(1);
lcd_en=1;
delay(1);
lcd_en=0;
}
//------------------------------------------------------------------------------
//LCD寫數(shù)據(jù)函數(shù)
void lcd_write_data(uchar dat)
{
lcd_rs=1;
lcd_rw=0;
data_port=dat;
delay(1);
lcd_en=1;
delay(1);
lcd_en=0;
}
//------------------------------------------------------------------------------
//LCD初始化函數(shù)
void lcd_init()
{
lcd_write_command(0x01);
delay(1);
lcd_write_command(0x38);
delay(1);
lcd_write_command(0x0c);
delay(1);
lcd_write_command(0x06);
delay(1);
}
//------------------------------------------------------------------------------
//LCD顯示一個(gè)字節(jié)函數(shù)
void lcd_display_char(uchar add,uchar dat)
{
lcd_write_command(add);
delay(1);
lcd_write_data(dat);
delay(1);
}
//------------------------------------------------------------------------------
//LCD顯示多個(gè)字節(jié)函數(shù)
void lcd_display_string(uchar add,uchar *s,uchar num)
{
uchar i;
lcd_write_command(add);
for(i=0;i {
lcd_write_data(s[i]);
delay(1);
}
}
IIC.c

//------------------------------------------------------------------------------
#include"reg52.h"
#include"MacroAndConst.h"
#include"intrins.h"
#include"Foot.h"
bitack;
//------------------------------------------------------------------------------
//開(kāi)始信號(hào)函數(shù)
void IIC_start()
{
sda=1;
_nop_();
scl=1;//開(kāi)始信號(hào)建立時(shí)間大于4.7us
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
sda=0; //開(kāi)始信號(hào)鎖定大于4us
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;//鉗住IIC總線,準(zhǔn)備發(fā)送或者接收數(shù)據(jù)
_nop_();
_nop_();
}
//------------------------------------------------------------------------------
//結(jié)束信號(hào)函數(shù)
void IIC_stop()
{
sda=0;
_nop_();
scl=1; //結(jié)束時(shí)間大于4us
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
sda=1;//保證一個(gè)終止信號(hào)和其實(shí)信號(hào)的空閑時(shí)間大于4.7us
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
//------------------------------------------------------------------------------
//主控器件接收應(yīng)答函數(shù)
void IIC_ack_acc()
{
_nop_();
_nop_();
sda=1;//8位數(shù)據(jù)發(fā)送完后,準(zhǔn)備接收數(shù)據(jù)
_nop_();
_nop_();
scl=1;
_nop_();
_nop_();
_nop_();
if(sda==1)
ack=0;
else
ack=1;//接收到應(yīng)答位,ack為1,否則置0
scl=0;
_nop_();
_nop_();
}



評(píng)論


技術(shù)專區(qū)

關(guān)閉