#include "stm8s.h"
#include "stdlib.h"
#define DIO0 GPIO_WriteLow(GPIOA,GPIO_PIN_6)
#define DIO1 GPIO_WriteHigh(GPIOA,GPIO_PIN_6)
#define DIO_Read GPIO_ReadInputPin(GPIOA, GPIO_PIN_6)
#define STB0 GPIO_WriteLow(GPIOA,GPIO_PIN_4)
#define STB1 GPIO_WriteHigh(GPIOA,GPIO_PIN_4)
#define CLK0 GPIO_WriteLow(GPIOA,GPIO_PIN_5)
#define CLK1 GPIO_WriteHigh(GPIOA,GPIO_PIN_5)
#define uchar unsigned char
//long abs();
u8 num,flag,count;//按键值
u8 jia,jian,cheng,chu,equal,qing,work_num;//加,减,乘,除,等,清标志位
char shumg[10]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
char add[16]={0xC0,0xC2,0xC4,0xC6,0xC8,0xCA,0xCC,0xCE,0xC1,0xC3,0xC5,0xC7,0xC9,0xCB,0xCD,0xCF};
char error[8]={0x77,0x3f,0x77,0x77,0x79,0x3f,0x3f,0x3f};
u8 tab2[]={0,1,2,3,4,5,6,7,8,9};
char byte[4];
char key_value;
long sum=0,value=0,sum1;
void display(uchar num);
//void anjian();
void gpio_init()
{
GPIO_DeInit(GPIOA);
GPIO_Init(GPIOA, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOA, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOA, GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_FAST);
}
void delay_us(u16 i)
{
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_2,1000);
while(i--)
{
TIM2_Cmd(ENABLE);
while(TIM2_GetFlagStatus(TIM2_FLAG_UPDATE)!=1);
TIM2_ClearFlag(TIM2_FLAG_UPDATE);
TIM2_Cmd(DISABLE );
}
}
void write_data(u8 x )
{
int i;
STB0;
for(i=0;i<8;i++)
{
CLK0;
if((x&0x01)==1)
{ DIO1; }
else
{ DIO0; }
CLK1;
x=x>>1;
}
//STB1;
}
void write_Command(uchar command)
{
write_data(command);
}
char read_data(void)
{
char i,temp;
STB0;
for(i=0;i<8;i++)
{
CLK0;
temp=temp>>1;
if(DIO_Read==0)
{ temp&=0x7f;} //最高位置1 ,其它位不变
else
{ temp|=0x80;} //最高位置0,其它位不变
CLK1;
}
return temp;
}
char keyscan(void)
{
char value1 =0;
char a,aa;
GPIO_Init(GPIOA, GPIO_PIN_6,GPIO_MODE_IN_PU_NO_IT);
for(a=0;a<4;a++)
{
byte[a]=read_data();
}
for(aa=0;aa<4;aa++)
{
switch(byte[aa])
{
case 0x02: value1=1+2*aa;break;
case 0x20: value1=2+2*aa;break;
case 0x01: value1=9+2*aa;break;
case 0x10: value1=10+2*aa;break;
}
}
return value1;
}
void clear_screen()
{
char j;
write_Command(0x8e); //显示模式
STB1;
write_Command(0x40);//地址自增加模式
STB1;
for(j=0;j<16;j++)
{
write_Command(add[j]);
write_data(0x00);
STB1;
}
}
void main()
{
u8 i;
gpio_init();
clear_screen();
while(1)
{
write_Command(0x42);//读键扫数据
key_value=keyscan();
GPIO_Init(GPIOA, GPIO_PIN_6,GPIO_MODE_OUT_PP_HIGH_FAST);
write_Command(0x8e); //显示模式
STB1;
write_Command(0x44);//地址固定模式
STB1;
if(key_value!=0)
{
num=key_value-1;
}
if(key_value!=num)
{
display(num);
if(qing==1)
{
qing=0;
for(i=0;i<8;i++)
{
write_Command(add[i]);
write_data(shumg[0]);
STB1;
}
sum=0;
value=0;
}
}
}
}
/**********显示部分*******************************/
void display(uchar num)
{
u8 i,workdata; //处理显示标志位
num=key_value-1;
switch(num)
{
case 0:flag=1;break;
case 1:flag=1;break;
case 2:flag=1;break;
case 3:flag=1;break;
case 4:flag=1;break;
case 5:flag=1;break;
case 6:flag=1;break;
case 7:flag=1;break;
case 8:flag=1;break;
case 9:flag=1;break;
case 10:jia=1;break;
case 11:jian=1;break;
case 12:cheng=1;break;
case 13:chu=1;break;
case 14:equal=1;break;
case 15:qing=1;break;
}
if(flag==1) //键值信息显示
{
flag=0;
num=key_value-1;
switch(count)
{
case 0: value+=tab2[num];break;
case 1: value=((value*10)+tab2[num]);break;
case 2: value=((value*10)+tab2[num]);break;
case 3: value=((value*10)+tab2[num]);break;
case 4: value=((value*10)+tab2[num]);break;
case 5: value=((value*10)+tab2[num]);break;
case 6: value=((value*10)+tab2[num]);break;
case 7: value=((value*10)+tab2[num]);break;
default: value=0;break;
}
write_Command(add[0]);
write_data(shumg[value%10]);
STB1;
write_Command(add[1]);
write_data(shumg[(value/10)%10]);
STB1;
write_Command(add[2]);
write_data(shumg[(value/100)%10]);
STB1;
write_Command(add[3]);
write_data(shumg[(value/1000)%10]);
STB1;
write_Command(add[4]);
write_data(shumg[(value/10000)%10]);
STB1;
write_Command(add[5]);
write_data(shumg[(value/100000)%10]);
STB1;
write_Command(add[6]);
write_data(shumg[(value/1000000)%10]);
STB1;
write_Command(add[7]);
write_data(shumg[value/10000000]);
STB1;
delay_us(1000);
count++;
if(count>=8)
count=0;
}
if(jia==1) //处理加法运算
{
jia=0;
count=0;
sum+=value;
value=0;
work_num=1;
}
if(jian==1) //处理减法运算
{
jian=0;
count=0;
sum+=value;
value=0;
work_num=2;
}
if(cheng==1) //处理乘法运算
{
cheng=0;
count=0;
sum+=value;
value=0;
work_num=3;
}
if(chu==1) //处理除法运算
{
chu=0;
count=0;
sum+=value;
value=0;
work_num=4;
}
if(equal==1) //求出运算结果
{
equal=0;
workdata=1;
switch(work_num)
{
case 1:work_num=0;sum+=value;value=0;break;
case 2:work_num=0;sum-=value;value=0;break;
case 3:work_num=0;sum*=value;value=0;break;
case 4:work_num=0;sum/=value;value=0;break;
default:;break;
}
}
if(workdata==1) //显示处理
{
workdata=0;
if((sum<0)&&(sum>-9999999))
{
sum1=abs(sum);
write_Command(add[0]);
write_data(shumg[sum1%10]);
STB1;
write_Command(add[1]);
write_data(shumg[(sum1/10)%10]);
STB1;
write_Command(add[2]);
write_data(shumg[(sum1/100)%10]);
STB1;
write_Command(add[3]);
write_data(shumg[(sum1/1000)%10]);
STB1;
write_Command(add[4]);
write_data(shumg[(sum1/10000)%10]);
STB1;
write_Command(add[5]);
write_data(shumg[(sum1/100000)%10]);
STB1;
write_Command(add[6]);
write_data(shumg[(sum1/1000000)%10]);
STB1;
write_Command(add[7]);
write_data(0x40);
STB1;
}
if((sum<99999999)&&(sum>=0))
{
write_Command(add[0]);
write_data(shumg[sum%10]);
STB1;
write_Command(add[1]);
write_data(shumg[(sum/10)%10]);
STB1;
write_Command(add[2]);
write_data(shumg[(sum/100)%10]);
STB1;
write_Command(add[3]);
write_data(shumg[(sum/1000)%10]);
STB
评论0