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

📄 main.cp

📁 Firmware that controls a sliding door via a GSM modem (Open and Close the door)
💻 CP
字号:
#line 1 "F:/Designs/MikroC codes/Text slide/main.c"





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};
#line 40 "F:/Designs/MikroC codes/Text slide/main.c"
void clear_message_buffers()
 {
 byte_count1 = 0;
 while (byte_count1 < 77)
 {
 message_buffer[byte_count1] = 0;
 byte_count1++;
 }
 }
void clear_code_locations()
 {
 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++;
 }
 }

void Delay1s()
 {
 Delay_ms(250);
 Delay_ms(250);
 Delay_ms(250);
 Delay_ms(250);
 }

void Delay250ms()
 {
 Delay_ms(250);
 }

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()
 {

 byte_count1 = 0;
 byte_count2 = 9;
 while (byte_count1 < 13)
 {
 senders_number[byte_count1] = message_buffer[byte_count2];

 byte_count1++;
 byte_count2++;
 }
 }

void transfer_sent_password()
 {

 byte_count1 = 0;
 byte_count2 = 49;
 while (byte_count1 < 6)
 {
 temp_password[byte_count1] = message_buffer[byte_count2];

 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;
 }


char read_keypad()
 {
 char data;
 data = 0;
  PORTC.F4  = 1;
 if ( PORTD.F4 )
 {
 Delay250ms();
 data = '1';
 }
 if ( PORTD.F7 )
 {
 Delay250ms();
 data = '4';
 }
 if ( PORTD.F6 )
 {
 Delay250ms();
 data = '7';
 }
 if ( PORTD.F5 )
 {
 Delay250ms();
 data = '*';
 }
  PORTC.F4  = 0;
  PORTD.F3  = 1;
 if ( PORTD.F4 )
 {
 Delay250ms();
 data = '2';
 }
 if ( PORTD.F7 )
 {
 Delay250ms();
 data = '5';
 }
 if ( PORTD.F6 )
 {
 Delay250ms();
 data = '8';
 }
 if ( PORTD.F5 )
 {
 Delay250ms();
 data = '0';
 }
  PORTD.F3  = 0;
  PORTC.F5  = 1;
 if ( PORTD.F4 )
 {
 Delay250ms();
 data = '3';
 }
 if ( PORTD.F7 )
 {
 Delay250ms();
 data = '6';
 }
 if ( PORTD.F6 )
 {
 Delay250ms();
 return '9';
 }
 if ( PORTD.F5 )
 {
 Delay250ms();
 data = '#';
 }
  PORTC.F5  = 0;
 return data;
 }

unsigned short compare_codes()
 {
 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()
 {
 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()
 {
 if (message_buffer[55] != '/')
 {
 return 0;
 }
 if (message_buffer[56] != 'C')
 {
 return 0;
 }
 if (message_buffer[57] != 'L')
 {
 return 0;
 }
 if (message_buffer[58] != 'O')
 {
 return 0;
 }
 if (message_buffer[59] != 'S')
 {
 return 0;
 }
 if (message_buffer[60] != 'E')
 {
 return 0;
 }
 if (message_buffer[61] != carriage_return)
 {
 return 0;
 }
 if (message_buffer[62] != line_feed)
 {
 return 0;
 }
 return 1;
 }

void get_imbedded_password()
 {
 unsigned short address;
 address = 0xF8;
 byte_count1 = 0;
 while (byte_count1 < 6)
 {
 password[byte_count1] = EEprom_Read(address);
 byte_count1++;
 address++;
 }
 }

void save_password()
 {
 unsigned short address;
 address = 0xF8;
 byte_count1 = 0;
 while (byte_count1 < 6)
 {
 EEprom_Write (address,password[byte_count1]);
 byte_count1++;
 address++;
 }
 }

void offset_message(short row, unsigned short location)
 {
 unsigned column;
 column = 1;
 while (column < 17)
 {
 buffer = EEprom_Read(location);
 LCD_Chr(row,column,buffer);
 location++;
 column++;
 }
 }


void motor_delay()
 {
 Delay_ms(15);
 }

void rotate2()
 {
  PORTA.F0  = 1;
 motor_delay();
  PORTA.F0  = 0;
  PORTA.F1  = 1;
 motor_delay();
  PORTA.F1  = 0;
  PORTE.F0  = 1;
 motor_delay();
  PORTE.F0  = 0;
  PORTE.F1  = 1;
 motor_delay();
  PORTE.F1  = 0;
 }

void rotate1()
 {
  PORTE.F1  = 1;
 motor_delay();
  PORTE.F1  = 0;
  PORTE.F0  = 1;
 motor_delay();
  PORTE.F0  = 0;
  PORTA.F1  = 1;
 motor_delay();
  PORTA.F1  = 0;
  PORTA.F0  = 1;
 motor_delay();
  PORTA.F0  = 0;
 }

void open_door()
 {
 unsigned short step_count;
 offset_message(1,0x40);
  PORTD.F1  = 1;
 delay1s();
 step_count = 25;
 while (step_count > 0)
 {
 rotate1();
 step_count--;

 }
  PORTD.F1  = 0;
 step_count = 150;
 while (step_count > 0)
 {
 rotate1();
 step_count--;
 }
 offset_message(1,0x60);
 Delay1s();
 Delay1s();
 Delay1s();
 }

void close_door()
 {
 unsigned short step_count;
 offset_message(1,0x50);
 step_count = 150;
 while (step_count > 0)
 {
 rotate2();
 step_count--;
 }
  PORTD.F1  = 1;
 step_count = 28;
 while (step_count > 0)
 {
 rotate2();
 step_count--;
 }
  PORTD.F1  = 0;
 offset_message(1,0x70);
 Delay1s();
 Delay1s();
 Delay1s();
 }

void main()
 {
 TRISA = 0;
 TRISB = 0;
 TRISC = 0;
 TRISE = 0;
 TRISD = 0xf4;
 PORTA = 0;
 PORTB = 0;
 PORTC = 0;
 PORTD = 0;
 PORTE = 0;
  PORTD.F1  = 0;
 PIE1.RCIE = 1;
 Delay1s();
 Delay1s();
 Usart_Init(9600);
 LCD_Init(&PORTB);
 LCD_Cmd(LCD_CLEAR);
 LCD_Cmd(LCD_CURSOR_OFF);
 Delay1s();
 Delay1s();
 Delay1s();
 gsm_modem_init();
 offset_message(1,0);
 Delay1s();
 Delay1s();
 Delay1s();
 LCD_Cmd(LCD_CLEAR);
 door_stat =  0  ;
 while (1)
 {
 buffer = read_keypad();
 if (buffer == '*')
 {
 offset_message(1,0x10);
 clear_code_locations();
 byte_count1 = 0;
 LCD_Chr(2,5,' ');
 while (byte_count1 < 6 && buffer != '#')
 {
 buffer = read_keypad();
 if (buffer != '*' && buffer != '#' && buffer != 0)
 {
 temp_password[byte_count1] = buffer;
 LCD_Chr_Cp('*');
 byte_count1++;
 }
 }
 get_imbedded_password();
 buffer = compare_codes();
 if (buffer)
 {
 LCD_Cmd(LCD_CLEAR);
 if (door_stat ==  0 )
 {
 offset_message(1,0x30);
 offset_message(2,0x80);
 }
 if (door_stat ==  1 )
 {
 offset_message(1,0xd0);
 offset_message(2,0x80);
 }
 while (buffer != '#')
 {
 buffer = read_keypad();
 if (buffer == '0' && door_stat ==  0 )
 {
 LCD_Cmd(LCD_CLEAR);
 open_door();
 door_stat =  1 ;
 goto exit1
 }
 if (buffer == '*')
 {
 LCD_Cmd(LCD_CLEAR);
 offset_message(1,0x90);
 byte_count1 = 0;
 LCD_Chr(2,5,' ');
 while (byte_count1 < 6 && buffer != '#')
 {
 buffer = read_keypad();
 if (buffer != '*' && buffer != '#' && buffer != 0)
 {
 password[byte_count1] = buffer;
 LCD_Chr_Cp('*');
 byte_count1++;
 }
 }
 LCD_Cmd(LCD_CLEAR);
 offset_message(1,0xa0);
 byte_count1 = 0;
 LCD_Chr(2,5,' ');
 while (byte_count1 < 6 && buffer != '#')
 {
 buffer = read_keypad();
 if (buffer != '*' && buffer != '#' && buffer != 0)
 {
 temp_password[byte_count1] = buffer;
 LCD_Chr_Cp('*');
 byte_count1++;
 }
 }
 buffer = compare_codes();
 if (buffer)
 {
 LCD_Cmd(LCD_CLEAR);
 offset_message(1,0xb0);
 save_password();
 Delay1s();
 Delay1s();
 goto exit1
 }
 if (!buffer)
 {
 LCD_Cmd(LCD_CLEAR);
 offset_message(1,0xc0);
 Delay1s();
 Delay1s();
 goto exit1
 }
 }
 }
 goto exit1
 }
 if (!buffer)
 {
 offset_message(1,0x20);
 Delay1s();
 Delay1s();
 Delay1s();
 LCD_Cmd(LCD_CLEAR);
 }
 clear_code_locations();
 exit1: LCD_Cmd(LCD_CLEAR);
 }
 if (buffer == '#' && door_stat ==  1 )
 {
 close_door();
 door_stat =  0 ;
 LCD_Cmd(LCD_CLEAR);
 }
 if ( PORTD.F2  && door_stat ==  1 )
 {
 close_door();
 door_stat =  0 ;
 LCD_Cmd(LCD_CLEAR);
 }
 if ( PORTD.F2  && door_stat ==  0 )
 {
 open_door();
 door_stat =  1 ;
 LCD_Cmd(LCD_CLEAR);
 }
 if (PIR1.RCIF)
 {
 clear_message_buffers();
 recieve_message(63);
 if (message_buffer[2] != 'R')
 {
 LCD_Cmd(LCD_CLEAR);
 transfer_senders_number();
 transfer_sent_password();
 get_imbedded_password();
 buffer = compare_codes();
 if (buffer)
 {
 buffer = compare_with_open_command();
 if (buffer)
 {
 if (!door_stat)
 {
 open_door();
 door_stat = 1;
 setup_reply_message();
 send_open_message();
 wait_acknowledgement();
 goto exit2
 }
 if (door_stat)
 {
 setup_reply_message();
 send_already_open_message();
 wait_acknowledgement();
 goto exit2
 }
 }
 buffer = compare_with_close_command();
 if (buffer)
 {
 if (door_stat)
 {
 close_door();
 door_stat = 0;
 setup_reply_message();
 send_close_message();
 wait_acknowledgement();
 goto exit2
 }
 if (!door_stat)
 {
 setup_reply_message();
 send_already_closed_message();
 wait_acknowledgement();
 }
 }
 }
 }
 exit2: LCD_Cmd(LCD_CLEAR);
 }
 }
 }

⌨️ 快捷键说明

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