📄 counter3.c
字号:
////////////////////////////
//private counter
//cjy 2006 11.28 at GDUT
///////////////////////////
#include "reg52.h"
#include "math.h"
#include "24c02.h"
////////端口定义///////////
#define com_port P0//共阳
#define bit_port P2 //位选
sbit motor =P3^1;//总计数LED
sbit src =P3^0;//可控硅
sbit relay =P3^6;//继电器
sbit count_in =P3^2;//计数传感器输入
///////按键定义///////////
#define set 1
#define right 2
#define up 3
#define rst 4
/////////led数码管笔画表////////////
unsigned char code led_tab[]={
0xC0,/*0*/
0xF9,/*1*/
0xA4,/*2*/
0xB0,/*3*/
0x99,/*4*/
0x92,/*5*/
0x82,/*6*/
0xF8,/*7*/
0x80,/*8*/
0x90,/*9*/
0x88,/*A*/
0x83,/*b*/
0xBF,/*-*/};
///////函数声明/////////////////////
void sys_init(void);
void led_display(void);
void conver(void);
unsigned char scan_key(void);
unsigned get_key(void);
void delay(unsigned int t);
void read_all(void);
void write_all(void);
void write_fall(void);
void write_rise(void);
///////变量声明/////////////////////
struct buff//定义一个结构体
{
unsigned char count_bits[6];//
unsigned long count_num;
};
struct buff count_buff[5];
struct buff *p;
char count_bit;
unsigned char status=0;
unsigned int fall,rise;
bit flash_all=0;
bit last_status=0;
unsigned char add_time=0;
////////////主函数//////////////////
void main(void)
{ // i=10,temp;
sys_init();
x24c02_init();
count_bit=-1;
status=0;
p=count_buff;
read_all(); //for (temp=0;temp<100;temp++) x24c02_write(temp, 0);
///x24c02_write(10, 1);y=x24c02_read(10);p->count_num= y;p->count_num<<=8;y=123;p->count_num|=y;
conver();
while(1)
{
switch(get_key())
{
case set:
flash_all=0;
p++;status++;count_bit=-1;
if(status>3)
{p=count_buff; status=0;}
write_all();
break;
case right:
flash_all=(status==0);
count_bit--;
if(count_bit<0)
{
if(status==1||status==2)
count_bit=3 ;
else
count_bit=5;
}
break;
case up:
if(count_bit>0)
{
if((p->count_bits[count_bit])==9)
p->count_num-=9*pow(10,(count_bit));
else
p->count_num+=pow(10,count_bit);
}
if(status==0&&flash_all==1)
{
p=count_buff;
flash_all=0;
count_bit=-1;
write_all();
}
break;
case rst:
status=0;
p=count_buff;
flash_all=0;
count_bit=-1;
count_buff[0].count_num=0;
write_all();
break;
default:
;
break;
}
conver();
led_display();
}
}
//T0中断服务程序
void timer0_int (void) interrupt 1
{
if(last_status^count_in)
{
if(last_status)
{
fall++;
if(fall>=count_buff[2].count_num)
{
fall=0;
src=0;relay=0;
}
write_fall();
}
else
{
rise++;
if(rise>=(count_buff[1].count_num-count_buff[2].count_num))
{
rise=0;
count_buff[0].count_num++; //write_all();
src=1;relay=1;
}
write_rise();
}
if(count_buff[1].count_num<=count_buff[0].count_num)
{
motor=1;
relay=1;
}
last_status=count_in;
}
}
void sys_init(void)
{
//这里编写需要初始化的参数
TMOD=0x02;//T0,方式2,8位自动重装
TH0=0;//12MHZ 晶振时,0.256ms
TL0=0;
ET0=1;
EA=1;
TR0=1;//开定时器
}
////////////////////////////////////////////////////
void conver(void)
{
unsigned long int i;
unsigned char j;
i=p->count_num;
for(j=0;j<6;j++)
{
p->count_bits[j]=i%10;
i/=10;
}
if(p-count_buff==1)
{
p->count_bits[5]=10;
p->count_bits[4]=12;
}
if(p-count_buff==2)
{
p->count_bits[5]=11;
p->count_bits[4]=12;
}
}
////////////////////////////////////////////////////
void led_display(void)
{
unsigned char k,n,temp,num;
unsigned int j;
j++;if(j>30) j=0;
if(status==0)
{
for(n=7;(n>1&&p->count_bits[n-1]==0);n--)//前面的0关掉不显示***********
;
}
else n=6;
temp=0x80;
for(k=0;k<n;k++)//从右到左扫描,低位到高位
{
num=p->count_bits[k];
if(p-count_buff==3)//*************
com_port=led_tab[num]&0x7f;
else
com_port=led_tab[num];
if(k==count_bit&&j<16&&count_bit>=0)
{
bit_port=0xff;
}
else if(j<14&& flash_all==1)
bit_port=0xff;
else
bit_port=~temp;
temp=temp>>1;
delay(150);
bit_port=0xff;//消除鬼影
}
}
/////////////////////////////////////////////////////////////
unsigned char scan_key(void)
{
unsigned char result=0;
P1|=0x78;//0111 1000
if((P1&0x78)!=0x78)
{
switch (P1&0x78)
{
case 0x38: result=1; break;
case 0x58: result=2; break;
case 0x68: result=3; break;
case 0x70: result=4; break;
default: result=0; break;
}
return result;
}
}
unsigned get_key(void)
{
unsigned char temp1,key_num=0;
unsigned char i=80;//按键时间
if((temp1=scan_key())!=0);
{
delay(100);//去抖动,可以适当改变
if(temp1==scan_key())
{
key_num=temp1;
while(scan_key()!=0&&i--)led_display();//放开按键或时间到都可以退出循环
}
}
return key_num;
}
//////////////////////////////////////////////////////////////////////////////////
void delay(unsigned int t)
{
unsigned int i;
for(i=0;i<t;i++)
;
}
///////////////////////////////////////////////////
void read_all(void)// EEPROM 高位在前面
{
unsigned char temp,i;
struct buff *temp_p;
temp_p=p;
p=count_buff;
for(i=6;i<26;i+=4,p++)
{
temp= x24c02_read(i);
p->count_num|=temp;
p->count_num<<=8;
temp= x24c02_read(i+1);
p->count_num|=temp;
p->count_num<<=8;
temp= x24c02_read(i+2);
p->count_num|=temp;
p->count_num<<=8;
temp= x24c02_read(i+3);
p->count_num|=temp;
}
p=temp_p;
count_buff[1].count_num=count_buff[2].count_num;
temp= x24c02_read(32);//fall high 8 bits
fall|=temp;
fall<<=8;
temp= x24c02_read(33);//fall low 8 bits
fall|=temp;
temp= x24c02_read(30);//rise high 8 bits
rise|=temp;
rise<<=8;
temp= x24c02_read(31);//rise low 8 bits
rise|=temp;
}
void write_all(void)
{
unsigned char temp,i;
unsigned long int k;
struct buff *temp_p;
temp_p=p;
p=count_buff;
for(i=10;i<27;i+=4,p++)
{
k=p->count_num;
temp=(unsigned char)k&0x000000ff;
x24c02_write(i+3, temp);
k>>=8;
temp=(unsigned char)k&0x000000ff;
x24c02_write(i+2, temp);
k>>=8;
temp=(unsigned char)k&0x000000ff;
x24c02_write(i+1, temp);
k>>=8;
temp=(unsigned char)k&0x000000ff;
x24c02_write(i, temp);
}
p=temp_p;
}
void write_fall(void)
{
unsigned char temp;
unsigned int k;
k=fall;
temp=(unsigned char)k&0x00ff;
x24c02_write(33, temp);//fall low 8 bits
k>>=8;
temp=(unsigned char)k&0x00ff;//fall high 8 bits
x24c02_write(32, temp);
}
void write_rise(void)
{
unsigned char temp;
unsigned int k;
k=rise;
temp=(unsigned char)k&0x00ff;
x24c02_write(31, temp);//rise low 8 bits
k>>=8;
temp=(unsigned char)k&0x00ff;//rise low 8 bits
x24c02_write(30, temp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -