CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

RS232 not working

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mengtashou103



Joined: 27 Jan 2018
Posts: 2

View user's profile Send private message

RS232 not working
PostPosted: Sat Jan 27, 2018 8:52 am     Reply with quote

Hi, I am new learner of PIC microcontroller. I am using microchip explore 8 board with PIC16f1719. I wrote a simple program try to send string through RS232 to computer but doesn't work. I have try with other demo program and confirmed the PIC could actually send data to the PC. I guess might be something wrong with the code? Would anyone help me out? Thanks

Please find code below:

Code:

#include <16f1719.h>
#include <string.h>
#use delay (clock=20M)
#use rs232 (baud=9600,xmit=PIN_C6,rcv=PIN_C7)
//#pin_select U1TX=PIN_C6
//#pin_select U1RX=PIN_C7
/*
 *
 */
int main()
{
    while(1)
    {
        //setup_uart(9600);
        //printf("Hello World");
       
        output_b(0x0);
        output_d(0x0);
        while(!input(PIN_A5))
        {
            printf("Hello World");
            delay_ms(10);
            output_bit(PIN_B3,1);
            delay_ms(10);
            output_bit(PIN_B2,0);
            delay_ms(10);
            output_bit(PIN_B1,1);
            delay_ms(10);
            output_bit(PIN_B0,0);
            delay_ms(10);
            output_bit(PIN_D3,1);
            delay_ms(10);
            output_bit(PIN_D2,0);
            delay_ms(10);
            output_bit(PIN_D1,1);
            delay_ms(10);
            output_bit(PIN_D0,0);
            delay_ms(10);
        }
    }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Sat Jan 27, 2018 9:25 am     Reply with quote

Code:

#include <16f1719.h>
#include <string.h>
#use delay (clock=20M)
#use rs232 (baud=9600,xmit=PIN_C6,rcv=PIN_C7)


First a slight syntactical change:
Code:

#include <16f1719.h>
#use delay (clock=20M)

#pin_select U1TX=PIN_C6
#pin_select U1RX=PIN_C7
//The PIN_SELECT stuff must be before you use the UART
#use rs232 (baud=9600,UART1)

//Generally only include things (except for the chip setup) _[u]after[/u]_
//the chip is configured
#include <string.h>


However now there is a big problem. Where is the 20Mhz clock coming from?....

The Explorer8, has an 8MHz crystal. Has yours been changed?.
Then the chip needs to be told to use this crystal. So:
Code:

#include <16f1719.h>
#FUSES HS //This enables the high speed oscillator
//Suitable for a 20MHz crystal
#FUSES NOWDT //Otherwise some chips will default to enabling this
#FUSES NOLVP //Should normally always be disabled.
#use delay (clock=20M)

#pin_select U1TX=PIN_C6
#pin_select U1RX=PIN_C7
//The PIN_SELECT stuff must be before you use the UART
#use rs232 (baud=9600,UART1)

//Generally only include things (except for the chip setup) _after_
//the chip is fully configured
#include <string.h>


Now whether this will work depends on what crystal your board has.

This is why the standard 'first test' should always be to simply flash an LED. Only once you have this flashing _at the right rate_, can you move on to trying serial.
mengtashou103



Joined: 27 Jan 2018
Posts: 2

View user's profile Send private message

PostPosted: Sat Jan 27, 2018 4:25 pm     Reply with quote

Ttelmah wrote:
Code:

#include <16f1719.h>
#include <string.h>
#use delay (clock=20M)
#use rs232 (baud=9600,xmit=PIN_C6,rcv=PIN_C7)


First a slight syntactical change:
Code:

#include <16f1719.h>
#use delay (clock=20M)

#pin_select U1TX=PIN_C6
#pin_select U1RX=PIN_C7
//The PIN_SELECT stuff must be before you use the UART
#use rs232 (baud=9600,UART1)

//Generally only include things (except for the chip setup) _[u]after[/u]_
//the chip is configured
#include <string.h>


However now there is a big problem. Where is the 20Mhz clock coming from?....

The Explorer8, has an 8MHz crystal. Has yours been changed?.
Then the chip needs to be told to use this crystal. So:
Code:

#include <16f1719.h>
#FUSES HS //This enables the high speed oscillator
//Suitable for a 20MHz crystal
#FUSES NOWDT //Otherwise some chips will default to enabling this
#FUSES NOLVP //Should normally always be disabled.
#use delay (clock=20M)

#pin_select U1TX=PIN_C6
#pin_select U1RX=PIN_C7
//The PIN_SELECT stuff must be before you use the UART
#use rs232 (baud=9600,UART1)

//Generally only include things (except for the chip setup) _after_
//the chip is fully configured
#include <string.h>


Now whether this will work depends on what crystal your board has.

This is why the standard 'first test' should always be to simply flash an LED. Only once you have this flashing _at the right rate_, can you move on to trying serial.


Oh I see. Yes, the clock setting was incorrect. I just randomly put a clock time on it since I saw some sample code online was just like that, but i have no clue how I should set it. Thanks for your help.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group