|
各位老大,我想做一個按鍵計數存儲在2402里面再通過串口發生,也可以通過1602顯示,搞了好多天,實在搞不了,請求大神們看看我的程序好嗎?
目前已完成的功能有:
1、按鍵計數可以通過1602顯示出來。
2、按鍵計數也可以通過串口發送。
請問怎么就是不能存儲到24C02里面呢?2402存儲的程序我是參照宋雪松老師的課程改寫的,是不是哪里不對呀?
有條件的朋友可以幫忙調試下,硬件接口如下:
#define LCD1602_DB P0
sbit LCD1602_RS = P2^7;
sbit LCD1602_RW = P2^6;
sbit LCD1602_E = P2^5;
sbit key1=P3^2;
sbit LED=P3^3;
sbit I2C_SCL = P3^7;
sbit I2C_SDA = P3^6;
再次多謝大家了!
*****************************************/
#include <reg52.h>
#include "uart.h"
#include "lcd1602.h"
#include "I2C.h"
#include <intrins.h>
#define I2CDelay() {_nop_();_nop_();_nop_();_nop_();}
unsigned char date;
#define uchar unsigned char
#define uint unsigned int
sbit key1=P3^2;
sbit LED=P3^3;
unsigned char count;
unsigned char dat;
unsigned int i;
unsigned char buf[5];
unsigned char str[10];
unsigned char uart_buf; //接收數據寄存器
unsigned char distate[12];//計數緩沖區數組 .
/* 函數申明 -----------------------------------------------*/
void delay(uint z);
//void Initial_com(void);
//***********************************************************
/*
********************************************************************************
** 函數名稱 : delay(uint z)
** 函數功能 : 延時函數
********************************************************************************
*/
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
/***************************************************
* 外部中斷0初始化子函數
* 下降沿觸發,開啟INT0中斷,開總中斷
****************************************************/
void int0_init(void)
{
IT0 = 1; //下降沿觸發
EX0 = 1; //開啟INT0中斷
EA = 1; //開啟總中斷
}
/* 將按鍵計數賦值給數組,便于液晶顯示---------------- */
void display()
{
buf[1] = count/1000+0x30;
buf[2] = count%1000/100+0x30; //0x30就是把十進制換成十六進制
buf[3] = count%1000%100/10+0x30;
buf[4] = count%1000%100%10+'0'; // '0'=0x30
}
/* 讀取EEPROM中的一個字節,addr-字節地址 */
unsigned char E2ReadByte(unsigned char addr)
{
unsigned char dat;
I2CStart();
I2CWrite(0x50<<1); //尋址器件,后續為寫操作
I2CWrite(addr); //寫入存儲地址
I2CStart(); //發送重復啟動信號
I2CWrite((0x50<<1)|0x01); //尋址器件,后續為讀操作
dat = I2CReadNAK(); //讀取一個字節數據
I2CStop();
return dat;
}
/* 向EEPROM中寫入一個字節,addr-字節地址 */
void E2WriteByte(unsigned char addr, unsigned char dat)
{
I2CStart();
I2CWrite(0x50<<1); //尋址器件,后續為寫操作
I2CWrite(addr); //寫入存儲地址
I2CWrite(dat); //寫入一個字節數據
I2CStop();
}
/* E2讀取函數,buf-數據接收指針,addr-E2中的起始地址,len-讀取長度 */
void E2Read(unsigned char *buf, unsigned char addr, unsigned char len)
{
do { //用尋址操作查詢當前是否可進行讀寫操作
I2CStart();
if (I2CWrite(0x50<<1)) //應答則跳出循環,非應答則進行下一次查詢
{
break;
}
I2CStop();
} while(1);
I2CWrite(addr); //寫入起始地址
I2CStart(); //發送重復啟動信號
I2CWrite((0x50<<1)|0x01); //尋址器件,后續為讀操作
while (len > 1) //連續讀取len-1個字節
{
*buf++ = I2CReadACK(); //最后字節之前為讀取操作+應答
len--;
}
*buf = I2CReadNAK(); //最后一個字節為讀取操作+非應答
I2CStop();
}
/* E2寫入函數,buf-源數據指針,addr-E2中的起始地址,len-寫入長度 */
void E2Write(unsigned char *buf, unsigned char addr, unsigned char len)
{
while (len--)
{
do { //用尋址操作查詢當前是否可進行讀寫操作
I2CStart();
if (I2CWrite(0x50<<1)) //應答則跳出循環,非應答則進行下一次查詢
{
break;
}
I2CStop();
} while(1);
I2CWrite(addr++); //寫入起始地址
I2CWrite(*buf++); //寫入一個字節數據
I2CStop(); //結束寫操作,以等待寫入完成
}
}
//*************************
//**********主函數*********
//*************************
main()
{
UART_INIT(); // 串口初始化
int0_init() ; // 外部中斷初始化
InitLcd1602();
while(1)
{
E2Read(buf, 0x22, sizeof(buf)); //從E2中讀取一段數據
display(); // 將按鍵計數賦值給數組,便于液晶顯示
LcdShowStr(0, 0, "ji shu :"); //顯示在液晶上
LcdShowStr(1,1,&buf[1]); //buf數量里面對應count,是外部中斷得到的按計數
E2Write(buf, 0x22, sizeof(buf)); //再寫回到E2中
// while(1);
}
}
/***************************************************
* 中斷響應子函數
* 響應外部中斷0,按鍵低電平一次就加一,中斷標志位自動清零
****************************************************/
void int0_process() interrupt 0
{
dat+=1; //進入一次中斷 就加一次
count = dat;
while(key1 == 0);
delay(200);
IE0 = 0; // 外部中斷標志位
// uart_send_byte(count) ; // 發送按鍵次數到串口
// delay(200);
}
/***************************************************
* 中斷服務子函數
* 清除RI,同時判斷接到的數據
****************************************************/
void uart(void) interrupt 4
{
// unsigned char uart_buf; //接收數據寄存器
if(RI) //是否收到數據
{
RI=0; //清中斷請求
uart_buf = SBUF;
if(uart_buf == 0xaa) //判斷
{
uart_send_byte(count) ; // 發送按鍵次數到串口
delay(200);
}
if(uart_buf == 0x11) //判斷
{
//uart_send_byte(count) ; // 發送按鍵次數到串口
LED =0; // (在這里和串口中斷都可以發送數據的,區別:是否連續)
delay(200);
}
if(uart_buf == 0x22) //判斷
{
//uart_send_byte(count) ; // 發送按鍵次數到串口
LED =1;
delay(200);
}
}
}
|
|