/**
* 机顶盒电源冲激程序
*
* @FILE INCLUDE : swash.C Ledcode.h
*
* Copyright 2005 SHENZHEN COSHIP ELECTRONICS CO.,LTD
*
* All Rights Reserved
*
* 2005/08/08
*
* @version 3.0 $
*/
#define uint unsigned int
#define uchar unsigned char
#define HIGH 1
#define LOW 0
#define TRUE 1
#define FALSE 0
#define L 0
#define R 1
#include <reg51.h>
#include <intrins.h>
#include "ledcode.h"
/* 键盘键位定义*/
sbit UP = P1^0;
sbit DOWN = P1^1;
sbit LEFT = P1^2;
sbit RIGHT = P1^3;
sbit CLEAR = P1^4;
sbit OK = P1^5;
sbit LED = P1^6;
/* 8个74HC595串接,各驱动一个共阴数码管*/
sbit ST_CP = P2^0; /* 74hc595存储(显示) 时钟*/
sbit SH_CP = P2^1; /* 74hc595移位时钟*/
sbit DS = P2^2; /* 74hc595串行数据输入*/
/* I2C总线定义*/
sbit SCL = P2^3; /* I2C总线时钟*/
sbit SDA = P2^4; /* I2C总线串行数据线*/
/* 电压转换控制,不能同时置低*/
sbit VOL1 = P2^5; /* 冲激电压1控制开关,低电平启动*/
sbit VOL2 = P2^6; /* 冲激电压2控制开关,低电平启动*/
bit run1time = 0;
bit press_SET = 0;
bit LRSwitch = 0;
uint T0IntCount = 100;
uchar I2C_Counter[2] ;
uint VolChangeCounter =0;
uchar OnTime = 30;
uchar OffTime = 5;
unsigned char code rst[]={0xe4,0xc0,0xe0,0xc0,0xe0,0x32}; /* 软复位代码*/
volatile uchar BCD_T1H,BCD_T1L,BCD_T2H,BCD_T2L,BCD_C1H,BCD_C1L,BCD_C2H,BCD_C2L;
uchar CODE_T1H,CODE_T1L,CODE_T2H,CODE_T2L,CODE_C1H,CODE_C1L,CODE_C2H,CODE_C2L;
void (*Process)();
void usDelay(uchar us)
{
for(;us>0;us--);
}
void msDelay(uint delay_ms)
{
unsigned char j;
for(;delay_ms>0;delay_ms--)
{
for(j=0;j<=165;j++)
{
;
}
}
}
void I2C_StartCond(void)
{
SDA = HIGH;
usDelay(10);
SCL = HIGH;
usDelay(10);
SDA = LOW;
usDelay(10);
SCL = LOW;
usDelay(10);
SDA = HIGH;
usDelay(10);
}
void I2C_StopCond(void)
{
SDA = LOW;
usDelay(10);
SCL = HIGH;
usDelay(10);
SDA= HIGH;
usDelay(10);
SCL = LOW;
usDelay(10);
}
bit I2C_SampleCLK(void)
{
bit sample;
usDelay(1);
SCL = HIGH;
usDelay(1);
sample = SDA;
usDelay(1);
SCL = LOW;
usDelay(1);
return(sample);
}
/****************************
void I2C_ACK(void)
{
SDA = LOW;
I2C_SampleCLK();
SDA = HIGH;
}
*****************************/
void I2C_NOACK(void)
{
SDA = HIGH;
I2C_SampleCLK();
SDA = HIGH;
}
bit I2C_WaitAck(void)
{
uchar timewait = 20;
SDA = 1;
usDelay(10);
SCL = 1;
usDelay(10);
while(SDA)
{
timewait--;
if(!timewait)
{
I2C_StopCond();
return FALSE;
}
}
SCL = 0;
return TRUE;
}
bit I2C_SendByte(uchar byte)
{
register uchar i;
for(i=8;i>0;i--)
{
SDA = (bit)(byte&0x80);
I2C_SampleCLK();
byte = byte<<1;
}
return I2C_WaitAck();
}
uchar I2C_ReceiveByte(void)
{
uchar revbyte = 0;
register uchar i;
for(i=8;i>0;i--)
{
revbyte = revbyte<<1;
if(I2C_SampleCLK())
revbyte++;
}
return revbyte;
}
bit I2C_WB(uchar addr1,uchar addr0,uchar savedata)
{
I2C_StartCond();
if(I2C_SendByte(0xA0)
&&I2C_SendByte(addr1)
&&I2C_SendByte(addr0)
&&I2C_SendByte(savedata))
{
I2C_StopCond();
return TRUE;
}
else
{
I2C_StopCond();
return FALSE;
}
}
bit I2C_RB(uchar addr1,uchar addr0,uchar *readbuf)
{
I2C_StartCond();
if(I2C_SendByte(0xA0)&&I2C_SendByte(addr1)&&I2C_SendByte(addr0))
{
I2C_StartCond();
if(I2C_SendByte(0xA1))
*readbuf = I2C_ReceiveByte();
I2C_NOACK();
I2C_StopCond();
return TRUE;
}
else
{
I2C_StopCond();
return FALSE;
}
}
void CT_tegether(void)
{
VolChangeCounter = I2C_Counter[1]*100+I2C_Counter[0];
}
void CT_become2part(void)
{
if(VolChangeCounter>9999)
VolChangeCounter = VolChangeCounter % 10000;
I2C_Counter[1] = VolChangeCounter /100;
I2C_Counter[0] = VolChangeCounter % 100;
}
void Save(void)
{
CT_become2part();
while(!I2C_WB(0x00,0x00,OnTime));
while(!I2C_WB(0x00,0x01,OffTime));
while(!I2C_WB(0x00,0x02,I2C_Counter[0]));
while(!I2C_WB(0x00,0x03,I2C_Counter[1]));
}
void BINtoBcd(uchar bin,uchar *bcdH,uchar*bcdL)
{
if(bin>99)
bin = bin%100;
*bcdH = bin /10;
*bcdL = bin % 10;
}
void BINtoBCD_CNT(void)
{
//CT_become2part();
BINtoBcd(I2C_Counter[1],&BCD_C1H,&BCD_C1L);
BINtoBcd(I2C_Counter[0],&BCD_C2H,&BCD_C2L);
}
void Shift595(uchar codebuf)
{
register uchar j;
DS = 0;
SH_CP = 0;
ST_CP = 0;
for(j=0;j<8;j++)
{
DS = (bit)(codebuf&0x80);
usDelay(1);
SH_CP = 1;
usDelay(1);
SH_CP = 0;
usDelay(1);
codebuf= _crol_(codebuf,1);
usDelay(1);
}
}
void Latch595(void)
{
ST_CP = 1;
usDelay(1);
ST_CP = 0;
}
uchar AND(uchar high,low)
{
uchar revalue;
revalue = high&low;
return revalue;
}
void BCDtoCODE(void)
{
CODE_T1H= AND(LH[BCD_T1H][0],LL[BCD_T1L][0]);
CODE_T1L = AND(LH[BCD_T1H][1],LL[BCD_T1L][1]);
CODE_T2H= AND(RH[BCD_T2H][0],RL[BCD_T2L][0]);
CODE_T2L = AND(RH[BCD_T2H][1],RL[BCD_T2L][1]);
CODE_C1H= AND(LH[BCD_C1H][0],LL[BCD_C1L][0]);
CODE_C1L = AND(LH[BCD_C1H][1],LL[BCD_C1L][1]);
CODE_C2H= AND(RH[BCD_C2H][0],RL[BCD_C2L][0]);
CODE_C2L = AND(RH[BCD_C2H][1],RL[BCD_C2L][1]);
}
void Display(void)
{
Shift595(CODE_T2H);
Shift595(CODE_T2L);
Shift595(CODE_T1H);
Shift595(CODE_T1L);
Shift595(CODE_C2H);
Shift595(CODE_C2L);
Shift595(CODE_C1H);
Shift595(CODE_C1L);
Latch595();
}
void DisplayError(void)
{
Shift595(RL_g0);
Shift595(RH_g1);
Shift595(LL_g0);
Shift595(LH_g1);
Shift595(RL_g0);
Shift595(RH_g1);
Shift595(LL_g0);
Shift595(LH_g1);
Latch595();
}
void T1Twinkle(void)
{
Shift595(CODE_T2H);
Shift595(CODE_T2L);
Shift595(0xff);
Shift595(0xff);
Shift595(CODE_C2H);
Shift595(CODE_C2L);
Shift595(CODE_C1H);
Shift595(CODE_C1L);
Latch595();
msDelay(300);
BINtoBcd(OffTime,&BCD_T1H,&BCD_T1L);
BINtoBcd(OnTime,&BCD_T2H,&BCD_T2L);
BCDtoCODE();
Shift595(CODE_T2H);
Shift595(CODE_T2L);
Shift595(CODE_T1H);
Shift595(CODE_T1L);
Shift595(CODE_C2H);
Shift595(CODE_C2L);
Shift595(CODE_C1H);
Shift595(CODE_C1L);
Latch595();
msDelay(300);
}
void T2Twinkle(void)
{
Shift595(0xff);
Shift595(0xff);
Shift595(CODE_T1H);
Shift595(CODE_T1L);
Shift595(CODE_C2H);
Shift595(CODE_C2L);
Shift595(CODE_C1H);
Shift595(CODE_C1L);
Latch595();
msDelay(300);
BINtoBcd(OffTime,&BCD_T1H,&BCD_T1L);
BINtoBcd(OnTime,&BCD_T2H,&BCD_T2L);
BCDtoCODE();
Shift595(CODE_T2H);
Shift595(CODE_T2L);
Shift595(CODE_T1H);
Shift595(CODE_T1L);
Shift595(CODE_C2H);
Shift595(CODE_C2L);
Shift595(CODE_C1H);
Shift595(CODE_C1L);
Latch595();
msDelay(300);
}
void OKprocess(void)
{
Save();
while(!I2C_RB(0x00,0x00,&OnTime));
while(!I2C_RB(0x00,0x01,&OffTime));
while(!I2C_RB(0x00,0x02,&I2C_Counter[0]));
while(!I2C_RB(0x00,0x03,&I2C_Counter[1]));
CT_tegether();
BINtoBCD_CNT();
LRSwitch = L;
press_SET = 0;
//(*((void (*)())(rst)))();
}
void LEFTprocess(void)
{
LRSwitch = 0;
T1Twinkle();
}
void RIGHTprocess(void)
{
LRSwitch = 1;
T2Twinkle();
}
void UPprocess(void)
{
if(LRSwitch)
{
if(OnTime<95)
{
OnTime = OnTime +5;
T2Twinkle();
}
}
else
{
if(OffTime<95)
{
OffTime = OffTime +5;
T1Twinkle();
}
}
}
void DOWNprocess(void)
{
if(LRSwitch)
{
if(OnTime>10)
{
OnTime = OnTime -5;
T2Twinkle();
}
}
else if(OffTime>5)
{
OffTime = OffTime -5;
T1Twinkle();
}
}
void CLEARprocess(void)
{
VolChangeCounter = 0;
CT_become2part();
while(I2C_WB(0x00,0x02,0x00));
while(I2C_WB(0x00,0x03,0x00));
BINtoBcd(OffTime,&BCD_T1H,&BCD_T1L);
BINtoBcd(OnTime,&BCD_T2H,&BCD_T2L);
BINtoBCD_CNT();
BCDtoCODE();
Display();
}
void SecDelay(unsigned char delay_sec)
{
for(;delay_sec>0;delay_sec--)
if(!press_SET)
{
if(LRSwitch==L)
{
BINtoBcd(delay_sec,&BCD_T1H,&BCD_T1L);
评论0