PIC USART communication problem - Page 1
Forum section: electronics / beginners

author: wicket
author_rank: Contributor
timestamp: March 17, 2014, 09:02:38 am
content: 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 charsO,K,\nor\rthe 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:volatile unsigned char uart_rd = 0; // serial input byte (RCREG)unsigned int b = 0;// UART Initializationvoid uartinit(){UART1_Init(9600); // initialize UART module at 9600 bpsDelay_ms(100); // let it stabilizePIE1.B5 = 1; // turn on USART interrupt bit RCIE}// UART Interrupt: Receive charactervoid 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 LEDPIR1.B5 = 0; // reset RCIF flag bit to 0}}// Main programvoid main(){uartinit();INTCON = 0b11000000; // enable PEIE and GIE interruptsADCON1 = 0b0111; // configure all pins as digitalTRISD = 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 futureUART1_Write_Text("at"); // phones response should be "OK"UART1_Write(13); // \r}}}

author: Psi
author_rank: Super Contributor
timestamp: March 17, 2014, 09:28:49 am
content: Both are running from the same speed RC osc right?

author: wicket
author_rank: Contributor
timestamp: March 17, 2014, 09:39:12 am
content: 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?

author: Psi
author_rank: Super Contributor
timestamp: March 17, 2014, 09:58:55 am
content: 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

author: wicket
author_rank: Contributor
timestamp: March 17, 2014, 10:03:51 am
content: 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/