#include <STC/STC15F2K60S2.H>
#include <stdio.h>
#define FOSC 30000000L //系统时钟频率
#define BAUD 115200 //串口波特率
struct pin_t
{
unsigned char pad;
unsigned char pin;
};
struct cp_t
{
struct pin_t cp_pin[4];
int cp_index;
int cp_time;
int cp_feq;
unsigned int cp_count;
char cp_flag;
int cp_align;
char cp_dir;
};
unsigned char secret_key[8]={'1','9','8','8','1','0','1','7'};
unsigned char random[8]={'d','r','a','g','o','n',0x10,0x17};
//unsigned char gpio[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
//unsigned char gpiodir[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char gpiopl_up[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char gpiopl_dn[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char gpiointr_chk[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char gpiointr_mask[8]={0};
unsigned char gpiointr_status[8]={0};
struct cp_t cp_control[4]={0};
char exmcu_i2c_test = 0x55;
char exmcu_intr_status = 0;
char exmcu_intr_en = 0;
//#define I2C_TIMEOUT 200
unsigned int i2c_timeout;//超时时间
#define I2C_TIMEOUT (i2c_timeout*2+5)
unsigned char I2CsADDR=0x17;
sbit I2CsCLK=P3^4;
sbit I2CsDAT=P3^3;
sbit IntrPin=P3^2;
#define I2CsDAT_IN I2CsDAT=1
#define I2CsDAT_OUT
#define I2CsCLK_IN I2CsCLK=1
#define I2CsCLK_OUT
void RSHash()
{
char i;
unsigned char a=0;
unsigned char hash=0;
for(i = 0; i < 8; i++)
{
a = random[i]+hash;
hash += a;
random[i]=secret_key[hash%8]*hash;
}
}
int i2c_wait_low()
{
int i;
for(i=0;i<I2C_TIMEOUT;i++)
{
if(I2CsCLK==0)
{
return i;
}
}
return -1;
}
int i2c_wait_high()
{
int i;
for(i=0;i<I2C_TIMEOUT;i++)
{
if(I2CsCLK==1)
{
if(i!=0)
i2c_timeout=i;
return i;
}
}
return -1;
}
int i2c_send_ack()
{
//int i;
//低电平变化
i2c_wait_low();
I2CsDAT_OUT;
I2CsDAT=0;
//高电平稳定
i2c_wait_high();
return 0;
}
int i2c_read_ack()
{
//int i;
I2CsDAT_IN;
//低电平变化
i2c_wait_low();
//高电平稳定
i2c_wait_high();
return I2CsDAT;
}
int i2c_read_byte(unsigned char * pbyte)
{
char i;
//int ret;
unsigned char byte=0;
I2CsCLK_IN;
for(i=0;i<8;i++)
{
if(i2c_wait_low()<0)
{//STOP位
//I2CsDAT=1;
if(i<2)
return 1;
return -1;
//printf("i2c read wait low time out i=%d\r\n",i);
}
I2CsDAT_IN;
//I2CsDAT=1;
if(i2c_wait_high()<0)
{
//printf("i2c read wait high timeout[%d] read[%d]\r\n",i2c_timeout,i);
return -1;
}
byte=byte|((char)I2CsDAT<<7-i);
}
*pbyte=byte;
return 0;
}
int i2c_send_byte(unsigned char * pbyte)
{
char i;
//int ret;
unsigned char byte = *pbyte;
I2CsCLK_IN;
for(i=0;i<8;i++)
{
I2CsDAT_OUT;
if(i2c_wait_low()<0)
{
return 1;
}
I2CsDAT=(byte>>7-i)&0x1;
if(i2c_wait_high()<0)
{
//printf("i2c send wait high timeout[%d] read[%d]\r\n",i2c_timeout,i);
return -1;
}
}
return 0;
}
void put_intr()
{
IntrPin=0;
}
void clr_intr()
{
IntrPin=1;
}
void gpio_set_pad(char pad,unsigned char Data)
{
switch(pad)
{
case 0:
P0 = Data;
break;
case 1:
P1 = Data;
break;
case 2:
P2 = Data;
break;
case 3:
P3 = Data;
break;
case 4:
P4 = Data;
break;
}
}
unsigned char gpio_get_pad(char pad)
{
switch(pad)
{
case 0:
return P0;
case 1:
return P1;
case 2:
return P2;
case 3:
return P3;
case 4:
return P4;
}
return 0;
}
void gpio_set_bit(char pad,char pin,char onoff)
{
if(onoff)
gpio_set_pad(pad,gpio_get_pad(pad)|(1<<pin));
else
gpio_set_pad(pad,gpio_get_pad(pad)&~(1<<pin));
}
unsigned char gpio_get_bit(char pad,char pin)
{
return (gpio_get_pad(pad)>>pin)&0x1;
}
void gpio_set_pad_dir(char pad,unsigned char Data)
{
return;
switch(pad)
{
case 0:
P0 = Data;
break;
case 1:
P1 = Data;
break;
case 2:
P2 = Data;
break;
case 3:
P3 = Data;
break;
case 4:
P4 = Data;
break;
}
}
unsigned char gpio_get_pad_dir(char pad)
{
switch(pad)
{
case 0:
return P0;
case 1:
return P1;
case 2:
return P2;
case 3:
return P3;
case 4:
return P4;
}
return 0;
}
void gpio_set_bit_dir(char pad,char pin,char onoff)
{
if(onoff)
gpio_set_pad_dir(pad,gpio_get_pad_dir(pad)|(1<<pin));
else
gpio_set_pad_dir(pad,gpio_get_pad_dir(pad)&~(1<<pin));
}
unsigned char gpio_get_bit_dir(char pad,char pin)
{
return (gpio_get_pad_dir(pad)>>pin)&0x1;
}
typedef int (*ft_i2c_proc)(unsigned char * cmd,int count);
ft_i2c_proc i2c_proc[0xff] = {0};
int cmd_math_hash(unsigned char * cmd,int count)
{//收到数据
RSHash();//计算哈希值
return 0;
}
int cmd_clr_intr(unsigned char * cmd,int count)
{//清中断
if(count==1)
{//GPIO读取
cmd[1] = exmcu_intr_status;
return 0;
}
else
{
exmcu_intr_status=exmcu_intr_status^cmd[1];
return 0;
}
}
int cmd_en_intr(unsigned char * cmd,int count)
{//清中断
if(count==1)
{//GPIO读取
cmd[1] = exmcu_intr_en;
return 0;
}
else
{
//printf("exmcu_intr_en=%x\r\n",cmd[1]);
exmcu_intr_en=cmd[1];
//printf("exmcu_intr_status = %x\r\n",exmcu_intr_status);
return 0;
}
}
int cmd_i2c_test(unsigned char * cmd,int count)
{//清中断
if(count==1)
{//GPIO读取
cmd[1] =exmcu_i2c_test;
return 0;
}
exmcu_i2c_test=cmd[1];
return 0;
}
int cmd_cp_control(unsigned char * cmd,int count)
{//脉冲控制
int cpid;
cpid=(cmd[0]-0x60)/8;
if(count==1)
{
cmd[1]=cp_control[cpid].cp_flag;
return 0;
}
else if(count==2)
{
//printf("cmd_cp_control = 0x%x\r\n",(int)cmd[1]);
switch((cmd[1]>>3)&0x3)
{
case 0:
cp_control[cpid].cp_align = 4;
break;
case 1:
cp_control[cpid].cp_align = 32;
break;
case 2:
cp_control[cpid].cp_align = 64;
break;
case 3:
cp_control[cpid].cp_align = 256;
break;
}
if((cmd[1]>>6)&0x1)
cp_control[cpid].cp_dir = 1;
else
cp_control[cpid].cp_dir = 0;
cp_control[cpid].cp_flag = cmd[1];
return 0;
}
}
int cmd_cp_addr1(unsigned char * cmd,int count)
{
int cpid;
cpid=(cmd[0]-0x60)/8;
if(count==1)
{//读
return 0;
}
else
{//写
cp_control[cpid].cp_pin[0].pad=(cmd[1]>>4)&0xf;
cp_control[cpid].cp_pin[0].pin=(cmd[1]>>0)&0xf;
//printf("0 pad=%d pin=%d\r\n",cp_control[0].cp_pin[0].pad,cp_control[0].cp_pin[0].pin);
return 0;
}
}
int cmd_cp_addr2(unsigned char * cmd,int count)
{
int cpid;
cpid=(cmd[0]-0x60)/8;
if(count==1)
{//读
return 0;
}
else
{//写
cp_control[cpid].cp_pin[1].pad=(cmd[1]>>4)&0xf;
cp_control[cpid].cp_pin[1].pin=(cmd[1]>>0)&0xf;
//printf("1 pad=%d pin=%d\r\n",cp_control[0].cp_pin[1].pad,cp_control[0].cp_pin[1].pin);
return 0;
}
}
int cmd_cp_addr3(unsigned char * cmd,int count)
{
int cpid;
cpid=(cmd[0]-0x60)/8;
if(count==1)
{//读
return 0;
}
else
{//写
cp_control[cpid].cp_pin[2].pad=(cmd[1]>>4)&0xf;
cp_control[cpid].cp_pin[2].pin=(cmd[1]>>0)&0xf;
//printf("2 pad=%d pin=%d\r\n",cp_control[0].cp_pin[2].pad,cp_control[0].cp_pin[2].pin);
return 0;
}
}
int cmd_cp_addr4(unsigned char * cmd,int count)
{
int cpid;
cpid=(cmd[0]-0x60)/8;
if(count==1)
{//读
return 0;
}
else
{//写
cp_control[cpid].cp_pin[3].pad=(cmd[1]>>4)&0xf;
cp_control[cpid].cp_pin[3].pin=(cmd[1]>>0)&0xf;
//printf("3 pad=%d pin=%d\r\n",cp_control[0].cp_pin[3].pad,cp_control[0].cp_pin[3].pin);
return 0;
}
}
int cmd_cp_count(unsigned char * cmd,int count)
{//旋转个数
int cpid;
cpid=(cmd[0]-0x60)/8;
if(count==1)
{
cmd[1]=cp_control[cpid].cp_count/cp_control[cpid].cp_align;
return 0;
}
else
{
cp_control[cpid].cp_count=(cmd[1]+1)*cp_control[cpid].cp_align;
//printf("cp_control[0
- 1
- 2
前往页