⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcf8583.c

📁 实现对PCF8583的读写
💻 C
字号:
#include"reg52.h"
#include "intrins.h"
#include "stdio.h"
#include "ctype.h"
#include "string.h"

#define RESET_ADD      0x02
#define RESET_CODE     0x01
#define LEFTMOV_ADD    0x03
#define LEFTMOV_CODE   0
#define LEFTCYC_ADD    0x03
#define LEFTCYC_CODE   0x01
#define RIGHTMOV_ADD   0x03
#define RIGHTMOV_CODE  0x02
#define RIGHTCYC       0x03
#define RIGHTCYC       0x03
#define SYSOFF_ADD     0x04
#define  SYSOFF_CODE   0x00
#define SYSON1_ADD     0x04
#define SYSON1_CODE    0x01
#define SYSON2_ADD     0x04
#define SYSON2_CODE    0x03
#define SYSON3_ADD     0x04
#define SYSON3_CODE    0x07
#define DSP_ADD        0x05
#define DSP_CODE       0x0a
#define BCD_ADD        0x05
#define BCD_CODE       0x80
#define TWINKLE_ADD    0x06
#define TWINKLE_CODE   0x00
#define DIG0           0x08
#define DIG1           0x09
#define DIG2           0x0a
#define DIG3           0x0b
#define DIG4           0x0c
#define DIG5           0x0d
#define DIG6           0x0e
#define DIG7           0x0f	
#define NO_KEY         0
#define MENU           0x7b
#define CLEAR          0x7f
#define OK             0x7c
#define UP             0x7d
#define DOWN           0x7e	
#define CODE0          0x7e
#define CODE1          0x28
#define CODE2          0x5b
#define CODE3          0x6b
#define CODE4          0x2d
#define CODE5          0x67
#define CODE6          0x77
#define CODE7          0x2e
#define CODE8          0x7f
#define CODE9          0x6f
#define CODEF          0x17
#define CODEU          0x7c
#define POINT          0x80
#define year       0x10
#define month      0x11
#define date       0x12
#define hours      0x13
#define minutes    0x14
#define seconds    0x15
#define weekday    0x16

sbit B0=B^0;
sbit B1=B^1;
sbit B2=B^2;
sbit B3=B^3;
sbit B4=B^4;
sbit B5=B^5;
sbit B6=B^6;
sbit B7=B^7;

sbit CH451_DCLK=P0^3;
sbit CH451_DIN=P0^2;
sbit CH451_LOAD=P0^1;
sbit CH451_DOUT=P0^0;
sbit SC=P0^5;
sbit SD=P0^4;

unsigned char timer,key,time;
unsigned char     keydata[8]={0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0};
unsigned char     dig0[8]={0,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char     uled[8]={CODE0,CODE1,CODE2,0,0,0,CODEF,0};
unsigned char     dled[8]={CODE0,CODE1,CODE2,CODE2,CODE2,CODEU,CODE2,CODE2};
unsigned char     dig[8]={DIG0,DIG1,DIG2,DIG3,DIG4,DIG5,DIG6,DIG7};
/*-------------------------------------------------------*/
void timer0(void) interrupt 1              /*中断T0*/
{
TH0=0x10;
timer++;
if(timer<8)return;                          /*计8次返回*/
timer=0;
}
/*------------------------------------------------------------*/
void CH451_WRITE(unsigned char uadd,unsigned char ux)
{
CH451_DIN=0;CH451_DIN=1;

B=ux;
CH451_DIN=B0;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=B1;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=B2;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=B3;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=B4;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=B5;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=B6;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=B7;CH451_DCLK=0;CH451_DCLK=1;
B=uadd;
CH451_DIN=B0;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=B1;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=B2;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=B3;CH451_DCLK=0;CH451_DCLK=1;

CH451_LOAD=0;CH451_LOAD=1;
}
/*-----------------------------------*/
char CH451_READ()                      /*读CH451,当有键按下时,从DOUT口串行读出数据*/
{
CH451_DIN=0;CH451_DIN=1;

CH451_DIN=1;CH451_DCLK=0;CH451_DCLK=1; /*发送两个字节,将第二个CH451高四位送入7,通知其读键值*/
CH451_DIN=1;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=1;CH451_DCLK=0;CH451_DCLK=1;
CH451_DIN=0;CH451_DCLK=0;CH451_DCLK=1;

CH451_LOAD=0;CH451_LOAD=1;               /*加载数据*/

B7=0;                                    /*读键值,并将其存入B寄存器中*/
B6=CH451_DOUT;CH451_DCLK=0;CH451_DCLK=1;
B5=CH451_DOUT;CH451_DCLK=0;CH451_DCLK=1;
B4=CH451_DOUT;CH451_DCLK=0;CH451_DCLK=1;
B3=CH451_DOUT;CH451_DCLK=0;CH451_DCLK=1;
B2=CH451_DOUT;CH451_DCLK=0;CH451_DCLK=1;
B1=CH451_DOUT;CH451_DCLK=0;CH451_DCLK=1;
B0=CH451_DOUT;CH451_DCLK=0;CH451_DCLK=1;
if(B6==0)B=0;                           /*如果键值为释放时的值,将B清零*/
return B;
}
/*-----------------------------------------------*/
display_led()                           
{
unsigned char i;
for(i=0;i<8;i++)
{
CH451_WRITE(dig[i],keydata[i]);
}
}
/*-------------------------------------------------------------------*/
start_pcf8583()
{
SD=0;_nop_();_nop_();SC=0;
}
/*-------------------------------------------------------------------*/
stop_pcf8583()
{
SD=0;SC=1;_nop_();_nop_();SD=1;
}
/*---------------------------------------------------------------------*/
void write_pcf8583(char x)
{
B=x;
SD=B7;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
SD=B6;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
SD=B5;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
SD=B4;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
SD=B3;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
SD=B2;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
SD=B1;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
SD=B0;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
SD=1;SC=1;_nop_();_nop_();SC=0;
}
/*-------------------------------------------------------------------*/
void write_byte_pcf8583(unsigned char add,char x)
{
start_pcf8583();
write_pcf8583(0xa0);						   
write_pcf8583(add);
write_pcf8583(x);
stop_pcf8583();
}
/*----------------------------------------------------------------------*/
char read_pcf8583()
{
SD=1;
B7=SD;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
B6=SD;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
B5=SD;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
B4=SD;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
B3=SD;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
B2=SD;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
B1=SD;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
B0=SD;SC=1;_nop_();_nop_();SC=0;_nop_();_nop_();
SC=1;_nop_();_nop_();SC=0;
return B;
}
/*-------------------------------------------------------------------------*/
char read_byte_pcf8583(unsigned char add)
{
start_pcf8583();
write_pcf8583(0xa0);
write_pcf8583(add);
stop_pcf8583();
start_pcf8583();
write_pcf8583(0xa1);
read_pcf8583();
stop_pcf8583();
}
/*---------------------------------------------------------------------------*/
void main(void)
{
TMOD=0x01;                                    /*设置定时中断T0*/
EA=1;
ET0=1;
TR0=1;
for(;;)
{
while(timer!=0) PCON=0x01;   
CH451_WRITE(SYSON2_ADD,SYSON2_CODE);
CH451_WRITE(DSP_ADD,DSP_CODE);
display_led();
key=CH451_READ();
CH451_WRITE(DIG7,key);
time=read_byte_pcf8583(seconds);
CH451_WRITE(DIG7,time);
}
}
/*------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -