lpc23xx uart karakter hatası

Başlatan armsistem, 05 Aralık 2012, 20:46:42

armsistem

   Arkadaşlar merhaba , lpc2368 ile sim300 haberleştiriyorum. Fakat bazen gönderdiğim komut satırında gelişi güzel hatalar oluyor. Program aşağıda docklight(hyperterminal) veriyi gönderiyorum uart0'a , alıyor aynen uart1 'e gönderiyor(sim300) ordan da geri dönüş sağlıyor ok veya Error diye ,ama bazen TCP yerine ADP bazen www yerine wwt hiç olmuyacak yerde farklı karakter çıkıyor.

AT+CIPSTART="TCP","www.armsistem.somee.com","8080"



Baudrate ayarından olduğunu düşünüyorum fakat çözemedim.Aşağıda ki sitede excel dosyası var baudrate ayarı yapmak için 'Best available fit for standard UART' ayarlarını yaptım fakat 'Best available fit for enhanced UART' ayarlarını yapmayı beceremedim.      

http://www.google.com.tr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&ved=0CDsQFjAC&url=http%3A%2F%2Fics.nxp.com%2Fsupport%2Fdocuments%2Fmicrocontrollers%2Fxls%2Flpc2000.uart.baudrate.calculator.xls&ei=85G_UKfsEM7a4QS7voCQCw&usg=AFQjCNEMqiXyyHhrNBmxxsBfk1J_XtUJpA



- C ve değişkenler konusunda zayıfım aşağıdaki kodu inceleyen ustalar hatalarımı veya eksikliklerimi yazabilirmi.
- Aşağıdaki program çalışıyor bazen sapıtıyor 16 mhz kristal var FCCLK 48 mhz,  uart hızı 48 mhz /24 mhz /12 mhz her şekilde denedim baudrate ayarlarını yaptım yine arada bir sapıyıyor ,3 kere doğru gönderirsem 1 kere sapıtıyor.




# include "lpc23xx.h"

#define IER_RBR          0x01
#define IER_THRE     0x02
#define IER_RLS          0x04

#define IIR_PEND     0x01
#define IIR_RLS          0x03
#define IIR_RDA          0x02
#define IIR_CTI          0x06
#define IIR_THRE     0x01

#define LSR_RDR          0x01
#define LSR_OE          0x02
#define LSR_PE          0x04
#define LSR_FE          0x08
#define LSR_BI          0x10
#define LSR_THRE     0x20
#define LSR_TEMT     0x40
#define LSR_RXFE     0x80

#define BUFSIZE          0x40
#define SYS32Mode          0x1F
#define IRQ32Mode          0x12
#define FIQ32Mode          0x11
#define I_Bit               0x80
#define F_Bit               0x40
#define IENABLE __asm { MRS sysreg, SPSR; MSR CPSR_c, #SYS32Mode }
#define IDISABLE __asm { MSR CPSR_c, #(IRQ32Mode|I_Bit); MSR SPSR_cxsf, sysreg }
typedef unsigned char  BYTE;
typedef unsigned short WORD;
typedef unsigned long  DWORD;
typedef unsigned int   BOOL;
static DWORD sysreg;          /* used as LR register */
volatile unsigned int  UART0Status, UART1Status, UART3Status;
volatile unsigned char  UART0CountOku,UART1CountOku,UART3CountOku;
volatile unsigned char UART0TxEmpty = 1, UART1TxEmpty = 1, UART3TxEmpty=1;
volatile unsigned char UART0Buffer[BUFSIZE], UART1Buffer[BUFSIZE], UART3Buffer[BUFSIZE];
volatile unsigned char UART0Count = 0, UART1Count = 0, UART3Count = 0;

int x=0;

     char *sonuc;


void karakter_gonder_u1 (unsigned char ch){while (!(U1LSR & 0x20));U1THR=ch;}
void string_gonder_u1(const char *ch){while(*ch)karakter_gonder_u1(*ch++);}
void karakter_gonder_u0 (unsigned char ch){while (!(U0LSR & 0x20));U0THR=ch;}
void string_gonder_u0(const char *ch){while(*ch)karakter_gonder_u0(*ch++);}

    
void InitUART0(void)
{ PINSEL0 = 0x40000050;   
    PINSEL1 = 0x00000001;  
    IODIR1 = 0x00000700;   
    FIO2DIR = 0x00002010;
IOSET1 = 0x00000700;   
    FIO2SET = 0x00002010;
    
    
    

  //( ( 12000000 / 16 ) / 19200 );     /*baud rate */
  

     //U0DLM = ( ( 12000000 / 16 ) / 19200 ); / 256;                                  
  //  U0DLL = ( ( 12000000 / 16 ) / 19200 ); % 256;
    
U0FCR = 0x07;
U0LCR = 0x83;
    
//U0DLL = 39;  //esas
//U0DLM = 0x00;
 U0DLM = ( ( 48000000 / 16 ) / 19200 ) / 256;                                  
 U0DLL = ( ( 48000000 / 16 ) / 19200 ) % 256;
U0LCR = 0x03;
U0IER = 0x07;
U1FCR = 0x07;
U1LCR = 0x83;
     U1DLM = ( ( 48000000 / 16 ) / 19200 )/ 256;                                  
    U1DLL = ( ( 48000000 / 16 ) / 19200 ) % 256;
//U1DLL = 39;
//U1DLM = 0x00;

U1LCR = 0x03;
U1IER = 0x07;    
    
}


__irq void UART0_IRQHandler(void) {
BYTE IIRValue, LSRValue;
  BYTE Dummy = Dummy;

     // IENABLE;                    /* handles nested interrupt */    


     IIRValue = U0IIR;
   
  IIRValue >>= 1;               /* skip pending bit in IIR */
  IIRValue &= 0x07;               /* check bit 1~3, interrupt identification */
  if ( IIRValue == IIR_RLS )          /* Receive Line Status */
  {
     LSRValue = U0LSR;
     /* Receive Line Status */
     if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
     {
       /* There are errors or break interrupt */
       /* Read LSR will clear the interrupt */
       UART0Status = LSRValue;
       Dummy = U0RBR;          /* Dummy read on RX to clear
                                   interrupt, then bail out */
    
//     IDISABLE;
       VICVectAddr = 0;          /* Acknowledge Interrupt */

          return;
     }
     if ( LSRValue & LSR_RDR )     /* Receive Data Ready */              
     {
       /* If no error on RLS, normal ready, save into the data buffer. */
       /* Note: read RBR will clear the interrupt */
       UART0Buffer[UART0Count] = U0RBR;
       UART0Count++;
       if ( UART0Count == BUFSIZE )
       {
          UART0Count = 0;          /* buffer overflow */
       }    
     }
  }
  else if ( IIRValue == IIR_RDA )     /* Receive Data Available */
  {
     /* Receive Data Available */
     UART0Buffer[UART0Count] = U0RBR;
     UART0Count++;
     if ( UART0Count == BUFSIZE )
     {
       UART0Count = 0;          /* buffer overflow */
     }
  }
  else if ( IIRValue == IIR_CTI )     /* Character timeout indicator */
  {
     /* Character Time-out indicator */
     UART0Status |= 0x100;          /* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE )     /* THRE, transmit holding register empty */
  {
     /* THRE interrupt */
     LSRValue = U0LSR;          /* Check status in the LSR to see if
                                             valid data in U0THR or not */
     if ( LSRValue & LSR_THRE )
     {
       UART0TxEmpty = 1;
     }
     else
     {
       UART0TxEmpty = 0;
     }
  }
    
     // IDISABLE;
  VICVectAddr = 0;    
}

void UART1_IRQHandler (void) __irq
{
  BYTE IIRValue, LSRValue;
  BYTE Dummy = Dummy;
//     IENABLE;                    /* handles nested interrupt */    

  IIRValue = U1IIR;
   
  IIRValue >>= 1;               /* skip pending bit in IIR */
  IIRValue &= 0x07;               /* check bit 1~3, interrupt identification */
  if ( IIRValue == IIR_RLS )          /* Receive Line Status */
  {
     LSRValue = U1LSR;
     /* Receive Line Status */
     if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
     {
       /* There are errors or break interrupt */
       /* Read LSR will clear the interrupt */
       UART1Status = LSRValue;
       Dummy = U1RBR;          /* Dummy read on RX to clear
                                   interrupt, then bail out */
     //     IDISABLE;
  VICVectAddr = 0;         
          return;
     }
     if ( LSRValue & LSR_RDR )     /* Receive Data Ready */              
     {
       /* If no error on RLS, normal ready, save into the data buffer. */
       /* Note: read RBR will clear the interrupt */
       UART1Buffer[UART1Count] = U1RBR;
       UART1Count++;
       if ( UART1Count == BUFSIZE )
       {
          UART1Count = 0;          /* buffer overflow */
       }    
     }
  }
  else if ( IIRValue == IIR_RDA )     /* Receive Data Available */
  {
     /* Receive Data Available */
     UART1Buffer[UART1Count] = U1RBR;
     UART1Count++;
     if ( UART1Count == BUFSIZE )
     {
       UART1Count = 0;          /* buffer overflow */
     }
  }
  else if ( IIRValue == IIR_CTI )     /* Character timeout indicator */
  {
     /* Character Time-out indicator */
     UART1Status |= 0x100;          /* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE )     /* THRE, transmit holding register empty */
  {
     /* THRE interrupt */
     LSRValue = U1LSR;          /* Check status in the LSR to see if
                                             valid data in U0THR or not */
     if ( LSRValue & LSR_THRE )
     {
       UART1TxEmpty = 1;
     }
     else
     {
       UART1TxEmpty = 0;
     }
  }
     //      IDISABLE;
  VICVectAddr = 0;    
}



void Delay(unsigned int time)
{
while(time--);
}

unsigned int Usart_Oku_u1 (unsigned int *veri1)
{
     unsigned int say=0;
     while(UART1Count==UART1CountOku)
          {
     say++;
     if(say==5000)return 0;
}
say=0;
     *veri1=UART1Buffer[UART1CountOku];    
     if(UART1CountOku == BUFSIZE)UART1CountOku=0;    
     UART1CountOku++;
     return 1;
}
unsigned int Usart_Oku_u0 (unsigned int *veri)
{
     unsigned int say=0;
     while(UART0Count==UART0CountOku)
          {
     say++;
     if(say==5000)return 0;
}    
say=0;
     *veri=UART0Buffer[UART0CountOku];    
     if(UART0CountOku == BUFSIZE)UART0CountOku=0;    
     UART0CountOku++;
     return 1;
}




void DelayMs (unsigned long int t)
{
T0IR  = 1;          // Interrupt flagi temizleniyor
T0CCR = 0;          // Timer modu
T0PR  = 0;             // Prescaler oranı 0
T0MR0 = (((12))*t*10);          // 1us için 12-1=11 yükleniyor
     T0MCR     = 3;          // MR0 TC ile esitlendiginde kesme olusacak ve TC degeri resetlenecek
    
T0TC  = 0;          // Timer calistiriliyor
T0TCR = 1;          // Timer calistiriliyor

     while(!(T0IR==1));
T0TCR = 0;          // Timer durduruluyor


}
    
int main(void)
{
     unsigned int veri;      unsigned int veri1;  char buffer [50];char buffer1 [50];
    
  VICVectAddr6 = (unsigned long) UART0_IRQHandler;           /* set interrupt vector 6 */
   VICVectPriority6 = 15 ;           /* default priority is 15 (lowest), can be set between 0-15 */
    VICIntEnable |= (1UL<<6);           /* Enable UART0 Interrupt */
   VICVectAddr7 = (unsigned long) UART1_IRQHandler;           /* set interrupt vector 7 */
  VICVectPriority7 = 15 ;           /* default priority is 15 (lowest), can be set between 0-15 */
VICIntEnable |= (1UL<<7);           /* Enable UART1 Interrupt */    
InitUART0();



     string_gonder_u0("HABERLESME HAZIR");            karakter_gonder_u1(0x0d);
     DelayMs(100);DelayMs(100);DelayMs(100);DelayMs(100);
               string_gonder_u0("HABERLESME HAZIR");            karakter_gonder_u1(0x0d);
                    DelayMs(100);DelayMs(100);DelayMs(100);DelayMs(100);
while(1)
          {    
 if (!Usart_Oku_u0 (&veri) == 1) { }
else {sprintf (buffer, "%c",veri);
string_gonder_u1(buffer); 
}
              
if (!Usart_Oku_u1 (&veri1) == 1) { }
else {sprintf (buffer1, "%c",veri1);
string_gonder_u0(buffer1);
}}

}

armsistem

#1
Arkadaşlar merhaba , uart konusundan artık beynim bulandı. Gerbay hocamın gönderdiği kodları inceledim UART veri gönderiyorum problem yok fakat alma konusunda sıkıntı var.

Aşağıda ki satırda docklightan ne geliyorsa geri göndermesi gerekmiyor mu?
while (com_getchar0() >= 0){com_putchar0 (com_getchar());}


12.12.2012 15:03:28.76 [TX] - deneme01234567890
12.12.2012 15:03:28.79 [RX] - eee13579ÿ


ve bir süre sonra donuyor. Aşağıdaki satır problemsiz çalışıyor.

putchar1(getchar1());


Aşağıdaki koddaki mantık nedir , hangi durumda char 'ı int'e çevirmem gerekiyor.

int com_getchar (void) 
{	
	int c;
	c=com_getchar0();  // char
	return 	c;
}



Baudrate ayarlarımda problem olsa aşağıdaki düzgün çalışmaz diye düşünüyorum.
    for (i = 0; i < 500000U; i++);
putStr0("deneme\r\n");


deneme<CR><LF>


#include "lpc23xx.h"
#include <stdio.h>

typedef unsigned char U8;
typedef unsigned int  U32;

typedef unsigned char  BYTE;
typedef unsigned short WORD;
typedef unsigned long  DWORD;
typedef unsigned int   BOOL;
#define TBUF_SIZE   256	     /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/
#define RBUF_SIZE   256      /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
#if TBUF_SIZE < 2
#error TBUF_SIZE is too small.  It must be larger than 1.
#elif ((TBUF_SIZE & (TBUF_SIZE-1)) != 0)
#error TBUF_SIZE must be a power of 2.
#endif

#if RBUF_SIZE < 2
#error RBUF_SIZE is too small.  It must be larger than 1.
#elif ((RBUF_SIZE & (RBUF_SIZE-1)) != 0)
#error RBUF_SIZE must be a power of 2.
#endif



#define __FALSE 0
#define __TRUE  (!__FALSE)

struct buf_st {
  unsigned int in;          /* Next In Index */
  unsigned int out;         /* Next Out Index */
  char buf [50];     /* Buffer */
};

static struct buf_st rbuf = { 0, 0, };
#define SIO_RBUFLEN ((unsigned short)(rbuf.in - rbuf.out))

static struct buf_st tbuf = { 0, 0, };
#define SIO_TBUFLEN ((unsigned short)(tbuf.in - tbuf.out))

static unsigned int tx_restart = 1;   /* NZ if TX restart is required */

static struct buf_st rbuf;
static struct buf_st tbuf;
static unsigned tx_active;






void InitUART0(void)
{     DWORD Fdiv;
	
	  rbuf.in   = 0;
  rbuf.out  = 0;
  tbuf.in   = 0;
  tbuf.out  = 0;
  tx_active = __FALSE;
	
	
	PINSEL0 = 0x40000050;   
    PINSEL1 = 0x00000001;  
    IODIR1 = 0x00000700;   
    FIO2DIR = 0x00002010;
   Fdiv = ( 12000000 / 16 ) / 19200 ;	/*baud rate */
	  U1DLM = Fdiv / 256;							
   U1DLL = Fdiv % 256;

	
	

U0FCR = 0x07;
U0LCR = 0x80;
  U0DLM = Fdiv / 256;							
   U0DLL = Fdiv % 256;
U0LCR = 0x03;
//U0FDR = (14 << 4) + 4;	
U0IER = 0x07;

U1FCR = 0x07;
U1LCR = 0x80;
  U1DLM = Fdiv / 256;							
   U1DLL = Fdiv % 256;
U1LCR = 0x03;
//U1FDR = (14 << 4) + 4;		
U1IER = 0x07;     
    
}


__irq void UART0_IRQHandler(void) {  volatile char dummy;
  volatile char IIR;
  struct buf_st *p;

  /*------------------------------------------------
  Repeat while there is at least one interrupt source.
  ------------------------------------------------*/
  while (((IIR = U0IIR) & 0x01) == 0) {
     switch (IIR & 0x0E) {
       case 0x06: /* Receive Line Status */
         dummy = U0LSR;  /* Just clear the interrupt source */
       	VICVectAddr = 0;		/* Acknowledge Interrupt */
	  
			 break;

       case 0x04: /* Receive Data Available */
      


			 case 0x0C: /* Character Time-Out */
         p = &rbuf;

         if (((p->in - p->out) & ~(RBUF_SIZE-1)) == 0) {
           p->buf [p->in & (RBUF_SIZE-1)] = U0RBR;
           p->in++;
         }
         break;

       case 0x02: /* THRE Interrupt */
         p = &tbuf;

         if (p->in != p->out) {
           U0THR = p->buf [p->out & (TBUF_SIZE-1)];
           p->out++;
           tx_restart = 0;
         }
         else {
           tx_restart = 1;
         }
         break;

       case 0x00: /* Modem Interrupt */
       //  dummy = U0MSR;  /* Just clear the interrupt source */
         break;

       default:
         break;
     }
  }

  VICVectAddr = 0; /* Acknowledge Interrupt */
}



void Delay(unsigned int time)
{
while(time--);
}





    
int com_putchar0 (int c) {
  struct buf_st *p = &tbuf;

  /*------------------------------------------------
  If the buffer is full, return an error value.
  ------------------------------------------------*/
  if (SIO_TBUFLEN >= TBUF_SIZE)
    return (-1);

  /*------------------------------------------------
  Add the data to the transmit buffer.  If the
  transmit interrupt is disabled, then enable it.
  ------------------------------------------------*/
  if (tx_restart)	{
    tx_restart = 0;
    U0THR = c;
  }
  else {
    p->buf [p->in & (TBUF_SIZE - 1)] = c;
    p->in++;
  }

  return (0);
}

int getchar1 (void) /* Read character from Serial Port */
  {while (!(U0LSR & 0x01));
     return (U0RBR);
  }
	
	int putchar1 (int ch)  /* Write character to Serial Port */
  {if (ch == '\n')
     {while (!(U0LSR & 0x20));
      U0THR = 0x0D; /* output CR */
     }
   while (!(U0LSR & 0x20));
      return (U0THR = ch);
  }











/*--------------------------- com_getchar0 -----------------------------------*/

int com_getchar0 (void) {
  struct buf_st *p = &rbuf;

  if (SIO_RBUFLEN == 0)
    return (-1);

  return (p->buf [(p->out++) & (RBUF_SIZE - 1)]);
}




/*--------------------------- putStr0--------------------------------------*/

void putStr0(const char *str)
{
    for ( ; str && *str; str++)
      com_putchar0(*str);
}


BOOL com_tx_active (void) {
  /* Return status Transmitter active/not active.         */
  /* When transmit buffer is empty, 'tx_active' is FALSE. */
  return (tx_active);
}

int com_getchar (void) 
{	
	int c;
	c=com_getchar0();
	return 	c;
}


int main(void)
{

  volatile unsigned  i = 0;
  volatile unsigned  k = 0; 
    
 VICVectAddr6 = (unsigned long) UART0_IRQHandler;           /* set interrupt vector 6 */
   VICVectPriority6 = 0 ;           /* default priority is 15 (lowest), can be set between 0-15 */
  VICIntEnable |= (1UL<<6);           /* Enable UART0 Interrupt */
  
InitUART0();
    for (i = 0; i < 500000U; i++);
putStr0("deneme\r\n");
while(1)
          {    

    for (i = 0; i < 500000U; i++);
						
while (com_getchar0() >= 0){com_putchar0 (com_getchar());}

//putchar1(getchar1());
                   
              

}
}




armsistem

#2
Evet evet haklısınız. Okumak ve okuyup anlamakta.

mesaj birleştirme:: 12 Aralık 2012, 17:13:02

Hocam dediğin yerleri 256 olarak değişitrdiğimde

12.12.2012 17:11:48.80 [TX] - 01234567890123456789012345678901
12.12.2012 17:11:48.86 [RX] - 1357913579135791