📄 io_i2c.c
字号:
#include "pic18.h" //* PIC18系列的头文件 */
/*************************************************************/
/*注意:广大的实验板的8583的芯片地址是0xA2 所以写的时候为0xA2 读的时候为0xA3
/* 我自己焊接的8583芯片地址是0xA0 所以写的时候为0xA2 读的时候为0xA3
/*************************************************************/
#define uchar unsigned char
#define uint unsigned int
char read[8];
//iic读写宏定义
#define SDA RC4
#define SCL RC3
#define SDA_IO TRISC4
#define SCL_IO TRISC3
#define no_ask 10 //无应答超时时间
//iic读写定义的数组及变量
//待写入数缓存
unsigned char write[8]={0x00,0x00,0x02,0x03,0x04,0x11,0x11,0x11};
unsigned char read[8]; //读出数缓存
bit err_flag; //iic工作是否正常标志(为1不正常;为0正常)
//iic读写函数声明
void dlay(void);
void tx(unsigned char k); //向iic里写数据
void start(void); //启动iic
void stop(void); //停止iic
unsigned char rx(void); //从iic里读出数据
void iic_read(unsigned char start_address,unsigned char count);
void iic_write(unsigned char start_address,unsigned char count);
//*************************************************************/
//************************ LCD *******************************/
//*************************************************************/
#define clear 0x01
#define home 0x02
#define dis_on 0x0f
#define in_mode 0x06
#define P_RS RD7 //4 CS
#define P_RW RD6 //5 SID
#define P_E RD5 //6 CLK
//*-----------------------------------------------------------*/
void delay_us(uint j) // time=j*10+7 (us)延时函数/
{
uint ch;
while(j--)
{
ch++; ch--;
}
}
/*----------------------- LCD --------------------------------*/
void bit8_serial_input(unsigned char ix)
{
unsigned char i,j,nc,ab;
ab=0x80;
P_RW=0;//P_RW_0();//PORTD&=0xbf;
P_E=0;//P_E_0();//PORTD&=0xdf;
for(i=0;i<8;i++)
{
if(ix&ab)P_RW=1;//P_RW_1();
else P_RW=0;//P_RW_0();
ab>>=1;
P_E=1;//P_E_1();//PORTD|=0x20;
for(j=0;j<44;j++) nc=0;
P_E=0;//P_E_0();//PORTD&=0xdf;
P_RW=0;//P_RW_0();//PORTD&=0xbf;
for(j=0;j<4;j++) nc=0;
}
}
/*---------------------------------------------------------*/
void instruct(unsigned char dat)
{
unsigned char ch;
RD7=1;//P_RS_1();
ch=0xf8;
bit8_serial_input(ch);
ch=dat&0xf0;
bit8_serial_input(ch);
ch=dat<<4;
bit8_serial_input(ch);
RD7=0;//P_RS_0();
}
/*---------------------------------------------------------*/
void write_abyte(unsigned char dat)
{
unsigned char ch;
RD7=1;//P_RS_1();//PORTD|=0x80;
ch=0xfa;
bit8_serial_input(ch);
ch=dat&0xf0;
bit8_serial_input(ch);
ch=dat<<4;
bit8_serial_input(ch);
RD7=0;//P_RS_0();//PORTD&=0x7f;
}
//* 写一个汉字到x,Y坐标处其中X为8列y为4行 */
void write_word(uint w)
{
uchar ch,cl;
ch=w>>8; //取高八位ch怎么看不出来
cl=w;
write_abyte(ch);
write_abyte(cl);
}
/*-------------------------------------------*/
dis_hz_str(uchar x, uchar y, uchar *p_hz,uchar number) //~~显示函数可
{ //~~能是显示文字(也可是字母)串一共
uchar loc,*p; //可显示4行汉字由x决定
uint xi;
uchar xline[5]={0,1,3,2,4};
x=xline[x];
instruct(home);
loc=((x<<3)+y-9)|0x80;//~~
instruct(loc); p=p_hz;
xi=((*p++)<<8)|*p++;
while(number--){
write_word(xi); //*p++;// );
xi=((*p++)<<8)|*p++; //*p++; xi=xi|*p++;
}
}
//*显示字母数字在x,y处*/
void dis_str(uchar x, uchar y, uchar *str,uchar number)
{
uchar loc,*p;
uchar xline[5]={0,1,3,2,4};
p=str;
x=xline[x];
instruct(home);
loc=(16*(x-1)+y-1);
if(loc&0x01){
loc/=2;
instruct(loc|0x80);
write_abyte(' ');
}else{
loc/=2;
instruct(loc|0x80);
}
while(number--){
write_abyte(*p++);
}
}
/*---------------------------------------------------------*/
void initial_screen(void)
{
unsigned char i,j;
PORTD&=0x7f; //P_RS=0;
PORTD&=0xdf;//P_E=0;
instruct(dis_on);
for(i=0;i<44;i++){;}
instruct(clear);
for(j=0;j<14;j++){
for(i=0;i<250;i++){}
}
instruct(in_mode);
for(i=0;i<44;i++){;}
instruct(home);
for(i=0;i<44;i++){;}
}
/*---------------------------------------------------------*/
void itoa(int x)
{
unsigned int n;
unsigned char chn,*p,*p2,str1[5];
unsigned char i,j;
if(x<0){
*p2++='-';
x=-x;
}
n=x;
p=str1;
j=0;
while(n!=0){
chn=n%10;
n=n/10;
*p++=chn+'0';
j++;
}
if(j!=0){
p--;
for(i=0;i<j;i++)*p2++=*p--;
}
else *p2++='0';
*p2=0;
return;
}
/*************************************************
start_address 需操作iic的首地址
count 需读写个数
**************************************************/
void dlay(void)
{
#asm
nop;
nop;
nop;
#endasm
}
/*************************************************/
void tx(unsigned char k) //向iic里写数据*/
{
unsigned char temp=8,err_count=0;
while(temp--)
{
SCL_IO=0;
SDA_IO=0;
dlay( );
if((k&0b10000000))
{
SDA=1;
}
else
{
SDA=0;
}
SCL=1;
dlay( );
SCL=0;
k<<=1;
}
SCL=1;
SDA_IO=1;
while(SDA) //ASK*/
{
dlay( );
SCL=0;
dlay( );
SCL=1;
dlay( );
err_count++;
if(err_count>no_ask)
{
err_flag=1; //无应答超时*/
return;
}
}
SCL=0;
}
/*************************************************/
void start(void) //启动iic*/
{
SCL_IO=0;
SDA_IO=0;
dlay( );
SDA=1;
SCL=1;
dlay( );
SDA=0;
dlay( );
SCL=0;
return;
}
/*************************************************/
void stop(void) //停止iic*/
{
dlay( );
SCL_IO=0;
SDA_IO=0;
dlay( );
SDA=0;
SCL=1;
dlay( );
SDA=1;
dlay( );
SCL=0;
return;
}
/*************************************************/
unsigned char rx(void) //从iic里读出数据*/
{
unsigned char temp=8,buf;
SDA_IO=1;
dlay( );
while(temp--)
{
SCL=1;
buf=(buf<<1)|SDA;
SCL=0;
}
return(buf);
}
/*************************************************/
void iic_write(unsigned char start_address,unsigned char count)
{
uchar i;
err_flag=0;
start( );
//tx(0xa0);
tx(0xa2); //根据器件接法,器件地址为0x1010000W/R,写为0。
tx(start_address);
/*
while(count--)
{
tx(write[count]);
}
//*/
for(i=0;i<count;i++)tx(write[i]);
stop( );
}
/*************************************************/
void iic_read(unsigned char start_address,unsigned char count)
{
uchar i;
err_flag=0;
start( );
tx(0xa2); //根据器件接法,器件地址为0x0x1010000W/R。
tx(start_address);
start( );
tx(0xa3); //根据器件接法,器件地址为0x1010000W/R,读为1。
/* while(count--)
{}*/
for(i=0;i<count;i++)
{
read[i]=rx( );
SCL_IO=0;
SDA_IO=0;
dlay( );
SDA=0;
dlay( );
SCL=1;
dlay( );
SCL=0;
}
read[i]=rx( ); //再读一次以便停止*/
dlay( );
SDA=1;
SCL=0;
dlay( );
SCL=1;
stop( );
}
/* ****************************************************************
** 函 数 名: initial()
** 功能描述: 系统初始化子程序,放在程序首部
*************************************************************** */
void initial()
{
INTCON=0x00; //* bit7-bit0:关总中断 */
ADCON1=0X07; //* 设置数字输入输出口 */
PIE1=0; //* PIE1 的中断不使能 */
PIE2=0; //* PIE2 的中断不使能 */
PIE3=0; //* PIE3 的中断不使能 */
PORTD=0x1f;
TRISD=0x1f;
}
/* **************************************************************
** 函 数 名: main()
** 功 能:主函数
****************************************************************** */
main()
{
int second,minute,hour;
uchar dd[]="东北农业大学";
uchar timer[8];
timer[0]=timer[1]=timer[3]=timer[4]=timer[6]=timer[7]=48;
timer[2]=timer[5]=':';
initial(); //* 系统初始化子程序 */
initial_screen();
dis_str(1,1,dd,12);
//I2C_Initial(); //* i2c初始化子程序 */
iic_write(0x00,1);
iic_write(0x00,5);
while(1)
{
delay_us(5000);delay_us(50000);
iic_read(1,4);
hour=(read[3]>>4)*10+(read[3]&0x0f);
minute=(read[2]>>4)*10+(read[2]&0x0f);
second=(read[1]>>4)*10+(read[1]&0x0f);
timer[0]=(read[3]>>4)+48;
timer[1]=(read[3]&0x0f)+48;
timer[3]=(read[2]>>4)+48;
timer[4]=(read[2]&0x0f)+48;
timer[6]=(read[1]>>4)+48;
timer[7]=(read[1]&0x0f)+48;
dis_str(2,3,timer,8);
delay_us(5000);delay_us(50000);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -