• AVR Freaks

Hot!Serial communication between PIC16F877 and computer

Page: < 12345 > Showing page 4 of 5
Author
AMPS
Super Member
  • Total Posts : 468
  • Reward points : 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 03:53:58 (permalink)
-1 (1)
Have you tested this potion. Just use Send_string function in main while loop and send me the output screen shot. I would use DOCKLIGHT software .
 

Send_string_uart1("Welcome back");


void Send_string_uart1(const unsigned char *string) {
unsigned char k=0;
 
do {
TXREG=string[k++];
while(TXIF==0);
TXIF=0;
} while(string[k]!='\0');
}

post edited by AMPS - 2019/11/12 03:55:22

Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#61
ric
Super Member
  • Total Posts : 24646
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 04:01:22 (permalink)
+1 (1)
AMPS, please stop confusing the issue by insisting they run your code.
That's doing nothing to help them find their issues.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#62
pcbbc
Super Member
  • Total Posts : 1388
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 05:05:49 (permalink)
+1 (1)
nidac
@pcbbc:
PIC is power on by USB port and it has a reset button.
After power on I reset hardware but first character(string) is unknown, please see my 34 post attachement.

Yes, but you are back to sending the string only once are you not?
 
When you sent the string multiple times (albeit due to a coding error on your part of not having a infinite loop in your main routine) it worked, did it not?  See output in post 11 where you showed it working!
 
I still suspect you have some initial start up condition (after you press the reset switch) which is garbling just the initial few character sent.  Perhaps the PIC clock is not yet up to speed after a reset?
 
Go back to the code you used in post 11.  Confirm that it is still "working".
 
Then fix the "error" in it like this:
void main()
{
        SPBRG = 25;
        TXSTA = 0b00100100; //transmit enable, async, high speed mode
        RCSTA = 0b10000000; //serial port enable
        DelayMs(100);
        printf("    Hello World!\n\r");
        //This line added - you NEED an infinite loop in main()...
        while(1);
}

Does it now stop "working"?
 
Next, put the printf inside the loop:
void main()
{
        SPBRG = 25;
        TXSTA = 0b00100100; //transmit enable, async, high speed mode
        RCSTA = 0b10000000; //serial port enable
        //Repeatedly send string inside while...
        while(1) {
            DelayMs(100);
            printf("    Hello World!\n\r");
       }
}

Now does it work, albeit with some garbled output at the start?
 
This shows that your DelayMs(100) is either not working, or not long enough to get over the initial start up conditions that are causing your garbled transmission problem.
#63
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 05:23:09 (permalink)
0
@pcbbc:
Sending strings with printf () works, I want to do the same thing but with pointers.
#64
pcbbc
Super Member
  • Total Posts : 1388
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 05:35:37 (permalink)
+1 (1)
nidac
@pcbbc:
Sending strings with printf () works, I want to do the same thing but with pointers.

You are missing the point.
 
You are completely hung up on your assumption that "printf works, pointers do not" that you cannot see the real problem.  Re-read my post and see the subtle (but very important) difference between the two examples of your code that you have posted in posts #5 and #11 that makes one work and the other not work.
 
The working code you posted that used printf (that uses "pointers" internally BTW) had a flaw in it.  It did not have a infinite loop in main.  As a result of that flaw the PIC would send the string over and over, because it would reset and restart when it fell out of the end of the "main" routine.
 
When you send the string over and over it gets past whatever initial conditions are causing the garbled output.
 
Try code that uses your own pointer routine, sends the string more than once.  I am guessing you have never tried this.  You may find it works!
 
Try the code that uses printf, and sends the string once only.  I am guessing you have never tried this either.  You may find it does not work!
 
Edit:
When you can post code that uses printf and your own "pointer" method that are functionally equivalent, and show that they behave the differently, then I will believe that there is a "problem with pointers".
Until you do that you are comparing apples with oranges I'm afraid.
post edited by pcbbc - 2019/11/12 05:41:06
#65
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 05:45:38 (permalink)
0
@AMPS:
I have installed docklight but can not track anything because your code does not compile with HI-TECH C Compiler v 7.85PL3, HI-TECH Compiler v9.83 (with MPLAB). Have you checked this code before?
post edited by nidac - 2019/11/12 05:47:27
#66
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 06:28:31 (permalink)
0
@pcbbc:
Item # 11 with or without while (1) has the same result on USART: the string of characters ("     Hello World!) is sent to infinity.
Now I'm interested in: why the code from item # 5 doesn't work.
If you have an idea in this code, I'll wait.
#67
pcbbc
Super Member
  • Total Posts : 1388
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 07:51:00 (permalink)
+1 (1)
nidac
@pcbbc:
Item # 11 with or without while (1) has the same result on USART: the string of characters ("     Hello World!) is sent to infinity.
Now I'm interested in: why the code from item # 5 doesn't work.
If you have an idea in this code, I'll wait.

It "sends to infinity"?  You mean it outputs...
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
...

Continuously in the terminal window?

Oh my goodness.  Can you not see the difference?
 
When you send the string continuously (either by design or coding error), it works.
When you do not send the string continuously (i.e. only once when the PIC first starts up, and as your code supplied in post #5 was doing) it does NOT work.
 
Have you tried sending only ONCE with printf?  Your code that you posed in #11 does NOT do that due to a coding error.  So as far as I am aware you have only ever tried printf by sending continuously.
 
Try both of these:
 
1. The printf version - but NOT sending continuously:
Put the while after the printf("Hello World!\r\r"); line.
I bet this will NOT work.
 
2. The "pointer" version - but DO send continuously:
Put the while around the txstr("Hello World!\r\r"); line.
I bet this WILL work.
 
Also post the code you are trying and state the results you are achieving.  You are changing many things about your code, and telling us in words what you have changed.  We can't tell for sure exactly what you are changing, or if you are completing those changes successfully.
post edited by pcbbc - 2019/11/12 07:52:47
#68
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 10:28:15 (permalink)
-1 (1)
@pcbbc:
Please see attached file.
It is very clear that txstr function is wrong.
post edited by nidac - 2019/11/12 10:56:54
#69
PStechPaul
Super Member
  • Total Posts : 2447
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 18:33:35 (permalink)
+1 (1)
I made a project in MPLABX 5.10 with XC8 2.00 and modified it to build and load. I ran the simulator and the txstr() function seems to be sending the proper characters to TXREG:
/*
 * File:   Serial_Test_877_Main.c
 * Author: paul
 *
 * Created on November 12, 2019, 8:02 PM
 */

#include <xc.h>

//#include <pic.h>
//#include "delay.h"
//#include "delay.c"
__CONFIG(0xd02);
#define _XTAL_FREQ  4000000
void ser_int();
void tx(unsigned char);
unsigned char rx();
void txstr(unsigned char *);
void main()
{
    TRISC6=0;
TRISC7=1;
    ser_int();
    __delay_ms(100);   //DelayMs(100);
    txstr("      Hello World\n\r\r");
    while(1)
    {
        tx(rx());
    }
}
void ser_int()
{
    TXSTA=0b00100100;
    RCSTA=0b10010000;
    SPBRG=25;
    __delay_ms(10); //DelayMs(10);
}
void tx(unsigned char a)
{
    //TXREG=a;
    while(!TXIF);
    TXREG = a;
}
unsigned char rx()
{
    while(!RCIF);
    //RCIF=0;
    return RCREG;
}
void txstr(unsigned char *s)
{
    while(*s)
    {
        tx(*s++);
    }
}

//void main(void) {
//    return;
//}

Please use this format for showing your code.

 
#70
AMPS
Super Member
  • Total Posts : 468
  • Reward points : 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/12 20:17:22 (permalink)
0
nidac
@AMPS:
I have installed docklight but can not track anything because your code does not compile with HI-TECH C Compiler v 7.85PL3, HI-TECH Compiler v9.83 (with MPLAB). Have you checked this code before?


i have used MPLAB IDE 8.86 with hitech compiler version 9.83
Dont ignore someone suggestion,PStechPaul and PCBBC giving proper explanation why results are coming properly. what it has to do when Statements are used.
 
One thing you can do, First write your code, just say what u expected for your written code.As per me results are coming properly as expected.
 

Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#71
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 00:38:29 (permalink)
0
@PStechPaul:
I have already installed MPLAB IDE v.8.92 and tried to work with XC8 v2.0, but it only works with .asm files. I saw that you used XC8 v2.0 but modified it to work with .c files("I made a project in MPLABX 5.10 with XC8 2.00 and modified it to build and load"). How did you do that? Thanks.
@AMPS:
Thanks, I will try.
#72
AMPS
Super Member
  • Total Posts : 468
  • Reward points : 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 01:10:21 (permalink)
0
checked DOC FILE. IF RESULTS ARE FINE . YOU CAN USE CODE DIRECTLY.
I HAVE  DOWNLOADED YOUR SOFTWARE & CHECKED ITS COMING PROPERLY.

Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#73
ric
Super Member
  • Total Posts : 24646
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 01:12:08 (permalink)
+1 (1)
nidac
I have already installed MPLAB IDE v.8.92 and tried to work with XC8 v2.0, but it only works with .asm files.

You have made some mistake then. XC8 does NOT work with ASM files at all.




I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#74
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 01:17:31 (permalink)
0
@ric:
Thanks.
I tried to say that my MPLAB 8.92 installed doesn't see the driver for XC8 v2.00, it only sees v1.33
#75
AMPS
Super Member
  • Total Posts : 468
  • Reward points : 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 01:22:10 (permalink)
0
Are you using MPLAB X IDE ??
i have used MPLAB IDE 8.86 with hitech compiler version 9.83
As i know XC8 compiler wont allow generate MCC file for particular controller

Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#76
ric
Super Member
  • Total Posts : 24646
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 01:22:12 (permalink)
+1 (1)
That is correct. You can not use XC8 v2.x in MPLAB8, you need MPLABX.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#77
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 01:43:36 (permalink)
0
@AMPS, @ric:
That's it, I will download MPLABX 5.10.
#78
AMPS
Super Member
  • Total Posts : 468
  • Reward points : 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 02:03:09 (permalink)
0
I have used MPlab X ide V5.15  & hitech compiler 9.83. It wil compile
 

#include <htc.h>
#include <stdio.h>
#include<pic.h>
#include"delay.h"

#define _XTAL_FREQ 4000000
unsigned int i = 0;
unsigned char get_value;
unsigned long baudRate = 9600;
unsigned char c = 0;
unsigned char ch;
#define LED RC5# define LED_RX RC7 // Pin assigned RX LED
#define LED_TX RC6 // Pin assigned TX LED
#define DE RC2
unsigned int j = 0;
unsigned char * str;
unsigned int count = 0;
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF);

void tx(unsigned char a) {
//TXREG=a;
while(!TXIF);
TXIF = 0;
TXREG = a;
}
unsigned char rx() {
while(!RCIF);
RCIF=0;
return RCREG;
}
void txstr(unsigned char *s) {
while(*s) {
tx(*s++);
}
}
void Serial_1_Send_byte(char trbuf1) {
TXREG = trbuf1;
while (0 == TRMT);
while (0 == TXIF);
TXIF = 0;
}
char Serial_Receive_byte() {
while (RCIF == 0); // Wait till the data is received
RCIF = 0; // Clear receiver flag
return (RCREG); // Return the received data to calling function
}
void Send_string_uart1(const unsigned char * string) {
unsigned char k = 0;
do {
TXREG = string[k++];
while (TXIF == 0);
TXIF = 0;
} while (string[k] != '\0');
}
 

void putch(char data) {
while(!TRMT);
TXREG = data;
}
char getch(void) {
if (OERR)
CREN=0;
CREN=1;
while(!RCIF);
return (RCREG);
}
 
void interrupt isr(void) {
unsigned char ch;
if (TMR1IF == 1) {
count++;
TMR1IF = 0;
TMR1H = 0xFD;
TMR1L = 0xE8;
if(count>= 250) {
// printf(" Hello World!\n\r");
// Send_string_uart1("");
count=0;
}
}
}
void Timer1_Interrupt() {
OPTION_REG = 0b00110000;
INTCON = 0b00000000;
PIE1 = 0b00000001;
PIR1 = 0x01;
TMR1H = 0xFD;
TMR1L = 0xE8;
T1CON = 0x21;
}
void Init_Controller() {
TRISC = 0x80;
PORTC = 0x80;
TRISB = 0XFF;
PORTB = 0X00;
TRISB0 = 1;
TRISB1 = 1;
TRISB2 = 1;
TRISB3 = 1;
TRISA = 0xC0;
PORTA = 0x0c0;
ADCON1 = 0x00;
ADCON0 = 0x00;
DE = 0;
Timer1_Interrupt();
}
void InitUART(void) {
TRISC6 = 1; // TX Pin
TRISC7 = 1; // RX Pin
// SPBRG = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
// BRG16 = 1;
BRGH = 1; // Fast baudrate
SYNC = 0; // Asynchronous
SPBRG = 25;
SPEN = 1; // Enable serial port pins
CREN = 1; // Enable reception
SREN = 0; // No effect
TXIE = 0; // Disable tx interrupts
RCIE = 1; // Enable rx interrupts
TX9 = 0; // 8-bit transmission
RX9 = 0; // 8-bit reception
TXEN = 0; // Reset transmitter
TXEN = 1; // Enable the transmitter
}
void main(void) {
Init_Controller();
InitUART();
GIE = 1;
PEIE = 1;
TMR1IE = 1;
while (1) {
//Send_string_uart1("Hello World\n\r\");
txstr(" Hello world\n\r\r");
printf(" Hello World!\n\r");
__delay_ms(1);
}
}
 

 

Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#79
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 02:05:54 (permalink)
0
@AMPS:
Thanks, now I download v5.10.
#80
Page: < 12345 > Showing page 4 of 5
Jump to:
© 2019 APG vNext Commercial Version 4.5