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.

mikroC SERIAL code PIC16f877a send , receive functions help

Status
Not open for further replies.

Sara89

Banned
Joined
Dec 22, 2013
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
0
hi i am doing a project and my prof asked us not to use any library,, so i am writing my own code and send, receive functions but i am having some troubles...
here's my code can anyone help plz as soon as possible ...
thank you



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
void USART_init(void);
void USART_trans_char(unsigned char x);
void USART_trans_string(char *z, int b);
void delay(void);
 
 
unsigned char myflag=0;
char AT[] = {'A','T'};
char CMGF[] = {'C','M','G','F'};
char CMGS[] = {'C','M','G','S'};
char msg[] = {'O','K'};
char phone[] = {'0','7','9','5','9','8','2','7','2','6'};
 
void main() {
USART_init ();
delay_ms(00);
USART_trans_string(AT, strlen(AT));
delay_ms(500);
USART_trans_char (13);
USART_trans_char (10);
 
 
USART_trans_string(AT,strlen(AT));
USART_trans_char (43);    //+
USART_trans_string (CMGF, strlen(CMGF));
USART_trans_char (61);    //=
USART_trans_char (49);   //1
USART_trans_char (13);
USART_trans_char (10);
delay_ms(2500);
 
USART_trans_string(AT,strlen(AT));
USART_trans_char (43);    //+
USART_trans_string (CMGS, strlen(CMGS));
USART_trans_char (61);                  //=
USART_trans_char (34);
USART_trans_string (phone, strlen (phone));
USART_trans_char (34);
USART_trans_char (13);
USART_trans_char (10);
delay_ms(2500);
 
USART_trans_string (msg, strlen(msg));
USART_trans_char (13);
USART_trans_char (10);
delay_ms(2500);
}
 
void USART_init(void){
   TXSTA=0x22; // low speed communication, asynchronous, Tx enable, 8 bit data
   RCSTA=0x90;// serial port enable, 8 bit data, continuous Rx, no address detection
   SPBRG=12;//9600 bps
   TRISC=0x80; //RC7 is Rx while RC6 is Tx
   INTCON=0xC0; // GIE and PEIE
   PIE1=PIE1|0x20;// Enable USART Rx interrupt
 
}
void USART_trans_char(unsigned char x){
    while(!(TXSTA& 0x02));
    TXREG=x;
}
 
 
void USART_trans_string(char* z, int b){
int i =0;
for (i=0; i<b; i++){
USART_trans_char(z[i]);
}}

 
Last edited by a moderator:

Hi;
Your main problem is that your char arrays (eg the AT) are not strings so the strlen function does not work (no end_sign).
Without strlen():

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
void USART_init(void);
void USART_trans_char(char x);
void USART_trans_string(char *z, char b);
void delay(void);
 
unsigned char myflag=0;
 
char AT[] = {'A','T'}; //len=2 but this is not a string !!
//better to use: 
//char AT[] = "AT"; //this is a valid string, strlen(AT) = 2
 
char CMGF[] = {'C','M','G','F'}; //ditto, len = 4
char CMGS[] = {'C','M','G','S'};
char msg[] = {'O','K'};
char phone[] = {'0','7','9','5','9','8','2','7','2','6'};
 
void main() {
   USART_init ();
   delay_ms(100);
   USART_trans_string(AT, 2); //in your case a fix number (here:2) must be used
   delay_ms(500);
   USART_trans_char (13);
   USART_trans_char (10);
 
 
   USART_trans_string(AT, 2);
   USART_trans_char (43);    //+
   USART_trans_string (CMGF, 4);
   USART_trans_char (61);    //=
   USART_trans_char (49);   //1
   USART_trans_char (13);
   USART_trans_char (10);
   delay_ms(2500);
 
   USART_trans_string(AT, 2);
   USART_trans_char (43);    //+
   USART_trans_string (CMGS, 4);
   USART_trans_char (61);                  //=
   USART_trans_char (34);
   USART_trans_string (phone, 10); //len of phone array;
   USART_trans_char (34);
   USART_trans_char (13);
   USART_trans_char (10);
   delay_ms(2500);
 
   USART_trans_string (msg, 2);
   USART_trans_char (13);
   USART_trans_char (10);
   //delay_ms(2500);
}
 
void USART_init(void) {
   TXSTA=0x22; // low speed communication, asynchronous, Tx enable, 8 bit data
   RCSTA=0x90;// serial port enable, 8 bit data, continuous Rx, no address detection
   SPBRG=12;//9600 bps
   TRISC=0x80; //RC7 is Rx while RC6 is Tx
   INTCON=0xC0; // GIE and PEIE
   PIE1=PIE1|0x20;// Enable USART Rx interrupt
}
 
void USART_trans_char(char x) {
    while(!(TXSTA& 0x02));
    TXREG=x;
}
 
void USART_trans_string(char* z, char b) {
    for ( ; b ; --b) USART_trans_char(*z++);
}

 
Last edited:
  • Like
Reactions: Sara89

    Sara89

    Points: 2
    Helpful Answer Positive Rating
https://saeedsolutions.blogspot.in/2012/11/pic16f877-uart-code-proteus-simulation.html

See if attached code works. Not tested.


If RCIE_bit is set then UART hangs after sending AT\r\n. I will debug and see why it is happening. For your project RCIE enabling is not needed as you are not receiving any data through UART.

101093d1389897238-test.png
 

Attachments

  • UART.rar
    35.8 KB · Views: 62
  • UART Working.rar
    99 KB · Views: 58
  • test.png
    test.png
    84 KB · Views: 134
Last edited:
  • Like
Reactions: Sara89

    Sara89

    Points: 2
    Helpful Answer Positive Rating
hello,

Give some details
what PIC ?
Quartz value or FOSC..
what is the problem , except this :

Code:
void main() {
USART_init ();
delay_ms(00);   [B][COLOR="#FF0000"]<- Problem because zero !![/COLOR][/B]
 
  • Like
Reactions: Sara89

    Sara89

    Points: 2
    Helpful Answer Positive Rating
She is using PIC16F877A at 16 MHz.


Found the problem. If CREN_bit is set then UART hangs after sending AT\r\n. Clearing CREN_bit works.


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
void USART_Init(void);
void USART_Write(unsigned char x);
void USART_Write_Text(char *z);
 
char AT[] = "AT\r\n";
char CMGF[] = "AT+CMGF=1\r\n";
char CMGS[] = "AT+CMGS=\"0795982726\"\r\n";     //0795982726
char msg[] = "Hi Sara";
 
void USART_init(void){
        TXSTA = 0x22; // low speed communication, asynchronous, Tx enable, 8 bit data
        RCSTA = 0x90;// serial port enable, 8 bit data, continuous Rx, no address detection
        SPBRG = 25;//9600 bps
        TRISC = 0x80; //RC7 is Rx while RC6 is Tx
        INTCON = 0xC0; // GIE and PEIE
 
 
}
 
void USART_Write(char x){
        while(!TRMT_bit);  // wait for previous transmission to finish
        TXREG = x;
 
}
 
void USART_Write_Text(char *z){
        while(*z)USART_Write(*z++);
}
 
 
void Interrupt(){
      if(RCIF_bit){
             if(OERR_bit){
                   CREN_bit = 0;
                   //CREN_bit = 1;
                   OERR_bit = 0;
             
             }
      
      
      }
      
      RCIF_bit = 0;
 
}
 
void main() {
 
        TRISA = 0xFF;
        
        USART_Init();
        Delay_ms(200);
        
        CREN_bit = 0;
        RCIE_bit = 1;
        
        while(1){
        
                  USART_Write_Text(AT);
                  Delay_ms(1000);
                  USART_Write_Text(CMGF);
                  Delay_ms(1000);
                  USART_Write_Text(CMGS);
                  Delay_ms(2000);
                  USART_Write_Text(msg);
                  USART_Write(0x1A);
                  Delay_ms(5000);
        }
}

 
Last edited:
  • Like
Reactions: Sara89

    Sara89

    Points: 2
    Helpful Answer Positive Rating
16Mhz.. so uart init is not correct..
i tested the program at 20Mhz with BRGH=1 and SPBRG=130;// OK ..9600 bps AT 20Mhz
for 16Mhz

Code:
void USART_init(void)
{
TXSTA=0; // 0x44  8bits TX enable Async  High speed(20Mhz!) 
TXSTA.BRGH=1;
TXSTA.TXEN=1;
RCSTA=0x90;// serial port enable, 8 bit data, continuous Rx, no address detection
//SPBRG=130;//9600 bps   AT 20Mhz   
SPBRG=104;//9600 bps   AT 16Mhz  with speed error less than -0.8%
TRISC=0x80; //RC7 is Rx while RC6 is Tx
INTCON=0xC0; // GIE and PEIE
PIE1=PIE1|0x20;// Enable USART Rx interrupt

}
 
  • Like
Reactions: Sara89

    Sara89

    Points: 2
    Helpful Answer Positive Rating
She was using BRGH = 0. For 16 MHz SPBRG = 25.

BRGH = 0

Fosc SPBRG

4 MHz 6
10 MHz 15
16 MHz 25
20 MHz 31


BRGH = 1

Fosc SPBRG

4 MHz 25
10 MHz 64
16 MHz 103
20 MHz 129
 
Last edited:

thank you all ,,, i've tried almost everything you mentioned but it didnt work,,, am using 8MHz ,,
is it possible that it isnt working because the interrupt and receiving code isn't fully included ,,, since if we send an AT command for example to the GSM it would reply with an OK and since the code doesnt cover that part??
am really confused... ?
could anyone help with a working GSM serial code ?
thank you

- - - Updated - - -

sir am sorry for this but this is 00 because i was editing my code but got confused and posted it here ,,, am using a delay_ms(1000) for this part
 
Last edited:

What is the SPBRG value you used for 8 MHz?


Here is the working code for 9600 bps, 8 MHz, SPBRG = 12 and BRGH = 0. Try to pull down the Rx and Tx lines of the microcontroller.


Connect your modem to PC UART or USB and try to send AT, AT+CMGF=1, AT+CMGS="number" and 0x1A commands in sequence and see if it sends the SMS.
 

Attachments

  • UART Working rev1.rar
    100.9 KB · Views: 74
Last edited:

MIKROC delay function

what is the default time for MikroC library delay function delay_ms()??
thank you
 

Re: MIKROC delay function

You can't use

Code C - [expand]
1
Delay_ms(0);

but you can use

Code C - [expand]
1
Delay_ms(1);

So, it is 1 ms. Use stop watch to calculate how much time it takes to execute

Code C - [expand]
1
Delay_ms(1);

 

Re: MIKROC delay function

thank you so much sir
sir i have another thing if portd is set as 0x20 which meand rd5 is set as input
how do we test rd5 for 1 ??
thank you
 

Re: MIKROC delay function


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
TRISD = 0x20;
PORTD = 0x00;
 
while(1){
 
    if(PORTD.F5){               //Is RD5 high?
        Delay_ms(50);
        if(PORTD.F5){          //Is RD5 still high?
              //your code here
        }
    }
 
}

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top