0% found this document useful (0 votes)
60 views4 pages

#Include XC.H

This document contains code for a microcontroller project that controls servomotors and displays on an LCD screen. It includes code to initialize the LCD and keypad, read button presses from the keypad, and rotate servomotors at different angles by generating PWM signals on specific pins. The main loop prompts the user for a password on the LCD. If correct, it displays a menu to select controlling either the first or second servo, and then enters a loop to continuously rotate that servo between two positions.

Uploaded by

Anass Okhita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views4 pages

#Include XC.H

This document contains code for a microcontroller project that controls servomotors and displays on an LCD screen. It includes code to initialize the LCD and keypad, read button presses from the keypad, and rotate servomotors at different angles by generating PWM signals on specific pins. The main loop prompts the user for a password on the LCD. If correct, it displays a menu to select controlling either the first or second servo, and then enters a loop to continuously rotate that servo between two positions.

Uploaded by

Anass Okhita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy