Haberler:

Foruma Resim Yükleme ve Boyut Sınırlaması ( ! )  https://bit.ly/2GMFb8H

Ana Menü

Uart RS232

Başlatan ademtosun58, 17 Nisan 2017, 20:52:18

ademtosun58

Arkadaşlar İyi günler rs232 ile haberleşme yapacağım.Elimde uart.h kütüphanesi mevcut pic18f4620 kullanıyorum ama kütüphaneyi nasıl kullanacagımı bilmiyorum.Aşağıda kodu paylaştım bana yazı yazdırma ve switch case ile bir uygulama konusunda yardımcı olabilirmisiniz.
/*
 * File:   uart.c
 * Author: adem
 *
 * Created on 17 Nisan 2017 Pazartesi, 20:25
 */


#include "Includes.h"
char UART_Init(const long int baudrate)
{
   unsigned int x;
   x = (_XTAL_FREQ - baudrate*64)/(baudrate*64);
   if(x>255)
   {
      x = (_XTAL_FREQ - baudrate*16)/(baudrate*16);
      BRGH = 1;
   }
   if(x<256)
   {
     SPBRG = x;
     SYNC = 0;
     SPEN = 1;
          TRISC7 = 1;
          TRISC6 = 1;
          CREN = 1;
          TXEN = 1;
     return 1;
   }
   return 0;
}

char UART_TX_Empty()
{
  return TRMT;
}

char UART_Data_Ready()
{
   return RCIF;
}
char UART_Read()
{
 
  while(!RCIF);
  return RCREG;
}

void UART_Read_Text(char *Output, unsigned int length)
{
   unsigned int i;
   for(int i=0;i<length;i++)
      Output[i] = UART_Read();
}

void UART_Write(char data)
{
  while(!TRMT);
  TXREG = data;
}

void UART_Write_Text(char *text)
{
  int i;
  for(i=0;text[i]!='\0';i++)
     UART_Write(text[i]);
}

mehmet

Yazı yazdırma derken?
Olan olmuştur,
olacak olan da olmuştur.
Olacak bir şey yoktur.
---------------------------------------------
http://www.mehmetbilgi.net.tr

skara1214

arkadaş emeklemeden koşmak istiyor
Herkes ölür ama herkes gerçekten yaşamaz

ademtosun58

Alıntı yapılan: skara1214 - 18 Nisan 2017, 13:19:39
arkadaş emeklemeden koşmak istiyor

yok hocam derdim şu aslında bir sistemim var sıcaklık,halleffect,role  ile ilgili kontrol ve ölçümler yapıyorum ama rs232 ile bağlantı kurdugumda bu ölçümleri görmek istiyorum,Terminale normal birşey yazdırabiliyorum ama ölçülen değerleri göremiyorum.Hani emeklemeden koşmak gibi bir istegim yok açıklama kısmına yanlış aktarmışım.

switch case ile bir şey yaptım hangi değeri girersem o işlemi terminale bassın diye ama bir türlü olmadı.

skara1214

printf komutunu inceleyin o zaman
Herkes ölür ama herkes gerçekten yaşamaz

mehmet

#5
Bu işe yaramaz mı?

/*
 * File:   main.c
 * Author: mbmb
 *
 * Created on 18 Nisan 2017 Salı, 02:04
 */



// PIC18F4620 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1H
#pragma config OSC = INTIO67    // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = ON        // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bits (Brown-out Reset enabled and controlled by software (SBOREN is enabled))
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = ON         // Code Protection bit (Block 0 (000800-003FFFh) code-protected)
#pragma config CP1 = ON         // Code Protection bit (Block 1 (004000-007FFFh) code-protected)
#pragma config CP2 = ON         // Code Protection bit (Block 2 (008000-00BFFFh) code-protected)
#pragma config CP3 = ON         // Code Protection bit (Block 3 (00C000-00FFFFh) code-protected)

// CONFIG5H
#pragma config CPB = ON         // Boot Block Code Protection bit (Boot block (000000-0007FFh) code-protected)
#pragma config CPD = ON         // Data EEPROM Code Protection bit (Data EEPROM code-protected)

// CONFIG6L
#pragma config WRT0 = ON        // Write Protection bit (Block 0 (000800-003FFFh) write-protected)
#pragma config WRT1 = ON        // Write Protection bit (Block 1 (004000-007FFFh) write-protected)
#pragma config WRT2 = ON        // Write Protection bit (Block 2 (008000-00BFFFh) write-protected)
#pragma config WRT3 = ON        // Write Protection bit (Block 3 (00C000-00FFFFh) write-protected)

// CONFIG6H
#pragma config WRTC = ON        // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) write-protected)
#pragma config WRTB = ON        // Boot Block Write Protection bit (Boot Block (000000-0007FFh) write-protected)
#pragma config WRTD = ON        // Data EEPROM Write Protection bit (Data EEPROM write-protected)

// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protection bit (Block 0 (000800-003FFFh) protected from table reads executed in other blocks)
#pragma config EBTR1 = ON       // Table Read Protection bit (Block 1 (004000-007FFFh) protected from table reads executed in other blocks)
#pragma config EBTR2 = ON       // Table Read Protection bit (Block 2 (008000-00BFFFh) protected from table reads executed in other blocks)
#pragma config EBTR3 = ON       // Table Read Protection bit (Block 3 (00C000-00FFFFh) protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = ON       // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>


#define _XTAL_FREQ      8000000UL

#define UART_RX         PORTCbits.RC7
#define UART_TX         PORTCbits.RC6
#define UART_RX_IO      TRISCbits.TRISC7
#define UART_TX_IO      TRISCbits.TRISC6


unsigned char UART_Init(const unsigned long int baudrate)
{
   unsigned int x;
   
   x = (_XTAL_FREQ - baudrate * 64) / (baudrate * 64);
   
   if(x > 255)
   {
      x = (_XTAL_FREQ - baudrate * 16) / (baudrate * 16);
      BRGH = 1;
   }
   
   if(x < 256)
   {
        SPBRG = x;
        SYNC  = 0;
        SPEN  = 1;
              //TRISC7 = 1;
        //TRISC6 = 1;
        UART_RX_IO = 0b1;
        UART_TX_IO = 0b1;

        CREN = 1;
        TXEN = 1;
        return 1;
   }
   return 0;
}

unsigned char UART_TX_Empty(void)
{
  return TRMT;
}

unsigned char UART_Data_Ready(void)
{
   return RCIF;
}

unsigned char UART_Read(void)
{
  while(!RCIF);
  return RCREG;
}

void UART_Read_Text(unsigned char *Output, unsigned int length)
{
   unsigned int i;
   for(i = 0; i < length; i++)
      Output[i] = UART_Read();
}

void UART_Write(unsigned char data)
{
  while(!TRMT);
  TXREG = data;
}

void UART_Write_Text(const unsigned char *text)
{
  int i;
  for(i = 0; text[i] != '\0'; i++)
     UART_Write(text[i]);
}

void mcuInit(void)
{
    OSCCONbits.IRCF = 0b111;        //8Mhz
    
    CMCONbits.CM = 0b111;           //Komparatorler kapali
    
    ADCON0bits.ADON = 0b0;          //ADC kapali
    ADCON1bits.PCFG = 0b1111;       //Tüm pinler dijital

    //Giris cıkış işlemleri
    PORTA = 0x00;
    TRISA = 0x00;

    PORTB = 0x00;
    TRISB = 0x00;
    
    PORTC = 0x00;
    TRISC = 0x00;
    
    PORTD = 0x00;
    TRISD = 0x00;
    
    PORTE = 0x00;
    TRISE = 0x00;
}

void main(void)
{
    const unsigned char *txt0 = "\n\r";
    const unsigned char *txt1 = "Saat    ";
    const unsigned char *txt2 = "Sicaklik";
    const unsigned char *txt3 = "ADC     ";
    const unsigned char *txt4 = "...Okunan Degerler...";
    const unsigned char drc   = 176;
    
    unsigned char say = 0;
    
    
    mcuInit();
    __delay_ms(50);
    UART_Init(9600);
    
    
    while(1)
    {
        switch(say++)
        {
        case 0:
            UART_Write_Text(txt4);
            UART_Write_Text(txt0);
            break;
        case 1:
            UART_Write_Text(txt1);
            UART_Write_Text("  : ");
            UART_Write_Text("12:34:56");
            UART_Write_Text(txt0);
            break;
        case 2:
            UART_Write_Text(txt2);
            UART_Write_Text("1 : ");
            UART_Write_Text("15");
            UART_Write(drc);
            UART_Write_Text("C");
            UART_Write_Text(txt0);
            break;
        case 3:
            UART_Write_Text(txt2);
            UART_Write_Text("2 : ");
            UART_Write_Text("17");
            UART_Write(drc);
            UART_Write_Text("C");
            UART_Write_Text(txt0);
            break;
        case 4:
            UART_Write_Text(txt2);
            UART_Write_Text("3 : ");
            UART_Write_Text("23");
            UART_Write(drc);
            UART_Write_Text("C");
            UART_Write_Text(txt0);
            break;
        case 5:                
            UART_Write_Text(txt3);
            UART_Write_Text("1 : ");
            UART_Write_Text("150");
            UART_Write_Text(txt0);
            break;
        case 6:
            UART_Write_Text(txt3);
            UART_Write_Text("2 : ");
            UART_Write_Text("675");
            UART_Write_Text(txt0);
            break;
        case 7:
            UART_Write_Text(txt3);
            UART_Write_Text("3 : ");
            UART_Write_Text("1020");
            UART_Write_Text(txt0);
            break;
        case 8:
            say = 0;
            UART_Write_Text(txt0);
            break;
        default:
            break;
        }
        
        __delay_ms(200);
    }
}
Olan olmuştur,
olacak olan da olmuştur.
Olacak bir şey yoktur.
---------------------------------------------
http://www.mehmetbilgi.net.tr

ademtosun58

Alıntı yapılan: mehmet - 18 Nisan 2017, 20:21:46
Bu işe yaramaz mı?

/*
 * File:   main.c
 * Author: mbmb
 *
 * Created on 18 Nisan 2017 Salı, 02:04
 */



// PIC18F4620 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1H
#pragma config OSC = INTIO67    // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = ON        // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bits (Brown-out Reset enabled and controlled by software (SBOREN is enabled))
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = ON         // Code Protection bit (Block 0 (000800-003FFFh) code-protected)
#pragma config CP1 = ON         // Code Protection bit (Block 1 (004000-007FFFh) code-protected)
#pragma config CP2 = ON         // Code Protection bit (Block 2 (008000-00BFFFh) code-protected)
#pragma config CP3 = ON         // Code Protection bit (Block 3 (00C000-00FFFFh) code-protected)

// CONFIG5H
#pragma config CPB = ON         // Boot Block Code Protection bit (Boot block (000000-0007FFh) code-protected)
#pragma config CPD = ON         // Data EEPROM Code Protection bit (Data EEPROM code-protected)

// CONFIG6L
#pragma config WRT0 = ON        // Write Protection bit (Block 0 (000800-003FFFh) write-protected)
#pragma config WRT1 = ON        // Write Protection bit (Block 1 (004000-007FFFh) write-protected)
#pragma config WRT2 = ON        // Write Protection bit (Block 2 (008000-00BFFFh) write-protected)
#pragma config WRT3 = ON        // Write Protection bit (Block 3 (00C000-00FFFFh) write-protected)

// CONFIG6H
#pragma config WRTC = ON        // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) write-protected)
#pragma config WRTB = ON        // Boot Block Write Protection bit (Boot Block (000000-0007FFh) write-protected)
#pragma config WRTD = ON        // Data EEPROM Write Protection bit (Data EEPROM write-protected)

// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protection bit (Block 0 (000800-003FFFh) protected from table reads executed in other blocks)
#pragma config EBTR1 = ON       // Table Read Protection bit (Block 1 (004000-007FFFh) protected from table reads executed in other blocks)
#pragma config EBTR2 = ON       // Table Read Protection bit (Block 2 (008000-00BFFFh) protected from table reads executed in other blocks)
#pragma config EBTR3 = ON       // Table Read Protection bit (Block 3 (00C000-00FFFFh) protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = ON       // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>


#define _XTAL_FREQ      8000000UL

#define UART_RX         PORTCbits.RC7
#define UART_TX         PORTCbits.RC6
#define UART_RX_IO      TRISCbits.TRISC7
#define UART_TX_IO      TRISCbits.TRISC6


unsigned char UART_Init(const unsigned long int baudrate)
{
   unsigned int x;
   
   x = (_XTAL_FREQ - baudrate * 64) / (baudrate * 64);
   
   if(x > 255)
   {
      x = (_XTAL_FREQ - baudrate * 16) / (baudrate * 16);
      BRGH = 1;
   }
   
   if(x < 256)
   {
        SPBRG = x;
        SYNC  = 0;
        SPEN  = 1;
              //TRISC7 = 1;
        //TRISC6 = 1;
        UART_RX_IO = 0b1;
        UART_TX_IO = 0b1;

        CREN = 1;
        TXEN = 1;
        return 1;
   }
   return 0;
}

unsigned char UART_TX_Empty(void)
{
  return TRMT;
}

unsigned char UART_Data_Ready(void)
{
   return RCIF;
}

unsigned char UART_Read(void)
{
  while(!RCIF);
  return RCREG;
}

void UART_Read_Text(unsigned char *Output, unsigned int length)
{
   unsigned int i;
   for(i = 0; i < length; i++)
      Output[i] = UART_Read();
}

void UART_Write(unsigned char data)
{
  while(!TRMT);
  TXREG = data;
}

void UART_Write_Text(const unsigned char *text)
{
  int i;
  for(i = 0; text[i] != '\0'; i++)
     UART_Write(text[i]);
}

void mcuInit(void)
{
    OSCCONbits.IRCF = 0b111;        //8Mhz
    
    CMCONbits.CM = 0b111;           //Komparatorler kapali
    
    ADCON0bits.ADON = 0b0;          //ADC kapali
    ADCON1bits.PCFG = 0b1111;       //Tüm pinler dijital

    //Giris cıkış işlemleri
    PORTA = 0x00;
    TRISA = 0x00;

    PORTB = 0x00;
    TRISB = 0x00;
    
    PORTC = 0x00;
    TRISC = 0x00;
    
    PORTD = 0x00;
    TRISD = 0x00;
    
    PORTE = 0x00;
    TRISE = 0x00;
}

void main(void)
{
    const unsigned char *txt0 = "\n\r";
    const unsigned char *txt1 = "Saat    ";
    const unsigned char *txt2 = "Sicaklik";
    const unsigned char *txt3 = "ADC     ";
    const unsigned char *txt4 = "...Okunan Degerler...";
    const unsigned char drc   = 176;
    
    unsigned char say = 0;
    
    
    mcuInit();
    __delay_ms(50);
    UART_Init(9600);
    
    
    while(1)
    {
        switch(say++)
        {
        case 0:
            UART_Write_Text(txt4);
            UART_Write_Text(txt0);
            break;
        case 1:
            UART_Write_Text(txt1);
            UART_Write_Text("  : ");
            UART_Write_Text("12:34:56");
            UART_Write_Text(txt0);
            break;
        case 2:
            UART_Write_Text(txt2);
            UART_Write_Text("1 : ");
            UART_Write_Text("15");
            UART_Write(drc);
            UART_Write_Text("C");
            UART_Write_Text(txt0);
            break;
        case 3:
            UART_Write_Text(txt2);
            UART_Write_Text("2 : ");
            UART_Write_Text("17");
            UART_Write(drc);
            UART_Write_Text("C");
            UART_Write_Text(txt0);
            break;
        case 4:
            UART_Write_Text(txt2);
            UART_Write_Text("3 : ");
            UART_Write_Text("23");
            UART_Write(drc);
            UART_Write_Text("C");
            UART_Write_Text(txt0);
            break;
        case 5:                
            UART_Write_Text(txt3);
            UART_Write_Text("1 : ");
            UART_Write_Text("150");
            UART_Write_Text(txt0);
            break;
        case 6:
            UART_Write_Text(txt3);
            UART_Write_Text("2 : ");
            UART_Write_Text("675");
            UART_Write_Text(txt0);
            break;
        case 7:
            UART_Write_Text(txt3);
            UART_Write_Text("3 : ");
            UART_Write_Text("1020");
            UART_Write_Text(txt0);
            break;
        case 8:
            say = 0;
            UART_Write_Text(txt0);
            break;
        default:
            break;
        }
        
        __delay_ms(200);
    }
}


Hocam öncelikle teşekkür ederim.Benim istediğim bu şekilde değil ama ilk başta istegimi yanlı aktarmışım ve vaktinizi harcadıgım için özür dilerim.Benim yapmak istedigim şey  Terminal den 1 karakter yollayıp (1,2,3 gibi) ekrana o karakter ile ilgili verileri almak istiyorum.

char say;

void main()

{
UART_init(9600);

while (1)
{
say=UART_Read();

switch(say)
{
case '1':
sicaklik();
break;
}
}
}


bazı şeyleri yazmadım buraya ama kalıp bu şekilde bir şey ile uğraştım.Ama bir sonuç alamadım.Bunu nasıl yapmam gerekiyor bu konuda bir yardım bekliyordum.

mehmet

Yapı bu şekilde, geri kalan fonksiyonları da siz anlayıp,
çalıştırabilirsiniz.

Okurken her hangi bir anda 1, 2, vs. gibi sayılar gelebilir.
Bunu aşmak için "header" eklenir. Mesela;  siz "1" girersiniz.
Ancak "adem1" yollanır. Dolayısıyla hatta tekrar eden veri
en aza indirilir. Çözerken de önce "header" doğru mu? ona
bakarsınız. Sonra gelen değere göre dallanma yapılır.

Önceliğiniz veri okuma kısmını halledin.
Olan olmuştur,
olacak olan da olmuştur.
Olacak bir şey yoktur.
---------------------------------------------
http://www.mehmetbilgi.net.tr

ademtosun58

Alıntı yapılan: mehmet - 18 Nisan 2017, 22:02:35
Yapı bu şekilde, geri kalan fonksiyonları da siz anlayıp,
çalıştırabilirsiniz.

Okurken her hangi bir anda 1, 2, vs. gibi sayılar gelebilir.
Bunu aşmak için "header" eklenir. Mesela;  siz "1" girersiniz.
Ancak "adem1" yollanır. Dolayısıyla hatta tekrar eden veri
en aza indirilir. Çözerken de önce "header" doğru mu? ona
bakarsınız. Sonra gelen değere göre dallanma yapılır.

Önceliğiniz veri okuma kısmını halledin.


Hocam Hala okuma okuma kısmını halledemedim acaba kütüphanemde bi eksik mi var daha önce hiç bu tip şeylerle uğrasmadıgım için işin içinden çıkamıyorum.Amacım switch case gibi bir yapıda terminalde değer girip o işlemi yaptırmak istiyorum.Ama bir türlü değer giremedm.

mehmet

Olan olmuştur,
olacak olan da olmuştur.
Olacak bir şey yoktur.
---------------------------------------------
http://www.mehmetbilgi.net.tr