1
votes

This is a simple program to on/off led in XC8 (Microchip):

1) This code work :

#include <xc.h> 

#define _XTAL_FREQ 4000000 

#pragma config FOSC = HS  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Disable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

 void main()
{
     TRISB0=1;
     TRISB4=0;

    if (RB0==1)
    {
        RB4 = 1;
    }
    else
    {
        RB4 = 0;
    }

} 

Switch Connected to portb RB0

2) This code doesn't work :

#include <xc.h> // Librería XC8

#define _XTAL_FREQ 4000000 // Indicamos a que frecuencia de reloj esta funcionando el micro

// PIC16F648A Configuration Bit Settings
#pragma config FOSC = HS  // Oscillator Selection bits (INTOSC 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)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

 void main()
{
     TRISA0=1;
     TRISB4=0;

    if (RA0==1)
    {
        RB4 = 1;
    }
    else
    {
        RB4 = 0;
    }

}  

Switch Connected to port RA0

Why I can on led If I get port B but not in port A ?

Best Regards.

1
Depending on the exact PIC device, it's common to have analog inputs on PORTA, which need to be disabled to use the pins as digital. So, you'd need something like ANSEL = 0; the exact name of the register will depend on the PIC device.Roger Rowland
Thanks a lot brother. I just to add ANS0=0 (from the library in xc8) and... It work.NIN

1 Answers

0
votes

The ANSEL should be set correctly or the ADC might override it and make it an input as commented on above.

But to set PORT pin outputs you generally use the LATCH register. RB4 is what you read when the pin is configured as a digital input. LATB4 is the output latch.

LATB4 = X;

where X is 1 or 0