05-21-2020, 09:10 AM
Tutoriel N° 2 : programmation PIC16F727 sous MPLABX compilateur XC8
Gestion des entrées et sorties
Création d'une fonction
Piloter trois LEDs sur le même port
Code :
// etape 1 configuration des registres :
// PIC16F727 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Selection bits (BOR enabled)
#pragma config BORV = 19 // Brown-out Reset Voltage selection bit (Brown-out Reset Voltage (VBOR) set to 1.9 V nominal)
#pragma config PLLEN = ON // INTOSC PLLEN Enable Bit (INTOSC Frequency is 16MHz (32x))
// CONFIG2
#pragma config VCAPEN = DIS // Voltage Regulator Capacitor Enable bits (All VCAP pin functions are disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define sortie 0
#define entree 1
#define ON 0
#define OFF 1
#define LED_D16 PORTCbits.RC0
#define LED_D11 PORTEbits.RE0
#define LED_D12 PORTEbits.RE1
#define LED_D13 PORTEbits.RE2
#define _XTAL_FREQ 16000000 // T = 1/f = 1 / 16 000 000 = 62.5 ns
// declaration des fonctions
void cmd_led( unsigned int valeur_tempo);
void tempo(unsigned int valeur_ms);
// programme principale
void main (void)
{
// la valeur de la féquence de l'osc
// fosc = 16 Mhz T = 1/f T = 1/16000000 = 62.5 ns
OSCCONbits.IRCF1 = 1 ;
OSCCONbits.IRCF0 = 1 ;
// configuration port E :
// les trois branche E2,E1, et E0 sont de type numérique
ANSELEbits.ANSE2 = 0 ;
ANSELEbits.ANSE1 = 0 ;
ANSELEbits.ANSE0 = 0 ;
// les trois branche E2,E1, et E0 sont des sorties
TRISEbits.TRISE2 = 0 ;
TRISEbits.TRISE1 = 0 ;
TRISEbits.TRISE0 = 0 ;
// Mettre le port E OFF
LED_D11 = OFF ; // etat haut --> led RD11 off
LED_D12 = OFF ; // etat haut --> led RD12 off
LED_D13 = OFF ; // etat haut --> led RD13 off
while (1)
{
cmd_led(150);
}
}
// la fonction commande led
void cmd_led( unsigned int valeur_tempo)
{
for (int i = 0 ; i < 3; i ++) // boucle x 3
{
LED_D11 = ON ; // 4 x periode = 250
tempo(valeur_tempo) ;
LED_D12 = ON ;
tempo(valeur_tempo);
LED_D13= ON ;
tempo(valeur_tempo);
}
LED_D11 = OFF ; // etat haut --> led RD11 off
LED_D12 = OFF ; // etat haut --> led RD12 off
LED_D13 = OFF ; // etat haut --> led RD13 off
tempo(valeur_tempo); // tempo 250 ms
}
// la fonction tempo
void tempo(unsigned int valeur_ms)
{
for (int i = 0 ; i < valeur_ms ; i++ )
{
__delay_ms(1);
}
}