PIC18 to PIC18 UART Problem GLCD-LCD

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
merdogan0761
Posts: 1
Joined: 16 May 2020 22:48

PIC18 to PIC18 UART Problem GLCD-LCD

#1 Post by merdogan0761 » 17 May 2020 01:52

I am using PIC18F46K22 as transmitter and PIC18F25K22 as receiver. I want to make UART communication with each other. I am getting result but, not correct and not stable. I cant understand why. Also, i have real setup to test. And result is same with Proteus 8.9 SP2. Also , i am using MicroC 7.0.1 version. In transmitter part has a 18F46K22 with GLCD(KS0108) . In receiver part has a 18f25k22 with LCD(4x20).Also,i worked on overrun and frame error for uart receiver part. Then , i could get better result then other training code,but not accurate. Also, i try to write UART library in MicroC from datasheet information with interrupt to receiver data. I could not get any result with this way. Maybe, i could wrote code wrongly. Now, i am trying to get data with MicroC liblary with very basic code. I cant accurate data flow.Therefore, i cant control led and buttons correctly.I had a big problem on receiver part. I added proteus and microC source code.This is transmitter code

Code: Select all

//#############################################################################
//Device MCU:PIC18F46K22
//OSC:20MHz
//Mustafa ERDGAN mustafa-erdogan@outlook.com
//MicroC Version 7.0.1 and Proteus 8.9 Sp2
//#############################################################################

//-------- GCLD BAGLANTILARI-------------
char GLCD_DataPort at PORTB;
sbit GLCD_CS1 at RD1_bit;
sbit GLCD_CS2 at RD0_bit;
sbit GLCD_RS  at RD2_bit;
sbit GLCD_RW  at RD3_bit;
sbit GLCD_EN  at RD4_bit;
sbit GLCD_RST at RD5_bit;
sbit GLCD_CS1_Direction at TRISD1_bit;
sbit GLCD_CS2_Direction at TRISD0_bit;
sbit GLCD_RS_Direction  at TRISD2_bit;
sbit GLCD_RW_Direction  at TRISD3_bit;
sbit GLCD_EN_Direction  at TRISD4_bit;
sbit GLCD_RST_Direction at TRISD5_bit;

//TANIMLAMALAR------------
#define Sensor1  PORTA.RA0
#define Sensor2  PORTA.RA1
#define Buzzer   PORTC.RC0
#define Motor11  PORTA.RA2
#define Motor12  PORTA.RA3
#define Motor21  PORTA.RA4
#define Motor22  PORTA.RA5

//DEGISKENLER------------
unsigned char uart_st;
unsigned char old_uart_st;
unsigned char uart_rd;
unsigned char old_uart_rd;
void send_text()  // OUT-----------
{
    old_uart_st=uart_st;
    if     (Sensor1==1)
    {
     uart_st='A';
    }
    else if(Sensor2==1)
    {
     uart_st='B';
    }
    else
    {
     uart_st='N';
    }
    //sending part
    if (UART1_Tx_Idle() == 1) {
        UART1_Write(uart_st);
    }
    //showing part
    if(old_uart_st!=uart_st)
    {
      Glcd_Box(0,0,6,8,0);     // for clear
      Glcd_Write_Char_Adv(uart_st,0,0);
    }
}
void system_control()
{
    //N-symbol for epmty time
    //A-symbol for Motor11=1;
    //B-symbol for Motor12=1;
    //K-symbol for Uart Disconnected
    
    //showing part   // this is not working very well because of receving part
    // not working very well
    if(old_uart_rd!=uart_rd)
    {
      Glcd_Box(120,0,126,8,0);     // for clear
      Glcd_Write_Char_Adv(uart_rd,120,0);
    }
    if(uart_rd=='A')
    {
     Motor11=1;
    }
    else
    {
     Motor11=0;
    }
    if(uart_rd=='B')
    {
     Motor12=1;
    }
    else
    {
     Motor12=0;
    }
}
void read_text()
{
     old_uart_rd=uart_rd;
     if (UART1_Data_Ready())
     {// If data is received,
      // A character is available.
      if(RCSTA1.OERR==1)
      {
          // Overrun error.
          // Clear the error and re-enable the receiver.
          RCSTA1.CREN=0;
          RCSTA1.CREN=1;
      }
      if(RCSTA1.FERR==1)     // STEP-9    CHECK FRAME ERROR
      {
         // Framing error.
         // Read the byte to clear the error but don't use the byte.
         uart_rd = UART1_Read();     // read the received data,
      }
      else
      {
        // A byte was received without error.
        uart_rd = UART1_Read();     // read the received data,
        // Do something with the byte.
        system_control();
      }
     }
     else
     {
         uart_rd ='K';
         system_control();
     }
}

void micro_setup()
{
   // ANALOG PORT IPTAL
   C1ON_bit = 0;                                  // Disable comparators
   C2ON_bit = 0;
   ADCON0 = 0;
   ADRESH=0;
   ADRESL=0;
   ANSELA=0;
   ANSELB=0;
   ANSELC=0;
   ANSELD=0;
   ANSELE=0;
   // PORT KURULUMLARI
   TRISA=0b11000011;
   TRISB=0b00000000;
   TRISC=0b11000000;
   TRISD=0;
   TRISE=0b00001111;
   PORTA=0;
   PORTB=0;
   PORTC=0;
   PORTD=0;
   PORTE=0;
   // GLCD EKRAN KURUMU
   Glcd_Init();       // GLCD AÇILIYOR
   Glcd_Fill(0x00);   // GLCD TEMIZLE
   // UART KURULUM
   PMD0.UART1MD=0;
   UART1_Init(115200);
   Delay_ms(100);
   UART1_Write('S');
}
void main() {
   micro_setup();
   while(1)
   {
    send_text();
    read_text();
    system_control();
   }
}
And this is my receiver code for PIC18F25K22

Code: Select all

//#############################################################################
//Device MCU:PIC18F25K22
//OSC:20MHz
//Mustafa ERDGAN mustafa-erdogan@outlook.com
//MicroC Version 7.0.1
//#############################################################################
//TANIMLAMALAR
#define Sensor1  PORTA.RA3
#define Sensor2  PORTC.RC0
#define Limit1   PORTA.RA0
#define Limit2   PORTA.RA1
#define LimitEMC PORTA.RA2
#define Buzzer   PORTA.RA4
#define Motor11  PORTC.RC1
#define Motor12  PORTC.RC2
#define Motor21  PORTC.RC4
#define Motor22  PORTC.RC5

sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB2_bit;
sbit LCD_D5 at LATB3_bit;
sbit LCD_D6 at LATB4_bit;
sbit LCD_D7 at LATB5_bit;


sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;

unsigned char uart_rd;

void system_control()
{
    //N-symbol for epmty time
    //A-symbol for Motor11=1;
    //B-symbol for Motor12=1;
    //K-symbol for Uart Disconnected
    
    Lcd_Chr(2,5,uart_rd);
    if(uart_rd=='A')
    {
     Motor11=1;
    }
    else
    {
     Motor11=0;
    }
    if(uart_rd=='B')
    {
     Motor12=1;
    }
    else
    {
     Motor12=0;
    }
}

void send_text()
{
    if     (Sensor1==1)
    {
     UART1_Write('A');
     Lcd_Chr(1,5,'A');
    }
    else if(Sensor2==1)
    {
     UART1_Write('B');
     Lcd_Chr(1,5,'B');
    }
    else
    {
     UART1_Write('N');
     Lcd_Chr(1,5,'N');
    }
}
void read_text()
{
     if (UART1_Data_Ready())
     {// If data is received,
      // A character is available.
      if(RCSTA1.OERR==1)
      {
          // Overrun error.
          // Clear the error and re-enable the receiver.
          RCSTA1.CREN=0;
          RCSTA1.CREN=1;
      }
      if(RCSTA1.FERR==1)     // STEP-9    CHECK FRAME ERROR
      {
         // Framing error.
         // Read the byte to clear the error but don't use the byte.
         uart_rd = UART1_Read();     // read the received data,
      }
      else
      {
        // A byte was received without error.
        uart_rd = UART1_Read();     // read the received data,
        // Do something with the byte.
        system_control();
      }
     }
     else
     {
         uart_rd ='K';
         system_control();
     }
}

void micro_setup()
{
   // ANALOG PORT IPTAL
   C1ON_bit = 0;                                  // Disable comparators
   C2ON_bit = 0;
   ADCON0 = 0;
   ANSELA=0;
   ANSELB=0;
   ANSELC=0;
   // PORT KURULUMLARI
   TRISA=0b00001111;
   TRISB=0b00000000;
   TRISC=0b11000001;
   PORTA=0;
   PORTB=0;
   PORTC=0;
   // LCD EKRAN KURUMU
   Lcd_Init();
   Lcd_Cmd(_LCD_CURSOR_OFF);     // Cursor off
   Lcd_Cmd(_LCD_CLEAR);          // Clear display
   // UART KURULUM
   PMD0.UART1MD=0;
   UART1_Init(115200);
   Delay_ms(100);
   UART1_Write('S');

}
void main() {
  micro_setup();
  Lcd_Out(1,1,"OUT:");
  Lcd_Out(2,2,"IN:");
  while(1)
  {
    read_text();
    send_text();
    system_control();
    // for following code flow.Not necassary.
    if(Sensor1==1)
    {
      Lcd_Out(3,3,"<");
    }
    else
    {
      Lcd_Out(3,3,">");
    }
  }
}
I am waiting your help. Thanks for your interest. Also, i added to oscilloscope to read Motor control led for analysis.For example incoming data is A,motor LED not to be turn off when data incoming.Image
Attachments
MicroC-Help.rar
(447.22 KiB) Downloaded 58 times

Post Reply

Return to “mikroC PRO for PIC General”