It looks like you're trying to blink an LED connected to RA2 on a PIC16F1503 microcontroller using the XC8 compiler. Your code is close, but there are a few issues that might prevent it from working as intended. Let's walk through your code and address these issues step by step.
1. Incorrect Comments and Potential Confusion
Firstly, within your code, there are comments that mention PORTB and RB3, but you're actually using PORTA and RA2. This might be a copy-paste error or leftover comments from previous code. It's important that comments accurately reflect what the code is doing to avoid confusion.
Correction:
Update the comments to match your code:
TRISA = 0x00; // Set all PORTA pins as output
PORTA = 0x00; // Initialize PORTA outputs to LOW
2. Missing Oscillator Configuration
You've defined _XTAL_FREQ as 16000000, which sets the oscillator frequency to 16 MHz for delay calculations. However, the default internal oscillator frequency of the PIC16F1503 is 1 MHz. Without configuring the OSCCON register, the microcontroller will not actually run at 16 MHz, and your delays will be off by a factor of 16.
Correction:
Configure the internal oscillator to run at 16 MHz by setting the OSCCON register:
OSCCON = 0x7A; // 16 MHz internal oscillator
Breaking down 0x7A:
IRCF = 111 (bits 6-4): Sets the frequency to 16 MHz
SPLLEN = 1 (bit 7): Enables the PLL for the oscillator (not necessary for 16 MHz internal clock)
SCS = 10 (bits 1-0): Selects the internal oscillator block
However, since the internal oscillator can run at 16 MHz without the PLL, you can also set:
OSCCON = 0x70; // Set internal oscillator to 16 MHz without PLL
3. Disabling Analog Functions on PORTA
On many PIC microcontrollers, the I/O pins default to analog mode on startup, especially for ports that support analog functions. To use RA2 as a digital output, you need to disable its analog function by clearing the appropriate bits in the ANSELA register.
Correction:
Disable analog inputs on PORTA before setting the TRIS and PORT registers:
ANSELA = 0x00; // Set all PORTA pins to digital I/O
4. Setting Up MCLR Pin Correctly
Since you have #pragma config MCLRE = OFF, the MCLR pin is configured as a digital input (RA3). Unconnected input pins can cause unpredictable behavior due to floating voltages. If RA3 is unused, it's good practice to either configure it as an output or enable internal pull-ups.
However, with TRISA = 0x00, all PORTA pins are set as outputs, so RA3 is configured as an output. Ensure that nothing is connected to RA3 that could cause contention when it's set as an output.
5. Finalized Corrected Code
Combining all the corrections, your code should look like this:
// PIC16F1503 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected)
#pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 16000000 // Specify the oscillator frequency (16 MHz)
void main() // The main function
{
OSCCON = 0x70; // Set internal oscillator to 16 MHz
ANSELA = 0x00; // Disable analog inputs on PORTA
TRISA = 0x00; // Set all PORTA pins as output
PORTA = 0x00; // Initialize PORTA outputs to LOW
while(1) // Infinite loop
{
RA2 = 1; // Turn LED ON
__delay_ms(100); // Wait 100 milliseconds
RA2 = 0; // Turn LED OFF
__delay_ms(100); // Wait 100 milliseconds
// Repeat
}
}
6. Additional Suggestions
-
Check Hardware Connections: Ensure that the LED is correctly connected to RA2 (pin 5 on the PIC16F1503). Use a current-limiting resistor (e.g., 220 Ω) in series with the LED to prevent damage.
-
Verify Power Supply Voltage: Make sure your microcontroller is powered with the correct voltage, typically 5V or 3.3V depending on your setup.
-
Pull-up/Pull-down Resistors on Unused Inputs: Although you've set all PORTA pins as outputs, if you later decide to configure any pins as inputs, consider using pull-up or pull-down resistors to prevent floating inputs.
-
Consider Enabling Brown-out Reset and Watchdog Timer: For more robust applications, enabling features like Brown-out Reset (BOR) and Watchdog Timer (WDT) can help recover from unexpected conditions.
-
Use MPLAB X Simulator or Debugger: If you have access to a debugger or simulator, stepping through the code can help identify any runtime issues.
7. Understanding the Corrections
Oscillator Configuration (OSCCON):
IRCF Bits (6-3): Internal Oscillator Frequency Select bits.
1110 or 1111 selects 8 MHz frequency (with or without PLL).
- For 16 MHz, since the internal oscillator is 16 MHz, we set
IRCF to 1110, and the PLL divides it appropriately.
SCS Bits (1-0): System Clock Select bits.
10 selects the internal oscillator block.
Analog Select (ANSELA):
- The
ANSELA register controls the analog functionality on PORTA.
- Setting
ANSELA = 0x00 ensures all pins are configured for digital I/O.
PORT and TRIS Registers:
TRISA Register:
- Setting
TRISA = 0x00 configures all PORTA pins as outputs.
PORTA Register:
- Setting
PORTA = 0x00 initializes all output latches to LOW.
Conclusion
By making the above corrections, your microcontroller should correctly blink the LED connected to RA2 with 100-millisecond intervals. If the LED still doesn't blink:
- Double-check your hardware connections.
- Ensure that the microcontroller is properly programmed.
- Verify that the LED and resistor are functioning by testing them separately.
Feel free to ask if you have any more questions or need further assistance!