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.

PIC16F877A UART Interface with PC

Status
Not open for further replies.

DHANANJAYA L

Banned
Junior Member level 2
Joined
Jan 21, 2009
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
0
Hi, I have problem... for pic16f877a micro controller interface with PC(computer) through RS232 ...


I have written the c code to print character like 'A','B', ...and numbers '0','1',.....it will print on copmputer on hyperterminal page for not corresponding characters like p,q,r.....
I think shall i will convert ASCII to HEX or Hex to ASCII in the programme...or add 0x30 for numbers and characters to 0x37...
otherwise any thing else problem...pleas let me..know
 

It is not real clear what you are asking. Is it you can send digits (0,1,2,....) and upper case (A,B,C...) but not lower case (a,b,c...)? If you are using a c compiler, then it should not matter if a character is upper or lower case. Somewhere, there seems to be a conversion of lower case to upper case. This can be in the microcontroller or the PC. I suspect it is a setting in the terminal emulator program on the PC. Please check your settings. If it was in the microcontroller, the conversion would probably be quite obvious. If you suspect the c code, could you post it?
 

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
void main()
{
uart_init();
TX(‘A’);
TX(‘B’);
.
.
.
TX(‘Y’);
TX(‘Z’);
TX(‘a’);
TX(‘b’);
.
.
.
TX(‘y’);
TX(‘z’);
TX(0);
TX(1);
.
.
 
TX(8);
TX(9);
 
 
 
 
 
}
void uart_init(void)
{
TRISC7 = 1;
TRISC6 = 0;
SPBRG =6;     // 4 Mhz 9600 budrate
TXSTA = 0×20;
RCIE =1 ; GIE =1; PEIE =1;
RCSTA = 0×90;
}
void interrupt receive(void)
{
if(RCIE && RCIF)
{
RCIF = 0;
TX(RCREG);
}
}
void TX(unsigned char TX_BYTE)
{
TXREG = TX_BYTE;
while(!TRMT);
}



what I have written this char code it will not print the same char it will print som other char and symbols..
 
Last edited by a moderator:

I think your baud rate setting is wrong you mostly need to put SPBRG = 25 for 9600 baud rate at 4 MHz crystal oscillator -

refer the code below -


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
void serial_setup(void)
{
    /* relates crystal freq to baud rate - see above and PIC16F87x data sheet under 'USART async. modes'
 
    BRGH=1, Fosc=3.6864MHz      BRGH=1, Fosc=4MHz      BRGH=1, Fosc=8MHz    BRGH=1, Fosc=16MHz
    ----------------------    -----------------      -----------------    ------------------
    Baud        SPBRG             Baud      SPBRG          Baud     SPBRG         Baud      SPBRG
    1200        191               1200      207.3          1200     415.7         9600    103
    2400        95                2400      103.2          2400     207.3         19200     51
    4800        47                4800      51.1           4800     103.2         38400   25
    9600        23                9600      25.0           9600     51.1          57600   16
    19200       11                19200     12.0           19200    25.0          115200  8
    38400       5                 38400     5.5            38400    12.0
    57600       3                 57600     3.3            57600    7.7
    115200  1                 115200    1.2            115200   3.3
 
    */
 
    /*
      Comms setup:
     */
 
    #define BAUD 19200
    #define DIVIDER 64
    //#define DIVIDER ((PIC_CLK/(16UL * BAUD) -1))
    #define HIGH_SPEED 1
 
    //you can comment these #assert statements out if you dont want error checking
//  #if PIC_CLK==3686400 && BAUD==19200
//      #assert DIVIDER==11
//  #elif PIC_CLK==4000000 && BAUD==19200
//      #assert DIVIDER==12
//  #elif PIC_CLK==16000000 && BAUD==19200
//      #assert DIVIDER==51
//  #elif PIC_CLK==20000000 && BAUD==19200
//      #assert DIVIDER==64
//  #endif
 
    SPBRG=DIVIDER;
    BRGH=HIGH_SPEED;    //data rate for sending
    SYNC=0;                     //asynchronous
    SPEN=1;                     //enable serial port pins
    CREN=1;                     //enable reception
    SREN=0;                     //no effect
    TXIE=0;                     //disable tx interrupts
    RCIE=0;                     //disable rx interrupts
    TX9=0;                      //8-bit transmission
    RX9=0;                      //8-bit reception
    TXEN=0;                     //reset transmitter
    TXEN=1;                     //enable the transmitter
}
 
//unsigned char dummy;
 
/*#define clear_usart_errors_inline \
//if (OERR)                                                 \
//{                                                                 \
//  TXEN=0;                                                 \
//  TXEN=1;                                                 \
//  CREN=0;                                                 \
//  CREN=1;                                                 \
//}                                                                 \
//if (FERR)                                                 \
//{                                                                 \
//  dummy=RCREG;                                        \
//  TXEN=0;                                                 \
//  TXEN=1;                                                 \
//}
 
*/
//writes a character to the serial port
void putch1(unsigned char c)
{
    while(!TXIF)            //set when register is empty
    {
    //  clear_usart_errors_inline;
        CLRWDT;
//      CLRWDT();
    }
    TXREG=c;
    //DelayUs(60);
}
 
 
 
void putst1(unsigned char *str)
{
    while((*str)!=0)
    {
        putch1(*str);
//    if (*str==13) putch1(10);
//    if (*str==10) putch1(13);
        str++;
    }
}



Good Luck
 

The code looks OK but you may have a hardware problem. Which PIC are you using and how do you have it connected to the computer? Are you using RS232 and if so, do you have a suitable TTL/RS232 level converter in line with the signal in both directions?

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top