MICROCONTROLEUR - C MPLABX - XC8 - CODES SOURCES

Documents pareils
MICROCONTROLEURS PIC PROGRAMMATION EN C. V. Chollet - cours-pic-13b - 09/12/2012 Page 1 sur 44

INITIATION AU LANGAGE C SUR PIC DE MICROSHIP

PIC : COURS ASSEMBLEUR

La programmation des PIC en C. Les fonctions, les interruptions.

Programmation en langage C d un µcontrôleur PIC à l aide du compilateur C-CCS Sommaire

AMICUS 18 (2ème partie) 4) Présentation du logiciel Amicus IDE

Contraintes, particularités. 1. Généralités Gestion de la mémoire a. Type des variables et constantes... 2

Informatique Industrielle

Configuration de TCPIPconfig.h

EPREUVE OPTIONNELLE d INFORMATIQUE CORRIGE

PIC 16F84. Philippe Hoppenot

Introduction au langage C

Flowcode - Mode d'emploi

Réalisation d un réseau de capteurs de température sans fil basé sur le protocole ZigBee

REPUBLIQUE ALGERIENNE DEMOCRATIQUE ET POPULAIRE UNIVERSITE M HAMED BOGARA DE BOUMERDES

Programmation C. Apprendre à développer des programmes simples dans le langage C

PIC EVAL Dev Board PIC18F97J60

Architecture des ordinateurs


Représentation d un entier en base b

Carte ARDUINO UNO Microcontrôleur ATMega328

Conférence sur les microcontroleurs.

Cours d Algorithmique-Programmation 2 e partie (IAP2): programmation 24 octobre 2007impérative 1 / 44 et. structures de données simples

Structure d un programme et Compilation Notions de classe et d objet Syntaxe

Info0101 Intro. à l'algorithmique et à la programmation. Cours 3. Le langage Java

INTRODUCTION A JAVA. Fichier en langage machine Exécutable

Éléments d informatique Cours 3 La programmation structurée en langage C L instruction de contrôle if

UE Programmation Impérative Licence 2ème Année

Introduction à Java. Matthieu Herrb CNRS-LAAS. Mars

DE L ALGORITHME AU PROGRAMME INTRO AU LANGAGE C 51

STS SE. FreeRTOS. Programmation réseau WIFI. Programmation réseau. Socket Tcp. FlyPort smart Wi-Fi module

1. Structure d un programme C. 2. Commentaire: /*..texte */ On utilise aussi le commentaire du C++ qui est valable pour C: 3.

IV- Comment fonctionne un ordinateur?

1/24. I passer d un problème exprimé en français à la réalisation d un. I expressions arithmétiques. I structures de contrôle (tests, boucles)

Traitement de données

as Architecture des Systèmes d Information

La mémoire. Un ordinateur. L'octet. Le bit

Rappels d architecture

PROGRAMMATION D'UN MICROCONTRÔLEUR

2013 Nos nouveaux outils basés sur Arduino

UE C avancé cours 1: introduction et révisions

Initiation. àl algorithmique et à la programmation. en C

Document de formation pour une solution complète d automatisation Totally Integrated Automation (T I A) MODULE A5 Programmation de la CPU 314C-2DP

ARDUINO DOSSIER RESSOURCE POUR LA CLASSE

Mesure de performances. [Architecture des ordinateurs, Hennessy & Patterson, 1996]

La carte à puce. Jean-Philippe Babau

Compilation (INF 564)

SUR MODULE CAMÉRA C38A (OV7620)

Conception de circuits numériques et architecture des ordinateurs

Spécialité Professionnelle. Patrick Boulandet

Logiciels DSPiy. DSPiyStudio logiciels et format des fichiers. incomplet

Cours 7 : Programmation d une chaîne d acquisition

Assembleur. Faculté I&C, André Maurer, Claude Petitpierre

Programmer en JAVA. par Tama

Java Licence Professionnelle CISII,

ARCEAU DE PARKING MOTORISÉ VIGIPARK

Cours d Algorithmique et de Langage C v 3.0

Programmation en langage C

Une carte pour vos projets

Programmation impérative

1 Introduction C+ + Algorithm e. languag. Algorigramm. machine binaire. 1-1 Chaîne de développement. Séance n 4

1. PRESENTATION DU PROJET

Machines virtuelles. Brique ASC. Samuel Tardieu Samuel Tardieu (ENST) Machines virtuelles 1 / 40

Bases de programmation. Cours 5. Structurer les données

Langage C. Patrick Corde. 22 juin Patrick Corde ( Patrick.Corde@idris.fr ) Langage C 22 juin / 289

V- Manipulations de nombres en binaire

Traduction binaire dynamique de l extension SIMD Néon de l ARMv7 dans Qemu

Plan du cours. Historique du langage Nouveautés de Java 7

Etudier l influence de différents paramètres sur un phénomène physique Communiquer et argumenter en utilisant un vocabulaire scientifique adapté

Mode d emploi pour lire des livres numériques

4. Utilisation d un SGBD : le langage SQL. 5. Normalisation

Adressage des données des contrôleurs du WAGO-I/O-SYSTEM 750. Note d application

Centre CPGE TSI - Safi 2010/2011. Algorithmique et programmation :

Exceptions. 1 Entrées/sorties. Objectif. Manipuler les exceptions ;

opti-vm Serveur Vocal et Standard Automatique Siemens HiPath 11xx et Hipath 12xx Installation et Guide Utilisateur Version 1.0

CM2 L architecture MIPS32

Travaux pratiques. Compression en codage de Huffman Organisation d un projet de programmation

Le langage C. Séance n 4

TP Bases de données réparties

Contexte de la réalisation : le projet HomeSIP

Introduction à la Programmation Parallèle: MPI

MICROINFORMATIQUE NOTE D APPLICATION 1 (REV. 2011) ARITHMETIQUE EN ASSEMBLEUR ET EN C

Analyse de sécurité de logiciels système par typage statique

Prototypage électronique

Introduction à l algorithmique et à la programmation M1102 CM n 3

Algorithmique I. Algorithmique I p.1/??

Cours intensif Java. 1er cours: de C à Java. Enrica DUCHI LIAFA, Paris 7. Septembre Enrica.Duchi@liafa.jussieu.fr

Cahier des charges. driver WIFI pour chipset Ralink RT2571W. sur hardware ARM7

Vers du matériel libre

Programmation système I Les entrées/sorties

TP : Gestion d une image au format PGM

Le langage C++ est un langage de programmation puissant, polyvalent, on serait presque tenté de dire universel, massivement utilisé dans l'industrie

Conventions d écriture et outils de mise au point

Introduction à la programmation orientée objet, illustrée par le langage C++ Patrick Cégielski

Séverine Marien Maxime Ringard. 2 ème année GEII. Régulateur de charge pour station de recharge photovoltaïque

Table des matières PRESENTATION DU LANGAGE DS2 ET DE SES APPLICATIONS. Introduction

IRL : Simulation distribuée pour les systèmes embarqués

Brefs rappels sur la pile et le tas (Stack. / Heap) et les pointeurs

Durée estimée :1 journée Date de la réalisation : Description Fournisseur Référence Nombre PU HT LM35CZ, LM35AZ LM35DZ

03/04/2007. Tâche 1 Tâche 2 Tâche 3. Système Unix. Time sharing

Transcription:

Institut Universitaire de Technologie de CRETEIL-VITRY Département de Génie Électrique et Informatique Industrielle MC-II2 - SYSTEMES EMBARQUES AVANCES MPLABX - XC8 - CODES SOURCES Année universitaire 2013-2014 J. VILLEMEJANE - julien.villemejane@u-pec.fr

GEII - MC-II2 TP 1 Programmation en C des PIC Objectifs Comprendre les mécanismes de compilation et de gestion des variables sur un système embarqué Savoir utiliser des bibliothèques de fonctions Savoir créer et utiliser ses propres bibliothèques de fonctions 1. PIC16F690 Le PIC16F690 est un microcontroleur 8 bits, c est à dire qu il traite des données de 8 bits. Ces données (ou variables) sont stockées en RAM. Chaque case de la RAM fait donc 1 octet. Il possède un jeu de 35 instructions qui sont codées chacune sur 14 bits. Ces instructions sont décrites en langage assembleur, le langage le plus proche de la machine. La suite d instructions, appelée programme, est stockée dans une ROM de type Flash. Chaque ligne de cette mémoire programme fait donc 14 bits. 2. Processus de compilation Le langage assembleur n est pas parmi les langages de programmation les plus faciles à comprendre par l homme. Des langages de plus haut niveau, tel que le C, peuvent alors faciliter la tache des programmateurs, par l utilisation d instructions écrites dans un langage plus scientifique (et donc plus proche des habitudes de l être humain). Il est alors nécessaire de traduire le langage de haut niveau vers le langage machine. Cette étape s appelle la compilation. Chaque instruction en langage C, par exemple, sera traduite par un bloc d instructions en assembleur permettant de réaliser le calcul demandé (affectation, test, opérations logiques ou arithmétiques...). Ensuite, le code compilé sera transféré dans le microcontroleur pour être ensuite exécuté par ce dernier : le programme en assembleur dans la ROM / Flash les variables utilisées se verront attribuées une place en RAM

3. Exemple de programme LANGAGE C ASSEMBLEUR MICROCONTROLEUR COMPILATION TRANSFERT ROM (lignes) RAM (octets) char a ; a EQU 0x20 1 int b ; b(1) EQU 0x21 2 b(2) EQU 0x22 double c ; c(1) EQU 0x23 3 c(2) EQU 0x24 c(3) EQU 0x25 a = 2 ; MOVLW 0x2 4 MOVWF _pcstak,f MOVF _pcstak,w MOVWF a b = 300 ; (12C) h MOVLW 0x2C 4 MOVWF b MOVLW 0x1 MOVWF 0x22 c = sin(300) ; CALL 0x0XXX 1800 70 4. Conclusions 4.1. Programmation Langage C = langage proche du langage humain, donc plus simple à écrire Langage assembleur = langage machine, donc plus optimal pour le microcontroleur 4.2. Variables Langage C = type de variables prédéfinies, pouvant s adapter facilement aux types de résultats attendus Langage assembleur = 1 octet par case mémoire, donc gestion des variables "longues" (autre que char) plus difficiles 4.3. Bibliothèques Langage C = gain de temps, car fonction déjà écrite meilleure lisibilité du code réutilisation possible des fonctions Langage assembleur = perte de place en ROM car une fonction écrite en C nécessite un grand nombre de lignes une fois traduite en assembleur fonction non optimisée (lignes inutiles) Il est également possible de réaliser ses propres bibliothèques pour faciliter l écriture et la lecture du code. 4.4. Temporisation - Fonction delay = fonction simple à utiliser en C, mais utilisation d un décompteur, donc fonction qui bloque le processeur Timer = système matériel qui génére une interruption à intervalle régulier, donc n utilise pas le processeur, mais plus difficile à mettre en oeuvre (calcul de la période, mise en place des interruptions...) 3

GEII - MC-II2 TP 2 Constitution d un système embarqué (PIC16F) Objectifs Configurer et utiliser les modules du PIC16F690 en langage C Réaliser des programmes simples pour système embarqué Chercher les informations dans une documentation technique 1. Utilisation des entrées / sorties numériques 1.1. Programme simple - Exercice 2.1 2 3 void main ( void ) { 4 PORTA = 0 ; 5 PORTB = 0 ; 6 PORTC = 0 ; 7 8 TRISA = 0xFF ; / / Tous l e s b i t s en e n t r e e 9 TRISB = 0xFF ; / / Tous l e s b i t s en e n t r e e 10 TRISC = 0 ; / / Tous l e s b i t s en s o r t i e 11 12 while ( 1 ) { 13 PORTC = 0xAA ; / / A f f i c h a g e d une v a l e u r s u r l e s LEDs 14 } 15 } 1.2. Mise en place de fonctions - Exercice 2.1 Fichier biblio.h 2 3 void i n i t P I C ( void ) ; 4 void t o u t A l l u m e r ( void ) ; 5 void t o u t E t e i n d r e ( void ) ; Fichier biblio.c 2 # i n c l u d e " b i b l i o. h " 3 4 void i n i t P I C ( void ) { 5 PORTA = 0 ; 6 PORTB = 0 ; 7 PORTC = 0 ; 8 9 TRISA = 0xFF ; / / Tous l e s b i t s en e n t r e e 10 TRISB = 0xFF ; / / Tous l e s b i t s en e n t r e e 11 TRISC = 0 ; / / Tous l e s b i t s en s o r t i e 12 } 13 void t o u t A l l u m e r ( void ) { 14 PORTC = 0xFF ; / / T o u t e s l e s LEDs a l l u m e e s

15 } 16 void t o u t E t e i n d r e ( void ) { 17 PORTC = 0 ; / / T o u t e s l e s LEDs e t e i n t e s 18 } 2 # i n c l u d e " b i b l i o. h " 3 4 void main ( void ) { 5 i n i t P I C ( ) ; 6 7 while ( 1 ) { 8 t o u t A l l u m e r ( ) ; 9 } 10 11 return ; 12 } 1.3. Clignotement des LED - Exercice 2.1 Les fichiers biblio.h et biblio.c ne changent pas par rapport au paragraphe précédent. 2 # i n c l u d e " b i b l i o. h " 3 4 # d e f i n e _XTAL_FREQ 4 e6 / / f r e q u e n c e d o s c i l l a t i o n 4~MHz 5 6 void main ( void ) { 7 i n i t P I C ( ) ; 8 9 while ( 1 ) { 10 t o u t A l l u m e r ( ) ; 11 delay_ms ( 5 0 0 ) ; 12 t o u t E t e i n d r e ( ) ; 13 delay_ms ( 5 0 0 ) ; 14 } 15 16 return ; 17 } 5

2. Utilisation des entrées analogiques - Exercice 2.2 Fichier biblio.h 2 3 # d e f i n e K1 PORTBbits. RB6 4 5 void i n i t P I C ( void ) ; Fichier biblio.c 2 # i n c l u d e " b i b l i o. h " 3 4 void i n i t P I C ( void ) { 5 PORTA = 0 ; 6 PORTB = 0 ; 7 PORTC = 0 ; 8 9 TRISA = 0xFF ; / / Tous l e s b i t s en e n t r e e 10 TRISB = 0xFF ; / / Tous l e s b i t s en e n t r e e 11 TRISC = 0 ; / / Tous l e s b i t s en s o r t i e 12 13 / / ENTREES ANALOGIQUES 14 ANSELbits. ANS2 = 1 ; / / RA2 e n t r e e a n a l o g 15 ANSELbits. ANS3 = 1 ; / / RA4 e n t r e e a n a l o g 16 / / ADC 17 ADCON1bits.ADCS = 0 b111 ; / / C o n v e r s i o n Clock FRC 18 ADCON0bits. CHS = 0 b0011 ; / / AN3 19 ADCON0bits.ADFM = 0 ; / / L e f t J u s t i f i e d 20 ADCON0bits.VCFG = 0 ; / / R e f e r e n c e VDD 21 ADCON0bits.ADON = 1 ; / / ADC On 22 } 2 # i n c l u d e " b i b l i o. h " 3 4 void main ( void ) { 5 i n i t P I C ( ) ; 6 7 while ( 1 ) { 8 i f ( ADCON0bits.GO == 0){ / / Fin c o n v e r s i o n 9 PORTC = ADRESH; / / L e c t u r e r e s u l t a t e t a f f i c h a g e 10 ADCON0bits.GO = 1 ; / / N o u v e l l e c o n v e r s i o n 11 } 12 i f ( K1 == 1) / / Choix c a n a l 13 ADCON0bits. CHS = 0 b0011 ; / / AN3 14 e l s e 15 ADCON0bits. CHS = 0 b0010 ; / / AN2 16 } 17 18 return ; 19 } 6

3. Utilisation d une sortie modulée (PWM ou MLI) - Exercice 2.3 Fichier biblio.h 2 3 # d e f i n e PERIODE 199 4 5 void i n i t P I C ( void ) ; Fichier biblio.c 2 # i n c l u d e " b i b l i o. h " 3 4 void i n i t P I C ( void ) { 5 PORTA = 0 ; 6 PORTB = 0 ; 7 PORTC = 0 ; 8 9 TRISA = 0xFF ; / / Tous l e s b i t s en e n t r e e 10 TRISB = 0xFF ; / / Tous l e s b i t s en e n t r e e 11 TRISC = 0 ; / / Tous l e s b i t s en s o r t i e 12 13 / / ENTREES ANALOGIQUES 14 ANSELbits. ANS2 = 1 ; / / RA2 e n t r e e a n a l o g 15 ANSELbits. ANS3 = 1 ; / / RA4 e n t r e e a n a l o g 16 / / ADC 17 ADCON1bits.ADCS = 0 b111 ; / / C o n v e r s i o n Clock FRC 18 ADCON0bits. CHS = 0 b0011 ; / / AN3 19 ADCON0bits.ADFM = 0 ; / / L e f t J u s t i f i e d 20 ADCON0bits.VCFG = 0 ; / / R e f e r e n c e VDD 21 ADCON0bits.ADON = 1 ; / / ADC On 22 23 / / PWM 24 PR2 = PERIODE ; / / p e r i o d e PWM = ( PR2 + 1) 4 PREDIV / FOSC 25 / / Avec PREDIV = 1 e t FOSC = 4MHz 26 / / f s = 5kHz 27 CCP1CONbits. CCP1M = 0 b1100 ; / / PWM Mode 28 CCP1CONbits. P1M = 0 b00 ; / / S i n g l e Output 29 CCPR1L = ( PERIODE+1) >> 1 ; / / r a p p o r t c y c l i q u e 1 / 2 30 / / MSB s u r CCPR1L 31 32 / / TIMER 2 33 P I R 1 b i t s. TMR2IF = 0 ; 34 T2CONbits. T2CKPS = 0 b00 ; / / PREDIV = 1 35 T2CONbits. TMR2ON = 1 ; / / TMR2 on 36 } 7

2 # i n c l u d e " b i b l i o. h " 3 4 void main ( void ) { 5 i n i t P I C ( ) ; 6 7 while ( 1 ) { 8 i f ( ADCON0bits.GO == 0){ / / Fin c o n v e r s i o n 9 r e s = ADRESH; / / S t o c k a g e r e s u l t a t 10 CCPR1L = r e s ( PERIODE + 1) / 256; 11 / / Mise en forme pour r a p p o r t c y c l i q u e 12 ADCON0bits.GO = 1 ; / / N o u v e l l e c o n v e r s i o n 13 } 14 } 15 16 return ; 17 } 8

GEII - MC-II2 TP 3 Migration vers les PIC18F Objectifs Découvrir une nouvelle architecture de microcontroleur Utiliser certains modules des PIC18F 1. Utilisation des entrées / sorties numériques - Exercice 3.1 Fichier biblio.h 1 # i n c l u d e < h t c. h> 2 # i n c l u d e <p i c 1 8. h> 3 4 / / SORTIES 5 # d e f i n e LED1 LATCbits. LATC2 6 # d e f i n e LED2 LATAbits. LATA3 7 # d e f i n e LED3 LATBbits. LATB4 8 # d e f i n e LED4 LATBbits. LATB5 9 # d e f i n e LED5 LATBbits. LATB6 10 # d e f i n e LED6 LATBbits. LATB7 11 12 / / ENTREES 13 # d e f i n e BPo1 PORTBbits. RB0 14 # d e f i n e BPo2 PORTBbits. RB1 15 # d e f i n e BPo3 PORTBbits. RB2 16 # d e f i n e SW1 PORTCbits. RC0 17 # d e f i n e SW2 PORTBbits. RB3 18 19 / / CONFIG ENTREES / SORTIES 20 # d e f i n e PORTACONF 0 b00110111 21 # d e f i n e PORTBCONF 0 b00001111 22 # d e f i n e PORTCCONF 0 b10111001 23 24 # d e f i n e _XTAL_FREQ 40 e6 / / O s c i l l a t i o n 40~MHz 25 26 void i n i t P I C ( void ) ; 27 void tempo_1s ( char nb ) ; 28 void a f f i c h a g e ( char c ) ;

Fichier biblio.c 1 # i n c l u d e < h t c. h> 2 # i n c l u d e <p i c 1 8. h> 3 # i n c l u d e " b i b l i o. h " 4 5 void i n i t P I C ( void ) { 6 PORTA = 0 ; 7 PORTB = 0 ; 8 PORTC = 0 ; 9 10 TRISA = PORTACONF; 11 TRISB = PORTBCONF; 12 TRISC = PORTCCONF; 13 14 return ; 15 } 16 17 void tempo_1s ( char nb ) { 18 char i, j ; 19 f o r ( i = 0 ; i < nb ; i ++){ 20 f o r ( j = 0 ; j < 100; j ++){ / / 100 x 10 ms = 1 s 21 delay_ms ( 1 0 ) ; 22 } 23 } 24 return ; 25 } 26 27 void a f f i c h a g e ( char v a l e u r ) { 28 LED1 = ( v a l e u r >> 0) && 0 x01 ; / / B i t 0 de v a l e u r s u r LED1 29 LED2 = ( v a l e u r >> 1) && 0 x01 ; / / B i t 1 de v a l e u r s u r LED2 30 LED3 = ( v a l e u r >> 2) && 0 x01 ; / / B i t 2 de v a l e u r s u r LED3 31 LED4 = ( v a l e u r >> 3) && 0 x01 ; / / B i t 3 de v a l e u r s u r LED4 32 LED5 = ( v a l e u r >> 4) && 0 x01 ; / / B i t 4 de v a l e u r s u r LED5 33 LED6 = ( v a l e u r >> 5) && 0 x01 ; / / B i t 5 de v a l e u r s u r LED6 34 return ; 35 } 10

1 # i n c l u d e < h t c. h> 2 # i n c l u d e <p i c 1 8. h> 3 # i n c l u d e " b i b l i o. h " 4 5 void main ( void ) { 6 i n i t P I C ( ) ; 7 8 while ( 1 ) { 9 LED1 = 1 ; 10 tempo_1s ( 1 ) ; 11 LED1 = 0 ; 12 tempo_1s ( 1 ) ; 13 } 14 15 return ; 16 } 11

2. Utilisation des entrées analogiques - Exercice 3.3 Les fichiers biblio.h et biblio.c ne changent pas. 1 # i n c l u d e < h t c. h> 2 # i n c l u d e <p i c 1 8. h> 3 # i n c l u d e < p l i b / adc. h> 4 # i n c l u d e " b i b l i o. h " 5 6 i n t r e s ; / / R e s u l t a t b r u t de l ADC 7 char r e s c ; / / R e s u l t a t s u r 6 b i t s 8 unsigned char c o n f i g 1, c o n f i g 2 ; / / C o n f i g u r a t i o n ADC 9 10 void main ( void ) { 11 i n i t P I C ( ) ; 12 13 / / ADC C o n f i g u r a t i o n p e u t e t r e mis dans i n i t P I C 14 c o n f i g 1 = ADC_FOSC_RC & ADC_3ANA_0REF & ADC_RIGHT_JUST ; 15 c o n f i g 2 = ADC_CH0 & ADC_INT_OFF ; 16 OpenADC( c o n f i g 1, c o n f i g 2 ) ; 17 18 while ( 1 ) { 19 i f (! BusyADC ( ) ) { / / Fin c o n v e r s i o n 20 r e s = ReadADC ( ) ; / / L e c t u r e r e s u l t a t (10 b i t s ) 21 r e s c = r e s >> 4 ; / / Decalage pour a f f i c h a g e 22 a f f i c h a g e ( r e s c ) ; / / A f f i c h a g e s u r l e s LEDs 23 } 24 } 25 26 return ; 27 } 12

3. Utilisation d un bus de communication série - USART 3.1. Envoi et réception de données via l USART - Exercice 3.4 Les fichiers biblio.h et biblio.c ne changent pas. 1 # i n c l u d e < h t c. h> 2 # i n c l u d e <p i c 1 8. h> 3 # i n c l u d e < p l i b / u s a r t. h> 4 # i n c l u d e " b i b l i o. h " 5 6 # d e f i n e _XTAL_FREQ 40 e6 7 # d e f i n e BAUDRATE 115200 8 9 unsigned char c o n f i g 1, s p b r g ; / / C o n f i g u r a t i o n USART 10 11 void main ( void ) { 12 i n i t P I C ( ) ; 13 14 / / USART C o n f i g u r a t i o n p e u t e t r e mis dans i n i t P I C 15 c o n f i g 1 = USART_TX_INT_OFF & USART_RX_INT_OFF & 16 USART_ASYNCH_MODE & USART_EIGHT_BIT & 17 USART_CONT_RX & USART_BRGH_HIGH & 18 USART_ADDEN_OFF; 19 s p b r g = ( char ) ( _XTAL_FREQ / (16 BAUDRATE) 1 ) ; 20 OpenUSART ( c o n f i g 1, s p b r g ) ; 21 22 putrsusart ( " Bienvenue " ) ; / / Message d a c c u e i l 23 24 while ( 1 ) { 25 i f ( DataRdyUSART ( ) ) { / / Si donnee r e c u e 26 c u s a r t = getcusart ( ) ; / / R e c u p e r a t i o n du c a r a c t e r e 27 putcusart ( c u s a r t ) ; / / Renvoi du c a r a c t e r e 28 } 29 } 30 return ; 31 } 13

3.2. Controle d un système par un PC via RS232 - Exercice 3.5 Les fichiers biblio.h et biblio.c ne changent pas. 1... 2 3 # d e f i n e _XTAL_FREQ 40 e6 4 # d e f i n e BAUDRATE 115200 5 6 unsigned char c o n f i g 1, c o n f i g 2, s p b r g ; 7 i n t r e s ; / / R e s u l t a t ADC 8 char conv ; / / C o n v e r s i o n demandee 9 char c u s a r t ; / / C a r a c t e r e r e c u 10 char ch [ 5 ] ; / / Chaine de c a r a c t e r e r e s u l t a t 11 12 void main ( void ) { 13 i n i t P I C ( ) ; 14 conv = 0 ; 15 16 / / ADC C o n f i g u r a t i o n p e u t e t r e mis dans i n i t P I C 17 c o n f i g 1 = ADC_FOSC_RC & ADC_3ANA_0REF & ADC_RIGHT_JUST ; 18 c o n f i g 2 = ADC_CH0 & ADC_INT_OFF ; 19 OpenADC( c o n f i g 1, c o n f i g 2 ) ; 20 / / USART C o n f i g u r a t i o n p e u t e t r e mis dans i n i t P I C 21 c o n f i g 1 = USART_TX_INT_OFF & USART_RX_INT_OFF & 22 USART_ASYNCH_MODE & USART_EIGHT_BIT & 23 USART_CONT_RX & USART_BRGH_HIGH & 24 USART_ADDEN_OFF; 25 s p b r g = ( char ) ( _XTAL_FREQ / (16 BAUDRATE) 1 ) ; 26 OpenUSART ( c o n f i g 1, s p b r g ) ; 27 28 putrsusart ( " Bienvenue " ) ; / / Message d a c c u e i l 29 30 while ( 1 ) { 31 / / Si donnee r e c u e e t aucune c o n v e r s i o n en c o u r s 32 i f ( DataRdyUSART ( ) && conv == 0){ 33 c u s a r t = getcusart ( ) ; / / R e c u p e r a t i o n du c a r a c t e r e 34 conv = 1 ; 35 s w i t c h ( c u s a r t ) { / / Choix c a n a l 36 c ase 0 : SelChanConvADC (ADC_CH0 ) ; break ; 37 c ase 1 : SelChanConvADC (ADC_CH1 ) ; break ; 38 c ase 2 : SelChanConvADC (ADC_CH2 ) ; break ; 39 d e f a u l t : putrsusart ( "VALEUR NON VALIDE \ r \ n " ) ; conv = 0 ; 40 } 41 } 42 43 i f (! BusyADC ( ) && conv == 1){ / / Si c o n v e r s i o n f i n i e 44 r e s = ReadADC ( ) ; / / L e c t u r e r e s u l t a t 45 i t o a ( ch, r e s, 1 0 ) ; / / C o n v e r s i o n c h a i n e c a r a c t e r e 46 putsusart ( ch ) ; / / Envoi r e s u l t a t 47 conv = 0 ; 48 } 49 } 50 return ; 51 } 14

GEII - MC-II2 TP 4 Gestion du temps / Timers matériel et logiciel Objectifs Mettre en oeuvre des interruptions en langage C sur microcontroleur Faire la différence entre timer matériel et timer logiciel Mettre en oeuvre un timer matériel pour la gestion du temps Mettre en oeuvre un timer logiciel pour la gestion de taches particulières 1. Utilisation du timer matériel TMR0 1.1. Application de base : astable / monostable - Exercice 4.2 Fichier biblio.h 1 # i n c l u d e < h t c. h> 2 # i n c l u d e <p i c 1 8. h> 3 4 / / SORTIES 5 # d e f i n e LED1 LATCbits. LATC2 6 # d e f i n e LED2 LATAbits. LATA3 7 # d e f i n e LED3 LATBbits. LATB4 8 # d e f i n e LED4 LATBbits. LATB5 9 # d e f i n e LED5 LATBbits. LATB6 10 # d e f i n e LED6 LATBbits. LATB7 11 12 / / ENTREES 13 # d e f i n e BPo1 PORTBbits. RB0 14 # d e f i n e BPo2 PORTBbits. RB1 15 # d e f i n e BPo3 PORTBbits. RB2 16 # d e f i n e SW1 PORTCbits. RC0 17 # d e f i n e SW2 PORTBbits. RB3 18 19 / / CONFIG ENTREES / SORTIES 20 # d e f i n e PORTACONF 0 b00110111 21 # d e f i n e PORTBCONF 0 b00001111 22 # d e f i n e PORTCCONF 0 b10111001 23 24 void i n i t P I C ( void ) ; 25 void a f f i c h a g e ( char c ) ;

Fichier biblio.c 1 # i n c l u d e < h t c. h> 2 # i n c l u d e <p i c 1 8. h> 3 # i n c l u d e " b i b l i o. h " 4 5 void i n i t P I C ( void ) { 6 PORTA = 0 ; 7 PORTB = 0 ; 8 PORTC = 0 ; 9 10 TRISA = PORTACONF; 11 TRISB = PORTBCONF; 12 TRISC = PORTCCONF; 13 14 return ; 15 } 16 17 void a f f i c h a g e ( char v a l e u r ) { 18 LED1 = ( v a l e u r >> 0) && 0 x01 ; / / B i t 0 de v a l e u r s u r LED1 19 LED2 = ( v a l e u r >> 1) && 0 x01 ; / / B i t 1 de v a l e u r s u r LED2 20 LED3 = ( v a l e u r >> 2) && 0 x01 ; / / B i t 2 de v a l e u r s u r LED3 21 LED4 = ( v a l e u r >> 3) && 0 x01 ; / / B i t 3 de v a l e u r s u r LED4 22 LED5 = ( v a l e u r >> 4) && 0 x01 ; / / B i t 4 de v a l e u r s u r LED5 23 LED6 = ( v a l e u r >> 5) && 0 x01 ; / / B i t 5 de v a l e u r s u r LED6 24 return ; 25 } 16

1 # i n c l u d e <p i c 1 8. h> 2 # i n c l u d e < h t c. h> 3 # i n c l u d e < p l i b / t i m e r s. h> 4 # i n c l u d e " b i b l i o. h " 5 6 # d e f i n e TAST 10 7 # d e f i n e TMONO 20 8 9 void i n t e r r u p t HighISR ( void ) ; 10 11 char t _ a s t, t_mono ; 12 13 void main ( void ) { 14 i n i t P I C ( ) ; 15 16 / / CONFIGURATION DU TIMER0 17 OpenTimer0 ( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_16 ) ; 18 / / p e r i o d e de 100~ms avec FOSC = 40 MHz 19 INTCONbits. GIE = 1 ; 20 21 while ( 1 ) 22 { 23 i f ( t _ a s t == 0){ / / a s t a b l e 24 t _ a s t = TAST ; 25 LED1 =! LED1 ; / / a c t i o n de l a s t a b l e 26 } 27 28 i f ( t_mono == 0){ / / f i n m o n o s t a b l e 29 LED2 = 0 ; / / a c t i o n de f i n 30 } 31 i f ( BPo1 == 1){ / / d e c l e n c h e m e n t m o n o s t a b l e 32 t_mono = TMONO; 33 LED2 = 1 ; / / a c t i o n de d e b u t 34 } 35 } 36 return ; 37 } 38 39 void i n t e r r u p t HighISR ( void ) / / I n t e r r u p t i o n 40 { 41 i f ( INTCONbits. TMR0IF ) { 42 i f ( t _ a s t!= 0) t _ a s t ; 43 i f ( t_mono!= 0) t_mono ; 44 INTCONbits. TMR0IF = 0 ; 45 } 46 } 17

1.2. Gradateur de LED - Exercice 4.3 Les fichiers biblio.h et biblio.c ne changent pas. 1 # i n c l u d e <p i c 1 8. h> 2 # i n c l u d e < h t c. h> 3 # i n c l u d e < p l i b / t i m e r s. h> 4 # i n c l u d e " b i b l i o. h " 5 6 # d e f i n e PERIODE 255 7 8 void i n t e r r u p t HighISR ( void ) ; 9 10 char t i m e r _ t o t a l, timer_on, resadc ; 11 12 void main ( void ) { 13 i n i t P I C ( ) ; 14 15 / / CONFIGURATION DU TIMER0 p e u t e t r e mis dans i n i t P I C 16 OpenTimer0 ( TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_1 ) ; 17 / / p e r i o d e de 25 us avec FOSC = 40 MHz 18 INTCONbits. GIE = 1 ; 19 / / ADC C o n f i g u r a t i o n p e u t e t r e mis dans i n i t P I C 20 OpenADC( ADC_FOSC_RC & ADC_3ANA_0REF & ADC_RIGHT_JUST, 21 ADC_CH0 & ADC_INT_OFF ) ; 22 23 while ( 1 ) 24 { 25 i f ( t i m e r _ t o t a l == 0){ / / n o u v e l l e p e r i o d e 26 t i m e r _ t o t a l = PERIODE ; / / 255 x 25 us = 6. 3 ms 27 28 t i m e r _ o n = resadc ; / / d e c l e n c h e m e n t m o n o s t a b l e 29 LED1 = 1 ; / / a c t i o n de d e b u t 30 } 31 i f ( t i m e r _ o n == 0){ / / f i n m o n o s t a b l e 32 LED1 = 0 ; / / a c t i o n de f i n 33 } 34 35 / / R e c u p e r a t i o n v a l e u r ADC 36 i f (! BusyADC ( ) ) { / / Fin c o n v e r s i o n 37 resadc = ReadADC ( ) >> 2 ; 38 / / L e c t u r e r e s u l t a t (10 b i t s >> 2 = 8 b i t s ) 39 } 40 } 41 return ; 42 } 43 44 void i n t e r r u p t HighISR ( void ) / / I n t e r r u p t i o n 45 { 46 i f ( INTCONbits. TMR0IF ) { 47 i f ( t i m e r _ t o t a l!= 0) t i m e r _ t o t a l ; 48 i f ( t i m e r _ o n!= 0) timer_on ; 49 INTCONbits. TMR0IF = 0 ; 50 } 51 } 18