PIC microcontroller i2c program

Publisher:RadiantSerenityLatest update time:2019-11-27 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include
#define uchar unsigned char
#define uint unsigned int
#define add 0xaa
__CONFIG(0xc3e4);
__CONFIG(0xffff);
#define DS1302 RC2 //Define to turn off the DS1302 clock chip so that the RC4 RC3 IO ports can be used for AT24C02 control.

const uchar ee_data[]={1,2,3,3,2,1};
uchar read_data[6];
const uchar table[]={0x3f, 0x6, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x7, 0x7f, 0x6f};
void delay(uint x);
void init();
void disp(uchar num1,uchar num2,uchar num3,uchar num4,uchar num5,uchar num6);
void write();
void read();
void main()
{
init();
DS1302=0;// 关掉DS1302
write();
delay(100);
read();

while(1)
{
disp(read_data[0],read_data[1],read_data[2],read_data[3],read_data[4],read_data[5]);
}
}
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void init()
{
TRISD=0;
TRISA=0;
//TRISC=0;
TRISE0=1;
// ADCON1=0x07;
//RE0=1;
PORTD=0x3f;
PORTA=0x00;
TRISC=0x18;//用到RC3 RC4 输入 1

SSPSTAT=0x80;
SSPCON=0x38;
SSPCON2=0;
SSPADD=0x09;
//PORTE=1;
}
void write()
{
uchar i;
SSPIF=0;
SEN=1;
while(!SSPIF);
SSPIF=0;
SSPBUF=0xA0;
while(!SSPIF);
SSPIF=0;
SSPBUF=add;
while(!SSPIF);
SSPIF=0;
for(i=0;i<6;i++)
{
SSPBUF=ee_data[i];
while(!SSPIF);
SSPIF=0;
}
PEN=1;
while(!SSPIF);
SSPIF=0;
}
void read()
{
uchar i;
SSPIF=0;
SEN=1;
while(!SSPIF);
SSPIF=0;
SSPBUF=0xA0;
while(!SSPIF);
SSPIF=0;
SSPBUF=add;
while(!SSPIF);
SSPIF=0;
SSPIF=0;
RSEN=1;
while(!SSPIF);
SSPIF=0;
SSPBUF=0xA1;
while(!SSPIF);
SSPIF=0;
for(i=0;i<6;i++)
{
RCEN=1;
while(!SSPIF);
read_data[i]=SSPBUF;
while(!SSPIF);
SSPIF=0;
if(i>=5)
{
ACKDT=1;
}
else
{
ACKDT=0;
}
ACKEN=1;
while(!SSPIF);
SSPIF=0;
}
PEN=1;
while(!SSPIF);
SSPIF=0;
}


void disp(uchar num1,uchar num2,uchar num3,uchar num4,uchar num5,uchar num6)
{
PORTD=table[num1];//Display the first digital tube
PORTA=0x01;//0010 0000
delay(2);
PORTD=table[num2];//Display the second digital tube
PORTA=0x02;//0001 0000
delay(2);
PORTD=table[num3];//Display the third digital tube
PORTA=0x04;//0000 1000
delay(2);
PORTD=table[num4];//Display the fourth digital tube
PORTA=0x08;//0000 0100
delay(2);
PORTD=table[num5];//Display the fifth digital tube
PORTA=0x10;//0000 0010
delay(2);
PORTD=table[num6]; //Display the sixth digital tube
PORTA=0x20; //0000 0001
delay(2);
}


Reference address:PIC microcontroller i2c program

Previous article:CONGFIG description of PIC microcontroller in ZC-ADK
Next article:LED single light flashing experiment of PIC microcontroller under proteus+MPLAB combination

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号