|
各位老大,我想做一个按键计数存储在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);
}
}
}
|
|