I am trying to run a very simple UART learning project but without any success. Please help.
For now , all I want is to sent some text, and a "byte" via the PIC, and then I want to read what I have sent on my PC. I have installed REALTERM serial capture program.
The hardware is a PIC16F887 connect to a LCD and pin25 + pin26 connected to a USB to TTL converter.
Windows device manager says the comport is working properly. The display on the LCD is correct.
REALTERM does not show any characters on the screen. I do not get any errors from REALTERM.
My source code is as follows :
program Serial_test;
{ Declarations section }
// LCD module connections
var LCD_RS : sbit at RB4_bit;
var LCD_EN : sbit at RB5_bit;
var LCD_D4 : sbit at RB0_bit;
var LCD_D5 : sbit at RB1_bit;
var LCD_D6 : sbit at RB2_bit;
var LCD_D7 : sbit at RB3_bit;
var LCD_RS_Direction : sbit at TRISB4_bit;
var LCD_EN_Direction : sbit at TRISB5_bit;
var LCD_D4_Direction : sbit at TRISB0_bit;
var LCD_D5_Direction : sbit at TRISB1_bit;
var LCD_D6_Direction : sbit at TRISB2_bit;
var LCD_D7_Direction : sbit at TRISB3_bit;
// End LCD module connections
var txt1 : array[16] of char;
n : byte;
begin
{main program}
ANSEL := 0; // Configure AN pins as digital I/O
ANSELH := 0;
TRISB := 0;
PORTB := 0xFF;
TRISB := 0xFF;
// TRISC := 0x80; //RC7 = Rx = Input en RC6 = Tx = Output res input
// PORTC := 0x00;
// TXSTA := 0xA5; // 0b10100100; set transmitter asyn + enable + 8bit
// RCSTA := 0x90; // 0b10010000 set Receiver async + enable + 8 bit
// TXREG := 0x00; // inisieer die begin TX waarde
txt1 := 'TEST SERIAL COMMS';
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Delay_ms(1000);
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
LCD_Out(1,1,txt1); // Write text in first row
Delay_ms(1500);
UART1_Init(9600); // initialize UART module
Delay_ms(100); // Wait for UART module to stabilize
while true do
begin
if (UART1_Tx_Idle() <> 0) then
begin
UART1_Write_text('OK'); // some text
LCD_Out(2,1,'Text'); //show on LCD that text has been sent
Delay_ms(200);
end else
begin
LCD_Out(2,1,'Not'); // display "not" if it has not been sent
Delay_ms(200);
end;
LCD_Out(2,1,' '); // clear message
Delay_ms(500);
if (UART1_Tx_Idle() <> 0) then UART1_Write(0x44); // sent data byte
LCD_Out(2,1,'Byte'); // show on LCD the word "byte"
Delay_ms(200);
LCD_Out(2,1,' '); // clear message for next loop
Delay_ms(2000);
end;
end.
Any help will appreciated.
Cannot read UART data on PC sent from PIC16877
Re: Cannot read UART data on PC sent from PIC16877
In the mean time, I made some progress, but still do not understand the reading I got from REALTERM. I sent via the PIC a byte 0x55 but read on the terminal "66E6".
My source code is changed as follows :
program Serial_test;
{ Declarations section }
// LCD module connections
var LCD_RS : sbit at RB4_bit;
var LCD_EN : sbit at RB5_bit;
var LCD_D4 : sbit at RB0_bit;
var LCD_D5 : sbit at RB1_bit;
var LCD_D6 : sbit at RB2_bit;
var LCD_D7 : sbit at RB3_bit;
var LCD_RS_Direction : sbit at TRISB4_bit;
var LCD_EN_Direction : sbit at TRISB5_bit;
var LCD_D4_Direction : sbit at TRISB0_bit;
var LCD_D5_Direction : sbit at TRISB1_bit;
var LCD_D6_Direction : sbit at TRISB2_bit;
var LCD_D7_Direction : sbit at TRISB3_bit;
// End LCD module connections
var txt1 : array[16] of char;
n : byte;
begin
{main program}
ANSEL := 0; // Configure AN pins as digital I/O
ANSELH := 0;
TRISB := 0;
PORTB := 0xFF;
TRISB := 0xFF;
TRISC := 0x80; //RC7 = Rx = Input en RC6 = Tx = Output res input
PORTC := 0x11;
// TXSTA := 0xA5; // 0b10100100; set transmitter asyn + enable + 8bit
// RCSTA := 0x90; // 0b10010000 set Receiver async + enable + 8 bit
// TXREG := 0x00; // inisieer die begin TX waarde
txt1 := 'TEST SERIAL COMMS';
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Delay_ms(1000);
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
LCD_Out(1,1,txt1); // Write text in first row
Delay_ms(1500);
UART1_Init(9600); // initialize UART module
Delay_ms(100); // Wait for UART module to stabilize
while true do
begin
if (UART1_Tx_Idle() <> 0) then UART1_Write(0x55); // sent data byte
LCD_Out(2,1,'Byte sent'); // show on LCD the action
Delay_ms(200);
LCD_Out(2,1,' '); // clear message for next loop
Delay_ms(2000);
end;
end.
Why would I read a different value on PC terminal from what I have sent with PIC?
My source code is changed as follows :
program Serial_test;
{ Declarations section }
// LCD module connections
var LCD_RS : sbit at RB4_bit;
var LCD_EN : sbit at RB5_bit;
var LCD_D4 : sbit at RB0_bit;
var LCD_D5 : sbit at RB1_bit;
var LCD_D6 : sbit at RB2_bit;
var LCD_D7 : sbit at RB3_bit;
var LCD_RS_Direction : sbit at TRISB4_bit;
var LCD_EN_Direction : sbit at TRISB5_bit;
var LCD_D4_Direction : sbit at TRISB0_bit;
var LCD_D5_Direction : sbit at TRISB1_bit;
var LCD_D6_Direction : sbit at TRISB2_bit;
var LCD_D7_Direction : sbit at TRISB3_bit;
// End LCD module connections
var txt1 : array[16] of char;
n : byte;
begin
{main program}
ANSEL := 0; // Configure AN pins as digital I/O
ANSELH := 0;
TRISB := 0;
PORTB := 0xFF;
TRISB := 0xFF;
TRISC := 0x80; //RC7 = Rx = Input en RC6 = Tx = Output res input
PORTC := 0x11;
// TXSTA := 0xA5; // 0b10100100; set transmitter asyn + enable + 8bit
// RCSTA := 0x90; // 0b10010000 set Receiver async + enable + 8 bit
// TXREG := 0x00; // inisieer die begin TX waarde
txt1 := 'TEST SERIAL COMMS';
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Delay_ms(1000);
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
LCD_Out(1,1,txt1); // Write text in first row
Delay_ms(1500);
UART1_Init(9600); // initialize UART module
Delay_ms(100); // Wait for UART module to stabilize
while true do
begin
if (UART1_Tx_Idle() <> 0) then UART1_Write(0x55); // sent data byte
LCD_Out(2,1,'Byte sent'); // show on LCD the action
Delay_ms(200);
LCD_Out(2,1,' '); // clear message for next loop
Delay_ms(2000);
end;
end.
Why would I read a different value on PC terminal from what I have sent with PIC?
- jovana.medakovic
- mikroElektronika team
- Posts: 986
- Joined: 18 Dec 2018 10:36
Re: Cannot read UART data on PC sent from PIC16877
Hello,
I tested your code and it works fine.
I used USART terminal from our compiler and in the attachment, you can find a picture where you can see that I got 0x55 on the terminal.
Can you also try to use our terminal? Also, can you send me a screenshot where I can see how you set your terminal and what you get as a result?
Kind regards,
Jovana
I tested your code and it works fine.
I used USART terminal from our compiler and in the attachment, you can find a picture where you can see that I got 0x55 on the terminal.
Can you also try to use our terminal? Also, can you send me a screenshot where I can see how you set your terminal and what you get as a result?
Kind regards,
Jovana
- Attachments
-
- Untitled.png (44.49 KiB) Viewed 2498 times
Re: Cannot read UART data on PC sent from PIC16877
Thank you very much Jovana for the reply.
I got the same results from MikroTerminal , namely 66E6- see attachments . The difference is that on my terminal the pins CTS DCD and DSR are green while yours show inactive.
My setup involves a USB extension cable. I did not connect the 5V and 3.3 V of the USB-TTL converter to my board, as my bread board is powered. The GND terminal is connected, my Tx of PIC go to Rx of converter. I also connect my Tx to Rx of PIC to read the value returned, and that value shows correctly on the LCD screen.
I got the same results from MikroTerminal , namely 66E6- see attachments . The difference is that on my terminal the pins CTS DCD and DSR are green while yours show inactive.
My setup involves a USB extension cable. I did not connect the 5V and 3.3 V of the USB-TTL converter to my board, as my bread board is powered. The GND terminal is connected, my Tx of PIC go to Rx of converter. I also connect my Tx to Rx of PIC to read the value returned, and that value shows correctly on the LCD screen.
- Attachments
-
- Screen3.jpg (125.57 KiB) Viewed 2492 times
-
- Screen2.jpg (87.7 KiB) Viewed 2492 times
Re: Cannot read UART data on PC sent from PIC16887
The PIC in the subject line was wrong. The PIC I used in the project is the PIC16F887 , not the PIC16F877
- jovana.medakovic
- mikroElektronika team
- Posts: 986
- Joined: 18 Dec 2018 10:36
Re: Cannot read UART data on PC sent from PIC16877
Hello,
In the attachment, you can find two projects.
In one project, I used the internal oscillator (8MHz). If you want to use the internal oscillator, you have to set OSCCON register in the code.
In the second project, I used a crystal oscillator of 8MHz.
If you open Project -> Edit Project you can see how I set configuration bits.
Kind regards,
Jovana
In the attachment, you can find two projects.
In one project, I used the internal oscillator (8MHz). If you want to use the internal oscillator, you have to set OSCCON register in the code.
In the second project, I used a crystal oscillator of 8MHz.
If you open Project -> Edit Project you can see how I set configuration bits.
Kind regards,
Jovana
- Attachments
-
- UART_Crystal.zip
- (25.24 KiB) Downloaded 82 times
-
- UART_Internal.zip
- (25.1 KiB) Downloaded 82 times
Re: Cannot read UART data on PC sent from PIC16877
Thank you Jovana I am going to work on it this evening, I almost can't wait - will let you know
Re: Cannot read UART data on PC sent from PIC16877
Hi Jovana, I got it right thank you for all the trouble. When your example also did not work I knew something else must be the problem. The problem I had is the USB to TTL converter and driver. I found the code worked on a different computer, the only difference being the driver. The older driver versions 3.3 showed it works properly but it does not. The driver 2018/10/01 version 3.8.25.0 works perfect with the code on the second machine. When I update the driver on my computer to the latest (2019/06/05 version 3.8.3.0), it says the device is working properly though it does not, and label it as : "This is not a Profilic PL2303 please contact your supplier". I will buy the right converter today.
Thank you again, if it was not for your code, I would not look further ...
Thank you again, if it was not for your code, I would not look further ...