PIC16 to PIC16 UART communication problem

Status
Not open for further replies.

lloydi12345

Member
Hi, I would like to ask again for your help. The problem is the pattern malfunctions. Even when the push button is not yet pushed, the pattern changes alternately from portd=0x0f to 0xf0. When I push the button, the pattern stops even if the pattern is 0x0f or it is 0xf0 it really stops. Can you help me? I'm using pic16f877a

My program should be: When push button is unpressed 2nd pattern - 0xF0 only should show. When push button is pressed 1st pattern - 0x0F only should show. They should be steady. I don't know if I'm having a problem on my C syntax or something else.

Here are my codes and schematic.
Transmitter:
Code:
unsigned int i;


void main() {
          ADCON1 = 6;
          trisb = 0xFF;

          UART1_Init(9600);           // initialize USART module
          while(1){
                   if (portb.f0 = 0){      //pressed
                       UART1_Write(0x0F);    //1st pattern
                       Delay_ms(300);
                   }
                   
                   else (portb.f0 = 1){
                       UART1_Write(0xF0);    //2nd pattern
                       Delay_ms(300);
                   }
          }
}

Receiver:

Code:
unsigned char i;


void check_char(void){
     if ( i == 0x0F ){
        portd = 0x0F;                       //light RD0-RD3
        delay_ms(300);
     }
     
     if ( i == 0xF0 ){
        portd = 0xF0;
        delay_ms(300);                      //light RD4-RD7
     }
}

void main(){
      portd = 0x00;
      adcon1 = 6;
      trisd = 0;

      UART1_Init(9600);

            while(1){
                     if (UART1_Data_Ready() == 1) {
                        i = UART1_Read();
                        check_char();     //light the appropriate LEDs

                     }
            }
}
 

Attachments

  • PICs.JPG
    132.5 KB · Views: 347
You are not passing any data to your subroutine check_char();

You should declare i as global

or

pass data to the subroutine by making the function check_char(i)

or

put the i = UART1_Read(); line inside the subroutine.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…