PIC to MAX232

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
I am getting wired characters on MikroC USART terminal. :eek:
The Rx LED does not want to turn off. That is why I get repeated "fo" character filling the screen. o_O
Feels like the PIC is giving me the 'Finger' :p
Sending a a command takes some time. even then the PIC is not responding to the AT command.

One question...Do I need pullups on the TX and RX pin between PIC and the MAX232 ?
 

AlbertHall

Joined Jun 4, 2014
12,346
You don't need pull-ups.
The first suspect is the baud rate which must match between transmitter and receiver.
Note that the PIC clock frequency must also be correct to get the expected baud rate.
If you have a 'scope you could check the actual transmitted rate otherwise post the code here and we can see if we can spot anything.
 

JohnInTX

Joined Jun 26, 2012
4,787
Send a 'U' (55h) from the PIC once and trigger your scope on a falling edge on the PIC TX line. 'U' gives a nice 0101010 pattern. Measure the bit time for the baud rate i.e. 104uS
Is 9600 baud (104us = 9600) and confirm good 5V logic levels on the TX line. Then look at the output of the MAX232. It should be the same except inverted and bigger. Then wrap the output of the MAX to its input (no PC). Send a U and break on RCIF. Inspect RCREG for U. If it is the same, you have basic comms.

If you are using a USB to RS232 cable know that some cheap ones use counterfeit chips that don't work well.
 

John P

Joined Oct 14, 2008
2,026
These days, an RS-232 port is about one step away from communication via smoke signals.

First check whether the MAX232 is generating the spurious characters itself. It has an internal oscillator, so maybe you've wired something to an output that's not meant as an external connection. If you disconnect the PIC, are you still seeing characters on the computer?

If the line is quiet, try a simple loopback test, with the connections that should go to the PIC RX and TX connected to each other. What the computer sends should come straight back again. If that passes, you're ready to try communicating with the processor.

I concur that a scope is pretty much essential for working with microcontrollers. I don't think I could accomplish much without one.
 

MaxHeadRoom

Joined Jul 18, 2013
28,696
These days, an RS-232 port is about one step away from communication via smoke signals.
There are still Many 1,000's of machines such as CNC M/C's that use RS232 for program loading, one advantage is you can transmit from one end of an industrial building to the other and select a baud rate that overcomes any noise issue.
Max.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
Darn ! And I expected to get a simple answer..:oops:
Oh well !
I am using internal osc at 8MHz.
The sample program I got works since I can see the LCD Display working. This program is giving me trouble. I am on leaning curve.

The maestro modem I am using works from the USART terminal program just with Tx and Rx connected, so no problem there. I made a RX232 adapter to interface with PIC. For now I am using the USART terminal to test.

I am venturing into uncharted territory and the first response I got starts with "F" :mad:

MikroC USART terminal can send ASCII, HEX, BIN and DEC if one needs to.

Below is the part of code using the UART1. PIC 16F887 at internal 8Mhz

C:
void interrupt(){
  if(INTCON.T0IF){
  //  BLAAAAAAAAH:
  }
  if(CCP1IE_bit){
  // More BLAAAAAAH;
  }
//******************************************************************************
  if(RCIE_bit){                       // RX interrupt enabled
    if(RCIF_bit){                     // If Interrupt occured
      Tmp = UART1_Read(); // get received byte
// process reception through state machine
// we are parsing only "OK" and "RING" responses
      switch (gsm_state) {
        case 0: {
          response = -1; // clear response
          if (Tmp == 'O') // we have 'O', it could be "OK"
            gsm_state = 1; // expecting 'K'
          if (Tmp == 'R') // we have 'R', it could be "RING"
            gsm_state = 10; // expecting 'I'
         break;
        }
        case 1: {
          if (Tmp == 'K') { // we have 'K' ->
            response = GSM_OK; // we have "OK" response
            gsm_state = 50; // expecting CR+LF
          }
C:
void main(){
OSCCON = 0x77;  // 8MHz Internal Osc.
ANSEL = 0x03;   // RA0 & RA1 as Analog Rest as Digital IO.
ANSELH = 0x00;  // Rest as Digital IO.
CM1CON0 = 0x07; // Disable Comparator 1.
CM2CON0 = 0x07; // Disable Comparator 2.
PORTA = 0;      // Clear PORT A.
PORTB = 0;      // Clear PORT B.
PORTC = 0;      // Clear PORT C.
PORTD = 0;      // Clear PORT D.
PORTE = 0;      // Clear PORT E.
TRISA = 0xFF;   // PORT A as Input.
TRISB = 0x00;   // PORT B as Output.
TRISC = 0xF8;   // RC <0-2> As Output, Rest as Input
TRISD = 0xFF;   //
TRISE = 0xFF;   // PORT E as Input.
//------- Initialte ADC & LCD & UART -------------------------------------------
ADC_Init();              // Innitiate LCD.
VCFG0_bit = 1;           // Externel Vref +.
VCFG1_bit = 0;           // Internal Vref - as GND.
Lcd_Init();              // Innitiate LCD.
Lcd_Cmd(_LCD_CLEAR);     // Clear LCD.
Lcd_Cmd(_LCD_CURSOR_OFF);// Cursor OFF.
Delay_ms(100);              // Wait to Stabilize.
UART1_Init(9600);           // Initialize UART module at 9600 baud.
Delay_ms(100);              // Wait for UART module to stabilize.
//------- Clear IRQ and Fire up the Timers -------------------------------------
OPTION_REG = 0x81;          // No pullups, Timer 0 at Prescaler 4.
INTCON = 0;                 // Disable All Interrupts First.
PIE1 = 0;                   // Disable All Peripheral Interrupts.
PIE2 = 0;                   // Disable All Peripheral Interrupts.
INTCON.T0IE = 1;            // Enable the Timer0 Interrupt.
INTCON.T0IF = 0;            // Clear Timer 0 Register Overflow Flag.
// TMR0 = TMR0set;           // Load Timer 0 with Preset.
T1CON = 0x30;               // No gate, Int Tcyc/8, TMR1 Stopped.
TMR1H = TMR1L = 0;          // Timer counts up to CCP value.
CCPR1 = 625;                // 1usTcyc * 8 Prescale * 625 = 5msec.
CCP1CON = 0x0B;             // NO Hardware PWM.
TMR1IF_bit = 0;             // PIR1 reg, Clear the TMR1 Overflow IRQ.
CCP1IF_bit = 0;             // Clear TMR1 IRQ flag bit.
RCIF_bit = 0;               // Clear Rx Interrupt Flag .................
T1CON.TMR1ON = 1;           // Start TIMER 1.
TMR1IE_bit = 1;             // Enable the Timer1 overflow Interrupt.
PIE1.RCIE = 1;              // Enable Rx Interrupt .................
CCP1IE_bit = 1;             // Enable the CCP1 Interrupt.
INTCON.GIE = 1;             // Global Interrupt Enabled.
  Lcd_Out(1,1,Const2Ram(String,lcd2));
  Lcd_Out(2,1,Const2Ram(String,lcd3));


while(1){
}
}
Above is a part of a fresh code I am starting since I got the " F " :D

Do I need to set any registry regarding UART besides
C:
UART1_Init(9600);          // Initialize UART module at 9600 baud.
RCIF_bit = 0;              // Clear Rx Interrupt Flag .................
PIE1.RCIE = 1;              // Enable Rx Interrupt .................
By the way I will check the above suggestions later tonight.
Thanks Guys. :)

PS...No USB to RS232. I tried that before, all it gave was BiiiiiG headache with the maestro Modem. So now Just Serial port ( luckily my PC has one ) to Modem
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
That code has Start.
I cleared that part and resend the text.

Why missing characters ?
My PC COM settings are identical to the terminal.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
Can you measure the voltages on pins 2 and 6 of the MAX232, they should be +8.5V and -8.5V respectively.
Around 9V

I checked as below.
C:
 while(1){
    UART1_Write_Text("Please Send SMS to this number ");
    UART1_Write(13);
    UART1_Write(10);
    delay_ms(2000);
    }

}
Terminal.png

Not one character is missing.
The PIC is sending text every 2 seconds..

So What's the deal here ?
Well atleast I can send AT command, EH !
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
Yureeka !
I managed to figure out the problem..
I had IRQ set up and it was causing a Rx issue.
I disabled the IRQ are it is working without dropping a single character..

So is it safe to assume my PIC is working with internal 8Mhz at 9600 baud ?
 

MaxHeadRoom

Joined Jul 18, 2013
28,696
If the baud rate was wrong you would see scrambled characters, when I use a UART and Pic, I usually use an interrupt on receive only.
Max.
 
Top