interrupt in project

Thread Starter

Parth786

Joined Jun 19, 2017
642
Sure. Implement the UART receiver then make your main routine just send the characters back to the terminal. Simple and that avoids the additional complexity of the LCD code.
I tried to send data to port of 8051. I have completed this
C:
#include <reg51.h>           

//Function Prototype
void Uart_initialize(void);
void Send_Data(unsigned char);
void Send_String(char String[]);

/*Function to set up Uart */
void Uart_initialize (void)   
{
    TR1  = 0;          // Stop Timer
    TMOD = 0x20;       // Timer 1 in mode 2 auto reload mode for baudrate generation
    SCON = 0x50;       // Serial Mode 1, 8-DATA bit 1-Start Bit, 1-Stop Bit, Receive Enable
    TH1  = 0xFD;       // Load value for 9600 baudrate
    TR1  = 1;          // Start Timer
}

/* Function to send a Data*/
void Send_Data(unsigned char DATA)
{
    SBUF = DATA;      //Load data into SBUF

    while(TI == 0)  //Wait untill transmission to complete
        {
        }
 
    TI = 0;                  //Clear transmitte interrupt(TI) flag
}

/* Function to send a characters untill a null*/
void Send_String(char String[])
{
  unsigned char i = 0;

    while(String[i] != '\0')
    {
        Send_Data(String[i]);
 
        i++;
    }
}

/* Main Function*/
void main(void)
{
  Uart_initialize();

  Send_String("Parth");

    while(1)       
  {

  }
}
Uart_send.png
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
OK.
Next, echo the characters received back to the terminal so that whatever you type shows up on the screen.
Start by just polling the receiver, no interrupts yet.
 

cmartinez

Joined Jan 17, 2007
8,218
OK.
Next, echo the characters received back to the terminal so that whatever you type shows up on the screen.
Start by just polling the receiver, no interrupts yet.
That's exactly the way I do it... one step at a time, debug, debug, debug... until each step works perfectly under all scenarios. Then I move on to the next step.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
My approach is not working. look at this code. what's wrong in this code
C:
#include <reg51.h>   

/*Function Prototype*/
void Uart_initialize(void);
void Send_Data(unsigned char);
char Receive_Data(void);
void Send_String(char String[]);

/*Function to set up Uart */
void Uart_initialize (void)
{
    TR1  = 0;          // Stop Timer
    TMOD = 0x20;       // Timer 1 in mode 2 auto reload mode for baudrate generation
    SCON = 0x50;       // Serial Mode 1, 8-DATA bit 1-Start Bit, 1-Stop Bit, Receive Enable
    TH1  = 0xFD;       // Load value for 9600 baudrate
    TR1  = 1;          // Start Timer
}

/* Function to send a Data*/
void Send_Data(unsigned char Data)
{
    SBUF = Data;        // Load Data in SBUF register
    while (TI == 0);    // Wait untill transmission to complete*/
    TI = 0;             // Clear TI flag */
}

/* Function to fetch a character */
char Receive_Data(void)
{
    while(RI = 0);        // Wait here until SBUF clear
    RI = 0;
    return SBUF;
}

/* Function to send a characters untill a null*/
void Send_String(char String[])
{
  unsigned char i = 0;
    while(String[i] != '\0')
    {
        Send_Data(String[i]);
        i++;
    }
}

/* Main Function*/
void main(void)
{
  char Receive ;

  Uart_initialize();
  Send_String("Parth");

  while(1)
  {
        if(RI==1)
        {
            Receive = SBUF;    //Read received data from SBUF
            RI = 0;            //Clear flag
            Send_Data( Receive);
        }
    }
}
 
Last edited:

cmartinez

Joined Jan 17, 2007
8,218
Parth, at what frequency is your MCU working?

Here's a table I made of the reload values normally used for a couple of "flavors" of 8051, depending on oscillator frequency:

Code:
;                                   Copyright TechnoMech, S.A. de C.V.
;                                          2/21/2018 2:58:39 PM:


;--------------------------------------------------------------------------------------------------------------+
;                             The following baudrate values should be doubled when                             |
;                             the PCON double baud rate instruction has been used                              |
;---------------------------------+-----------+------------+------------+------------+------------+------------+
;                            MCU: |Traditional| AT89LP4052 | AT89LP4052 | AT89LP4052 | Traditional| AT89LP4052 |
;                        Crystal: | 11.0592MHZ| 14.7456MHZ | 11.0592MHZ |  9.8304MHZ |  22.1184MHZ| 22.1184MHZ |
;---------------------------------+-----------+------------+------------+------------+------------+------------+
;        MOV     TH1,  #-2        ;           |            |            |153600 baud |            |            |
;        MOV     TH1,  #-3        ; 9600 baud |153600 baud |            |            | 19200 baud |            |
;        MOV     TH1,  #-4        ;           |            |            | 76800 baud |            |            |
;        MOV     TH1,  #-8        ;           | 57600 baud |            | 38400 baud |            |            |
;        MOV     TH1,  #-9        ;           |            | 38400 baud |            |            | 76800 baud |
;        MOV     TH1,  #-12       ; 2400 baud | 38400 baud |            |            |  4800 baud |            |
;        MOV     TH1,  #-16       ;           | 28800 baud |            | 19200 baud |            |            |
;        MOV     TH1,  #-18       ;           |            | 19200 baud |            |            | 38400 baud |
;        MOV     TH1,  #-24       ; 1200 baud | 19200 baud |            |            |  2400 baud |            |
;        MOV     TH1,  #-32       ;           |            |            |  9600 baud |            |            |
;        MOV     TH1,  #-36       ;           |            |  9600 baud |            |            | 19200 baud |
;        MOV     TH1,  #-48       ;  600 baud |  9600 baud |            |            |  1200 baud |            |
;        MOV     TH1,  #-32       ;           |            |            |            |            |            |
;        MOV     TH1,  #-64       ;           |            |            |  4800 baud |            |            |
;        MOV     TH1,  #-72       ;           |            |  4800 baud |            |            |  9600 baud |
;        MOV     TH1,  #-96       ;  300 baud |  4800 baud |            |            |   600 baud |            |
;        MOV     TH1,  #-128      ;           |            |            |  2400 baud |            |            |
;        MOV     TH1,  #-144      ;           |            |  2400 baud |            |            |  4800 baud |
;        MOV     TH1,  #-192      ;  150 baud |  2400 baud |            |            |   300 baud |            |
;---------------------------------+-----------+------------+------------+------------+------------+------------+
      
        MOV     TMOD, #00100000B    ;timer 1 mode 2 (baud generator)
        SETB    TR1                 ;start timer 1 as baud generator
        MOV     PCON, #10000000B    ;double baud rate
        MOV     SCON, #01010010B    ;serial port, mode 1, receiver enable
                                    ;clear transmit and receive flags
 

JohnInTX

Joined Jun 26, 2012
4,787
I doesn't look like you're using it but at line 30 you need a double ==, not a single one.
Otherwise it looks OK so far. Does it say 'Parth' still?
Are you sure the terminal is sending characters when you type? Try tying RTS and CTS together on the terminal.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Are you sure the terminal is sending characters when you type? Try tying RTS and CTS together on the terminal.
Program show only the string "Parth" at beginning but if I type something like "john" It doesn't send back to terminal. I am not receiving echo data on terminal

At89c51 = 11.0592 Mhz
 

be80be

Joined Jul 5, 2008
2,072
Your using a free copy each time you open it. You have to configure it

I used mplab and went to the bathroom came back and text shows up. The setting was way to slow google and figuring what people called the problem fixed it.

Ian has told a lot of people how to fix uart problems with the sim your using
 

JohnInTX

Joined Jun 26, 2012
4,787
I'd take a look at the sim, too. I'm not an 8051 expert but I don't see anything wrong with the last echo code. One test would be to disconnect the 8051 entirely and connect the terminal RX and TX together in full duplex mode. If you see what you type, proceed. Then disconnect the RX from TX and type again. No chars back means the terminal is behaving as it should i.e. send a char on TX but only display chars received on RX.
 

JohnInTX

Joined Jun 26, 2012
4,787
As I have said..(Numerous times ) There is nothing wrong with that code..

The function that is never called :-

while(RI = 0); // Wait here until SBUF clear

Is wrong it should read :-

while(RI == 0); // Wait here until SBUF clear
Agreed. Post #46. Don’t know why he’s not calling it..
 

Ian Rogers

Joined Dec 12, 2012
1,136
Agreed. Post #46. Don’t know why he’s not calling it.
I wrote most of the code... But as the loop waits for the interrupt flag, it was just re-made in the main loop ( easier for him..)

Parth's problem is he is trying to work with the free copy of Proteus... This copy will not allow hardware changes..
If he were to use MCU8051IDE he could simulate all day for free... It has been suggested several times..

I will even give him a copy as the latest version is uncompiled...
 

JohnInTX

Joined Jun 26, 2012
4,787
I wrote most of the code... But as the loop waits for the interrupt flag, it was just re-made in the main loop ( easier for him..)

Parth's problem is he is trying to work with the free copy of Proteus... This copy will not allow hardware changes..
If he were to use MCU8051IDE he could simulate all day for free... It has been suggested several times..

I will even give him a copy as the latest version is uncompiled...
Thanks for the clarification, Ian. I wondered..
 

be80be

Joined Jul 5, 2008
2,072
Proteus They use it on you tube. But the free copy is a pain In the butt I think what made me stop using it was swordfish basic.

It worked great on there samples but I gave up On it 10 years ago it's out of my reach $$$ LOL and I'm not into cracked copies.

This is really good I think parth would really do well with this.
Screenshot from 2018-02-28 15-54-44.png

P.S I've tried every piece of code Ian and parth's Or Ian's I give up who But.
It works and sim's fine
 
Top