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.

PIC18F1220 Program Runs in simulation but not once chip is programmed?

Status
Not open for further replies.

B9RRT

Newbie level 4
Joined
Apr 28, 2012
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,338
Hi i have the following program which is supposed to start and stop a timer when a sensor pair is activated and do this again for another pair. I have used the stimulus in MPLAB v8.84 and everything works exactly as it should.
I programmed a chip and using NI Elvis MyDaq to provide 5v to simulate the stimulus. I have also connected this to a Comm port to send reset function and Ready. This is what appears not to be working i'm using 232Analyzer (RS232/485/422/TTL Analyzer) the free version to provide the reset and ready stimuli dec 94 and 255 respectively.

I'm at a loss as to why this wont work? The output from the pic works fine and i'm getting the required data output on the comm port when i disable the need for a ready signal. Which leads me to think its the Comm software's ability to send the data....? but i have tried multiple and none work as desired.

Code:
#include "p18f1220.h"
#include "timers.h"
#include "usart.h"
#include "delays.h"

void Send_To_UART(void);		//Prototypes
void timer_0 (void);
void timer_1 (void);
void tmr0low_interupt(void);
void enable_global_interrupts(void);
void UART_putc(void);
void WriteUSART(char Msb0);
char ReadUSART(void);
char ReadPortB(void);

void timer0 (void);
void timer1 (void);

unsigned int portb=0;
unsigned int e= 0;
unsigned int d= 0;						//Counter for Transmit
unsigned int a= 0;						
unsigned int b= 0; 
unsigned int s= 0;
unsigned int myVar0=0;
unsigned int myVar1=0;
unsigned int count=0;
unsigned int count1=0;
unsigned int int1 =0;
unsigned int int2 =0;
unsigned char ready = 0;
unsigned char sreset = 0;

//Data to Transmit
unsigned char Msb0 =0;
unsigned char Msb1=0;
unsigned char Lsb0 =0;
unsigned char Lsb1=0;
unsigned char cMsb0=0;
unsigned char cMsb1=0;
unsigned char cLsb0=0;
unsigned char cLsb1;


#pragma config OSC = HSPLL

						//Register Setup

void enable_global_interrupts(void){
	RCONbits.IPEN = 1;						//enable interrupt priority
	PIE1 = 0x20;							//TX and RX interupt enabled
	IPR1bits.RCIP= 1;						//RX high Pririty
	INTCON = 0xc8;							//Global, low priority and RB interupts enabled
	INTCON2 = 0x00;							//RB interupt high priority
	BAUDCTLbits.BRG16 = 1;
	LVDCONbits.LVDEN = 0;
}


								
void init_tris_regs(void){
	ADCON1 = 0x7f;
	ADCON0bits.ADON = 0;
	TRISA = 0xA3;					// b'10100011' RA0 , RA1 Inputs
	TRISB = 0xD0;					// b'11010000' RB6 , RB7 inputs
	PORTB = 0x00;
}


void init(void){								//setup timer0 
	T0CONbits.TMR0ON = 0;						// stop timer0 
	T0CONbits.T016BIT = 0;						// 16 bit timer  
	T0CONbits.T0CS = 0;	 						// internal clock source 
	T0CONbits.PSA = 0;	 						// select prescaler 
	T0CONbits.T0PS0 = 1;						// 1:1 Prescale value 
	T0CONbits.T0PS1 = 0; 
	T0CONbits.T0PS2 = 0;  
	TMR0H=0;
	TMR0L=0;    
}

void init1(void){								//setup timer1 
	T1CONbits.RD16 = 1;							// 16 bit timer
	T1CONbits.T1RUN = 0;						  
	T1CONbits.T1OSCEN = 0;	 					 
	T1CONbits.T1CKPS0 = 0;						// select prescaler				
	T1CONbits.T1CKPS1 = 0;					 	// 1:1 Prescale value 
	T1CONbits.T1SYNC = 1;
	T1CONbits.TMR1CS = 0; 
	T1CONbits.TMR1ON = 0;      					//TMR1 On/Off
	TMR1H=0;
	TMR1L=0;

}

#pragma interrupt tmr0low_isr										// Stop timer interupt routine
void tmr0low_isr(void){
	if (INTCONbits.RBIF){
		if (a!=1){
			myVar0 = ((TMR0H & 0xFFFF) << 8) | (TMR0L & 0xFFFF); 	//Read 16bit timer value
				portb=PORTB;
				  _asm
		        MOVF    PORTB,0,0								//Read PORTB to clear flag
		       NOP
		      _endasm
		        INTCONbits.RBIF = 0;
		       _asm
		       MOVF    PORTB,0,0								//Read PORTB to clear flag
		      _endasm 								
			b = ++(b);
			int1 = 1;
			}
		else if (b =1){
			myVar1 = ((TMR1H & 0xFFFF) << 8) | (TMR1L & 0xFFFF); 	//Read 16bit timer value
						  _asm
			        MOVF    PORTB,0,0
			        NOP
			        _endasm
			        INTCONbits.RBIF = 0;
			        _asm
			        MOVF    PORTB,0,0
			        _endasm									//Clear interupt flag
 			a = --(a);
			int2 = ++(int2);
			}
		}
	}


#pragma interrupt USART_isr 
void USART_isr (void){
	if (PIR1bits.RCIF==1){
 		sreset = RCREG;
		if(RCSTA&0x06) 				//check for reception error 
	     {
	      RCSTAbits.CREN=0;    		//Overrun error (can be cleared by clearing bit CREN)
	      sreset=RCREG;    			//clear Framing error 
	      RCSTAbits.CREN=1;
	   	 }
		else if(sreset == 0xff)
		{
			_asm 
	      reset
			_endasm
		}
		else if (sreset == 0x5E)
		{
		ready=sreset;
		}
	}
}	


void main(void){
	while(1){
	enable_global_interrupts();		//Set Registers
    init_tris_regs();               
 	init();							//Timer0 Timer1 setups
    init1();
	e = 0;							//Set loop counters back to 0
	d=0;

 // Configure USART
  OpenUSART( USART_TX_INT_OFF  &		//USART 9600 baud (40mhz) 16bit baud
             USART_RX_INT_ON  &
             USART_ASYNCH_MODE &
             USART_EIGHT_BIT   &
             USART_CONT_RX&
			 USART_BRGH_HIGH,
             1040 );
				

	
while (e==0){								//Loop once
	while (sreset!=0){ 						//Wait for reset interupt
 			while(s<2){						//Loop until both timers operated
	 
	        while(!PORTAbits.RA0);   				// stay here until PORTAbits.RA0==1
		        s= ++(s);							//Increment s Counter
				timer_0();                 			//Start Timer0
		 	while(!PORTAbits.RA1);  				// stay here until PORTAbits.RA1==1
		        s = ++(s);							//Increment s Counter
				timer_1();                 			// Start Timer1
				}
	     	Send_To_UART( );						//Send data to Usart
			count  =0;								//Reset Timer Overflow Counters
			count1 =0;
	 		s=0;									//Reset S counters
			sreset = 0;								//Set sreset back to ZERO
	        CloseTimer0();                  		// close modules Timer0 and USARt
	        CloseTimer1();
	      
		e = ++(e);    }								//Final loop counter so Regs and set every time
	
}
}
}

					

void timer_0 (void){													
																//Setup timer 0
	T0CONbits.TMR0ON = 1;										//Start timer0 
		while(int1!=1){   
 			while ((INTCONbits.TMR0IF == 0)==(int1==0));  		//wait for T0 to roll over 1= overflow or exit if returned from interupt
				if(int1==1){break;}								// exit loop if interupt occured
				count = ++(count);								//Increment overflow timer
				INTCONbits.TMR0IF = 0; 							//clear TF0 (overflow Flag)				
		}
	int1= 0;													//reset interupt identifier
	a = ++(a);													// Used to detect which RB pin changed
	CloseTimer0();												//Close Timer		
}
	

void timer_1 (void){											//wait for T1 to roll over 1= overflow or exit if returned from interupt
																//Setup timer 0
	T1CONbits.TMR1ON = 1;										//Start timer0 
		while(int2 != 1){  
 			while ((PIR1bits.TMR1IF == 0)==(int2==0));  		//wait for T1 INTERUPT FLAG to roll over 1= overflow
				if(int2==1){break;}								//Exit after interupt
				count1 = ++(count1);							//Increment overflow timer
				PIR1bits.TMR1IF = 0; 							//Clear TF0 (overflow Flag)					
			}
	int2= 0;													//Reset interupt identifier
	b = ++(b);													//RB pin Identifier
	CloseTimer1();												//Close timer1					
}
	


#pragma code low_vector = 0x18									//Interupt service routine RBpin change (Stops TMR)
void tmr0low_interupt(void){
 	_asm 
		goto tmr0low_isr 
	_endasm
}
#pragma code

#pragma code high_vector = 0x08									//Interupt service routine USART recieved Reset or Ready
void USART_interupt(void){
 	_asm 
		goto USART_isr 
	_endasm
}
#pragma code
					


void Send_To_UART(){								//Data sent via Usart times from timers and timer
											// overflow Counts

	Msb0 = (myVar0 / 256);
	Lsb0 = (myVar0 % 256);
	Msb1 = (myVar1 / 256);
	Lsb1 = (myVar1 % 256);
	cMsb0 =(count/256);
	cLsb0 =(count % 256);
	cMsb1 =(count1 / 256);
	cLsb1 =(count1 % 256);

while(d<=1){	
		WriteUSART((char) Lsb0);		
		while (BusyUSART()){Nop();}
		WriteUSART((char) Msb0);			
		while (BusyUSART()){Nop();}
		WriteUSART((char) Msb1);			
		while (BusyUSART()){Nop();}
		WriteUSART((char) Lsb1);			
		while (BusyUSART()){Nop();}
		WriteUSART((char) cLsb0);		
		while (BusyUSART()){Nop();}		
		WriteUSART((char) cMsb0);			
		while (BusyUSART()){Nop();}
		WriteUSART((char) cMsb1);			
		while (BusyUSART()){Nop();}
		WriteUSART((char) cLsb1);			
		while (BusyUSART()){Nop();}
		d = ++(d);

}	
}

The circuit im using is very simple with resistors on the inputs decoupling cap and RX TX pins connected directly to TX and RX pins of an FTDI MM232R which is connected to the USB. I think the problem is with the code not the circuit.

Thanks in Advance any help is appreciated.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top