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.

UART not working properly

Status
Not open for further replies.

wa'id

Junior Member level 1
Joined
May 13, 2012
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,380
I was reading data from USART Terminal into PIC in proteus, buh UART_Read() only reads like first bit of whatever input data and ignores others. Please how do I read the whole data into my PIC..I use mikrobasic
 

You are reading data from form same PC from its USART Terminal to Proteus..

Have u used any virtual serial port driver...

Post ur code, and schematic diagram, so that we can help u.

---------- Post added at 20:58 ---------- Previous post was at 20:57 ----------

You are reading data from form same PC from its USART Terminal to Proteus..

Have u used any virtual serial port driver...

Post ur code, and schematic diagram, so that we can help u.
 

Yeah, I used VSPD from Eltima software.. Kindly find attached the simulation design and the mikrobasic codes
 

Attachments

  • Proteus%20design%20and%20mikrobasic%20code.pdf
    281.4 KB · Views: 69

Okay..

Create two ports using Elitma and then check whether they are working or not..
Open MikroBasic USART Terminal and then Set com port to the desired com port and then connect proteus com pim to virtual terminal..

Then check whether your communication part is okay or not...
 

Getting data from USART Terminal to proteus is not the issue as this is perfectly working. The issue is "Reading the data into PIC". Fine, I used UART_Read() in my code to read the data into a variable buh it doesn't work correctly. It only read the first bit of the data ie only a 1 (high) is displayed on the oscilloscope i use to check the data.

Kindly refer to the design and codes attached.

Thank you
 

Attachments

  • Proteus%20design%20and%20mikrobasic%20code.pdf
    281.4 KB · Views: 64

Code:
while (TRUE) ' Endless loop
if (UART1_Data_Ready() <> 0) then ' If data is received,
uart_rd = UART1_Read() ' read the received data,
UART1_Write(uart_rd)
Lcd_Ch(uart_rd)            ' and send data via UART
end if
wend

Try this code..
 

That is the code I used buh the problem is that the UART_Read() is only reading the first bit of the input data..

Can Interrupt help in this case??
 

Try this.

Code:
main()
{
   unsigned char ch;
   ---------------
   ---------------
   ch=UART_read();
   ---------
}

unsigned char UART_read()
{
      unsigned char ch;
      CREN=1;
      uart_wait();
      ch=RCREG;
      return ch;
}

void uart_wait()
{
      unsigned char ch;
      if(!OREE)
      CREN=0;
      ch=RCREG;
      CREN=1;
      while(!RCIF)         //Continue until you receive a byte from transmitter.
      continue;
}

Try this code and inform us if you get any error.
 

@sherazi

The codes and the design link are as follow:





Code Basic4GL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
program UART_project
 
dim uart_rd as word
dim LCD_RS as sbit at RB4_bit
dim LCD_EN as sbit at RB5_bit
dim LCD_D7 as sbit at RB3_bit
dim LCD_D6 as sbit at RB2_bit
dim LCD_D5 as sbit at RB1_bit
dim LCD_D4 as sbit at RB0_bit
 
dim LCD_RS_Direction as sbit at TRISB4_bit
dim LCD_EN_Direction as sbit at TRISB5_bit
dim LCD_D7_Direction as sbit at TRISB3_bit
dim LCD_D6_Direction as sbit at TRISB2_bit
dim LCD_D5_Direction as sbit at TRISB1_bit
dim LCD_D4_Direction as sbit at TRISB0_bit
 
dim output as char[1]
 
main:
TRISB = 0
PORTB = %11111111
ANSEL = 0
ANSELH = 0
Lcd_init()
Lcd_Cmd(_LCD_CLEAR)
 
  UART1_Init(9600)                         ' Initialize UART module at 9600 bps
  Delay_ms(100)                           ' Wait for UART module to stabilize
 
  while (TRUE)                             ' Endless loop
    if (UART1_Data_Ready() <> 0) then     ' If data is received,
      uart_rd = UART1_Read()               '   read the received data,
      UART1_Write(uart_rd)
      ByteToStr(uart_rd,output)
      Lcd_Out_Cp(output)                '   and send data via UART
    end if
  wend
end

.



---------- Post added at 23:33 ---------- Previous post was at 23:29 ----------

Try this.

Code:
main()
{
   unsigned char ch;
   ---------------
   ---------------
   ch=UART_read();
   ---------
}

unsigned char UART_read()
{
      unsigned char ch;
      CREN=1;
      uart_wait();
      ch=RCREG;
      return ch;
}

void uart_wait()
{
      unsigned char ch;
      if(!OREE)
      CREN=0;
      ch=RCREG;
      CREN=1;
      while(!RCIF)         //Continue until you receive a byte from transmitter.
      continue;
}

Try this code and inform us if you get any error.


This code looks C... I use mikrobasic
 
Last edited by a moderator:

You have to use UART1_Read_Text() to read strings. UART1_Read() only reads one char at a time. if you want to use UART1_Read() and you are receiving a string of 12 characters then you have to make a loop from 0 to 11 and read 1 char at a time and put it in an array and finally display the array on lcd.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top