Author Topic: PIC USART communication problem  (Read 2121 times)

0 Members and 1 Guest are viewing this topic.

Offline wicketTopic starter

  • Contributor
  • Posts: 35
PIC USART communication problem
« on: March 17, 2014, 09:02:38 am »
Thread moved to Microcontrollers & FPGA's !
https://www.eevblog.com/forum/microcontrollers/pic-usart-communication-problem-28340/


Hi,

I'm using a PIC16F628A USART to communicate with my SE T610 GSM. MikroC compiler.

Circuit:


How program works - when RB0 goes high, uC transmits "at" command to my GSM and it responds "OK". I've included a char compare routine to see if the response is indeed "OK". And it works - if it receives chars O, K, \n or \r the LED turns on.

My problem - I'm trying to use the exact same program and circuit (with appropriate pin adjustments, of course) with my PIC18F4520 and PIC16F887 but it doesn't work.

Transmittion works fine, I've tried "atd" command instead of "at", and GSM makes a call, which means transmission is ok. And uC also receives something back, because interrupt does occur (i've tested it), but when I compare incoming chars, none of them are what they are supposed to be ("OK\n\r").

I've also connected the GSM to my PC directly and tested it using putty to make sure it's not GSMs fault. It works perfectly.
Voltage levels of Tx and Rx pins are correct, I've also tried different Fosc's and baudrates, but couldn't make it work. Unfortunatley I don't have an access to any oscilloscope to check what am I actually receiving.

I don't understand why it works on 16F628A, but not on other two PICs. I suspect it has something to do with USART configuration. I've read all 3 datasheets but it seems to me nothing should change regarding USART init.

I would really appreciate any help.

Code from PIC16F887, where I'm using PORD intead of PORTB I/O:
Code: [Select]
volatile unsigned char uart_rd = 0; // serial input byte (RCREG)
unsigned int b = 0;

// UART Initialization
void uartinit()
{
UART1_Init(9600); // initialize UART module at 9600 bps
Delay_ms(100); // let it stabilize
PIE1.B5 = 1; // turn on USART interrupt bit RCIE
}

// UART Interrupt: Receive character
void interrupt()
{
if (PIR1.B5) // if interrupt occurs - if RCIF = 1(RCREG full)
{
uart_rd = RCREG; // read incoming byte (character)
if (uart_rd == 79) PORTD.B1 = 1; //if incoming byte is "O", turn on LED
PIR1.B5 = 0; // reset RCIF flag bit to 0
}
}

// Main program
void main()
{
uartinit();

INTCON = 0b11000000; // enable PEIE and GIE interrupts
ADCON1 = 0b0111; // configure all pins as digital
TRISD = 0b01;
PORTD = 0b00;

while (1)
{
if ((PORTD.B0 == 1) && (b == 0)) // if input goes high
{
b = 1; // set flag, so this if loop is disabled in future
UART1_Write_Text("at"); // phones response should be "OK"
UART1_Write(13); // \r
}
}
}
« Last Edit: March 17, 2014, 10:01:55 am by wicket »
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9875
  • Country: nz
Re: PIC USART communication problem
« Reply #1 on: March 17, 2014, 09:28:49 am »
Both are running from the same speed RC osc right?
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline wicketTopic starter

  • Contributor
  • Posts: 35
Re: PIC USART communication problem
« Reply #2 on: March 17, 2014, 09:39:12 am »
With 16F628A and 16F887  I'm using internal osc @ 4MHz, as with 18F4520 I'm using HS ocs @ 10MHz. If I'm not mistaken, uC is also driving GSM's clock?
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9875
  • Country: nz
Re: PIC USART communication problem
« Reply #3 on: March 17, 2014, 09:58:55 am »
Im not sure how PICs do it but check your code to make sure the UART1_Init()  function is taking the correct MCU clock speed into account. Otherwise the baudrate will be totally wrong with a different speed mcu.

On AVRs this is done with a #define statement so all functions to do with timing can work out the correct delays. Dunno about PICs
« Last Edit: March 17, 2014, 10:01:14 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline wicketTopic starter

  • Contributor
  • Posts: 35
Re: PIC USART communication problem
« Reply #4 on: March 17, 2014, 10:03:51 am »
Generated baudrate is 9615 bps (0.16 % error), which I believe is OK.

Thread moved to Microcontrollers & FPGA's !
https://www.eevblog.com/forum/microcontrollers/pic-usart-communication-problem-28340/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf