Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] megawin 8051 mcu interfacing with I2C 16x2 LCD

Status
Not open for further replies.

funguamongu

Newbie
Joined
Apr 18, 2024
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
27
Hello
I recently got my hands on Megawin MG82F6D17 mcus. They are cheaper than PIC and ATMEGA mcus in my county. I have been trying to use the sample code provided by the support site of the manufacturer but I cannot figure out how to get it to work with the LCD.

Code:
u8 TWI0_WriteBuf(u8 DevAddr,u16 RegStartAddr,u8 *pBuf,u8 Len)
{
    u8 i;
    u8 Flag;
    Flag=1;

    SICON |=STA;                        // Send START
    TWI0OvTime=5;
    SICON &=~SI;           
    while((SICON&SI)!=SI){if(TWI0OvTime==0) goto TWI0_WRITE_ERR;}        // wait completed, if time overflow,then return fail.
    SICON &=~STA;

    SICON=SICON|(AA);      

    Flag++;
    SIDAT = DevAddr&0xFE;                // send Slave Device address 
    TWI0OvTime=5;
    SICON &=~SI;           
    while((SICON&SI)!=SI){if(TWI0OvTime==0) goto TWI0_WRITE_ERR;}        // wait completed, if time overflow,then return fail.

    Flag++;
    SIDAT = HIBYTE(RegStartAddr);        // send Slave Data address high
    TWI0OvTime=5;
    SICON &=~SI;           
    while((SICON&SI)!=SI){if(TWI0OvTime==0) goto TWI0_WRITE_ERR;}        // wait completed, if time overflow,then return fail.

    Flag++;
    SIDAT = LOBYTE(RegStartAddr);        // send Slave Data address low
    TWI0OvTime=5;
    SICON &=~SI;           
    while((SICON&SI)!=SI){if(TWI0OvTime==0) goto TWI0_WRITE_ERR;}        // wait completed, if time overflow,then return fail.


    Flag++;
    i=0;
    while(i<Len)
    {
        if(i==(Len-1))
        {
            SICON=SICON&(~AA);      
        }
        else
        {
            SICON=SICON|(AA);      
        }
        SIDAT=pBuf[i];            // send  Data
        TWI0OvTime=5;
        SICON &=~SI;           
        while((SICON&SI)!=SI){if(TWI0OvTime==0) goto TWI0_WRITE_ERR;}        // wait completed, if time overflow,then return fail.
        i++;
    }
                   
    Flag++;
    SICON |= STO;                // Send STOP
    TWI0OvTime=5;
    SICON &=~SI;           
    while((SICON&STO)==STO){if(TWI0OvTime==0) goto TWI0_WRITE_ERR;}        // wait completed, if time overflow,then return fail.
    SICON &=~STO;

    SICON = SICON &(~SI);       

    return 0;
   
TWI0_WRITE_ERR:
    return Flag;
}
This was the sample code in the package, I need help getting it to work

Many thanks
--- Updated ---

Full sample code for I2C provided by Megawin
 

Attachments

  • MG82F6D17_I2C_Master.zip
    157.2 KB · Views: 27

Hello!

A good way to start would be to clearly explain what you did.
Apparently you included a code snippet that you didn't write yourself.
But what did you do with it?
Is it a hardware you have developed? -> Are you sure the wiring is correct?
Or do you use the dev board indicated in the source code (MG82F6D17 SSOP20_V10 EV Board)?
By the way, does it compile without errors?
Some more info may help.

Dora.
 

while ( (SICON&SI)!=SI){if(TWI0OvTime==0) goto TWI0_WRITE_ERR; }
This was the sample code in the package, I need help getting it to work
If I were you, I wouldn't spend a second debugging a code like this, which in addition to being poorly written ( goto's should be avoided as much as possible ), it is also not quite intelligible.
 

Yes it did compile without errors

This is my first project with an 8051 mcu. The I2C code provided by the manufacturer looked too complex so I have moved on to using 4 bit mode without the i2c module. Now I have new problems. No matter what I do, the first line is filled with blocks as if its on full contrast.
Here is the code I wrote (partially)

Code:
#include ".\include\REG_MG82F6D17.H"
#include ".\include\Type.h"
#include ".\include\API_Macro_MG82F6D17.H"
#include ".\include\API_Uart_BRGRL_MG82F6D17.H"
#include <Intrins.h>
#define LCD_Port P1        /* P1 port as data port */
#define rs P22            /* Register select pin */
#define en P24            /* Enable pin */
#define MCU_SYSCLK        12000000
#define MCU_CPUCLK        (MCU_SYSCLK)


void DelayXus(u8 xUs)
{
    while(xUs!=0)
    {
#if (MCU_CPUCLK>=11059200)
        _nop_();
#endif
#if (MCU_CPUCLK>=14745600)
        _nop_();
        _nop_();
        _nop_();
        _nop_();
#endif
#if (MCU_CPUCLK>=16000000)
        _nop_();
#endif

#if (MCU_CPUCLK>=22118400)
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
#endif
#if (MCU_CPUCLK>=24000000)
        _nop_();
        _nop_();
#endif        
#if (MCU_CPUCLK>=29491200)
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
#endif
#if (MCU_CPUCLK>=32000000)
        _nop_();
        _nop_();
#endif

        xUs--;
    }
}


void DelayXms(u16 xMs)
{
    while(xMs!=0)
    {
        CLRWDT();
        DelayXus(200);
        DelayXus(200);
        DelayXus(200);
        DelayXus(200);
        DelayXus(200);
        xMs--;
        
    }
}






void LCD_Command (char cmnd)    /* LCD16x2 command funtion */
{
    LCD_Port=0;
    rs=0;

    LCD_Port =((cmnd >>4 )& 0x0F);
                
    en=1; 
    DelayXus(1);
    en=0;
  
    rs=0;

    LCD_Port = (cmnd & 0x0F);        
    en=1; 
    DelayXus(1);
    en=0;
    LCD_Port=0;



}

void LCD_Char (char char_data)  /* LCD data write function */
{
    LCD_Port=0;
    rs=1;

    LCD_Port = ((char_data >>4 ) & 0x0F);
    
    en=1;
    DelayXus(1);
    en=0;
    
    rs=1;

    LCD_Port =(char_data & 0x0F);
    
    en=1;
    DelayXus(1);
    en=0;

  LCD_Port=0;

}



void LCD_Init (void)        /* LCD Initialize function */
{

    LCD_Command(0x28);
    LCD_Command(0x06);
    LCD_Command(0x0F);
    LCD_Command(0x10);
    LCD_Command(0x01);
    
}

void main()
{ 
  PORT_SetP1PushPull(BIT0|BIT1|BIT5|BIT6|BIT7);
  PORT_SetP2PushPull(BIT2|BIT4);
    LCD_Init();    
    DelayXms(10);
    while(1);
}

1713579953667.png

This is the description of port 1. In all the tutorials ive found via google, the mcus they use have 8 pins on the port they use for lcd data communication, and the lcd is wired to the last 4 pins. Mine only has 5 and im using 1,5,6,7. Could this be an issue?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top