  | 
	  | 
		 
	 
	
		| View previous topic :: View next topic   | 
	 
	
	
		| Author | 
		Message | 
	 
	
		
			statechamp
 
 
  Joined: 13 Apr 2009 Posts: 1
  
			
			 
			 
			 
			
			
			
			
			
			
  
		  | 
		
			
				| help for usart on pic18f1320 | 
			 
			
				 Posted: Mon Apr 13, 2009 7:57 am     | 
				     | 
			 
			
				
  | 
			 
			
				I have been working on programming this MCU for a senoir design project, and im have problems with the usart in the PIC18F1320. My project has to do with reading the received data and doing if statments and such based on what is received by the MCU. What i am trying to do is have a xbee module connected to the TX and RX pins. the thing that i am having a problem with is that in my program i can get the MCU to retrieve and transmit data, but i cannot read what is in the RCREG(receive register). Is there anyone that can help me? I will be really greatful, thanks.   
 
 
this is my code ran at 4mhz and created in MPLAB IDE:
 
 
 	  | Code: | 	 		  #include "p18f1320.h"
 
#include "usart.h"
 
#include "sw_uart.h"
 
#include "delays.h"
 
#include "stdlib.h"
 
#include "timers.h"
 
         
 
int DATABYTE;
 
char data;
 
 
 
void main(){
 
 
OSCCON=0xEF;
 
OSCTUNE=0b00000010;
 
 
for(;;)
 
{
 
 
if(RCSTAbits.FERR=1)
 
{
 
RCREG=0;
 
}
 
 
if(RCSTAbits.OERR=1)
 
{
 
RCSTAbits.CREN=0;
 
RCSTA=0b10010000;
 
}
 
 
T0CON=0b11001101;
 
TRISB = 0;    
 
TXSTA=0b00100010; 
 
 
//THREE THINGS THAT WAS SAID FOR CONFIG. USART
 
TRISB=0b00010010;
 
ADCON1=0b01100000; 
 
RCSTA=0b10010000; 
 
 
//SET UP FOR THE BAUD RATE
 
BAUDCTL=0b0101010;
 
SPBRG=25;      
 
TXSTA=0b00100000;
 
TXSTAbits.SYNC=0;
 
 
//interrupts
 
PIE1bits.TXIE=1; 
 
PIE1bits.RCIE=1;
 
INTCONbits.GIE=1;
 
INTCONbits.PEIE=1;
 
 
//SETTING UP AN 8 BIT ASYNC MODE (USART PINS MAY HAVE TO BE SET UP AS INPUTS)
 
RCSTA=0b10000000; 
 
SPBRG=25;       
 
TXSTA=0b00100000; 
 
 
//OTHER STUFF
 
while(PIR1bits.TXIF=0)
 
{
 
;
 
}
 
 
PIE1bits.TXIE=0;  
 
TXREG=DATABYTE;
 
 
while(PIR1bits.RCIF=0)
 
{
 
;
 
}
 
DATABYTE=RCREG; 
 
}
 
}
 
 | 	  
 
I have been working on this for like the last month and i do not know where to read the information that is being put in the MCU. _________________ thegoldenone34 | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Ttelmah Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Mon Apr 13, 2009 8:24 am     | 
				     | 
			 
			
				
  | 
			 
			
				You are writing code like an assembler programmer, but using a compiler. Either go assembler, or let the compiler do it's job....
 
Now, you don't show the definitions you have used for the registers, so it is impossible to know whether the code should work. 
 
Then, you are writing  fixed value into the OSCTUNE register. This is basically a 'no no'. It means your code will only give the correct timings on the one chip, this tuning value was from. Again, let the compiler (and the chip itself), do it's job, and read the tuning value from the configuration memory, and load it for you.
 
Other problems. You are manually controlling TRIS, but do not show fast_io being selected. Without this, the compiler will be controlling the TRIS, and probably doing a better job than you....
 
Let the compiler do it's job.
 
Something like:
 
 	  | Code: | 	 		  
 
#include <18F1320.h>
 
#device adc=8
 
#FUSES NOWDT, INTRC_IO, NOFCMEN, BROWNOUT, BORV27, PUT, NOCPD, STVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTC, IESO, NOEBTR, NOEBTRB, MCLR, NOPROTECT, NOCPB, NOWRTB
 
#use delay(clock=4000000)
 
#use rs232(baud=9600,parity=N,xmit=PIN_B1,rcv=PIN_B4,bits=8,errors)
 
//Change to suit your required baud rate.
 
 
int DATABYTE;
 
char data;
 
 
void main()
 
{
 
 
   setup_oscillator(OSC_4MHZ|OSC_INTRC);
 
   //Most of this is not needed
 
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
 
   setup_adc(ADC_OFF);
 
   setup_timer_0(RTCC_INTERNAL);
 
   setup_timer_1(T1_DISABLED);
 
   setup_timer_2(T2_DISABLED,0,1);
 
   while (TRUE) {
 
      DATABYTE=getc();
 
      //You will only arrive here, when a character is received.
 
      //The error checking is automatically done for you by selecting 
 
      //'ERRORS'. The compiler automatically loads OSCTUNE from the
 
      //stored value, etc. etc....
 
 
   }
 
}
 
 | 	  
 
 
Best Wishes | 
			 
		  | 
	 
	
		  | 
	 
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		 | 
	 
	
		  | 
	 
	
		
			Guest
 
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Mon Apr 13, 2009 4:49 pm     | 
				     | 
			 
			
				
  | 
			 
			
				OSCCON=0xEF; //freq to 4mhz
 
baudUSART(BAUD_IDLE_CLK_HIGH & BAUD_8_BIT_RATE& BAUD_WAKEUP_OFF& BAUD_AUTO_OFF); // set baud rate to clock to read high,8bit baud rate,no wake up, no autobaud
 
 
PORTB=0x00; //dont really know why i put this here lol
 
 
OpenTimer0 (T0_8BIT & T0_SOURCE_INT & T0_PS_1_1 );// OPEN TIMERO
 
CloseTimer1 ();//CLOSE TIMER1
 
CloseTimer2 ();//CLOSE TIMER1
 
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH,25);//OPEN USART CONTINUOUS RX AT 9600 BAUD
 
while(1){
 
  DATABYTE=getcUSART();  //READ FROM RCREG AND STORE IN DATABYTE
 
 
I TRYED TO GO OFF OF WHAT HAD BEEN GIVEN ME BEFORE, AND TRY TO USE MORE LIBRARY THEN ASSEMBLY BUT I AM STILL HAVING SOME PROBLEMS | 
			 
		  | 
	 
	
		  | 
	 
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Mon Apr 13, 2009 4:58 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Do you understand what the C18 compiler is ?   Your code is written
 
for that compiler.   It won't work with the CCS compiler.   This forum
 
is about the CCS compiler.     You're on the wrong forum.  
 
 
This is the correct forum for you:
 
http://www.microchip.com/forums/tt.aspx?forumid=3 | 
			 
		  | 
	 
	
		  | 
	 
	
		 | 
	 
 
  
	 
	    
	   | 
	
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
  
		 |