⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 Firmware that controls a sliding door via a GSM modem (Open and Close the door)
💻 C
📖 第 1 页 / 共 2 页
字号:
// Firmware Version : v1.0
// Encoding Date    : July 08,2008
// Last revision    : July 08,2008
// Status           : Fully working

char password[6],temp_password[6],buffer,door_stat;
char message_buffer[77],senders_number[13],command_message[6];
short byte_count1,byte_count2;
unsigned int time_count;
const char line_feed = 0x0a;
const char carriage_return = 0x0d;
const char send_message_header[8] = {'A','T','+','C','M','G','S','='};
const char open_door_text[10] = {'D','O','O','R',' ','O','P','E','N',0x1A};
const char close_door_text[12] = {'D','O','O','R',' ','C','L','O','S','E','D',0x1A};
const char already_opened_text[20] = {'D','O','O','R',' ','A','L','R','E','A','D','Y',' ',
                               'O','P','E','N','E','D',0x1A};
const char already_closed_text[20] = {'D','O','O','R',' ','A','L','R','E','A','D','Y',' ',
                               'C','L','O','S','E','D',0x1A};

const char settings [18] = {'A','T','+','C','N','M','I','=',
                         '1',',','2',',','2',',','1',',','0',carriage_return};

#define row1 PORTD.F4
#define row2 PORTD.F7
#define row3 PORTD.F6
#define row4 PORTD.F5
#define column1 PORTC.F4
#define column2 PORTD.F3
#define column3 PORTC.F5
#define step_A PORTA.F0
#define step_B PORTA.F1
#define step_C PORTE.F0
#define step_D PORTE.F1
#define solenoid PORTD.F1
#define inside_switch PORTD.F2
#define close 0
#define open 1


//----------------------------buffer initializations----------------------------
void clear_message_buffers() // this routine will clear the memory locations
     {                       // allocated for the message
     byte_count1 = 0;
     while (byte_count1 < 77)
           {
           message_buffer[byte_count1] = 0;
           byte_count1++;
           }
     }
void clear_code_locations() // this routine will clear the memory locations
     {                      // allocated for the passcodes
     byte_count1 = 0;
     while (byte_count1 < 6)
           {
           password[byte_count1] = 0;
           byte_count1++;
           }
     byte_count1 = 0;
     while (byte_count1 < 6)
           {
           temp_password[byte_count1] = 0xff;
           byte_count1++;
           }
     }
//-------------------------------time delay commands----------------------------
void Delay1s() //ok
     {
     Delay_ms(250);
     Delay_ms(250);
     Delay_ms(250);
     Delay_ms(250);
     }

void Delay250ms() //ok
     {
     Delay_ms(250);
     }
//-----------------------------GSM and transmission commands--------------------
void recieve_message(unsigned short message_length)
     {
     byte_count1 = 0;
     time_count = 0;
     while (byte_count1 < message_length && time_count < 1250)
           {
           if (PIR1.RCIF)
              {
              message_buffer[byte_count1] = Usart_Read();
              byte_count1++;
              time_count = 0;
              }
           time_count++;
           }
     }

void transfer_senders_number()
     {
     //LCD_Cmd(LCD_FIRST_ROW);//
     byte_count1 = 0;
     byte_count2 = 9;
     while (byte_count1 < 13)
           {
           senders_number[byte_count1] = message_buffer[byte_count2];
           //LCD_Chr_Cp (senders_number[byte_count1]);
           byte_count1++;
           byte_count2++;
           }
     }
     
void transfer_sent_password()
     {
     //LCD_Cmd(LCD_SECOND_ROW);//
     byte_count1 = 0;
     byte_count2 = 49;
     while (byte_count1 < 6)
           {
           temp_password[byte_count1] = message_buffer[byte_count2];
           //LCD_Chr_Cp (senders_number[byte_count1]);
           byte_count1++;
           byte_count2++;
           }
     }

void setup_reply_message()
     {
     byte_count1 = 0;
     while (byte_count1 < 8)
           {
           Usart_Write(send_message_header[byte_count1]);
           byte_count1++;
           }
     byte_count1 = 0;
     while (byte_count1 < 13)
           {
           Usart_Write(senders_number[byte_count1]);
           byte_count1++;
           }
     Usart_Write (carriage_return);
     while (buffer != '>') buffer = Usart_Read();
     while (buffer != ' ') buffer = Usart_Read();
     }

void send_open_message()
     {
     byte_count1 = 0;
     while (byte_count1 < 10)
           {
           Usart_Write(open_door_text[byte_count1]);
           byte_count1++;
           }
     Usart_Write (carriage_return);
     }

void send_close_message()
     {
     byte_count1 = 0;
     while (byte_count1 < 12)
           {
           Usart_Write(close_door_text[byte_count1]);
           byte_count1++;
           }
     Usart_Write (carriage_return);
     }

void send_already_open_message()
     {
     byte_count1 = 0;
     while (byte_count1 < 20)
           {
           Usart_Write(already_opened_text[byte_count1]);
           byte_count1++;
           }
     Usart_Write (carriage_return);
     }

void send_already_closed_message()
     {
     byte_count1 = 0;
     while (byte_count1 < 20)
           {
           Usart_Write(already_closed_text[byte_count1]);
           byte_count1++;
           }
     Usart_Write (carriage_return);
     }

void wait_acknowledgement()
     {
     while (buffer != line_feed)
           {
           if (PIR1.RCIF) buffer = Usart_Read();
           }
     while (buffer != carriage_return)
           {
           if (PIR1.RCIF) buffer = Usart_Read();
           }
     Delay250ms();//
     }
     
void gsm_modem_init()
     {
     do {
        byte_count1 = 0;
        TXREG = settings[0];
        while (byte_count1 < 18)
              {
              if (PIR1.TXIF)
                 {
                 byte_count1++;
                 TXREG = settings[byte_count1];
                 }
              }
        time_count = 0;
        while (buffer != 'O' && time_count < 6250)
              {
              if (PIR1.RCIF)
                 {
                 buffer = RCREG;
                 time_count = 0;
                 }
              time_count++;
              }
           }while (buffer != 'O');
     while(buffer != line_feed) buffer = RCREG;
     }

//----------------------------keypad detection commands-------------------------
char read_keypad()//ok
     {
     char data;
     data = 0;
     column1 = 1;
     if (row1)
        {
        Delay250ms();
        data = '1';
        }
     if (row2)
        {
        Delay250ms();
        data = '4';
        }
     if (row3)
        {
        Delay250ms();
        data = '7';
        }
     if (row4)
        {
        Delay250ms();
        data = '*';
        }
     column1 = 0;
     column2 = 1;
     if (row1)
        {
        Delay250ms();
        data = '2';
        }
     if (row2)
        {
        Delay250ms();
        data = '5';
        }
     if (row3)
        {
        Delay250ms();
        data = '8';
        }
     if (row4)
        {
        Delay250ms();
        data = '0';
        }
     column2 = 0;
     column3 = 1;
     if (row1)
        {
        Delay250ms();
        data = '3';
        }
     if (row2)
        {
        Delay250ms();
        data = '6';
        }
     if (row3)
        {
        Delay250ms();
        return '9';
        }
     if (row4)
        {
        Delay250ms();
        data = '#';
        }
     column3 = 0;
     return data;
     }
//-----------------------------comparison commands------------------------------
unsigned short compare_codes() //ok
     {
     if (password[0] != temp_password[0])
        {
        return 0;
        }
     if (password[1] != temp_password[1])
        {
        return 0;
        }
     if (password[2] != temp_password[2])
        {
        return 0;
        }
     if (password[3] != temp_password[3])
        {
        return 0;
        }
     if (password[4] != temp_password[4])
        {
        return 0;
        }
     if (password[5] != temp_password[5])
        {
        return 0;
        }
     return 1;
     }
     
unsigned short compare_with_open_command() //ok
     {
     if (message_buffer[55] != '/')
        {
        return 0;
        }

     if (message_buffer[56] != 'O')
        {
        return 0;
        }
     if (message_buffer[57] != 'P')
        {
        return 0;
        }
     if (message_buffer[58] != 'E')
        {
        return 0;
        }
     if (message_buffer[59] != 'N')
        {
        return 0;
        }
     if (message_buffer[60] != carriage_return)
        {
        return 0;
        }
     if (message_buffer[61] != line_feed)
        {
        return 0;
        }
     return 1;
     }

unsigned short compare_with_close_command() //ok
     {
     if (message_buffer[55] != '/')
        {
        return 0;
        }
     if (message_buffer[56] != 'C')
        {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -