help needed for solving pic18f452 uart problem

General discussion on mikroC PRO for PIC.
Author
Message
kathir
Posts: 96
Joined: 13 Feb 2012 23:26
Location: Chennai
Contact:

help needed for solving pic18f452 uart problem

#1 Post by kathir » 25 Mar 2012 20:42

hi guys.
i made a program for rfid reader. but i test the program with mikroc terminal program. my lcd constantly showing the same message and my terminal program reads weird characters.
Hardware = pic18f452
software = mikroc terminal program
here is my program

Code: Select all

// LCD module connections
sbit Lcd_RS at RA0_bit;
sbit Lcd_EN at RA2_bit;
sbit Lcd_D7 at RB7_bit;
sbit Lcd_D6 at RB6_bit;
sbit Lcd_D5 at RB5_bit;
sbit Lcd_D4 at RB4_bit;
sbit Lcd_D3 at RB3_bit;
sbit Lcd_D2 at RB2_bit;
sbit Lcd_D1 at RB1_bit;
sbit Lcd_D0 at RB0_bit;
sbit Lcd_RS_Direction at TRISA0_bit;
sbit Lcd_EN_Direction at TRISA2_bit;
sbit Lcd_D7_Direction at TRISB7_bit;
sbit Lcd_D6_Direction at TRISB6_bit;
sbit Lcd_D5_Direction at TRISB5_bit;
sbit Lcd_D4_Direction at TRISB4_bit;
sbit Lcd_D3_Direction at TRISB3_bit;
sbit Lcd_D2_Direction at TRISB2_bit;
sbit Lcd_D1_Direction at TRISB1_bit;
sbit Lcd_D0_Direction at TRISB0_bit;
// End LCD module connections
void main()
{
char rcard[16];
char uart_rd;
int n=0;
int cnt=0;

  ADCON1 |= 0x07;
  INTCON = 0;
  INTCON1 = 0;
  INTCON2 = 0;
  CCP1CON = 0;
  CCP2CON = 0;
  TRISA = 0;
  TRISB = 0;

  Lcd_Init();                              // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                     // Clear display
//  Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off
  Lcd_Out(1, 1, "show the card");
  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize

  while (n<12) {                     // Endless loop
    if (UART1_Data_Ready()) {   // If data is received,
      uart_rd = UART1_Read();     // read the received data,
      UART1_Write(uart_rd);
      n = n+1;
      rcard[n] = uart_rd;

    }
    
  }
  n=n+1;
  rcard[n] = 0;
        Lcd_out(2,1, rcard);    // and send data via UART
}
i sent data via terminal (for example 212121212121 and asdfghjdkslmqz and 1a2ws34)
what is the mistake in the program. nothing received in lcd except "show the card" and my terminal receives weird characters.
My blog @ http://blog.kathir.co.in

Kathir

A Happy Mikroc User
I Love Mikroelektronika and Support Team

supra
Posts: 187
Joined: 08 Apr 2009 11:54
Location: Ontario, Canada

Re: help needed for solving pic18f452 uart problem

#2 Post by supra » 26 Mar 2012 02:14

Code: Select all

 rcard[n] = uart_rd;

    }
to:

Code: Select all

 rcard[n] = uart_rd;
 Lcd_out(2,1, rcard);    // and send data via UART
    }

kathir
Posts: 96
Joined: 13 Feb 2012 23:26
Location: Chennai
Contact:

Re: help needed for solving pic18f452 uart problem

#3 Post by kathir » 26 Mar 2012 15:00

hi i changed the code as you said.
but still i got some problem
i sent 400204067817
and received 4000> in controller and 4000 and some weird character like box or something
when i sent it again and again . same occurs
and then it doesn't receive anything

sent 400204067817
received 4000> in lcd and 4000,. in computer
sent 400204067817
showed 4000> 4000> in lcd and 4000,.4000,. in computer
sent 400204067817
showed 4000> 4000>4000> in lcd and 4000,.4000,.4000,. in computer
sent 400204067817 then nothing receives shows the above displayed only

i use pic18f452 and 4.1952mhz oscillator,






a
My blog @ http://blog.kathir.co.in

Kathir

A Happy Mikroc User
I Love Mikroelektronika and Support Team

supra
Posts: 187
Joined: 08 Apr 2009 11:54
Location: Ontario, Canada

Re: help needed for solving pic18f452 uart problem

#4 Post by supra » 27 Mar 2012 19:57

R using xt or hs oscillator setting?

try this:

Code: Select all

  }
    n=n+1;
  rcard[n] = 0;
  }
  

kathir
Posts: 96
Joined: 13 Feb 2012 23:26
Location: Chennai
Contact:

Re: help needed for solving pic18f452 uart problem

#5 Post by kathir » 29 Mar 2012 20:26

i am using extrenal oscillator

n=n+1
rcard[n] = 0;
i already did that too
but problem not solved
My blog @ http://blog.kathir.co.in

Kathir

A Happy Mikroc User
I Love Mikroelektronika and Support Team

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: help needed for solving pic18f452 uart problem

#6 Post by MARIO » 29 Mar 2012 20:39

For PIC18 Family use:

Code: Select all

sbit Lcd_RS at LATA0_bit;
sbit Lcd_EN at LATA2_bit;
sbit Lcd_D7 at LATB7_bit;
sbit Lcd_D6 at LATB6_bit;
sbit Lcd_D5 at LATB5_bit;
sbit Lcd_D4 at LATB4_bit;
//sbit Lcd_D3 at RB3_bit; //not used for LCD 4-bit library
//sbit Lcd_D2 at RB2_bit; // idem
//sbit Lcd_D1 at RB1_bit; // idem
//sbit Lcd_D0 at RB0_bit; // idem
See help file about LCD library.

BR

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: help needed for solving pic18f452 uart problem

#7 Post by MARIO » 29 Mar 2012 21:31

Try this:

Code: Select all

  while (n<12) {                     // Endless loop
    if (UART1_Data_Ready()) {   // If data is received,
      uart_rd = UART1_Read();     // read the received data,
      UART1_Write(uart_rd);
      //n = n+1;
      rcard[n] = uart_rd;
      n = n+1;
    }
These lines are not necessary too:

Code: Select all

sbit Lcd_D3_Direction at TRISB3_bit;
sbit Lcd_D2_Direction at TRISB2_bit;
sbit Lcd_D1_Direction at TRISB1_bit;
sbit Lcd_D0_Direction at TRISB0_bit;
BR

kathir
Posts: 96
Joined: 13 Feb 2012 23:26
Location: Chennai
Contact:

Re: help needed for solving pic18f452 uart problem

#8 Post by kathir » 31 Mar 2012 15:30

hi mario, supra
i have changed my code as you described

Code: Select all

while(1)
   { 
    if (UART1_Data_Ready()) 
{
    uart_rd = UART1_Read();
    UART1_Write(uart_rd);
    dum[i] = uart_rd;
    dum[i+1] = 0;
    i=i+1;
    Lcd_Out(1, 1, dum);       // and send data via UART
    }
}
but stil the problem not solved
when i send four numbers. it receives all the four numbers. but when i send more than 5 numbers. it receives as the following
i receive first 4 numbers (or) first 5 numbers (or) 5 random numbers on the terminal and the lcd.
the above is the code i used
My blog @ http://blog.kathir.co.in

Kathir

A Happy Mikroc User
I Love Mikroelektronika and Support Team

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: help needed for solving pic18f452 uart problem

#9 Post by p.erasmus » 31 Mar 2012 17:31

kathir wrote:i have changed my code as you described
No have not changed your code as Mario described you modified your code too look similar to what Mario has shown you!
kathir wrote:but stil the problem not solved
Are you surprised that it does not work?
Do as Mario showed you that might help
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

kathir
Posts: 96
Joined: 13 Feb 2012 23:26
Location: Chennai
Contact:

Re: help needed for solving pic18f452 uart problem

#10 Post by kathir » 31 Mar 2012 20:43

yeah now i implemented exactly as you said. i got it right displaying all the twelve characters correctly. but the terminal doesn't receive any data. that doesn't matter. when i connected the card reader to the same circuit with same program and replaced rfid connections instead of pc rs232 connection. i didn't receive any data on the lcd. may i know why?. can you please help me to correct my mistake
my code is here

Code: Select all

// LCD module connections
sbit Lcd_RS at RA0_bit;
sbit Lcd_EN at RA2_bit;
sbit Lcd_D7 at RB7_bit;
sbit Lcd_D6 at RB6_bit;
sbit Lcd_D5 at RB5_bit;
sbit Lcd_D4 at RB4_bit;

sbit Lcd_RS_Direction at TRISA0_bit;
sbit Lcd_EN_Direction at TRISA2_bit;
sbit Lcd_D7_Direction at TRISB7_bit;
sbit Lcd_D6_Direction at TRISB6_bit;
sbit Lcd_D5_Direction at TRISB5_bit;
sbit Lcd_D4_Direction at TRISB4_bit;

// End LCD module connections
unsigned char uart_rd;
unsigned char dum[12];
void main()
{
int i=0;
char uart_rd;
int n=0;
int cnt=0;

  ADCON1 |= 0x07;
  INTCON = 0;
  INTCON1 = 0;
  INTCON2 = 0;
  CCP1CON = 0;
  CCP2CON = 0;
  TRISA = 0;
  TRISB = 0;

  Lcd_Init();                              // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                     // Clear display
//  Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off
  Lcd_Out(1, 1, "swipe atm card");
  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize

  while (n<12) {                     // Endless loop
    if (UART1_Data_Ready()) {   // If data is received,
      uart_rd = UART1_Read();     // read the received data,
      UART1_Write(uart_rd);
      //n = n+1;
      dum[n] = uart_rd;
      n = n+1;
                }
  Lcd_Out(3, 1, dum);

  }
dum[n]=0
        Lcd_Out(4, 1, dum);
}
and my connections are
tx - rx
rx - tx
common gnd to gnd
is anything else should i do
My blog @ http://blog.kathir.co.in

Kathir

A Happy Mikroc User
I Love Mikroelektronika and Support Team

kathir
Posts: 96
Joined: 13 Feb 2012 23:26
Location: Chennai
Contact:

Re: help needed for solving pic18f452 uart problem

#11 Post by kathir » 02 Apr 2012 09:36

Hi guys, the following observations may help you to solve my problems.

1) i sent data via pc terminal. the data shows exactly as i send. no problem(only transmitter pin of max232 is connected to receiver pin of controller)
2) i sent data via pc terminal. the data shows with some random characters.(both tx and rx pin of max232 is connected to rx and tx pin of controller). terminal receives the characters displayed in lcd
3)when i connected a rfid reader. the lcd displays only 4 characters(the first 2 are first 2 characters in card and the next 2 are random of numbers in card). when i show the card again it receives only 3 characters(first two as in above and the 3rd is random).
the following is the lcd screen after showing the card over the reader 3 times
Image
My blog @ http://blog.kathir.co.in

Kathir

A Happy Mikroc User
I Love Mikroelektronika and Support Team

supra
Posts: 187
Joined: 08 Apr 2009 11:54
Location: Ontario, Canada

Re: help needed for solving pic18f452 uart problem

#12 Post by supra » 02 Apr 2012 13:00

can u rem it out in void main procedure?

Code: Select all

//char uart_rd;
try this

Code: Select all

if (UART1_Data_Ready() == 1) {          // if data is received 
Btw, when u put card inside for 5 sec, then u can remove card. did u try it?

kathir
Posts: 96
Joined: 13 Feb 2012 23:26
Location: Chennai
Contact:

Re: help needed for solving pic18f452 uart problem

#13 Post by kathir » 06 Apr 2012 18:29

yeah but same happens
My blog @ http://blog.kathir.co.in

Kathir

A Happy Mikroc User
I Love Mikroelektronika and Support Team

kathir
Posts: 96
Joined: 13 Feb 2012 23:26
Location: Chennai
Contact:

Re: help needed for solving pic18f452 uart problem

#14 Post by kathir » 07 Apr 2012 20:28

thanks for your replies guys, but my problem not solved. i tried soft uart also.
when i tried soft uart instead of hardware uart
only two digits received (1rst digit and 7th digit of my card(card has 12 digit number))
can anybody help me outta this.
My blog @ http://blog.kathir.co.in

Kathir

A Happy Mikroc User
I Love Mikroelektronika and Support Team

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: help needed for solving pic18f452 uart problem

#15 Post by p.erasmus » 08 Apr 2012 10:03

Do you use a ICD to debug your code and hardware?

if not then you should think about it we can help by finding obvious bugs in the code however
at some stage you must realize that you need to debug the code and hardware yourself we can not see
your hardware ,concetions and settings so you have to go at it yourself with a debugger.
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

Post Reply

Return to “mikroC PRO for PIC General”