View Single Post
Old 05-12-2009, 02:00 PM   #63
harakiri
Đệ tử 1 túi
 
Tham gia ngày: Sep 2007
Bài gửi: 12
:
Code:
#include <p33FJ256GP710.h>
#include <stdlib.h> 
_FOSCSEL(FNOSC_FRC);			
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF  & POSCMD_NONE);  

#define TRUE	1
#define FALSE	0
unsigned char S2Flag, S3Flag, S4Flag, S5Flag;

unsigned int on_off ,time_out,j,t,i,x1,k;
unsigned char input[50];
unsigned char getc();
void putc(unsigned char value);
unsigned char* GetWord(unsigned char a[], int size, int index);
/***********************************************************/
void __attribute__((__interrupt__)) _U2RXInterrupt(void)
{	
input[0]=getc();
j=0;
if(input[0]=='T'||input[0]=='S')
	{
		while(input[j]!='r')
		{
			j++;
			input[j]=getc();
		
		}
		
		U2STAbits.OERR = 0;
        IFS1bits.U2RXIF = 0;    //Clear the UART1 transmitter interrupt flag
		on_off=1;	
	}	        	
}
/************************************************************/
void __attribute__((__interrupt__)) _U1RXInterrupt(void)
{	
input[0]=getc();
j=0;
if(input[0]=='T'||input[0]=='S')
	{
		while(input[j]!='r')
		{
			j++;
			input[j]=getc();
		
		}
		
		U1STAbits.OERR = 0;
        IFS0bits.U1RXIF = 0;    //Clear the UART1 transmitter interrupt flag
		on_off=1;	
	}	        	
}
/**************************************************************/
void __attribute__ ((interrupt, no_auto_psv)) _U2TXInterrupt(void) {
	IFS1bits.U2TXIF = 0;
	
}
/**************************************************************/
void __attribute__ ((interrupt, no_auto_psv)) _U1TXInterrupt(void) {
	IFS0bits.U1TXIF = 0;
}
/***************************************************************/
void InitClock() {
	PLLFBD = 38;	// M = 40
	CLKDIVbits.PLLPOST = 0;	// N1 = 2
	CLKDIVbits.PLLPRE = 0;	// N2 = 2
	OSCTUN = 0;//khong su dung oscillator tuning register
	RCONbits.SWDTEN = 0; // disabe watch dog timer

// Clock switch to incorporate PLL
	__builtin_write_OSCCONH(0x01);		// Initiate Clock Switch to FRC with PLL (NOSC=0b001)
										// luu y : doc thanh ghi OSCCON de thay
	__builtin_write_OSCCONL(0x01);		// Start clock switching
										//ghi lan luot vao 8 bit cao va thap cua OSCCON
	while (OSCCONbits.COSC != 0b001);	// Wait for Clock switch to occur	

	while(OSCCONbits.LOCK != 1) {};		//pll lock status bit
}
/****************************************************************/
void Init_UART1 (void)
{	
	int i;
	U1MODE = 0x0000;        //Clear UART1 registers
    U1STA = 0x0000;

	U1BRG=20;			//set baud rate

	U1MODEbits.PDSEL = 0;	//8bits, no parity
	U1MODEbits.STSEL = 0; 	//1stop bit
	U1MODEbits.ABAUD = 0; 	//No autobaud
	U1MODEbits.BRGH = 0;	//Low speed

	U1STAbits.UTXISEL0 = 0; 
	U1STAbits.UTXISEL1 = 1;
	U1STAbits.URXISEL = 0;

	IEC0bits.U1TXIE = 1; // Enable UART Tx interrupt
	IEC0bits.U1RXIE = 1;
	U1MODEbits.UARTEN = 1; // Enable UART
	IFS0bits.U1RXIF = 0;
	IFS0bits.U1TXIF = 0;  
	U1STAbits.UTXEN = 1; // Enable UART Tx
	/* wait at least 104 usec (1/9600) before sending first char*/ 
	for(i = 0; i < 4160; i++)
	{
		 Nop();
	}

}
/**********************************************************************/
void Init_UART2 (void)
{	
	int i;
	U2MODE = 0x0000;        //Clear UART1 registers
    U2STA = 0x0000;

	U2BRG=20;			//set baud rate

	U2MODEbits.PDSEL = 0;	//8bits, no parity
	U2MODEbits.STSEL = 0; 	//1stop bit
	U2MODEbits.ABAUD = 0; 	//No autobaud
	U2MODEbits.BRGH = 0;	//Low speed

	U2STAbits.UTXISEL0 = 0; 
	U2STAbits.UTXISEL1 = 1;
	U2STAbits.URXISEL = 0;

	IEC1bits.U2TXIE = 1; // Enable UART Tx interrupt
	IEC1bits.U2RXIE = 1;
	U2MODEbits.UARTEN = 1; // Enable UART
	IFS1bits.U2RXIF = 0;
	IFS1bits.U2TXIF = 0;  
	U2STAbits.UTXEN = 1; // Enable UART Tx
	/* wait at least 104 usec (1/9600) before sending first char*/ 
	for(i = 0; i < 4160; i++)
	{
		 Nop();
	}

}
/*****************************************************************************/
void InitPorts() {
	//ODCFbits.ODCF3 = 1;
	//ODCFbits.ODCF5 = 1;
	// S2 (portB Pin 3, chosen as trigger for sending ... to UART)
	// S3 (portB Pin 2, chosen as trigger for sending ... to UART)
	// S4 (portB Pin 1, chosen as trigger for sending ... to UART)
	// S5 (portB Pin 0, chosen as trigger for sending ... to UART)
	AD1PCFGLbits.PCFG0 = 1;      // This is important.  RB0 is muxxed with AN0,
								 // So we need to config the pin as DIGITAL
	AD1PCFGLbits.PCFG1 = 1;
	AD1PCFGLbits.PCFG2 = 1;  
	AD1PCFGLbits.PCFG3 = 1;  
	AD2PCFGLbits.PCFG0 = 1;      // This is important.  RB0 is muxxed with AN0,
								 // So we need to config the pin as DIGITAL
	AD2PCFGLbits.PCFG1 = 1;
	AD2PCFGLbits.PCFG2 = 1;  
	AD2PCFGLbits.PCFG3 = 1;  
	TRISB = 0x000F; //B0&B1&B2&B3 is input  

	S2Flag = S3Flag = S4Flag = S5Flag = 0;	// Some Debounce Flags
}
/*******************************************************************************/


/*************************************************************************/

int main(void) {
	InitPorts();
	InitClock();	// This is the PLL settings
	Init_TMR1();
	Init_UART1();	// Initialize UART1 for 9600,8,N,1 TX/RX
	Init_UART2();	// Initialize UART1 for 9600,8,N,1 TX/RX


	while (1)               
	        {	
				
	          	if (on_off == 1)      
	            {  
				 on_off = 0; 
				// Chuong trinh xuat ra thanh phan thu 1 cua chuoi
					int index = 1; 
					int i=0;
				    int j=0; 
				    int wordIndex = 0;
					unsigned char a[5];
				    while(i<50 || a[i] != 'r')
				     {
				   	  if(input[i] == ' ')
				      {
				      	wordIndex++;
				         i++;
				      }
				      if(wordIndex == index)
				      {
				   		 a[j] = input[i];
				         j++;
				      }
				      i++;
				     }
					   a[j] = ' ';

					i=0;
					while (a[i] != ' ')
					{ 
						int x = atoi(a);
						delay_ms(10);
						x1 = x;
						//putc(a[i]);
						i++;
						
					}
		// Chuong trinh xuat ra thanh phan thu 7 cua chuoi
					int index1 = 7; 
					int i1=0;
				    int j1=0; 
				    int wordIndex1 = 0;
					unsigned char a1[5];
				    while(i1<50 || a1[i1] != 'r')
				     {
				   	  if(input[i1] == ' ')
				      {
				      	wordIndex1++;
				         i1++;
				      }
				      if(wordIndex1 == index1)
				      {
				   		 a1[j1] = input[i1];
				         j1++;
				      }
				      i1++;
				     }
					   a1[j1] = ' ';

					i1=0;
					while (a1[i1] != ' ')
					{ 
						int y = atoi(a1);
						delay_ms(100);
						delay_ms(100);
						delay_ms(100);
						t=y;
						//putc(a1[i1]);
						i1++;
						
					}
					k= t/2 + x1*2;
					putc(k); 
					
				}    
			}

}

 /*******************************************************************************************
//Function: void Init_TMR1(void)
//Input:	None
//Output:	Setup Parameter to Timer1 Operate
//		TMR1 use for delay
*******************************************************************************************/
void Init_TMR1(void)
{
	T1CONbits.TON=0;		//Stop Timer1
	T1CONbits.TGATE=0;		//Disable Gated Timer mode
	T1CONbits.TCKPS=0;		//Select Prescale 1:1
	T1CONbits.TCS=0;
	_T1IF=0;
	_T1IE=1;
	TMR1=0;
}
/*******************************************************************************************
//Function: void delay_us(unsigned int n)
//Input:	n: n us to delay
//Output:	Delay n us
*******************************************************************************************/
void delay_us(unsigned int n)
{
	PR1=n*40;
	time_out=0;
	T1CONbits.TON=1;
	while(time_out==0);
	T1CONbits.TON = 0;
	TMR1=0;
}
/*******************************************************************************************
//Function: void delay_ms(unsigned int n)
//Input:	n: n ms to delay
//Output:	Delay n ms
*******************************************************************************************/
void delay_ms(unsigned int n)
{
	unsigned int i;
	i=0;
	while(i<=n)
	{ 
		delay_us(1000);
		i++;	
	}
}
void _ISR _T1Interrupt(void) 
{
	_T1IF = 0;			//Clear Timer1 flag
	time_out = 1;
}

/*************************************************************************/

void putc(unsigned char value)
{
  while (U2STAbits.UTXBF);
	U2TXREG=value;
}

/****************************************************************************/

unsigned char getc()
{
	
		while (!U2STAbits.URXDA);
			
		return U2RXREG;
}


Đây là đoạn chương trình em định dung 2 module uart một lúc,nhưng khi chạy thử,gửi từ uart2 thì có lúc gửi, lúc không, em đoán là hàm ngat có vấn đề. Mong mọi người xem giúp

thay đổi nội dung bởi: namqn, 05-12-2009 lúc 02:54 PM. Lý do: định dạng code
harakiri vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn