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.

[PIC] connect Microcontroller PIC16F84A to PC via rs232 using MPlab xc8 (xc8 Code)

Status
Not open for further replies.

Harman302

Newbie level 3
Joined
Nov 11, 2013
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
33
Hii Everyone

I have a PIC16F84A uC which I have connected to the Max3100 to communicate to the PC via RS232. I can get the uC individually to work perfectly by writing my code in MPlabXC8.

My problem is that I want a xc8 code which will help me to send/receive the data to/from the PC and I am unable to do so. I searched many forums and found some code examples but the code syntax is not recognized by the xc8.

for example Uart_Init (9600); is giving me an error, if I try USE command- it turns red and gives me an error and so on.

Does anyone know how I can overcome this problem. Does anyone have the code for xc8???
I am a newbie!!! please help if possible!!!!! :???:
 


Code C - [expand]
1
UART_Init(9600);

is mikroC Code. Read datasheet and write your own code.
 

    V

    Points: 2
    Helpful Answer Positive Rating
Isn't the UART_ command for the internal UART module? If you are using the MAX3100 you need to write your own routine but the example on the Maxim data sheet works fine and if I remember correctly will work on the 16F84A without modification.

Brian.
 

    V

    Points: 2
    Helpful Answer Positive Rating
hii Brian

Thanx for your reply!!
The code on MAX3100 datasheet is an assembly language code, it neither specifies the syntax nor the code for coding in c or xc8.
Many thanx again!!!
Isn't the UART_ command for the internal UART module? If you are using the MAX3100 you need to write your own routine but the example on the Maxim data sheet works fine and if I remember correctly will work on the 16F84A without modification.

Brian.

- - - Updated - - -

Dear Jayanth

thnx for your reply bro!!!

yes u right, I was doing that mistake, however I couldn't find the code for UART communication in xc8 user manual.
It specifies that we can use uSart communication using getch command. Thats it!!! do you know from where can I get the syntax for this code. That would be really helpful.
Many thanx !!!!! I really appreciate!!

Code C - [expand]
1
UART_Init(9600);

is mikroC Code. Read datasheet and write your own code.
 

Hiii guys.... i tried this code in order to set up the communication... What do u think could be the problem?

Code:
 #include 
 #include "delay.h"
 
 // Clock Speed for delay function
 #define	XTAL_FREQ	4MHZ  
 
 // Manual SPI control for MAX3100	
 #define CS RD2        				// Chip Select
 #define SPI_OUT RC5				// SPI Data Out
 #define SPI_IN	RC4					// SPI Data In
 #define SPI_CLK RC3 				// SPI Clock

 // Burn Configuration word
 __CONFIG(XT & WDTDIS & PWRTEN & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT);
 
 /*--------------------------------------------------------------------*
  Function: main
  Inputs:	None (void)
  Outputs:	None (void)
 *---------------------------------------------------------------------*/
 void main(void)
 {
 	 // Local variables
 	 char int_array[16] = {1,1,0,0,0,0,0,0,0,0,0,0,1,0,1,1};		// MAX3100 Initiate sequence 9600 baud
 	 char cmd_array[16] = {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1};		// MAX3100 Command sequence: TEST MODE
 	 int i=0;					// Integer used for loop counting
 	 
	 // Set Clock to 0
	 SPI_CLK = 0;
	 
	 // Setup SPI direction bits
	 TRISD2 = 0;				// Set Chip Select as output
	 TRISC5 = 0; 				// Set SPI data out as output
	 TRISC4 = 1;				// Set SPI data in as input
	 TRISC3 = 0;				// Set SPI clock as output
	 	 
 	 // Send MAX3100 Initiation command
 	 CS = 0;						// Take Chip Select low
 	 for(i=0; i<16; i++)
     {
	 	 SPI_OUT = int_array[i];	// Send Array bit by bit
		 DelayMs(100);				// Wait a bit
		 SPI_CLK ^= 0x0F;			// Toggle clock
		 DelayMs(100);				// Wait a bit
	 }
	 CS = 1;						// Take Chip Select high
	 
	 DelayMs(500);					// Wait before next write
 	 
 	 // Send MAX3100 TEST command
 	 CS = 0;						// Take Chip Select low
 	 for(i=0; i<16; i++)
     {
	 	 SPI_OUT = cmd_array[i];	// Send Array bit by bit
		 DelayMs(100);				// Wait a bit
		 SPI_CLK ^= 0x0F;			// Toggle clock
		 DelayMs(100);				// Wait a bit
	 }
	 //CS = 1;						// Take Chip Select high
	 
	// Loop forever 
 	while(1);
 }
 
Last edited by a moderator:

https://singularengineer.com/programming-pic-18-using-xc8-mplab-x-usart-pic18f-serial/

It uses the same functions as C18 Compiler. Search for C18 User Guide and Library manual.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
unsigned int MAX3100Init = 0xC00B;
 
mask = 0x0001;  //LSB first
//mask = 0x8000;    //MSB first
for(i = 0; i < 16; i++){
    (MAX3100Init & mask)? SPI_OUT = 1: SPI_OUT = 0;
    mask = mask << 1;   //LSB first
    //mask = mask >> 1; //MSB first
    
}

 
Last edited:

And note that the MAX3100 shifts data out at the same time as you shift data in to it. Using the same mask technique you can 'build' the response from the '3100 without having to send it dummy commands.

It should be VERY easy to convert the assembly language routine into 'C'. It is well commented and is only a loop and a few bit set/reset lines.

Brian.
 

Hi jayanth

Thnx for the advice. i tried the c18 compiler but the problem is that the c18 compiler uses plib library which does not support pic16f84a.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top