标题:
pcf8576的单片机例程
[打印本页]
作者:
378911501
时间:
2018-11-27 09:14
标题:
pcf8576的单片机例程
#include <reg52.h>
#include <intrins.h>
//============================ constant define =============================
bit ACK;
bit Pauseflag;
typedef unsigned char uchar;
typedef unsigned int uint;
unsigned char time_count;
unsigned char mode=6;//1/4duty,1/3bias
unsigned char data Displaybuf[4];
unsigned char code Work_Mode[][4]={
{0x70,0xe0,0Xcd,0x00}, //static,oPen display,normal work mode //0
{0x70,0xe0,0xce,0x00}, //1/2duty,1/2bias,open display,normal work mode //1
{0x70,0xe0,0xca,0x00}, //1/2duty,1/3bias,open display,normal work mode //2
{0x70,0xe0,0xcf,0x00}, //1/3duty,1/2bias,open display,normal work mode //3
{0x70,0xe0,0xcb,0x00}, //1/3duty,1/3bias,open display,normal work mode //4
{0x70,0xe0,0xcc,0x00}, //1/4duty,1/2bias,open display,normal work mode //5
{0x70,0xe0,0xc8,0x00}, //1/4duty,1/3bias,open display,normal work mode //6
};
unsigned char code Num[10]={
//0 1 2 3 4 5 6 7 8 9
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
};
//uchar code SEG0[]={Hz,_2D,_2F,_2C,_2B,_1D,_1F,_1C,_1B,0};
//uchar code SEG1[]={_2E,_2G,vpm,_2A,_1E,_1G,_1DP,_1A,0};
//============================= I/0 define =================================
sbit SDA = P1^7; //Serial Data
sbit SCL = P1^6; //Serial Clock
sbit Pause_key = P2^5; //Pause key
//=========================== function prototype ===========================
void Delayms(unsigned int ms);
void Delayus(void);
void I2C_Start(void);
void I2C_Stop(void);
void I2C_Check_ACK(void);
void I2C_SendByte(unsigned char dat);
void Set_Mode(void);
void Init_PCF8576(void);
void DisplayUpdata(void);
void Filled_RAM(unsigned char dat);
void Init_CPU(void);
//void CLR_buf(void);
//void Fill_buf(unsigned char *pp);
void Scan_key(void);
void Delay_ms(unsigned int ms);
//void WritePixe(unsigned char seg,bit ss);
//void WriteStep1();
//void WriteStep2();
//============================ Delayms function ============================
void Delayms(unsigned int ms)
{
unsigned char i;
while(--ms)
{
for(i=125;i>0;i--);
}
}
//============================ Delay5us function ============================
void Delayus(void)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
//=========================== I2C_Start function ============================
void I2C_Start(void)
{
SDA = 1;
SCL = 1;
Delayus();
SDA = 0;
Delayus();
SCL = 0;
}
//=========================== I2C_Stop function =============================
void I2C_Stop(void)
{
SDA = 0;
SCL = 1;
Delayus();
SDA = 1;
Delayus();
SCL = 0;
}
//========================= I2C_Check_ACK function ===========================
void I2C_Check_ACK(void)
{
SDA = 1;
SCL = 1;
Delayus();
ACK = 0;
if (SDA==1) ACK = 1;
SCL = 0;
}
//========================== I2C_SendByte function ===========================
void I2C_SendByte(unsigned char dat)
{
unsigned char j;
for(j=0;j<8;j++)
{
SDA = (dat & 0x80) ? 1 : 0;
SCL = 1;
Delayus();
SCL = 0;
Delayus();
dat <<= 1;
}
I2C_Check_ACK();
}
//=========================== Set_Mode function ===============================
void Set_Mode(void)
{
unsigned char j;
I2C_Start();
for(j=0;j<4;j++)
{
I2C_SendByte(Work_Mode[mode][j]);
}
I2C_Stop();
// Filled_RAM(0xff);
}
//========================== Init_PCF8576 function ============================
void Init_PCF8576(void)
{
Set_Mode();
//Delay_ms(1000);
}
//========================== DisplayUpdata function ============================
void DisplayUpdata(void)
{
uchar i;
I2C_Start();
I2C_SendByte(0x70);//order
I2C_SendByte(0xe0);//address
I2C_SendByte(0x00);//address
for(i=0;i<4;i++)
{
I2C_SendByte(Displaybuf[i]);
}
I2C_Stop();
}
//=========================== Filled_RAM function ==============================
void Filled_RAM(unsigned char dat)
{
uchar i;
for(i=0;i<4;i++)
{
Displaybuf[i]=dat;
}
DisplayUpdata();
}
//============================ Init_CPU function ==============================
void Init_CPU(void)
{
P1 = 0xff;
Pauseflag=0;
}
/*******************************************************************************
//============================= CLR_buf function ==============================
void CLR_buf(void)
{
unsigned char j;
for(j=0;j<27;j++)
{
Displaybuf[j] = 0;
}
}
//============================= Fill_buf function ==============================
void Fill_buf(unsigned char *pp)
{
CLR_buf();
while(*pp)
{
Displaybuf[(*pp-1)/8] |= (0x80 >> (*pp-1)%8);
pp++;
}
}
/*******************************************************************************/
//============================= Scan_key function ==============================
void Scan_key(void)
{
if(Pause_key==0)
{
Delayms(10);
if(Pause_key==0)Pauseflag=~Pauseflag;
while(Pause_key==0);
}
}
//============================ Delay_ms function ============================
void Delay_ms(unsigned int ms)
{
unsigned char i;
while(--ms)
{
Scan_key();
for(i=125;i>0;i--);
}
}
/*
//=============================== WritePixe ================================
void WritePixe(unsigned char seg,bit ss) //seg = 1; buf[0]=0x40
{
if(ss)
{
Displaybuf[(seg-1)/8] |= ( 0x80 >> ((seg-1)%8));
}
else
{
Displaybuf[(seg-1)/8] &= ~( 0x80 >> ((seg-1)%8));
}
}
/*******************************************************************************
void WriteAll()
{
unsigned char i;
for(i=0;i<193;i++)
{
WritePixe(X10+i,1);
DisplayUpdata();
Delay_ms(1000);
}
}
/*******************************************************************************
//=============================== WriteStep ================================
void WriteStep1()
{
unsigned char *p;
p=&SEG0[0];
while(*p)
{
WritePixe(*p,1);
p++;
}
DisplayUpdata();
Delay_ms(2000);
while(Pauseflag==1)Scan_key();
}
void WriteStep2()
{
unsigned char *p;
p=&SEG1[0];
while(*p)
{
WritePixe(*p,1);
p++;
}
DisplayUpdata();
Delay_ms(2000);
while(Pauseflag==1)Scan_key();
}
/*******************************************************************************/
//=============================== main function ================================
void main(void)
{
// unsigned char i;
Init_CPU();
Init_PCF8576();
Filled_RAM(0x00);
while(1)
{
Filled_RAM(0xFF);
Delay_ms(3000);
while(Pauseflag==1)Scan_key();
Filled_RAM(0x00);
Filled_RAM(0x5A);
Delay_ms(2000);
while(Pauseflag==1)Scan_key();
Filled_RAM(0x00);
Filled_RAM(0xA5);
Delay_ms(2000);
while(Pauseflag==1)Scan_key();
};
}
复制代码
作者:
378911501
时间:
2018-11-27 09:15
大家看看
作者:
yoling
时间:
2021-7-2 15:57
378911501 发表于 2018-11-27 09:15
大家看看
写得真不错,感谢楼主
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1