#include <xc.
h>
#include <pic16f877a.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _XTAL_FREQ 20000000
// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial
Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for
programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM
code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write
protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code
protection off)
//END CONFIG
// Définition des broches pour le LCD
#define RS RC3
#define RW RC4
#define EN RC5
// Définition des broches pour le clavier matriciel
#define C1 RB0
#define C2 RB1
#define C3 RB2
#define R1 RB4
#define R2 RB5
#define R3 RB6
#define R4 RB7
// Définition des broches pour les servomoteurs
#define SERVO1 RC1
#define SERVO2 RC2
#define SERVO_MIN_PULSE_WIDTH 1000
#define SERVO_MAX_PULSE_WIDTH 2000
char key_pressed = 0;
void lcd_data(unsigned char data) {
PORTD = data;
RS = 1;
RW = 0;
EN = 1;
__delay_ms(5);
EN = 0;
}
void lcd_command(unsigned char cmd) {
PORTD = cmd;
RS = 0;
RW = 0;
EN = 1;
__delay_ms(5);
EN = 0;
}
void lcd_string(const char *str, unsigned char num) {
unsigned char i;
for (i = 0; i < num; i++) {
lcd_data(str[i]);
}
}
void lcd_initialise() {
lcd_command(0x38);
lcd_command(0x06);
lcd_command(0x0C);
lcd_command(0x01);
}
void keypad() {
key_pressed = 0;
C1 = 1; C2 = 0; C3 = 0;
if (R1 == 1) { key_pressed = '1'; while (R1 == 1); }
if (R2 == 1) { key_pressed = '4'; while (R2 == 1); }
if (R3 == 1) { key_pressed = '7'; while (R3 == 1); }
if (R4 == 1) { key_pressed = '*'; while (R4 == 1); }
C1 = 0; C2 = 1; C3 = 0;
if (R1 == 1) { key_pressed = '2'; while (R1 == 1); }
if (R2 == 1) { key_pressed = '5'; while (R2 == 1); }
if (R3 == 1) { key_pressed = '8'; while (R3 == 1); }
if (R4 == 1) { key_pressed = '0'; while (R4 == 1); }
C1 = 0; C2 = 0; C3 = 1;
if (R1 == 1) { key_pressed = '3'; while (R1 == 1); }
if (R2 == 1) { key_pressed = '6'; while (R2 == 1); }
if (R3 == 1) { key_pressed = '9'; while (R3 == 1); }
if (R4 == 1) { key_pressed = '#'; while (R4 == 1); }
}
void servoRotate0() //0 Degree
{
unsigned int i;
for(i=0;i<50;i++)
{
RC1 = 1;
__delay_us(800);
RC1 = 0;
__delay_us(19200);
}
}
void servoRotate180() //180 Degree
{
unsigned int i;
for(i=0;i<50;i++)
{
RC1 = 1;
__delay_us(2200);
RC1 = 0;
__delay_us(17800);
}
}
void main(void) {
char motor1_on = 0;
char motor2_on = 0;
TRISC1 = 0; // Configuration de RC1 (servo1) comme sortie PWM
TRISC2 = 0; // Configuration de RC2 (servo2) comme sortie PWM
TRISC = 0x00;
TRISD = 0x00;
TRISB = 0xF0;
lcd_initialise();
lcd_command(0x80);
lcd_string("Entrez le mot", 13);
lcd_command(0xC0);
lcd_string("de passe:", 10);
char password[] = "123456";
char enteredPassword[7];
int i;
while (1) {
for (i = 0; i < 6; i++) {
while (1) {
keypad();
if (key_pressed) {
enteredPassword[i] = key_pressed;
lcd_data('*');
break;
}
}
}
enteredPassword[6] = '\0';
if (strcmp(enteredPassword, password) == 0) {
lcd_command(0x01);
lcd_command(0x80);
lcd_string("Mot de passe vrai", 17);
lcd_command(0xC0);
lcd_string("Choisissez M1/M2:", 17);
bool choice_made = false;
while (!choice_made) {
keypad();
if (key_pressed == '1') {
lcd_command(0x01);
lcd_command(0x80);
lcd_string("Motor 1 active", 15);
do
{
servoRotate180(); //180 Degree
__delay_ms(2000);
servoRotate0(); //0 Degree
}while(1);
motor1_on = 1;
motor2_on = 0;
choice_made = true;
} else if (key_pressed == '2') {
lcd_command(0x01);
lcd_command(0x80);
lcd_string("Motor 2 active", 15);
motor1_on = 0;
motor2_on = 1;
choice_made = true;
}
}
} else {
lcd_command(0x01);
lcd_command(0x80);
lcd_string("Mot de passe faux", 17);
__delay_ms(2000);
lcd_command(0x01);
lcd_command(0x80);
lcd_string("KEYPAD:", 7);
lcd_command(0xC0);
key_pressed = 0;
}
}