PIC18F14K50: Puertos de Entrada y Salida

Page 1

Por: Omar Gurrola

02/18/13

http://www.proprojects.wordpress.com

PIC18F14K50: PUERTOS DE ENTRADA Y SALIDA (I/O PORTS) Este uC cuenta con tres puertos: PORTA, PORTB y PORTC. En general los puerto de entrada o salida son de 8-bit cada uno, pero debido a la limitante de pines del uC no todos los puertos están completos o solo pueden funcionar como entrada o salida. En general todos los puertos cuentan con los siguientes tres registros para su configuración: 1.

TRIS:

Este registro se utiliza para definir la dirección de cada pin (1 = Input/Entrada, 0 = Output/Salida).

2.

PORT:

Se utiliza para leer datos del puerto.

3.

LAT:

Se utiliza para escribir datos al puerto.

Cada puerto en específico puede necesitar realizar ajustes adicionales en base a que funciones comparta, por ejemplo si se comparte con las funciones análogas, se deben desactivar para poderlo utilizar como puerto de entrada.

En el siguiente diagrama se muestran los puertos del uC y su dirección permitida:

1


Por: Omar Gurrola

02/18/13

http://www.proprojects.wordpress.com

Para facilitar el trabajo realice una cabecera llamada pic18f14k50_io.h donde se define como configurar como entrada y salida cada puerto y pin, donde solo basta con llamar OpenInRA3(); para poner en entrada ese pin y OpenOutRA3(); para ponerlo como salida y así para todos los pines restantes. pic18f14k50_io.h /* * Copyright (c) 2011-2013, http://www.proprojects.wordpress.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1.- Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2.- Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /********************************************************************* * By: Omar Gurrola * Site: http://proprojects.wordpress.com * Processor: PIC18 * Compiler: C18 v3.45 * File Name: pic18f14k50_io.h * Description: Defines to use IO for PIC18F14K50 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Rev. Date Comment * 1.0 01/31/13 Initial version (PORTA Only) * 1.1 02/07/13 Re-organize and added PORTB & PORC *********************************************************************/ #ifndef __PIC18F14K50_IO_H #define __PIC18F14K50_IO_H /** INCLUDES *******************************************************/ #include <p18f14k50.h> /** INTERFACE CONFIGURATION ****************************************/ #define PIN 1 // Pin input #define INPUT 0xFF // Port input #define POUT 0 // Pin out #define OUTPUT 0x00 // Port output /** PORTA DEFINES *************************************************/ // GENERICS -------------------------------------------------------// Disable Analog function on RA4, Clear PA, All PA as B #define OpenPA(B) ANSELbits.ANS3 = 0; LATA = 0x00; TRISA = B // Disable Analog function on RA4, Clear RAX, RAX as b #define OpenRA4(b) ANSELbits.ANS3 = 0; LATAbits.LATA4 = 0; TRISAbits.TRISA4 = b // Clear RAX, RAX as b #define OpenRA5(b) LATAbits.LATA5 = 0; TRISAbits.TRISA5 = b // INPUTS ---------------------------------------------------------// IOCAX must be enable to make a RAX pin input work #define OpenInPA() OpenPA(INPUT); IOCAEnable() #define OpenInRA0() IOCA0Enable() #define OpenInRA1() IOCA1Enable() #define OpenInRA3() IOCA3Enable() #define OpenInRA4() OpenRA4(PIN); IOCA4Enable() #define OpenInRA5() OpenRA5(PIN); IOCA5Enable() // OUTPUTS --------------------------------------------------------#define OpenOutPA() OpenPA(OUTPUT) #define OpenOutRA4() OpenRA4(POUT)

2


Por: Omar Gurrola

02/18/13

#define OpenOutRA5() // READ #define #define #define #define #define #define

http://www.proprojects.wordpress.com

OpenRA5(POUT)

-----------------------------------------------------------ReadPA() PORTA ReadRA0() PORTAbits.RA0 ReadRA1() PORTAbits.RA1 ReadRA3() PORTAbits.RA3 ReadRA4() PORTAbits.RA4 ReadRA5() PORTAbits.RA5

// WRITE ----------------------------------------------------------#define WritePA(B) LATA = B #define WriteRA4(b) LATAbits.LATA4 = b #define WriteRA5(b) LATAbits.LATA5 = b // WEAK #define #define #define #define

PULL UP ---------------------------------------------------WPUAEnable() WPUA = 0xFF WPUA3Enable() WPUAbits.WPUA3 = 1 WPUA4Enable() WPUAbits.WPUA4 = 1 WPUA5Enable() WPUAbits.WPUA5 = 1

#define #define #define #define

WPUADisable() WPUA3Disable() WPUA4Disable() WPUA5Disable()

WPUA = 0x00 WPUAbits.WPUA3 = 0 WPUAbits.WPUA4 = 0 WPUAbits.WPUA5 = 0

// INTERRUPT ON CHANGE --------------------------------------------#define IOCAEnable() IOCA = 0xFF #define IOCA0Enable() IOCAbits.IOCA0 = 1 #define IOCA1Enable() IOCAbits.IOCA1 = 1 #define IOCA3Enable() IOCAbits.IOCA3 = 1 #define IOCA4Enable() IOCAbits.IOCA4 = 1 #define IOCA5Enable() IOCAbits.IOCA5 = 1 #define #define #define #define #define #define

IOCADisable() IOCA0Disable() IOCA1Disable() IOCA3Disable() IOCA4Disable() IOCA5Disable()

IOCA = 0x00 IOCAbits.IOCA0 IOCAbits.IOCA1 IOCAbits.IOCA3 IOCAbits.IOCA4 IOCAbits.IOCA5

= = = = =

0 0 0 0 0

/** PORTB DEFINES *************************************************/ // GENERICS -------------------------------------------------------// Disable Analog function on RB4-ANS10 & RB5-ANS-11, Clear PX, All PX as B #define OpenPB(B) ANSELHbits.ANS11 = 0; ANSELHbits.ANS10 = 0; LATB = 0x00; TRISB = B // Diable analog function on RB4-ANS10, Clear PX, PX as b #define OpenRB4(b) ANSELHbits.ANS10 = 0; LATBbits.LAT4 = 0; TRISBbits.TRIS4 = b // Diable analog function on RB5-ANS11, Clear PX, PX as b #define OpenRB5(b) ANSELHbits.ANS11 = 0; LATBbits.LAT5 = 0; TRISBbits.TRIS5 = b // Clear PX, PX as b #define OpenRB6(b) LATBbits.LAT6 = 0; TRISBbits.TRIS6 = b // Clear PX, PX as b #define OpenRB7(b) LATBbits.LAT7 = 0; TRISBbits.TRIS7 = b // INPUTS ---------------------------------------------------------#define OpenInPB() OpenPB(INPUT) #define OpenInRB4() OpenRB4(PIN) #define OpenInRB5() OpenRB5(PIN) #define OpenInRB6() OpenRB6(PIN) #define OpenInRB7() OpenRB7(PIN) // OUTPUTS --------------------------------------------------------#define OpenOutPB() OpenPB(OUTPUT) #define OpenOutRB4() OpenRB4(POUT) #define OpenOutRB5() OpenRB5(POUT) #define OpenOutRB6() OpenRB6(POUT) #define OpenOutRB7() OpenRB7(POUT) // READ #define #define #define #define #define

-----------------------------------------------------------ReadPB() PORTB ReadRB4() PORTBbits.RB4 ReadRB5() PORTBbits.RB5 ReadRB6() PORTBbits.RB6 ReadRB7() PORTBbits.RB7

// WRITE ----------------------------------------------------------#define WritePB(B) LATB = B

3


Por: Omar Gurrola

02/18/13

#define #define #define #define

WriteRB4(b) WriteRB5(b) WriteRB6(b) WriteRB7(b)

LATBbits.LAT4 LATBbits.LAT5 LATBbits.LAT6 LATBbits.LAT7

// WEAK #define #define #define #define #define

PULL UP ---------------------------------------------------WPUBEnable() WPUB = 0xFF WPUB4Enable() WPUBbits.WPUB4 = 1 WPUB5Enable() WPUBbits.WPUB5 = 1 WPUB6Enable() WPUBbits.WPUB6 = 1 WPUB7Enable() WPUBbits.WPUB7 = 1

#define #define #define #define #define

WPUBDisable() WPUB4Disable() WPUB5Disable() WPUB6Disable() WPUB7Disable()

WPUB = 0x00 WPUBbits.WPUB4 WPUBbits.WPUB5 WPUBbits.WPUB6 WPUBbits.WPUB7

= = = =

http://www.proprojects.wordpress.com

b b b b

= = = =

0 0 0 0

// INTERRUPT ON CHANGE --------------------------------------------#define IOCBEnable() IOCB = 0xFF #define IOCB4Enable() IOCBbits.IOCB4 = 1 #define IOCB5Enable() IOCBbits.IOCB5 = 1 #define IOCB6Enable() IOCBbits.IOCB6 = 1 #define IOCB7Enable() IOCBbits.IOCB7 = 1 #define #define #define #define #define

IOCBDisable() IOCB4Disable() IOCB5Disable() IOCB6Disable() IOCB7Disable()

IOCB = 0x00 IOCBbits.IOCB4 IOCBbits.IOCB5 IOCBbits.IOCB6 IOCBbits.IOCB7

= = = =

0 0 0 0

/** PORTC DEFINES *************************************************/ // GENERICS -------------------------------------------------------// Disable Analog function on RC0-ANS4, RC1-ANS5, RC2-ANS6, RC3-ANS7, RC6-ANS8, RC7-ANS9, Clear PX, All PX as B #define OpenPC(B) ANSEL &= 0x0F; ANSELH &=0xF8; LATC = 0x00; TRISC = B // Diable analog function on RC0-ANS4, Clear PX, PX as b #define OpenRC0(b) ANSELbits.ANS4 = 0; LATCbits.LAT0 = 0; TRISCbits.TRIS0 = b // Diable analog function on RC1-ANS5, Clear PX, PX as b #define OpenRC1(b) ANSELbits.ANS5 = 0; LATCbits.LAT1 = 0; TRISCbits.TRIS1 = b // Diable analog function on RC2-ANS6, Clear PX, PX as b #define OpenRC2(b) ANSELbits.ANS6 = 0; LATCbits.LAT2 = 0; TRISCbits.TRIS2 = b // Diable analog function on RC3-ANS7, Clear PX, PX as b #define OpenRC3(b) ANSELbits.ANS7 = 0; LATCbits.LAT3 = 0; TRISCbits.TRIS3 = b // Clear PX, PX as b #define OpenRC4(b) LATCbits.LAT4 = 0; TRISCbits.TRIS4 = b // Clear PX, PX as b #define OpenRC5(b) LATCbits.LAT5 = 0; TRISCbits.TRIS5 = b // Diable analog function on RC6-ANS8, Clear PX, PX as b #define OpenRC6(b) ANSELHbits.ANS8 = 0; LATCbits.LAT6 = 0; TRISCbits.TRIS6 = b // Diable analog function on RC7-ANS9, Clear PX, PX as b #define OpenRC7(b) ANSELHbits.ANS9 = 0; LATCbits.LAT7 = 0; TRISCbits.TRIS7 = b // INPUTS ---------------------------------------------------------#define OpenInPC() OpenPC(INPUT) #define OpenInRC0() OpenRC0(PIN) #define OpenInRC1() OpenRC1(PIN) #define OpenInRC2() OpenRC2(PIN) #define OpenInRC3() OpenRC3(PIN) #define OpenInRC4() OpenRC4(PIN) #define OpenInRC5() OpenRC5(PIN) #define OpenInRC6() OpenRC6(PIN) #define OpenInRC7() OpenRC7(PIN) // OUTPUTS --------------------------------------------------------#define OpenOutPC() OpenPC(OUTPUT) #define OpenOutRC0() OpenRC0(POUT) #define OpenOutRC1() OpenRC1(POUT) #define OpenOutRC2() OpenRC2(POUT) #define OpenOutRC3() OpenRC3(POUT) #define OpenOutRC4() OpenRC4(POUT) #define OpenOutRC5() OpenRC5(POUT) #define OpenOutRC6() OpenRC6(POUT) #define OpenOutRC7() OpenRC7(POUT) // READ -----------------------------------------------------------#define ReadPC() PORTC #define ReadRC0() PORTCbits.RC0

4


Por: Omar Gurrola #define #define #define #define #define #define #define

02/18/13 ReadRC1() ReadRC2() ReadRC3() ReadRC4() ReadRC5() ReadRC6() ReadRC7()

http://www.proprojects.wordpress.com

PORTCbits.RC1 PORTCbits.RC2 PORTCbits.RC3 PORTCbits.RC4 PORTCbits.RC5 PORTCbits.RC6 PORTCbits.RC7

// WRITE ----------------------------------------------------------#define WritePC(B) LATC = B #define WriteRC0(b) LATCbits.LAT0 = b #define WriteRC1(b) LATCbits.LAT1 = b #define WriteRC2(b) LATCbits.LAT2 = b #define WriteRC3(b) LATCbits.LAT3 = b #define WriteRC4(b) LATCbits.LAT4 = b #define WriteRC5(b) LATCbits.LAT5 = b #define WriteRC6(b) LATCbits.LAT6 = b #define WriteRC7(b) LATCbits.LAT7 = b // WEAK PULL UP ---------------------------------------------------// NA // INTERRUPT ON CHANGE --------------------------------------------// NA #endif // __PIC18F14K50_IO_H

1.1. PORTA Este puerto cuenta únicamente con 5 bits PORTA<5:3,1:0> de los cuales:  PORTA<5:4>: Pueden ser entradas o salidas.  PORTA<3,1:0>: Solamente pueden ser entradas. Para configurar PORTA<4> como entrada: 1. ANSELbits.ANS3=0; Se debe desactivar la funciona análoga. 2. LATAbits.LATA4=0; Limpiamos el puerto. 3. TRISAbits.TRISA4=1; Definimos que será un pin de entrada. 4. IOCAbits.IOCA4 = 1; Habilitamos Interrupt On Change (Es un caso especial). Para configurar el PORTA<4> como salida: 1. ANSELbits.ANS3=0; Se debe desactivar la funciona análoga. 2. LATAbits.LATA4=0; Limpiamos el puerto. 3. TRISAbits.TRISA4=0; Definimos que será un pin de salida.

1.1.1. EJEMPLO PORTA Es claro que la mayoría de los pines del PORTA están en uso en la tablilla de desarrollo, razón por la cual se programara sin el BL en los siguientes ejemplos. Se tienen tres botones de entrada y dos LEDs de salida, realizar un dispositivo que pueda realizar las siguientes tres funciones, una con cada botón y se utilice el oscilador interno a 32MHz: 1.

Parpadeen dos LEDs cada segundo.

2.

Parpadeen dos LEDs cada 500ms.

3.

Parpadeen dos LEDs cada 250ms. 5


Por: Omar Gurrola

02/18/13

http://www.proprojects.wordpress.com

Realizar la simulaci贸n con Proteus ISIS y armar el circuito f铆sicamente. main.c /* * * * * * * * * * * * * * * * * * * * * * */

Copyright (c) 2011-2013, http://www.proprojects.wordpress.com All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1.- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2.- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/********************************************************************************** * Autor: Omar Gurrola * Site: http://www.proprojects.wordpress.com * Processor: PIC18 * Compiler: C18 v3.45 * File Name: main.c * Description: Main program * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Rev. Date Comment * 1.0 02/07/13 Initial version *********************************************************************************/ /** INCLUDES *******************************************************/ #include <p18f14k50.h> #include "pic18f14k50_cbits.h" #include "pic18f14k50_io.h" #include "stdvars.h" #include "wait.h" /** PROTOTYPES *****************************************************/ /** VARIABLES ******************************************************/ /** DECLARATIONS ***************************************************/ #pragma code // Forces the code below this line to be put into the code section (Memory Adress >= 0x'REMDIR'02A) /** Interrupt Service Routines (ISR)********************************/ #pragma interrupt HighPriorityISR void HighPriorityISR (void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. } //This return will be a "retfie fast", since this is in a #pragma interrupt section #pragma interruptlow LowPriorityISR void LowPriorityISR (void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. }

//This return will be a "retfie", since this is in a #pragma interruptlow section

#define T1 1000 #define T2 500 #define T3 250 void main(void){

6


Por: Omar Gurrola // Start-up configuration OSCCONbits.IRCF = 0b110; OSCTUNEbits.SPLLEN = 1;

02/18/13

http://www.proprojects.wordpress.com

// Poscaler selected to 8 MHz // PLLx4 Enable

OpenInRA0(); OpenInRA1(); OpenInRA3(); OpenOutRA4(); OpenOutRA5(); while(true){ switch(ReadPA() & 0b00001011){ case 0b00000001: // RA0 on WriteRA4(0); WriteRA5(0); Waitmsx(T1); WriteRA4(1); WriteRA5(1); Waitmsx(T1); break; case 0b00000010: // RA1 on WriteRA4(0); WriteRA5(0); Waitmsx(T2); WriteRA4(1); WriteRA5(1); Waitmsx(T2); break; case 0b00001000: // RA3 on WriteRA4(0); WriteRA5(0); Waitmsx(T3); WriteRA4(1); WriteRA5(1); Waitmsx(T3); break; default: WriteRA4(0); WriteRA5(0); break; } }; } // end main()

7


Por: Omar Gurrola

02/18/13

http://www.proprojects.wordpress.com

1.2. PORTB Este puerto cuenta únicamente con 4 bits PORTB<7:4>, cualquiera de estos puede ser entrada o salida.

1.2.1. EJEMPLO PORTB Realizar un programa que prenda los cuatro LEDs y lo recorra de derecha a izquierda y viceversa, un movimiento cada 250ms. Realizar la simulación con Proteus ISIS y armar el circuito físicamente. main.c /* * * * * * * * * * * * * * * * * * * * * * */

Copyright (c) 2011-2013, http://www.proprojects.wordpress.com All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1.- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2.- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/********************************************************************************** * Autor: Omar Gurrola * Site: http://www.proprojects.wordpress.com * Processor: PIC18 * Compiler: C18 v3.45 * File Name: main.c * Description: Main program * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Rev. Date Comment * 1.0 02/07/13 Initial version *********************************************************************************/ /** INCLUDES *******************************************************/ #include <p18f14k50.h> #include "pic18f14k50_cbits.h" #include "pic18f14k50_io.h" #include "stdvars.h" #include "wait.h" /** PROTOTYPES *****************************************************/ /** VARIABLES ******************************************************/ /** DECLARATIONS ***************************************************/ #pragma code // Forces the code below this line to be put into the code section (Memory Adress >= 0x'REMDIR'02A) /** Interrupt Service Routines (ISR)********************************/ #pragma interrupt HighPriorityISR void HighPriorityISR (void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc.

8


Por: Omar Gurrola

02/18/13

http://www.proprojects.wordpress.com

} //This return will be a "retfie fast", since this is in a #pragma interrupt section #pragma interruptlow LowPriorityISR void LowPriorityISR (void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. }

//This return will be a "retfie", since this is in a #pragma interruptlow section

#define DELAY 250 void main(void){ // Variables u8 C; // Start-up configuration OSCCONbits.IRCF = 0b110; OSCTUNEbits.SPLLEN = 1;

// Poscaler selected to 8 MHz // PLLx4 Enable System FQ = 32 MHz

OpenOutPB(); while(TRUE){ for(C = 0b00010000; C < 0b10000000; C <<= 1){ WritePB(C); Waitmsx(DELAY); } for(C = 0b10000000; C > 0b00010000; C >>= 1){ WritePB(C); Waitmsx(DELAY); } }; } // end main()

9


Por: Omar Gurrola

02/18/13

http://www.proprojects.wordpress.com

1.3. PORTC Este puerto cuenta con 8 bits PORTC<7:0>, cualquiera de estos puede ser entrada o salida.

1.3.1. EJEMPLO PORTC Se tienen cuatro botones de entrada y cuatro LEDs de salida, realizar un dispositivo que pueda realizar las siguientes cuatro funciones, una con cada bot贸n y se utilice el oscilador interno a 32MHz: 1.

Corrimiento de derecha a izquierda de los cuatro LEDs cada 250ms.

2.

Corrimiento de derecha a izquierda y viceversa de los cuatro LEDs cada 250ms.

3.

Prendan los dos primeros LEDs y luego los dos 煤ltimos cada 250ms.

4.

Prendan el LED 0 y 2 y luego el 1 y 3 cada 250ms.

Realizar la simulaci贸n con Proteus ISIS y armar el circuito f铆sicamente. main.c /* * * * * * * * * * * * * * * * * * * * * * */

Copyright (c) 2011-2013, http://www.proprojects.wordpress.com All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1.- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2.- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/********************************************************************************** * Autor: Omar Gurrola * Site: http://www.proprojects.wordpress.com * Processor: PIC18 * Compiler: C18 v3.45 * File Name: main.c * Description: Main program * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Rev. Date Comment * 1.0 02/08/13 Initial version *********************************************************************************/ /** INCLUDES *******************************************************/ #include <p18f14k50.h> #include "pic18f14k50_cbits.h" #include "pic18f14k50_io.h" #include "stdvars.h" #include "wait.h" /** PROTOTYPES *****************************************************/ /** VARIABLES ******************************************************/

10


Por: Omar Gurrola

02/18/13

http://www.proprojects.wordpress.com

/** DECLARATIONS ***************************************************/ #pragma code // Forces the code below this line to be put into the code section (Memory Adress >= 0x'REMDIR'02A) /** Interrupt Service Routines (ISR)********************************/ #pragma interrupt HighPriorityISR void HighPriorityISR (void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. } //This return will be a "retfie fast", since this is in a #pragma interrupt section #pragma interruptlow LowPriorityISR void LowPriorityISR (void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. }

//This return will be a "retfie", since this is in a #pragma interruptlow section

#define DELAY 250 void main(void){ // Variables u8 C; // Start-up configuration OSCCONbits.IRCF = 0b110; OSCTUNEbits.SPLLEN = 1;

// Poscaler selected to 8 MHz // PLLx4 Enable System FQ = 32 MHz

OpenPC(0xF0); // RC4-RC7 Input, RC0-RC3 Output while(TRUE){ switch(ReadPC() & 0xF0){ // Read high nibble only case 0x10: // RC4, Right to left movement for(C = 0x01; C < 0x10; C <<= 1){ WritePC(C); Waitmsx(DELAY); } break; case 0x20: // RC5, Right to left & viceverse movement for(C = 0x01; C < 0x08; C <<= 1){ WritePC(C); Waitmsx(DELAY); } for(C = 0x08; C > 0x01; C >>= 1){ WritePC(C); Waitmsx(DELAY); } break; case 0x40: // RC6, Dual WritePC(0b00000011); Waitmsx(DELAY); WritePC(0b00001100); Waitmsx(DELAY); break; case 0x80: // RC7, Dual interval WritePC(0b00000101); Waitmsx(DELAY); WritePC(0b00001010); Waitmsx(DELAY); break; default: WritePC(0x00); break; } }; } // end main()

11


Por: Omar Gurrola

02/18/13

http://www.proprojects.wordpress.com

12


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.