我的程序现在可以一次读取两个数据了,我想将读取的数据通过串口发送,目前protues仿真也可以通过串口实时发送了。但是烧到单片机中用串口助手查看数据时发现没有数据, 不知道问题出在哪,请大家帮忙看一下,谢谢!
#include<pic.h>
#include<string.h>
#include<stdio.h>
#define nop() asm("nop")
#define uchar unsigned char
#define uint unsigned int
#define SCL RC3
#define SDA RC4
#define SCLIO TRISC3
#define SDAIO TRISC4
#define OP_READ1 0xa3
#define OP_WRITE1 0xa2
#define OP_READ2 0xa1
#define OP_WRITE2 0xa0
#define _XTAL_FREQ 4000000UL
uchar val1;
uchar val2;
uchar val3;
uchar val4;
void delay();
void I2C_start();
void I2C_stop();
unsigned char RecByte();
bit SendByte(uchar write_data);
void write_EEPROM(uchar dat,uchar addr);
uchar read_EEPROM(uchar addr);
/****延时函数****/
void delay()
{
int i;
for(i=0;i<100;i++)
{;}
}
/****启动信号函数****/
void I2C_start()
{
SDA = 1;
nop();
SCL = 1;
nop();nop();nop();nop();nop();
SDA=0;
nop();nop();nop();nop();nop();
SCL=0;
nop();nop();
}
/****停止信号函数****/
void I2C_stop()
{
SDA=0;
nop();
SCL=1;
nop();nop();nop();nop();nop();
SDA=1;
nop();nop();nop();nop();
}
/****I2C总线初始化函数****/
void I2C_init()
{
SCL=0;
I2C_stop();
}
/****发送应答函数****/
void I2C_Ack()
{
SDA=0;
SCL=0;
delay();
SCL=0;
SDA=1;
}
/****发送非应答函数****/
void I2C_NAck()
{
SDA=1;
SCL=1;
delay();
SCL=0;
SDA=0;
}
/****从I2C总线芯片接受1字节数据函数****/
uchar RecByte()
{
uchar i,read_data;
for(i=0;i<8;i++)
{
nop();nop();nop();
SCL=1;
nop();nop();
read_data<< = 1;
if(SDA == 1)
read_data = read_data+1;
nop();
SCL=0;
}
return(read_data);
}
/****从I2C总线芯片发送1字节数据函数****/
bit SendByte(uchar write_data)
{
uchar i;
uchar ack_bit;
for(i=0;i<8;i++)
{
if(write_data&0x80)
SDA=1;
else
SDA=0;
nop();
SCL=1;
nop();nop();nop();
SCL=0;
nop();
write_data << = 1;
}
nop();nop();
SDA=1;
nop();nop();
SCL=1;
nop();nop();nop();
ack_bit = SDA;
SCL=0;
nop();nop();
return ack_bit;
I2C_NAck();
}
/****向指定地址写数据函数****/
void write_EEPROM1(uchar dat,uchar addr)
{
I2C_start();
SendByte(OP_WRITE1);
SendByte(addr);
SDAIO=0;
SendByte(dat);
I2C_stop();
delay();
}
/****向指定地址写数据函数****/
void write_EEPROM2(uchar dat,uchar addr)
{
I2C_start();
SendByte(OP_WRITE2);
SendByte(addr);
SDAIO=0;
SendByte(dat);
I2C_stop();
delay();
}
/****向指定地址读数据函数****/
uchar read_EEPROM1(uchar addr)
{
uchar read_data;
I2C_start();
SendByte(OP_WRITE1);
SendByte(addr);
I2C_start();
SendByte(OP_READ1);
SDAIO=1;
read_data=RecByte();
I2C_stop();
return(read_data);
}
/****向指定地址读数据函数****/
uchar read_EEPROM2(uchar addr)
{
uchar read_data;
I2C_start();
SendByte(OP_WRITE2);
SendByte(addr);
I2C_start();
SendByte(OP_READ2);
SDAIO=1;
read_data=RecByte();
I2C_stop();
return(read_data);
}
void main()
{
TXSTA=0x24;
RCSTA=0x90;
SPBRG=0x19;//9600波特率
GIE=1;
PEIE=1;
RCIE=1;//打开中断
TRISB=0x00;
TRISC=0x00;
val1=read_EEPROM1(0x66);
delay();delay();delay();delay();delay();delay();
TXREG=val1;
delay();delay();delay();delay();delay();delay();
TRISC3=0xff;TRISC4=0xff;
delay();delay();delay();delay();delay();delay();
TRISC3=0x00;TRISC4=0x00;
delay();delay();delay();delay();delay();delay();
val2=read_EEPROM1(0x68);
delay();delay();delay();delay();delay();delay();
TXREG=val2;
delay();delay();delay();delay();delay();delay();
TRISC3=0xff;TRISC4=0xff;
delay();delay();delay();delay();delay();delay();
TRISC3=0x00;TRISC4=0x00;
delay();delay();delay();delay();delay();delay();
}
void interrupt com()
{
if(RCIF) //确定可以进入中断
{
RCIF=0;
}
}
|