Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

UART on PIC16F877A problem

Status
Not open for further replies.

lloydi12345

Member
UART Serial Communication on PIC16F877A problem

Hi, I am new on serial programming using UART and this is my first attempt to program it. I used to program on ASM but now I wanna give it a shot on mikroC. I hope someone can tell me where my mistake is. RD0 - RD3 is supposed to light and RD4 - RD7 is supposed to be off whenever my push button is unpressed. When the push button is pressed, it does the opposite.

mikroC code:
Code:
unsigned int i;

void main() {
            PORTD  = 0;
            TRISB  = 1;
            TRISD  = 0;
            
            UART1_Init(9600);           // initialize USART module
                                         //  (8 bit, 9600 baud rate, no parity bit...)

            if (PORTB = 0x01 ){     //if push button is unpressed
                   UART1_Write(0b00001111);
                   Delay_ms(100);

                   while (1) {
                         if (UART1_Data_Ready()) {
                         i = UART1_Read();                  // read the received data

                         PORTD = i;
                         Delay_ms(100);      //light LEDs on RB0 - RB3
                         }
                   }
             }
             
             else  {                //if push button is pressed
                   UART1_Write(0b11110000);
                   Delay_ms(100);

                   while (1) {
                         if (UART1_Data_Ready()) {
                         i = UART1_Read();                  // read the received data

                         PORTD = i;
                         Delay_ms(100);    //light LEDs on RB4- RB7
                         }
                   }
             }
                   
                   
}

I've attached my schematic below.
 

Attachments

  • Capture.JPG
    Capture.JPG
    117.4 KB · Views: 364
Last edited:
Hi, I am new on serial programming using UART and this is my first attempt to program it. I used to program on ASM but now I wanna give it a shot on mikroC. I hope someone can tell me where my mistake is. RD0 - RD3 is supposed to light and RD4 - RD7 is supposed to be off whenever my push button is unpressed. When the push button is pressed, it does the opposite.

mikroC code:
Code:
unsigned int i;

void main() {
            PORTD  = 0;
            TRISB  = 1;
            TRISD  = 0;
            
            UART1_Init(9600);           // initialize USART module
                                         //  (8 bit, 9600 baud rate, no parity bit...)

           [COLOR=Red] if (PORTB = 0x01 )[/COLOR]{     //if push button is unpressed
                   UART1_Write(0b00001111);
                   Delay_ms(100);

                   while (1) {
                         if (UART1_Data_Ready()) {
                         i = UART1_Read();                  // read the received data

                         PORTD = i;
                         Delay_ms(100);      //light LEDs on RB0 - RB3
                         }
                   }
             }
             
             else  {                //if push button is pressed
                   UART1_Write(0b11110000);
                   Delay_ms(100);

                   while (1) {
                         if (UART1_Data_Ready()) {
                         i = UART1_Read();                  // read the received data

                         PORTD = i;
                         Delay_ms(100);    //light LEDs on RB4- RB7
                         }
                   }
             }
                   
                   
}
I've attached my schematic below.

I don't use mikroC, and I'm not at all familiar with their libraries, but did notice one small thing. Change the part in red to this:
if (PORTB == 0x01 )

With only one = you are making an assignment; you need two == for an equality operator.
This is one of the most common mistakes in C. Have done it myself.:eek:

Hope it helps you.:)

EDIT: You do know that 100 milliseconds is only one tenth of a second, don't you? Try 500, which is a half second, or if the maximum value you can assign delay_ms is 255, then call it twice to get a half second. One tenth of a second will not be enough time for your eyes to catch it.
 
Last edited:
Hi, thank you for the reply, but it seems still not working. I also corrected my mistake. I connected the push button on portb pin0. I also tried changing the delay but still no improvement. :C
 
Last edited:
So does your UART work? You have two or more things going on at once, so there is no way of telling what is working and what is not.
You can simplify your code too; since you have the same thing in your endless loop, you could close your if/else before while(1) and only type it once, instead of twice.

Which endless loop do you end up in? (what is the state of the LEDs?)

Just do one thing at a time, and make sure that works before starting to work on the next thing. Do the next thing by itself and make sure it works, then do them together.

It would be a lot easier to test serial comms using a PC.

Since you know assembly, look at the listing file and see if your UART is actually getting initialized, and even run the thing in MPLab sim to see what is going on.
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top