#define LED_COM1 P10
#define LED_COM2 P11
#define LED_COM3 P12
#define LED_COM4 P13
#define LED_COM5 P15
#define LED_COM6 P14
#define LED_CONTROL P16
#define SEG1 P24
#define SEG2 P25
#define SEG3 P26
#define SEG4 P27
#define SEG5 P00
#define SEG6 P01
#define SEG7 P02
#define SEG8 P03
#define KEY1_H P31
#define KEY2_H P30
#define KEY3_H P55
#define KEY1_V P54
#define KEY2_V P17
#define BZUER_DRV P23
#define HOTING_CONTROL P22
#define GRING_MOT_CONTROL P35
#define PTC_CONTRL P34
#define DOOR_MOT_CONTROL1 P37
#define DOOR_MOT_CONTROL2 P36
#define DOOR_CLOSE_STATE P20
#define DOOR_OPEN_STATE P21
#define LED_DIGITAL_COM1 1
#define LED_DIGITAL_COM2 2
#define LED_DIGITAL_COM3 3
#define LED_DIGITAL_COM4 4
#define LED_DIGITAL_COM5 5
#define LED_DIGITAL_COM6 6
#define LED_INDICATOR_COM 7
#define NUMBER0 0
#define NUMBER1 1
#define NUMBER2 2
#define NUMBER3 3
#define NUMBER4 4
#define NUMBER5 5
#define NUMBER6 6
#define NUMBER7 7
#define NUMBER8 8
#define NUMBER9 9
#define FIRST_UNIT_NUMBER 0
#define FIRST_DECADE_NUMBER 1
#define FIRST_HUNDREDS_NUMBER 2
#define FIRST_THOUSAND_NUMBER 3
#define SECOND_UNIT_NUMBER 4
#define SECOND_DECADE_NUMBER 5
#define BOILING_COFFEE 1
#define GRIND_COFFEE_BEAN 2
#define POWDER_COFFEE_BOILING 3
#define APPOINTMENT_BOILING 4
#define CHROMA_LIGHT 1
#define CHROMA_MIDDLE 2
#define CHROMA_HIGH 3
#define HIGH 1
#define LOW 0
#define CHROMA_COFFEE_KEY 0X01
#define GRIND_POWDER_KEY 0X02
#define COFFEE_CUPS_KEY 0X03
#define APPOINTMENT_BOILING_KEY 0X04
#define HOURS_ADJ 0X05
#define MINUTE_ADJ 0X06
#define BOILING_COFFEE_KEY 0X07
#define KEY_INVALID 0x00
#define KEY_SCAN_TIMER 5
#define BZUER_DRV_TIME 9
#define TURE 1
#define FALSE 0
//--8---
//| | O SEG3 LED_COM2
//7 6
//|-5---|
//1 4
//| | O SEG3 LED_COM1
//--2---
unsigned char interrupt1ms_flag;
unsigned char led_com_flag;
unsigned char key_scan_flag;
unsigned char clock_minutes,clock_hours,cups;
unsigned char coffee_mode,coffee_chroma;
unsigned char appointment_boiling_coffee_state;
unsigned char grind_powder_state;
unsigned char boiling_coffee_state;
unsigned char press_key_time_counter;
unsigned char filter_shiver_flag;
unsigned char key_data;
unsigned char bzuer_drv_time;
unsigned char bzuer_drv_flag;
unsigned char bzuer_speaking_flag;
unsigned char timer_counterh;
unsigned char timer_counterl;
unsigned char timer_over_flag;
void led_scan(void);
unsigned char display[6];
unsigned char test_flag;
void delay10us(unsigned int time){
unsigned int i,j;
for(j=0;j<time;j++){
for(i=0;i<15;i++){
_nop_();
_nop_();
_nop_();
}
}
}
void delay10us_test(void){
LED_CONTROL=HIGH;
delay10us(1);
LED_CONTROL=LOW;
delay10us(1);
}
void test(void){
if(test_flag){
test_flag=0;
}
else {
test_flag=1;
}
if(test_flag){
LED_CONTROL=HIGH;
}
else{
LED_CONTROL=LOW;
}
}
void number0(void){
SEG1=HIGH;
SEG2=HIGH;
SEG4=HIGH;
SEG6=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void number1(void){
SEG1=HIGH;
SEG7=HIGH;
}
void number2(void){
SEG1=HIGH;
SEG2=HIGH;
SEG5=HIGH;
SEG6=HIGH;
SEG8=HIGH;
}
void number3(void){
SEG2=HIGH;
SEG4=HIGH;
SEG5=HIGH;
SEG6=HIGH;
SEG8=HIGH;
}
void number4(void){
SEG4=HIGH;
SEG6=HIGH;
SEG7=HIGH;
SEG5=HIGH;
}
void number5(void){
SEG2=HIGH;
SEG4=HIGH;
SEG5=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void number6(void){
SEG1=HIGH;
SEG2=HIGH;
SEG4=HIGH;
SEG5=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void number7(void){
SEG4=HIGH;
SEG6=HIGH;
SEG8=HIGH;
}
void number8(void){
SEG1=HIGH;
SEG2=HIGH;
SEG4=HIGH;
SEG5=HIGH;
SEG6=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void number9(void){
SEG2=HIGH;
SEG4=HIGH;
SEG5=HIGH;
SEG6=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void clock_dot(void){
SEG3=HIGH;
}
// -----3-----
// | |
// 4 8
// | |
// |----1----|
// | |
// 6 7
// |____5____|
void second_number0(void){
SEG3=HIGH;
SEG4=HIGH;
SEG5=HIGH;
SEG6=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void second_number1(void){
SEG4=HIGH;
SEG6=HIGH;
}
void second_number2(void){
SEG3=HIGH;
SEG5=HIGH;
SEG6=HIGH;
SEG1=HIGH;
SEG8=HIGH;
}
void second_number3(void){
SEG1=HIGH;
SEG3=HIGH;
SEG5=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void second_number4(void){
SEG1=HIGH;
SEG4=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void second_number5(void){
SEG1=HIGH;
SEG3=HIGH;
SEG4=HIGH;
SEG5=HIGH;
SEG7=HIGH;
}
void second_number6(void){
SEG1=HIGH;
SEG3=HIGH;
SEG4=HIGH;
SEG5=HIGH;
SEG6=HIGH;
SEG7=HIGH;
}
void second_number7(void){
SEG3=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void second_number8(void){
SEG1=HIGH;
SEG3=HIGH;
SEG4=HIGH;
SEG5=HIGH;
SEG6=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void second_number9(void){
SEG1=HIGH;
SEG3=HIGH;
SEG4=HIGH;
SEG5=HIGH;
SEG7=HIGH;
SEG8=HIGH;
}
void clear_led(void){
LED_COM1=HIGH;
LED_COM2=HIGH;
LED_COM3=HIGH;
LED_COM4=HIGH;
LED_COM5=HIGH;
LED_COM6=HIGH;
LED_CONTROL=HIGH;
SEG1=LOW;
SEG2=LOW;
SEG3=LOW;
SEG4=LOW;
SEG5=LOW;
SEG6=LOW;
SEG7=LOW;
SEG8=LOW;
}
void port_initialization(void){
KEY1_H=HIGH;
KEY2_H=HIGH;
KEY3_H=HIGH;
KEY1_V=HIGH;
KEY2_V=HIGH;
BZUER_DRV=LOW;
DOOR_MOT_CONTROL1=LOW;
DOOR_MOT_CONTROL2=LOW;
P0M0=0X0F;
P0M1=0X00;
//P1M0=0X00;
P1M0=0X7f;
P1M1=0X00;
P2M0=0xf8;//0XF0;
P2M1=0X00;
P3M0=0XC0;
P3M1=0X00;
clear_led();
cups=12;
clock_minutes=00;
clock_hours=12;
coffee_chroma=CHROMA_LIGHT;
bzuer_speaking_flag=TURE;
timer_over_flag=0;
timer_counterl=0;
timer_counterh=0;
TMOD=0X00;
AUXR=0X00;
TL0=0X3C;
TH0=0XF6;
TL1=0XE7; //0xe7
TH1=0XFF;
T2L=0XB0; //20MS
T2H=0X3C; //20ms
EA=TURE;
}
unsigned char timer_20ms_flag;
unsigned char timer_5s_flag;
unsigned char timer2_5s_counter;
void timer2_20ms(void) interrupt 12{
IE2=0X00;
AUXR=0X00;
timer_20ms_flag=1;
if(timer2_5s_counter<250){
timer2_5s_counter++;
}
if(timer2_5s_counter){
timer_5s_flag=1;
timer2_5s_counter=0x00;
}
}
unsigned char power_updata_on_flag;
void timer0_1ms (void) interrupt 1{
ET0=FALSE;
TR0=FALSE;
interrupt1ms_flag=TURE;
led_com_flag++;
if(key_scan_flag<KEY_SCAN_TIMER){
key_scan_flag++;
}
if(power_updata_on_flag<3){
if(timer_counterh<2){
if(timer_counterl<0xff){
timer_counterl++;
}
else{
timer_counterh++;
timer_counterl=FALSE;
}
}
else{
if(power_updata_on_flag==0){
power_updata_on_flag=1;
bzuer_speaking_flag=TURE;
}
else if(power_updata_on_flag==1){
power_updata_on_flag=2;
// bzuer_speaking_flag=TURE;
}
else{
power_updata_on_flag=3;
// bzuer_speaking_flag=TURE;
}
timer_counterl=FALSE;
timer_counterh=FALSE;
}
}
}
void timer1_10us(void) interrupt 3{
ET1=FALSE;
TR1=FALSE;
if(bzuer_drv_flag){
if(bzuer_drv_time>0){
bzuer_drv_time--;
}
}
else{
if(bzuer_drv_time<BZUER_DRV_TIME){
bzuer_drv_time++;
}
}
}
unsigned char bzuer_spking_counter1;
unsigned char bzuer_spking_counter2;
void bzuer_spk(void){
ET1=TURE;
TR1=TURE;
if(power_updata_on_flag<3){
if(bzuer_spking_counter1==255){
bzuer_spking_counter1=0;
bzuer_spking_counter2++;
if(bzuer_spking_counter2==8){
bzuer_speaking_flag=0;
bzuer_spking_counter1=0;
bzuer_spking_counter2=0;
ET1=FALSE;
TR1=FALSE;
}
}
}
else{
if(bzuer_spking_counter1==100){
bzuer_speaking_flag=0;
bzuer_spking_counter1=0;
ET1=FALSE;
TR1=FALSE;
}
}
if(bzuer_drv_flag){
if(bzuer_drv_time==0){
BZUER_DRV=LOW;
bzuer_drv_flag=FALSE;
}
}
else{
if(bzuer_drv_time==BZUER_DRV_TIME){
BZUER_DRV=HIGH;
bzuer_drv_flag=TURE;
bzuer_spking_counter1++;
}
}
}
void close_door_coffee(void){
DOOR_MOT_CONTROL1=HIGH;
DOOR_MOT_CONTROL2=LOW;
}
void open_door_coffee(void){
DOOR_MOT_CONTROL1=LOW;
DOOR_MOT_CONTROL2=HIGH;
}
void stop_close_open_door(void){
DOOR_MOT_CONTROL1=LOW;
DOOR_MOT_CONTROL2=LOW;
}
unsigned char error_flag;
void door_check_control(void){
if((!DOOR_CLOSE_STATE)&&(DOOR_OPEN_STATE)){
stop_close_open_door();
}
else if((DOOR_CLOSE_STATE)&&(!DOOR_OPEN_STATE)){
close_door_coffee();
}
else{
if(timer_5s_flag){
IE2=0X00;
AUXR=0X00;
if((!DOOR_CLOSE_STATE)&&(DOOR_OPEN_STATE)){
stop_close_open_door();
}
else{
stop_close_open_door();
error_flag=1;
}
}
else{
close_door_coffee();
IE2=0X04;
AUXR=0X10;
}
}
}
void hex_to_dex_convert(unsigned char dnumber1l,dnumber1h,dnumber2){
unsigned char dl,dh,d2;
dl=dnumber1l;
dh=dnumber1h;
d2=dnumber2;
display[FIRST_UNIT_NUMBER]=dl%10;
display[FIRST_DECADE_NUMBER]=dl/10;
display[FIRST_HUNDREDS_NUMBER]=dh%10;
display[FIRST_THOUSAND_NUMBER]=dh/10;
display[SECOND_UNIT_NUMBER]=d2%10;
display[SECOND_DECADE_NUMBER]=d2/10;
}
void disply_number(unsigned char disbit){
if(display[disbit-1]==NUMBER0){
if(disbit!=4){
number0();
}
return;
}
else if(display[disbit-1]==NUMBER1){
number1();
return;
}
else if(display[disbit-1]==NUMBER2){
number2();
return;
}
else if(display[disbit-1]==NUMBER3){
number3();
return;
}
else if(display[disbit-1]==NUMBER4){
number4();
return;
}
else if(display[disbit-1]==NUMBER5){
number5();
return;
}
else if(display[disbit-1]==NUMBER6){
number6();
return;
}
else if(display[disbit-1]==NUMBER7){
number7();
return;
}
else if(display[disbit-1]==NUMBER8){
number8();
return;
}
else if(display[disbit-1]==NUMBER9){
number9();
return;
}
else{
return;
}
}
void second_disply_number(unsigned char disbit){
if(display[disbit-1]==NUMBER0){
if(disbit!=6){
second_number0();
}
return;
}
else if(display[disbit-1]==NUMBER1){
second_number1();
return;
}
else if(display[disbit-1]==NUMBER2){
second_number2();
return;
}
else if(display[disbit-1]==NUMBER3){
second_number3();
return;
}
else if(display[disbit-1]==NUMBER4){
second_number4();
return;
}
else if(display[disbit-1]==NUMBER5){
second_number5();
return;
}
else if(display[disbit-1]==NUMBER6){
second_number6();
return;
}
else if(display[disbit-1]==NUMBER7){
second_number7();
return;
}
else if(display[disbit-1]==NUMBER8){
second_number8();
return;
}
else if(display[disbit-1]==NUMBER9){
second_number9();
return;
}
else{
return;
}
}
void led_indicator(unsigned char chroma){
unsigned char coffee_chroma;
coffee_chroma=chroma;
if(grind_powder_state){
SEG6=HIGH;
}
else{
SEG7=HIGH;
}
if(appointment_boiling_coffee_state){
SEG2=HIGH;
}
if(boiling_coffee_state){
SEG1=HIGH;
}
switch(coffee_chroma){
case CHROMA_LIGHT:{
SEG5=HIGH;
break;
}
case CHROMA_MIDDLE:{
SEG4=HIGH;
break;
}
case CHROMA_HIGH:{
SEG3=HIGH;
break;
}
default:{
break;
}
}
}
void led_scan(void){
unsigned char led_com_control;
hex_to_dex_convert(clock_minutes,clock_hours,cups);
if(interrupt1ms_flag){
interrupt1ms_flag=FALSE;
led_com_control=led_com_flag;
clear_led();
switch(led_com_control){
case LED_DIGITAL_COM1:{
disply_number(led_com_control);
clock_dot();
LED_COM1=LOW;
break;
}
case LED_DIGITAL_COM2:{
disply_number(led_com_control);
clock_dot();
LED_COM2=LOW;
break;
}
case LED_DIGITAL_COM3:{
disply_number(led_com_control);
LED_COM3=LOW;
break;
}
case LED_DIGITAL_COM4:{
disply_number(led_com_control);
LED_COM4=LOW;
break;
}
case LED_DIGITAL_COM5:{
second_disply_number(led_com_control);
LED_COM5=LOW;
break;
}
case LED_DIGITAL_COM6:{
second_disply_number(led_com_control);
LED_COM6=LOW;
break;
}
case LED_INDICATOR_COM:{
led_indicator(coffee_chroma);
LED_CONTROL=LOW;
led_com_flag=0;
break;
}
default:{
led_com_flag=0;
break;
}
}
}
}
unsigned char key_filter(void){
unsigned char key_ok_flag=0;
if(filter_shiver_flag<2){
filter_shiver_flag++;
}
else{
if(press_key_time_counter<60){
press_key_time_counter++;
}
else{
press_key_time_counter=0;
key_ok_flag=TRUE;
filter_shiver_flag=FALSE;
}
}
return key_ok_flag;
}
void key_scan(void){
KEY1_V=HIGH;
KEY2_V=HIGH;
delay10us(1);
if(!KEY3_H){
if(key_filter()){
key_data=BOILING_COFFEE_KEY;
bzuer_speaking_flag=TURE;
}
return;
}
KEY1_V=HIGH;
KEY2_V=LOW;
delay10us(1);
if(!KEY1_H){
if(key_filter()){
key_data=MINUTE_ADJ;
bzuer_speaking_flag=TURE;
}
return;
}
else if(!KEY2_H){
if(key_filter()){
key_data=HOURS_ADJ;
bzuer_speaking_flag=TURE;
}
return;
}
else if(!KEY3_H){
if(key_filter()){
key_data=APPOINTMENT_BOILING_KEY;
bzuer_speaking_flag=TURE;
}
return;
}
else {
}
KEY1_V=LOW;
KEY2_V=HIGH;
delay10us(1);
if(!KEY1_H){
if(key_filter()){
key_data=COFFEE_CUPS_KEY;
bzuer_speaking_flag=TURE;
}
return;
}
else if(!KEY2_H){
if(key_filter()){
key_data=GRIND_POWDER_KEY;
bzuer_speaking_flag=TURE;
}
return;
}
else if(!KEY3_H){
if(key_filter()){
key_data=CHROMA_COFFEE_KEY;
bzuer_speaking_flag=TURE;
}
return;
}
else {
}
}
void key_function_manage(void){
key_scan();
switch(key_data){
case MINUTE_ADJ:{
if(appointment_boiling_coffee_state){
if(clock_minutes<59){
clock_minutes++;
}
else{
clock_minutes=0;
}
}
break;
}
case HOURS_ADJ:{
if(appointment_boiling_coffee_state){
if(clock_hours<23){
clock_hours++;
}
else{
clock_hours=0;
}
}
break;
}
case APPOINTMENT_BOILING_KEY:{
if(boiling_coffee_state==FALSE){
if(appointment_boiling_coffee_state){
appointment_boiling_coffee_state=FALSE;
}
else{
appointment_boiling_coffee_state=TRUE;
}
}
break;
}
case COFFEE_CUPS_KEY:{
if(boiling_coffee_state==FALSE){
if(cups<12){
cups=cups+2;
}
else{
cups=2;
}
}
break;
}
case GRIND_POWDER_KEY:{
if(boiling_coffee_state==FALSE){
if(grind_powder_state){
grind_powder_state=FALSE;
}
else{
grind_powder_state=TRUE;
}
}
break;
}
case CHROMA_COFFEE_KEY:{
if(boiling_coffee_state==FALSE){
if(coffee_chroma==CHROMA_LIGHT){
coffee_chroma=CHROMA_MIDDLE;
}
else if(coffee_chroma==CHROMA_MIDDLE){
coffee_chroma=CHROMA_HIGH;
}
else{
coffee_chroma=CHROMA_LIGHT;
}
}
break;
}
case BOILING_COFFEE_KEY:{
if(boiling_coffee_state==FALSE){
boiling_coffee_state=TRUE;
if(appointment_boiling_coffee_state){
appointment_boiling_coffee_state=FALSE;
clock_minutes=00;
clock_hours=12;
}
}
break;
}
default:{
key_data=KEY_INVALID;
break;
}
key_data=KEY_INVALID;
}
key_data=KEY_INVALID;
}
main(void){
port_initialization();
while(1){
led_scan();
if(key_scan_flag==KEY_SCAN_TIMER){
key_scan_flag=0;
key_function_manage();
}
if(bzuer_speaking_flag){
bzuer_spk();
}
ET0=TURE;
TR0=TURE;
}
}