Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Problem on UART PIC16F628A Tx - Rx

Status
Not open for further replies.

sacban

Junior Member level 3
Joined
Aug 20, 2015
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
412
All Members Active,
I have problem to add more button, can anyone help me to give some guidance to solve this issue.

Below are my code :

रिसीवर
HTML:
 char txt,txt1;
 char hrd,hrd1;
 char hmd,hmd1;
 
void main() {

     TRISB.F0=0;
     TRISB.F3=0;
     TRISB.F4=0;
     PORTB=0x00;
     
     UART1_Init(9600);
     delay_ms(120);

     while(1){
              if(UART1_Data_Ready() == 1){
              hrd = UART1_Read();
                      if(hrd == 'Z'){
                      delay_ms(100);
                      hrd1 = UART1_Read();
                                 if(hrd1 == 'X'){
                                    delay_ms(100);
                                    txt = UART1_Read();
                                    if(txt == 'A'){
                                    PORTB.F0=~PORTB.F0;
                                    }else if(txt == 'A'){
                                    PORTB.F0=~PORTB.F0;
                                     }
                                    }

            //***************************************************//

                    if(UART1_Data_Ready() == 1){
                      hmd = UART1_Read();
                      if(hmd == 'Z'){
                      delay_ms(100);
                      hmd1 = UART1_Read();
                                 if(hmd1 == 'X'){
                                             delay_ms(100);
                                             txt1 = UART1_Read();
                                             if(txt1 == 'A'){
                                             PORTB.F4=~PORTB.F4;
                                             }else if(txt1 == 'A'){
                                             PORTB.F4=~PORTB.F4;
                                             }else{

                                             }
                                  }
                       }
               }
        }
   }
  }
  }



ट्रांसमीटर
HTML:
int asu,asu1;
 
void main() {
     asu =0;
     asu1=0;


     TRISA.F0=1;  // RA0 As Input
     TRISA.F1=1;  // RA1 As Input

     TRISB = 0x00; // Set To Output
     PORTB=0x00;   //  Clear
     
     UART1_Init(1200);
     delay_ms(100);


     while(1){
         if(PORTA.F0 == 1){
         asu = 1;
         }
         if(asu == 1 && PORTA.F0 == 0){
         asu = 0;
                     if(UART1_Tx_Idle() == 1){
                     UART1_Write('Z');
                     UART1_Write('X');
                     UART1_Write('A');
                     }
         }

         if(PORTA.F1 == 1){
         asu1=1;
         }
         if(asu1 == 1 && PORTA.F1 == 0){
         asu1=0;
                     if(UART1_Tx_Idle() == 1){
                     UART1_Write('Z');
                     UART1_Write('X');
                     UART1_Write('A');
                     }
                }

             }
         }

:bsdetector::bsdetector::bsdetector:
Sacban.
 

Hi,

What is the problem? Give an error description.

Klaus
 

Hi,

What is the problem? Give an error description.

Klaus

Hi Klaus,
There is no an error (except I place UART2_Read), but I can't adding more Button on PORTA.F1 (Tx) and Led PORTB.F4 (Rx), I trying to write for hmd1 = UART2_Read(); but compiler is getting an error.

How to write similiar UART1_Read, so I can PORT it to Tx and Rx (PORTA.FI & PORTB.F4).

I want to remote from PIC16F628A to another PIC16F628A, with 2 Button and 2 Led.


:bsdetector::bsdetector::bsdetector:
Sacban
 

Try this.

Code:
TRISB = 0x02;

RX pin has to be set as input in receiver. If it is a Transreceiver then you have to set RX pin as input in both Transreceivers.
 

Why the delays between reading characters from the USART?
You send the characters one immediately after the other but between reading them you have a 100mS delay.

Brian.
 

baileychic & betwixt :
TRISB= 0x02; and Delay, still doesn't works.

Transmitter :
Code:
  =>  if(PORTA.F1 == 1){ // Toggle RA1


Receiver :
Code:
// Read RB0 not RB4.
  =>  if(UART1_Data_Ready() == 1){
                      hmd = UART1_Read();
                      if(hmd == 'Z'){
                      delay_ms(100);
                      hmd1 = UART1_Read();
                                 if(hmd1 == 'X'){
                                             delay_ms(100);
                                             txt1 = UART1_Read();
                                             if(txt1 == 'A'){
                                             PORTB.F4=~PORTB.F4;
                                             }else if(txt1 == 'A'){
                                             PORTB.F4=~PORTB.F4;

Any else ?, Please find attachment file, then you can test.

Code:
 char txt,txt1;
 char hrd,hrd1;
 char hmd,hmd1;

void main() {

     TRISB.F0=0;
     TRISB.F3=0;
     TRISB.F4=0;
     TRISB.F1=1;
     PORTB=0x00;

     UART1_Init(9600);
     delay_ms(120);

     while(1){
              if(UART1_Data_Ready() == 1){
              hrd = UART1_Read();
                      if(hrd == 'Z'){
                      delay_ms(120);
                      hrd1 = UART1_Read();
                                 if(hrd1 == 'X'){
                                    delay_ms(120);
                                    txt = UART1_Read();
                                    if(txt == 'A'){
                                    PORTB.F0=~PORTB.F0;
                                    }else if(txt == 'A'){
                                    PORTB.F0=~PORTB.F0;
                                     }
                                    }

            //***************************************************//

                    if(UART1_Data_Ready() == 1){
                      hmd = UART1_Read();
                      if(hmd == 'Z'){
                      delay_ms(120);
                      hmd1 = UART1_Read();
                                 if(hmd1 == 'X'){
                                             delay_ms(120);
                                             txt1 = UART1_Read();
                                             if(txt1 == 'A'){
                                             PORTB.F4=~PORTB.F4;
                                             }else if(txt1 == 'A'){
                                             PORTB.F4=~PORTB.F4;
                                             }else{

                                             }
                                  }
                       }
               }
        }
   }
  }
  }

Code:
 int asu,asu1;

void main() {
     asu =0;
     asu1=0;


     TRISA.F0=1;  // RA0 As Input
     TRISA.F1=1;  // RA1 As Input

     TRISB = 0x00; // Set To Output
     PORTB=0x00;   //  Clear

     UART1_Init(9600);
     Delay_ms(120);


     while(1){
         if(PORTA.F0 == 1){
         asu = 1;
         }
         if(asu == 1 && PORTA.F0 == 0){
         asu = 0;
                     if(UART1_Tx_Idle() == 1){
                     UART1_Write('Z');
                     UART1_Write('X');
                     UART1_Write('A');
                     }
         }

         if(PORTA.F1 == 1){
         asu1=1;
         }
         if(asu1 == 1 && PORTA.F1 == 0){
         asu1=0;
                     if(UART1_Tx_Idle() == 1){
                     UART1_Write('Z');
                     UART1_Write('X');
                     UART1_Write('A');
                     }
                }

             }
         }


Sacban
 
Last edited by a moderator:

At what speed PORTA.B0 and PORTA.B1 toggles ? Are they connected to buttons ? If yes, you should implement debounce for the buttons.

What problem ? What error ? demo limit error ? If yes, buy a licence for the Compiler.
 

I'm trying to understand what the code is supposed to do, and failing miserably.
It seems to be waiting for a character to be received then storing it and waiting for a while then reading again, whether or not anything is there and then repeating it over again.

Are you trying to see if 'ZXA' has been received then do something? If so, you need to check for the character arriving, not just wait for a while, especially when the delay you have used is likely to miss the next character altogether. Consider that at 9600 Bauds you are sending 960 characters per second so each one only takes just over 1mS so when you delay reading the UART for 100mS both the following characters will have been lost.

Brian.
 

baileychic :
PORTA.F0 & PORTA.F1 is an input, is not about debounce or speed, ofcourse their connected to button.
The code only work for PORTA.F0, but not for PORTA.F1, if they same like this (for PORTA.F1)
Code:
 //if(UART1_Tx_Idle() == 1){
UART1_Write('Z');
UART1_Write('X');
UART1_Write('A');

PORTB.F0 will high but not for PORTB.F4 (receiver side).

betwixt :
You are right, if I write for


Code:
if(PORTA.F1 == 1){
asu1=1;
}
if(asu1 == 1 && PORTA.F1 == 0){
asu1=0;
if(UART1_Tx_Idle() == 1){
UART1_Write('Z');
UART1_Write('X');
UART1_Write('A');

PORTB.F0 will high, not for PORTB.F4, if I change UART1_Write('Z');
Code:
UART1_Write('X');
UART1_Write('A');
to other word (example STU) then it doesn't work, for PORTA.F1.

but is not for delay, delay is fine, main problem is communication (UART1).

I think if I made UART2_Write, from transmitter then for receiver UART2_Data_Ready I am sure it will work, but the problem, if I write UART2, mikroC doesn't except it (errors).
My question is, how to write similar UART1 for next button.

Guys, try attached files.View attachment edaboard_mcforum.7z

:bsdetector::bsdetector::bsdetector:
Sacban.
 
Last edited by a moderator:

Well, the simple answer to why UART2 doesn't compile is because it doesn't exist in the 16F628A.

I still can't understand what you are trying to do with this code. You don't seem to be using the TX or RX code correctly, especially the RX side. When you say "next button" what exactly do you mean, are you adding an extra switch or sending extra characters or both? If it's a simple switch it almost certainly needs debouncing. Perhaps you can show a flow chart.

I'm coding a serial application for someone using a 16F628A right now and it runs at 38400 Bauds, sends and receives a block of characters, decodes the received block and then runs one of several tasks depending on the block contents. It doesn't seem much different to what you are trying to do. My serial routines are working fine but I don't use MikroC as the compiler so my code won't be compatible

Brian.
 

betwixt & baileychic :
This issue has been done.

I write
Code:
if(asu1 == 1 && PORTA.F1 == 0){
asu1=0;
if(UART1_Tx_Idle() == 1){
UART1_Write('Z');
UART1_Write('X');
UART1_Write('A');

and next toggle I write
Code:
if(asu1 == 1 && PORTA.F1 == 0){
asu1=0;
if(UART1_Tx_Idle() == 1){
UART1_Write('Z');
UART1_Write('X');
UART1_Write('B');
and next
Code:
if(asu1 == 1 && PORTA.F1 == 0){
asu1=0;
if(UART1_Tx_Idle() == 1){
UART1_Write('Z');
UART1_Write('X');
UART1_Write('C');
and next
Code:
if(asu1 == 1 && PORTA.F1 == 0){
asu1=0;
if(UART1_Tx_Idle() == 1){
UART1_Write('Z');
UART1_Write('X');
UART1_Write('D');
and next
Code:
if(asu1 == 1 && PORTA.F1 == 0){
asu1=0;
if(UART1_Tx_Idle() == 1){
UART1_Write('Z');
UART1_Write('X');
UART1_Write('E');

you just changing next word for the UART1_Write, you can't adding UART2, except you need to declare them with SUart1, SUart2, SUart3, SUart4 and xxx.

Anyway until today still not test on the hardware, hope will works too on the hardware, on Proteus work fine.

:blah::blah::blah::blah::blah::laugh::laugh:
Sacban
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top