How to convert a mikroBasic code to mikroC?

General discussion on mikroC.
Post Reply
Author
Message
vinnnie
Posts: 2
Joined: 08 Apr 2010 06:10

How to convert a mikroBasic code to mikroC?

#1 Post by vinnnie » 09 Apr 2010 16:00

Hi everyone, I found a code in mikroBasic-Forum written in mikroBasic (obviously :lol: :mrgreen: ) and I would like to convert it to mikroC because I only have the mikroC compiler. It will be possible? because might not exist the same instructions and data types for C.

The code is:

Code: Select all

program compass
dim  sw_revision,ee_high, ee_low,ee_byte as byte
data as word
Text as char[17]
sub procedure initLCD
  PORTB  = 0                       ' Clear portb
  TRISB  = 0                       ' Designate portb as output (LCD is connected to portb)
  INTCON = 0                       ' Disable all interrupts
  LCD_init(PORTB)                  ' Initialize  (4-bit interface connection)
  LCD_cmd(LCD_CURSOR_OFF)          ' Send command to LCD (cursor off)
  LCD_cmd(LCD_CLEAR)               ' Send command  to LCD (clear LCD)
  text = "CMPS03 Compass"
  LCD_Out(1,1,text)                   ' Print string a on LCD, 1st row, 1st column
end sub
main:
    initlcd
    delay_ms(1000)          ' wait before every system component is ready
    while true               'infinite loop
    I2C_Init(100000)        ' initialize I2C module 100 kHz clk, full master mode

    '===============start reading========================
    I2C_Start                ' issue I2C start signal
     I2C_Wr(0xC0)             ' send byte via I2C to cmps03 addres 0xC0
    I2C_Wr(0x00)             ' send byte (register adres)
    I2C_Repeated_Start       ' issue I2C signal repeated start
    I2C_Wr(0xC1)             ' send byte (request data from EEPROM)
    sw_revision=0            ' reset sw_revision to check if it is really read from cmps03
    sw_revision = I2C_rd(1)  ' Read the software version from register 0 and send acknowledge
    ee_byte=0                ' reset sw_revision to check if it is really read from cmps03
    delay_ms(5)
    ee_byte = I2C_rd(1)      ' Read the unused register from register 1 and send acknowledge (it should read 0x80)
    delay_ms(5)
    ee_high=0
    ee_high = I2C_rd(1)      ' Read the highbyte data  and send acknowledge
    delay_ms(5)
    ee_low=0
    ee_low = I2C_rd(0)       ' Read the low byte data  and  do NOT send acknowledge
    I2C_stop                 ' issue I2C stop sinal
    '===============format and display data========================
    data = word(ee_high << 8) + ee_low  'Combine highbyte and low byte
    LCD_cmd(LCD_CLEAR)               ' Send command  to LCD (clear LCD)
            bytetostr(sw_revision,text)' Format low_byte
            LCD_Out(1,1,text)         ' and print it on LCD
            bytetostr(ee_byte,text)    ' Format low_byte
            LCD_Out(1,5,text)         ' and print it on LCD
            bytetostr(ee_low,text)    ' Format low_byte
            LCD_Out(2,1,text)         ' and print it on LCD
            bytetostr(ee_high,text)   ' Format high_byte
            LCD_Out(2,5,text)         ' and print it on LCD
            wordToStr(data,text)      ' Format low_byte
            LCD_Out(2,10,text)        ' and print it on LCD

    delay_ms(500)      'wait 0.5 second before next measurement
    wend

end.

Yeah, I know that i can use I2C_Start, I2C_stop, I2C_Wr() instructions in both, but types as word or wordToStr() mmmm.... i dont know :roll:

Any ideas or suggestion?

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: How to convert a mikroBasic code to mikroC?

#2 Post by tihomir.losic » 12 Apr 2010 16:39

Hello,

please, try this code (mikroC PRO for PIC):

Code: Select all

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

bit sw_revision;
bit ee_high;
bit ee_low;
bit ee_byte;
unsigned int _data;
unsigned char text[] = "CMPS03 Compass";

void initLCD(){

    PORTB = 0;
    TRISB = 0;
    INTCON = 0;
    Lcd_Init();
    Lcd_Cmd(_LCD_CURSOR_OFF);
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1, 1, text);

}

void main() {

    ANSEL = 0;
    ANSELH = 0;
    C1ON_bit = 0;
    C2ON_bit = 0;

    initLCD();
    delay_ms(1000);
    while(1){

        I2C1_Init(100000);
        I2C1_Start();
        I2C1_Wr(0xC0);
        I2C1_Wr(0x00);
        I2C1_Repeated_Start();
        I2C1_Wr(0xC1);
        sw_revision = 0;
        sw_revision = I2C1_Rd(1);
        ee_byte = 0;
        Delay_ms(5);
        ee_byte = I2C1_Rd(1);
        Delay_ms(5);
        ee_high = 0;
        ee_high = I2C1_Rd(1);
        Delay_ms(5);
        ee_low = 0;
        ee_low = I2C1_Rd(0);
        I2C1_Stop();

        _data = ee_low + (ee_high << 8);

        Lcd_Cmd(_LCD_CLEAR);

        ByteToStr(sw_revision, text);
        LCD_Out(1,1,text);              // and print it on LCD

        bytetostr(ee_byte,text);        // Format low_byte
        LCD_Out(1,5,text);              // and print it on LCD

        bytetostr(ee_low,text);         // Format low_byte
        LCD_Out(2,1,text);              // and print it on LCD

        bytetostr(ee_high,text);        // Format high_byte
        LCD_Out(2,5,text);              // and print it on LCD

        wordToStr(_data,text);          // Format low_byte
        LCD_Out(2,10,text);             // and print it on LCD

      }
}
mikroC PRO for PIC 2009 includes a set of libraries and examples intended to facilitate application development.
Routines are documented in detail and allow quick start in programming microcontrollers;
browse through the supplied examples and learn how to utilize PIC with minimum of code and effort.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

vinnnie
Posts: 2
Joined: 08 Apr 2010 06:10

Re: How to convert a mikroBasic code to mikroC?

#3 Post by vinnnie » 12 Apr 2010 22:30

tihomir.losic wrote:Hello,

please, try this code (mikroC PRO for PIC):

mikroC PRO for PIC 2009 includes a set of libraries and examples intended to facilitate application development.
Routines are documented in detail and allow quick start in programming microcontrollers;
browse through the supplied examples and learn how to utilize PIC with minimum of code and effort.

Best regards,

Losic Tihomir

Thank you very much, but at moment to compile (with mikroC) tells me about several errors with sbit so i guess i have to download mikroC PRO, right?

Cheers

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: How to convert a mikroBasic code to mikroC?

#4 Post by tihomir.losic » 12 Apr 2010 23:28

Hello,

yes, my suggestion is that you try to use our latest version of mikroC PRO compiler.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

Post Reply

Return to “mikroC General”