0
\$\begingroup\$

i'm facing problem regarding the code that i made for my pic16f627a microcontroller. here are the details so that you may understand exactly what i'm trying to achieve. i have set porta as input and portb as output. when ra0 get high it blinks led at rb0,when ra1 get high it blinks led at rb1,when ra2 get high it blinks led at rb2, when ra3 get high it blinks led at rb3 and when ra4 get high it blinks led at rb4 my code is working great ans exactly as i wanted ra0, ra1, ra2, ra3,,,, but but but the problem arises here is when i make the ra4 pin high it doest light up the led at rb4.. i have checked the code and all the pins and connections too but everything is fine .. the problem is only with the pin ra4 rest of the pins are working great (ra0,ra1,ra2,ra3). i dont know where is the problem... can anyone help me out with this problem.. please guide me regarding the issue... im posting the complete original code below that i wrote for my microcontroller...

#include <xc.h> // Configuration word #pragma config FOSC = INTOSCIO // Internal oscillator #pragma config WDTE = ON // Watchdog Timer disabled #define _XTAL_FREQ 4000000 // 4MHz internal oscillator frequency void main (void) { TRISAbits.TRISA0 = 1; // Set RA0 as input TRISAbits.TRISA1 = 1; // Set RA1 as input TRISAbits.TRISA2 = 1; // Set RA2 as input TRISAbits.TRISA3 = 1; // Set RA3 as input TRISAbits.TRISA4 = 1; // Set RA4 as input TRISBbits.TRISB0 = 0; // Set RB0 as output TRISBbits.TRISB1 = 0; // Set RB1 as output TRISBbits.TRISB2 = 0; // Set RB2 as output TRISBbits.TRISB3 = 0; // Set RB3 as output TRISBbits.TRISB4 = 0; // Set RB4 as output PORTBbits.RB0 = 0; // Initialize RB0 to zero PORTBbits.RB1 = 0; // Initialize RB1 to zero PORTBbits.RB2 = 0; // Initialize RB2 to zero PORTBbits.RB3 = 0; // Initialize RB3 to zero PORTBbits.RB4 = 0; // Initialize RB4 to zero PORTA = PORTB; while(1) { if(PORTAbits.RA0 == 1) { // Blink at 100 ms flash rate with 400 ms gap PORTBbits.RB0 = 1; // Toggle RB0 __delay_ms(200); // Flash rate PORTBbits.RB0 = 0; __delay_ms(200); // Gap PORTBbits.RB0 = 1; // Toggle RB0 __delay_ms(200); // Flash rate PORTBbits.RB0 = 0; __delay_ms(200); // Gap } if(PORTAbits.RA1 == 1) { // Blink at 30 ms flash rate with 30 ms gap PORTBbits.RB1 = 1; // Toggle RB0 __delay_ms(200); // Flash rate PORTBbits.RB1 = 0; __delay_ms(200); // Gap PORTBbits.RB1 = 1; // Toggle RB0 __delay_ms(200); // Flash rate PORTBbits.RB1 = 0; __delay_ms(200); // Gap } if(PORTAbits.RA2 == 1) { // Blink at 100 ms flash rate with 500 ms gap PORTBbits.RB2 = 1; // Toggle RB0 __delay_ms(300); // Flash rate PORTBbits.RB2 = 0; __delay_ms(300); // Gap PORTBbits.RB2 = 1; // Toggle RB0 __delay_ms(300); // Flash rate PORTBbits.RB2 = 0; __delay_ms(300); // Gap } if(PORTAbits.RA3 == 1) { // Blink at 100 ms flash rate with 500 ms gap PORTBbits.RB3 = 1; // Toggle RB0 __delay_ms(500); // Flash rate PORTBbits.RB3 = 0; __delay_ms(500); // Gap PORTBbits.RB3 = 1; // Toggle RB0 __delay_ms(500); // Flash rate PORTBbits.RB3 = 0; __delay_ms(500); // Gap } if(PORTAbits.RA4 == 1) { // Blink at 100 ms flash rate with 500 ms gap PORTBbits.RB4 = 1; // Toggle RB0 __delay_ms(500); // Flash rate PORTBbits.RB4 = 0; __delay_ms(500); // Gap PORTBbits.RB4 = 1; // Toggle RB0 __delay_ms(500); // Flash rate PORTBbits.RB4 = 0; __delay_ms(500); // Gap } } } ``` 
\$\endgroup\$
6
  • 2
    \$\begingroup\$ Please don't shout, use proper capitalization. \$\endgroup\$ Commented May 15, 2024 at 15:28
  • \$\begingroup\$ SHOUTING removed. I didn't fix the capitalisation or punctuation in the post. Code formatting fixed. \$\endgroup\$ Commented May 15, 2024 at 15:30
  • \$\begingroup\$ Time to roll up your sleeves and debug. Is RA4 being read correctly?? is RB4 actually working?? Try to blink RB4 when RA1 goes high, and see what happens. \$\endgroup\$ Commented May 15, 2024 at 15:48
  • 1
    \$\begingroup\$ Here you will find the answer electronics.stackexchange.com/questions/644328/… Simply you need to disable low voltage programming mode in CONFIG register. #pragma config LVP = OFF \$\endgroup\$ Commented May 15, 2024 at 15:48
  • \$\begingroup\$ A note in 16F627A data sheet suggests low-voltage-programming has been used. Try programming the other way (high-voltage programming)... The LVP bit is normally erased to ‘1’ which enables the low-voltage programming. In this mode, the RB4/PGM pin is dedicated to the programming function and ceases to be a general purpose I/O pin. \$\endgroup\$ Commented May 15, 2024 at 18:38

1 Answer 1

1
\$\begingroup\$

The comments have addressed the OPs immediate issue.

There are a few other issues that the OP should consider:

  • Only some of the configuration bits are initialized.
  • The watch dog timer is enabled but not reset in the code.
  • The interrupts are not explicitly disabled
  • The analog inputs for the comparators are not explicitly disabled

This code is more complete:

/* * File: main.S * Target: PIC16F627A * Author: dan1138 * Date: 2024-05-14 * Compiler: XC8 v2.45 * IDE: MPLABX v6.00 * * Description: * * Answer for homework see: https://electronics.stackexchange.com/questions/713282/problem-regarding-the-code-that-i-made-for-my-pic16f627a-microcontroller * * PIC16F627A * +----------:_:----------+ * SW2 <> 1 : RA2 RA1 : 18 <> SW1 * SW3 <> 2 : RA3 RA0 : 17 <> SW0 * SW4 <> 3 : RA4(*) OSC1/RA7 : 16 <> * VPP -> 4 : RA5/MCLRn OSC2/RA6 : 15 <> * GND -> 5 : VSS VDD : 14 <- 5v0 * LED0 <> 6 : RB0 PGD/RB7 : 13 <> /PGD * LED1 <> 7 : RB1/RX/DT PGC/RB6 : 12 <> /PGC * LED2 <> 8 : RB2/RX/CK RB5 : 11 <> * LED3 <> 9 : RB3/CCP1 PGM/RB4 : 10 <> LED4 * +-----------------------: * DIP-18 * Notes: * (*) RA4 is an open drain output. Requires an external pull-up for output. * * Created on May 17, 2024, 1:01 PM */ /* * Define the system oscillator frequency this code will setup */ #define _XTAL_FREQ 4000000 // PIC16F627A Configuration Bit Settings // CONFIG #pragma config FOSC = INTOSCIO // 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 = ON // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR) #pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled) #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) #include <xc.h> void main (void) { INTCON = 0; /* disable interrupts */ CMCON = 0x07; /* turn off comparator analog inputs */ TRISAbits.TRISA0 = 1; // Set RA0 as input TRISAbits.TRISA1 = 1; // Set RA1 as input TRISAbits.TRISA2 = 1; // Set RA2 as input TRISAbits.TRISA3 = 1; // Set RA3 as input TRISAbits.TRISA4 = 1; // Set RA4 as input TRISBbits.TRISB0 = 0; // Set RB0 as output TRISBbits.TRISB1 = 0; // Set RB1 as output TRISBbits.TRISB2 = 0; // Set RB2 as output TRISBbits.TRISB3 = 0; // Set RB3 as output TRISBbits.TRISB4 = 0; // Set RB4 as output PORTBbits.RB0 = 0; // Initialize RB0 to zero PORTBbits.RB1 = 0; // Initialize RB1 to zero PORTBbits.RB2 = 0; // Initialize RB2 to zero PORTBbits.RB3 = 0; // Initialize RB3 to zero PORTBbits.RB4 = 0; // Initialize RB4 to zero PORTA = PORTB; while(1) { if(PORTAbits.RA0 == 1) { // Blink at 100 ms flash rate with 400 ms gap PORTBbits.RB0 = 1; // Toggle RB0 __delay_ms(200); // Flash rate PORTBbits.RB0 = 0; __delay_ms(200); // Gap PORTBbits.RB0 = 1; // Toggle RB0 __delay_ms(200); // Flash rate PORTBbits.RB0 = 0; __delay_ms(200); // Gap } if(PORTAbits.RA1 == 1) { // Blink at 30 ms flash rate with 30 ms gap PORTBbits.RB1 = 1; // Toggle RB0 __delay_ms(200); // Flash rate PORTBbits.RB1 = 0; __delay_ms(200); // Gap PORTBbits.RB1 = 1; // Toggle RB0 __delay_ms(200); // Flash rate PORTBbits.RB1 = 0; __delay_ms(200); // Gap } if(PORTAbits.RA2 == 1) { // Blink at 100 ms flash rate with 500 ms gap PORTBbits.RB2 = 1; // Toggle RB0 __delay_ms(300); // Flash rate PORTBbits.RB2 = 0; __delay_ms(300); // Gap PORTBbits.RB2 = 1; // Toggle RB0 __delay_ms(300); // Flash rate PORTBbits.RB2 = 0; __delay_ms(300); // Gap } if(PORTAbits.RA3 == 1) { // Blink at 100 ms flash rate with 500 ms gap PORTBbits.RB3 = 1; // Toggle RB0 __delay_ms(500); // Flash rate PORTBbits.RB3 = 0; __delay_ms(500); // Gap PORTBbits.RB3 = 1; // Toggle RB0 __delay_ms(500); // Flash rate PORTBbits.RB3 = 0; __delay_ms(500); // Gap } if(PORTAbits.RA4 == 1) { // Blink at 100 ms flash rate with 500 ms gap PORTBbits.RB4 = 1; // Toggle RB0 __delay_ms(500); // Flash rate PORTBbits.RB4 = 0; __delay_ms(500); // Gap PORTBbits.RB4 = 1; // Toggle RB0 __delay_ms(500); // Flash rate PORTBbits.RB4 = 0; __delay_ms(500); // Gap } } } 

This homework assignment may be more involved than the OP has described. My suspicion is that the LEDs are supposed to all flash at different rates when each switch is asserted rather than just one at a time. Note that the code as posted does not do this.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.