Max232N Connection problem or bug in firmware?

Status
Not open for further replies.

tonyyy

Member level 1
Joined
Dec 26, 2012
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,663
Hello guys.
I am trying to send chars to pic16876a using max232N (texas).
The simulation with proteus works just fine but with real circuit I am
facing with some strange beaviors. I have the following connections:

DB9 Connector <-> MAX232N

2 -> 14
3 -> 13
5-> gnd 15

max232n - PIc16876A

11 -> 17
12 -> 18

By using Terminal software I can see that max232 and cable are working. Infact
if I stick 11 and 12 togheter on max232 I got the
echo string in Terminal.

The firmare in pic is a simple Hitec C program which should give
me back data, here it is:


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
#include <stdio.h>
#include <htc.h>
#include "usart.h"
#include <stdlib.h>
 
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 10000000
#endif
 
void putch(unsigned char byte) 
{
while(!TXIF) //wait until TXIF is high
continue; 
TXREG = byte; //put byte into Transmit Register 
TXIF = 0;
}
 
 
unsigned char getch() {/* retrieve one byte */
while(!RCIF)    /* set when register is not empty */
continue;
RCIF = 0;
return RCREG;   
}
 
unsigned char
getche(void)
{
    unsigned char c;
    putch(c = getch());
    return c;
}
 
void init_enable()
{
    BRGH = 1;               /* high baud rate */
    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- or 9-bit transmission */
    RX9  = 0;               /* 8- or 9-bit reception */
    TXEN = 1;               /* enable the transmitter */
    RCIE =1; 
   PEIE =1;
} 
 
 
void main(void){
     //printf("TEST - 12/12/2013", '\n');
     char rxbyte;
     TRISB = 0;
     PORTB = 0;
     INTCON=0;     
     INIT_COMMS();    // set up the USART - settings defined in usart.h
     init_enable();
 
  // just blinking a led
 
            int a;
                a = 0;
                while(a < 5) {
                PORTB=0xFF;
                __delay_ms(300);
                PORTB=0x00;
                __delay_ms(300);
                a = a +1 ;
                }
 
    printf("********* WELCOME ***********", '\n');
    
 
     while(1){
        rxbyte= getche();
        unsigned char input;
        switch(rxbyte)
        {
            case '1':
            {          
            PORTB=0xFF;
            printf("led on", '\n');
            putch('\n');
            }
            break;
            case '2':
            {
            PORTB=0x00;
            printf("led off", '\n');
            putch('\n');
            }
            break;
            
            case '4':
            {
            int i;
            for(i=0;i!=100; i++)
                {
                   putch('a'); <- when i send 4 i don't get it back. But with PRoteus and PIMCOM works!
                }
            }   
            default :
            break;
 
        }
     }
 }





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
#ifndef _SERIAL_H_
#define _SERIAL_H_
 
#define BAUD 9600      
#define FOSC 10000000L
#define NINE 0     /* Use 9bit communication? FALSE=8bit */
 
#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1
 
#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif
 
#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif
 
#define RX_PIN TRISC7   
#define TX_PIN TRISC6   
       
/* Serial initialization */   
#define INIT_COMMS()\
    RX_PIN = 1;\
    TX_PIN = 1;\
    SPBRG = DIVIDER;\
    RCSTA = (NINE_BITS|0x90);\
    TXSTA = (SPEED|NINE_BITS|0x20)
 
 
void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);
 
#endif




The rs232 parameters are the same. What's wrong?


When i power on the pic I should get the led blinking on port b, but it doesn't. Components are 10Mhz 22pf , mclr to 5V.


Thanks
 

Sorry I mean I am not allowed to do it

- - - Updated - - -

Sorry I mean I am not allowed to do it
 

If the LEDs dont blink, your program may not even be running at all.

It seems like you dont have CONFIG statements your code.

It makes me think that maybe you failed to set the right CONFIG for your PIC hardware? Proteus might have set it for you automatically (i could be wrong here) during simulation.

Or, maybe you did set it manually in the MPLAB, but who knows.
 

Hi themask. Thanks for your reply.
I am trying of localizing the problem by symplifing the circuit an program as well. For instance I have eliminated the rs232 part. here the code and
configuration in mplab. Compier is hitech c Lite version.


OScillator HS OScillator
PWRTE Power-up Timer Enable bit PWRT disabled
BOREN Brown-out Reset Enable bit BOR enabled
LVP Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bitRB3/PGM pin has PGM function; low-voltage programming enabled
CPD Data EEPROM Memory Code Protection bit Data EEPROM code protection off
WRT Flash Program Memory Write Enable bits Write protection off; all program memory may be written to by EECON control
CP Flash Program Memory Code Protection bit Code protection off


Code:
#include <stdio.h>
#include <htc.h>
#include <stdlib.h>

#ifndef _XTAL_FREQ
#define _XTAL_FREQ 10000000
#endif


void main(void){
 	 TRISB = 0;
	 PORTB = 0;    
 	 int a;
	 a = 0;
	 while(a < 25) {
				PORTB=0xFF;
                __delay_ms(300);
				PORTB=0x00;
				__delay_ms(300);
 				a = a +1 ;
				}
     PORTB=0xFF; 
 }





I don't think is matter of circuit connection and components because I have tried it with asm version and it works. The asm version uses configuration word in
the code as:

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _FOSC_HS & _WRT_OFF & _LVP_OFF & _CPD_OFF
 

Hi.
Well, it is what i have done with code above. The configuration bits for C code above is specified with mplab. what should I do?
 

Hi.
Ok, I made some changes thanks your suggestion. I can get messages from the pic but not able to send commands. In the code
I see that I get both messages after and before while, but the swtch-case doens't work. What's wrong? Is the usart configuration ok?

Thanks




Code:
#include <stdio.h>
#include <htc.h>
#include "usart.h"
#include <stdlib.h>

 /* A simple demonstration of serial communications which
  * incorporates the on-board hardware USART of the Microchip
  * PIC16Fxxx series of devices. */

#ifndef _XTAL_FREQ
#define _XTAL_FREQ 10000000
#endif

// bit di configurazione
__CONFIG(CP_OFF  & BOREN_OFF & LVP_OFF & WDTE_OFF & PWRTE_ON & FOSC_HS );

void putch(unsigned char byte) 
{
    while(!TXIF)     //wait until TXIF is high
        continue;  
    TXREG = byte;    //put byte into Transmit Registe
}


unsigned char getch() {/* retrieve one byte */
while(!RCIF)    /* set when register is not empty */
continue;
return RCREG;   
}

unsigned char
getche(void)
{
	unsigned char c;
	putch(c = getch());
	return c;
}

void init_enable()
{
 	BRGH = 1;               /* high baud rate */
    SYNC = 0;               /* asynchronous */
    SPEN = 1;               /* enable serial port pins */
    CREN = 1;               /* enable reception */
    SREN = 0;               /* no effect */
    TXIE = 0;               /* disable tx interrupts */
    RCIE = 1;               /* disable rx interrupts */
    TX9  = 0;               /* 8- or 9-bit transmission */
    RX9  = 0;               /* 8- or 9-bit reception */
    TXEN = 1;               /* enable the transmitter */
	PEIE =1;
} 




void main(void){
	 char rxbyte;
 	 TRISB = 0;
	 PORTB = 0;
     INTCON=0;     
     INIT_COMMS();    // set up the USART - settings defined in usart.h
  	 init_enable();
		
  putch('a');
  putch('b');
  putch('c');
  printf("before while ", '\n');

 while(1){
	    rxbyte = getch();
	    putch('a');
		putch('b');
	 	putch('c');
		printf(" i am in while ", '\n');

		switch(rxbyte)
		{
			case '1':
  			{          
			PORTB=0xFF;
			printf("led on", '\n');
			putch('\n');
			}
			break;
			case '2':
			{
			PORTB=0x00;
			printf("led off", '\n');
			putch('\n');
			}
			break;
			case '3':
			{
				int a;
				a = 0;
				printf("blinking", '\n');
				while(a < 5) {
				PORTB=0xFF;
                __delay_ms(300);
				__delay_ms(300);
 				a = a +1 ;
				}
			}
			break;


			case '4':
			{
			int i;
			for(i=0;i!=100; i++)
				{
			       putch('a');
				}
			}

		case '5':
			{
			
			}
		
			default :
			break;

		}
     }

}
 

Right now this is the situation. I get the word "test" in tera term and led blink. I don't get "hello" when i type 1 in terminal and any charaters back
I really now don't have a clue what's going on. Seems that everything is ok. Connection db9-r232-pic is ok.

#include <stdio.h>
#include <htc.h>
#include "usart.h"


/* A simple demonstration of serial communications which
* incorporates the on-board hardware USART of the Microchip
* PIC16Fxxx series of devices. */

#ifndef _XTAL_FREQ
#define _XTAL_FREQ 10000000
#endif

// bit di configurazione
__CONFIG(CP_OFF & BOREN_OFF & LVP_OFF & WDTE_OFF & PWRTE_ON & FOSC_HS );

void putch(unsigned char byte)
{
while(!TXIF)
continue;
TXREG = byte;
}


char getch() {/* retrieve one byte */
while(!RCIF) /* set when register is not empty */
continue;
return RCREG;
}

unsigned char
getche(void)
{
unsigned char c;
putch(c = getch());
return c;
}

void init_enable()
{
//Passo 1
BRGH = 1; /* high baud rate */

// Passo 2
SPEN = 1;
SYNC = 0; /* async */
//passo 3
TXIE = 0; /* disable tx interrupts */
TX9 = 0; /* 8- or 9-bit transmission */
RX9 = 0; /* 8- or 9-bit reception */
// passo 4 attiva trasmissione
TXEN = 1; /* enable the transmitter */
RCIE = 0; /* disable rx interrupts */
// passo 5
CREN = 1; /* enable reception */
SREN = 0; /* no effect */
}



void main(void){
char rxbyte;
PORTB = 0;
TRISB = 0;
INTCON=0;
INIT_COMMS(); // set up the USART - settings defined in usart.h
init_enable();

while (1) {
int a;
a = 0;
while(a < 5) {
PORTB=0xFF;
__delay_ms(600);
PORTB=0x00;
__delay_ms(600);
a = a +1 ;
printf("test", '\n');


}
rxbyte = getch();

char input;
while(1){
input = getch(); // read a response from the user
printf("\rI detected [%c]\n",input);
if (rxbyte == '1') {
printf("hello");
}
}


}

}


#ifndef _SERIAL_H_
#define _SERIAL_H_

#define BAUD 9600
#define FOSC 10000000L
#define NINE 0 /* Use 9bit communication? FALSE=8bit */

#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

#define RX_PIN TRISC7
#define TX_PIN TRISC6

/* Serial initialization */
#define INIT_COMMS()\
RX_PIN = 0;\
TX_PIN = 1;\
SPBRG = DIVIDER;\
RCSTA = (NINE_BITS|0x90);\
TXSTA = (SPEED|NINE_BITS|0x20)

void putch(unsigned char);
char getch(void);
unsigned char getche(void);
#endif
 

Hmm...good hint. Where I should find it, cannot find in Setup.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…