|
測試流程:
1把7326_test_1.c 生成的hex 燒進(jìn)單片機(jī)
2打開串口
這里會(huì)收到循環(huán)發(fā)送的00 00 00 00
3按單片機(jī)測試按鍵key1 看配置寄存器是不是10 不是要重新往配置寄存器寫入0x10
4長按7326按鍵 串口接收窗口會(huì)收到鍵值
24c02.c
#define device_add_w 0xb0
#define device_add_r 0xb1
#include "24c02.H"
void delay()
{
;;
}
void i2c_init()
{
SDa = 1;
delay();
scl = 1;
delay();
}
void delayms(uint xms)
{
uchar x, y;
for(x = xms; x > 0; x--)
for(y = 110; y > 0; y--);
}
void start() //啟動(dòng)i2c
{
sda = 1;
scl = 1;
delay(); //延時(shí)必須大于4.7us,此約為五微秒
sda = 0; //在scl為高電平時(shí),sda一個(gè)下降沿為啟動(dòng)信號
delay();
}
void stop() //停止i2c
{
sda = 0;
scl = 1;
delay();
sda = 1; //在scl為高電平時(shí),sdasda一個(gè)上升沿為停止信號
delay();
}
void ack() //應(yīng)答信號0
{
uchar i = 0; //等待變量
scl = 1; //在scl為高電平期間等待應(yīng)答
delay();
while((sda == 1) && i < 250) //若為應(yīng)答0即退出,從機(jī)向主機(jī)發(fā)送應(yīng)答信號
i++; //等待一段時(shí)間
scl = 0; //應(yīng)答之后將scl拉低
delay();
}
void nack() //非應(yīng)答
{
scl = 1; //在scl為高電平期間,由主機(jī)向從機(jī)發(fā)送一個(gè)1,非應(yīng)答信號
delay();
sda = 1;
scl = 0; //應(yīng)答之后將scl拉低
delay();
}
void send_byte(uchar date) //寫一個(gè)字節(jié)8位
{
uchar i, temp;
temp = date; //存入要寫入的數(shù)據(jù),即要發(fā)送到sda上的數(shù)據(jù)
for(i = 0; i < 8; i++)
{ //發(fā)送8位
temp <<= 1; //to CY 將數(shù)據(jù)的最高位移入到PSW中的CY位中
scl = 0; //只有在scl為低電平時(shí),才允許sda上的數(shù)據(jù)變化
delay();
sda = CY; //將CY里的數(shù)據(jù)發(fā)送到sda數(shù)據(jù)線上
delay(); //可以延時(shí)
scl = 1; //在scl為高電平時(shí),不允許sda上的數(shù)據(jù)變化,使數(shù)據(jù)穩(wěn)定
delay();
scl = 0; //允許sda數(shù)據(jù)線的數(shù)據(jù)變化,等待下一個(gè)數(shù)據(jù)的傳輸
delay();
}
//wait ack:發(fā)送完一個(gè)字節(jié)數(shù)據(jù)后要主機(jī)要等待從機(jī)的應(yīng)答,第九位
scl = 0; //允許sda變化
delay();
sda = 1; //wait:ack,sda拉高等待應(yīng)答,當(dāng)sda=0時(shí),表示從機(jī)的應(yīng)答
delay();
}
uchar read_byte() //讀一個(gè)字節(jié)數(shù)據(jù)
{
char i, j, k;
scl = 0; //讀之前先允許sda變化
delay(); //等待數(shù)據(jù)
for(i = 0; i < 8; i++)
{
scl = 1; //不允許sda變化
delay(); //使sda數(shù)據(jù)穩(wěn)定后開始讀數(shù)據(jù)
j = sda; //讀出sda上的數(shù)據(jù)
k = (k << 1)| j; //將數(shù)據(jù)通過|運(yùn)算存入k中
scl = 0; //允許sda變化等待下一位數(shù)據(jù)的到來
delay();
}
//delay(); //可不用延時(shí)
return k; //返回讀出的數(shù)據(jù)
}
//write:at24c02 在at24c02中的指定地址寫入數(shù)據(jù)
void write_at24c02(uchar address, uchar date)
{
start(); //啟動(dòng)i2c
send_byte(device_add_w); //寫入期間地址和寫操作
ack(); //從機(jī)應(yīng)答0
send_byte(address); //寫入寫數(shù)據(jù)的單元地址
ack(); //ack0
send_byte(date); //在指定地址中寫入數(shù)據(jù)
ack(); //從機(jī)應(yīng)答0
stop(); //停止i2c
}
//read: at24c02 在at24c02的指定地址中讀出寫入的數(shù)據(jù)
uchar read_at24c02(address)
{
uchar dat; //用來存儲讀出的數(shù)據(jù)
start(); //啟動(dòng)i2c
send_byte(device_add_w); //寫入at24c02器件地址和寫操作
ack(); //從機(jī)應(yīng)答0
send_byte(address); //寫入要讀取AT24C02的數(shù)據(jù)的單元地址
ack(); //從機(jī)應(yīng)答0
start(); //再次啟動(dòng)i2c
send_byte(device_add_r); //寫入AT24C02器件地址和讀操作
ack(); //從機(jī)應(yīng)答‘0’
dat = read_byte(); //讀出指定地址中的數(shù)據(jù)
nack(); //主機(jī)發(fā)出非應(yīng)答‘1’
stop(); //停止i2c
return dat; //返回讀出的數(shù)據(jù)
}
24c02.h
#include
typedef unsigned char uchar;
typedef unsigned int uint;
sbit sda= P1^0;
sbit scl = P1^1;
void delay(void);
void i2c_init(void); //I2C初始化
void delayms(uint xms);
void start(void); //啟動(dòng)I2C
void stop(void); //停止I2C
void ack(void); //應(yīng)答
void nack(void); //不應(yīng)答
void send_byte(uchar d); //寫一個(gè)字節(jié)
uchar read_byte(void); //讀一個(gè)字節(jié),返回?cái)?shù)據(jù)
void write_at24c02(uchar addr,uchar dd);//寫入指定地址的一個(gè)字節(jié) (要寫入的寄存器地址,寫入十六進(jìn)制數(shù)據(jù))
uchar read_at24c02(uchar addr); //讀出指定地址的一個(gè)字節(jié) (要讀取的寄存器地址)
創(chuàng)建整理:ly
時(shí)間:2016.8.24
功能:用51單片機(jī)對7326智能掃描按鍵的測試
文檔組成:24c02.h 24c02.c構(gòu)成了I2C驅(qū)動(dòng)
7326_test_1.c 為串口測試程序
7326_test_1.c
#include
#include "24C02.H"
unsigned char uart_data;
//測試的兩個(gè)按鍵
sbit key1=P3^4;
sbit key2=P3^5;
//發(fā)送函數(shù)
void SendASC(uchar ASC)
{
TI=0;
SBUF=ASC;
while(!TI);
}
//串口0 中斷函數(shù) 這個(gè)中斷函數(shù)可以不用響應(yīng) 這個(gè)是再向單片機(jī)發(fā)送數(shù)據(jù)
void serial_IT() interrupt 4
{
if (RI == 1)//如果是接收中斷
{
RI = 0; //清除接收中斷標(biāo)志
uart_data = SBUF; //接收數(shù)據(jù)
SendASC(uart_data);//將接收的數(shù)據(jù)發(fā)送
}
}
//主程序
void main()
{
//串口設(shè)置
unsigned n,nn,nnn;
SCON = 0x50; //8位串行口模式1,允許接收,REN=1
TMOD = TMOD | 0x20; //TMOD = 0x20; //定時(shí)器1,工作在模式2
TH1 = 0xe6;//波特率為1200bps,晶振頻率為12MHz
TL1 = 0xe6;//波特率為1200bps,晶振頻率為11.059MHz,則加載e8
ES=1; //打開串行口中斷
TR1 = 1; //啟動(dòng)定時(shí)器
EA=1; //打開總中斷
i2c_init(); // I2C的初始化
start(); // I2C開始
while(1){
if(key1==0)
{ delay();//消抖
if(key1==0)
{
write_at24c02(0x08,0x10); //用于向配置寄存器寫值 這個(gè)可以參考7326技術(shù)文檔
delayms(10);//需等待十毫秒
n=read_at24c02(0x08); //讀取7326設(shè)備配置寄存器的值
nn= n;
SendASC(nn);
}
}
//這個(gè)是結(jié)合自己的51 寫的。
//write_at24c02(0,2);
//delayms(10);
write_at24c02(0x08,0x10);
n=read_at24c02(0x10); //讀緩存器的地址 由7326的文檔結(jié)合電路原理圖片可知
nnn= n;
SendASC(nnn);
}
}
|
|