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.

[PIC] Help require to convert the hi-tech c to mikro c.

Status
Not open for further replies.

hemghosh04

Newbie level 4
Newbie level 4
Joined
Jun 28, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
49
Hi,

Can anyone help me out to convert this HI-TECH C language to MIKRO C for Pic microcontroller.



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*sci.h file*/
 
#define FOSC    (4000000L)
#define SCI_EIGHT   (0)
#define SCI_NINE    (1)
 
unsigned char   sci_Init(unsigned long int, unsigned char);
void            sci_PutByte(unsigned char);
unsigned char   sci_GetByte(void);
void            sci_PutNinth(unsigned char);
unsigned char   sci_GetNinth(void);
unsigned char   sci_GetFERR(void);
unsigned char   sci_CheckOERR(void);
 
 
//
 
 
 
unsigned char
sci_GetByte(void)
{
    while(!RCIF)    /* set when register is not empty */
        continue;
 
    return RCREG;   /* RXD9 and FERR are gone now */
}
 
 
 
 
 
void SendByteSerially(unsigned char Byte)  //Writes a character to the serial port
 {
         while(!TXIF);  //wait for previous transmission to finish
         TXREG = Byte;
 }
 
 
 
 
void SendStringSerially(const unsigned char* st)
 {
          while(*st)
               SendByteSerially(*st++);
                 SendByteSerially('\n');
 }
 
 
 
 
unsigned char ReceiveByteSerially(void)   //Gets a character from the serial port
 {
          if(OERR) //if over run error, then reset the receiver
          {
                CREN = 0;
                CREN = 1;
           }
           while(!RCIF);  //wait for transmission to receive
           return RCREG;
 }
 
 
 
void start_lcd()
{   TRISB=0;
    Lcd4_Init();
    Lcd4_Set_Cursor(1,0);
    Lcd4_Write_String("    WELCOME     ");  
    Lcd4_Set_Cursor(2,0);
    Lcd4_Write_String("                 "); 
    __delay_ms(2000);
    Lcd4_Set_Cursor(1,0);
    Lcd4_Write_String("  INITIALISING  ");  
    Lcd4_Set_Cursor(2,0);
    Lcd4_Write_String("      GSM       ");
    SendStringSerially("AT\r");
    __delay_ms(3000);
    SendStringSerially("AT+CMGF=1\r");
    __delay_ms(3000);
    SendStringSerially("AT+CNMI=2,2,0,0,0\r");
        __delay_ms(2000);
    
}
 
 
void delete_sms()
{
        SendStringSerially("AT+CMGD=1\r");  
        SendByteSerially('\r'); 
        SendByteSerially(0x1a); 
}



Thanks.
 
Last edited by a moderator:

So much for the portability of 'C' code!

What error do you get when you try to compile? MikroC has some strange variations on the official language.

Brian.
 

MikroC has some strange variations on the official language.
Unfortunately there's no "official" (C-standard) syntax for the presentation of special function registers. mikro-C offers a set of built-in functions for hardware related actions. But you can also use direct SFR access similar to the Hightech code. As with other PIC Compilers, the syntax has to be modified a bit.
 


Here you are. Next time don't forget to tell which PIC.

Code:
/*sci.h file*/
 
#define FOSC    (4000000L)
#define SCI_EIGHT   (0)
#define SCI_NINE    (1)
 
unsigned char   sci_Init(unsigned long int, unsigned char);
void            sci_PutByte(unsigned char);
unsigned char   sci_GetByte(void);
void            sci_PutNinth(unsigned char);
unsigned char   sci_GetNinth(void);
unsigned char   sci_GetFERR(void);
unsigned char   sci_CheckOERR(void);
 
 
//
 
 
 
unsigned char
sci_GetByte(void)
{
    while(!RCIF)    /* set when register is not empty */
        continue;
 
    return RCREG;   /* RXD9 and FERR are gone now */
}
 
 
 
 
 
void SendByteSerially(unsigned char Byte)  //Writes a character to the serial port
 {
         while(!TXIF);  //wait for previous transmission to finish
         TXREG = Byte;
 }
 
 
 
 
void SendStringSerially(const unsigned char* st)
 {
          while(*st)
               SendByteSerially(*st++);
                 SendByteSerially('\n');
 }
 
 
 
 
unsigned char ReceiveByteSerially(void)   //Gets a character from the serial port
 {
          if(OERR) //if over run error, then reset the receiver
          {
                CREN = 0;
                CREN = 1;
           }
           while(!RCIF);  //wait for transmission to receive
           return RCREG;
 }
 
 
 
void start_lcd()
{   TRISB=0;
    Lcd_Init();
    //Lcd4_Set_Cursor(1,0);
    Lcd_Out(1,0,"    WELCOME     ");
    //Lcd_Set_Cursor(2,0);
    Lcd_Out(2,0,"                 ");
    Delay_ms(2000);
    //Lcd_Set_Cursor(1,0);
    Lcd_Out(1,0,"  INITIALISING  ");
    //Lcd_Set_Cursor(2,0);
    Lcd_Out(2,0,"      GSM       ");
    SendStringSerially("AT\r");
    Delay_ms(3000);
    SendStringSerially("AT+CMGF=1\r");
    Delay_ms(3000);
    SendStringSerially("AT+CNMI=2,2,0,0,0\r");
        Delay_ms(2000);
    
}
 
 
void delete_sms()
{
        SendStringSerially("AT+CMGD=1\r");  
        SendByteSerially('\r'); 
        SendByteSerially(0x1a); 
}
 

Hi Vase,

I am using PIC16F877A MICROCONTROLLER, if possible please help to convert.
 

No errors provides by the MikroC compiler, but serial port sent garbage value instead of AT command.
 

Zip and post the Hi-Tech C project files.

OERR, CREN, etc, have to be named like

Code:
OERR_bit
CREN_bit
RCIF_bit

Also mikroC PRO has UART library and it can be used instead.
 

Hi Milan,

My complete project file attached herewith,kindly help if possible. i Hav already convert some portion of this code but getting problem with UART convertion portion.Converted C file also attached.


Thanks
 

Attachments

  • original code.rar
    147 KB · Views: 97
  • partially converted.rar
    1.6 KB · Views: 97

Partially converted code. SMS_Read() has to be implemented.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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
 
char msg[21];
 
const char msg1[] = "    WELCOME     "; 
const char msg2[] = "                "; 
const char msg3[] = "  INITIALISING  "; 
const char msg4[] = "      GSM       ";
 
const char cmd1[] = "AT\r\n";
const char cmd2[] = "AT+CMGF=1\r\n";
const char cmd3[] = "AT+CNMI=2,2,0,0,0\r\n";
const char cmd4[] = "AT+CMGD=4,1\r\n";
 
char gsmBuffer[50];
char i = 0;
 
void interrupt() {
 
     if(OERR_bit) {
           OERR_bit = 0;       
           CREN_bit = 0;
           CREN_bit = 1;     
     }
     
     if(RCIF_bit) {
     
          gsmBuffer[i++] = RCREG;
          gsmBuffer[i] = '\0';     
                  
          RCIF_bit = 0;
     }
}    
// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src){
    char *d;
    
    d = dest;
    for(;*dest++ = *src++;);
    
    return d;
}
 
void Delate_SMS(const char *s1, char s2) {
     UART1_Write_Text(CopyConst2Ram(s2, s1));
     Delay_ms(2000);
}
 
 
 
void main() {
 
    CMCON = 0x07;
    
    ADCON1 = 0x87;
    
    TRISA = 0xC0;
    TRISB = 0x00;
    TRISC = 0x87;
    TRISD = 0x00;
    
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    
    LCD_Init();
    LCD_Cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
    
    LCD_Out(1,1,CopyConst2Ram(msg, msg1));  
    LCD_Out(2,1,CopyConst2Ram(msg, msg2));  
    Delay_ms(2000);
    LCD_Out(1,1,CopyConst2Ram(msg, msg3));  
    LCD_Out(2,1,CopyConst2Ram(msg, msg4));
    
    UART1_Init(9615);
    Delay_ms(200);
    
    
    UART1_Write_Text(CopyConst2Ram(msg, cmd1));
    Delay_ms(2000);
    UART1_Write_Text(CopyConst2Ram(msg, cmd2));
    Delay_ms(2000);
    UART1_Write_Text(CopyConst2Ram(msg, cmd3));
    Delay_ms(2000);
    
    Delate_SMS(cmd4, msg);
    
    RCIE_bit = 1;
    PEIE_bit = 1;
    GIE_bit = 1;
    
    RCIF_bit = 0;
     
    while(1) {
    
          
    
    }
}



- - - Updated - - -

What do you want to do ? Read SMS and control devices ?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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
 
char msg[21];
 
const char msg1[] = "    WELCOME     "; 
const char msg2[] = "                "; 
const char msg3[] = "  INITIALISING  "; 
const char msg4[] = "      GSM       ";
 
const char cmd1[] = "AT\r\n";
const char cmd2[] = "AT+CMGF=1\r\n";
const char cmd3[] = "AT+CNMI=2,1\r\n";
const char cmd4[] = "AT+CMGD=4,1\r\n";
 
const char resp1[] = "\r\nOK\r\n";
const char resp2[] = "+CMTI: \"SM\",";
 
char gsmBuffer[50], smsIndex[4];
char i = 0;
 
void interrupt() {
 
     if(OERR_bit) {
           OERR_bit = 0;       
           CREN_bit = 0;
           CREN_bit = 1;     
     }
     
     if(RCIF_bit) {
     
          gsmBuffer[i++] = RCREG;
          gsmBuffer[i] = '\0';     
                  
          RCIF_bit = 0;
     }
}    
// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src){
    char *d;
    
    d = dest;
    for(;*dest++ = *src++;);
    
    return d;
}
 
void Delate_SMS(const char *s1, char s2) {
     UART1_Write_Text(CopyConst2Ram(s2, s1));
     Delay_ms(2000);
}
 
void Get_SMS_Index(char *s1, char *s2) {
 
      while(*s1 != ',')
             *s1++;
 
      *s1++;
      while(*s1 != '\r') {
          *s2++ = *s1++;
      }
 
      *s2 = '\0';
}
 
void main() {
 
    CMCON = 0x07;
    
    ADCON1 = 0x87;
    
    TRISA = 0xC0;
    TRISB = 0x00;
    TRISC = 0x87;
    TRISD = 0x00;
    
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    
    LCD_Init();
    LCD_Cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
    
    LCD_Out(1,1,CopyConst2Ram(msg, msg1));  
    LCD_Out(2,1,CopyConst2Ram(msg, msg2));  
    Delay_ms(2000);
    LCD_Out(1,1,CopyConst2Ram(msg, msg3));  
    LCD_Out(2,1,CopyConst2Ram(msg, msg4));
    
    UART1_Init(9615);
    Delay_ms(200);
    
    
    UART1_Write_Text(CopyConst2Ram(msg, cmd1));
    Delay_ms(2000);
    UART1_Write_Text(CopyConst2Ram(msg, cmd2));
    Delay_ms(2000);
    UART1_Write_Text(CopyConst2Ram(msg, cmd3));
    Delay_ms(2000);
    
    Delate_SMS(cmd4, msg);
    
    RCIE_bit = 1;
    PEIE_bit = 1;
    GIE_bit = 1;
    
    RCIF_bit = 0;
     
    while(1) {
    
           if(strstr(gsmBuffer, CopyConst2Ram(msg, resp2))) {
                   Get_SMS_Index(gsmBuffer, smsIndex);
                   
           }
    
    }
}

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top