Hello,
I've been having some difficulties making the damn serial communication to work.
For 3 days I couldn't sleep, eat, drink .. It's driving me crazy.
(Slightly exaggerated, but for real, I don't know what to do anymore)
So here is the scenario.
I've set up and connected the PIC16F689 with CP2102 (USB to UART Bridge) and with putty I get the output from the MCU but I can't for the love of what is holy send anything to it. It just doesn't cooperate.
Details :
Env : MPLAB X IDE 1.50
Programmer : PICkit3 (used to power the MCU as well, 3,3V)
Compiler Toolchain : XC8 (v1.11)
PIN 10 (RB7/TX/CK) connected to RX pin on CP2102
PIN 12 (RB5/AN11/RX/DT) connected to TR on CP2102
PIN 20 (Vss) connected to the GND on CP2102
I don't know how to draw schematics so here is a picture (just for completeness, I don't know If you can see anything from all the wires, but maybe you will see something else that I did wrong) :
Configuration :
Important Bits / Whats Going On / The Problem :
I've tried the code in Stimulus, and it works as expected .. Or so I think since
MPLABX Stimulus is a bit different from the older one in mplab 8.
The thing is in Stimulus here I can set the register RCREG, which apparently sets the RCIF interrupt bit as well. So it executes nicely.
However, a thing to notice is that it doesn't skip the while(!RCIF) at startup.
Somehow I think this is "cheating" and the actual transmission on the RX pin is skipped and the only way this works on Stimulus is because im directly setting the RCREG register.
For reference complete code :
*The page # is the page from the datasheet for memo
You can probably by now notice that I am beginner at this.
I always liked electronics and decided it was about time to learn it.
After reading datasheets, comparing other C code from PIC site samples, comparing assembler code from other sources and reading datasheets from those MCUs used in the examples, tried finding the similarities .. blah blah ..
No success .. And I'm stuck
So please, any advice / tips / pointers / hints / solutions are more than welcome.
I've been having some difficulties making the damn serial communication to work.
For 3 days I couldn't sleep, eat, drink .. It's driving me crazy.
(Slightly exaggerated, but for real, I don't know what to do anymore)
So here is the scenario.
I've set up and connected the PIC16F689 with CP2102 (USB to UART Bridge) and with putty I get the output from the MCU but I can't for the love of what is holy send anything to it. It just doesn't cooperate.
Details :
Env : MPLAB X IDE 1.50
Programmer : PICkit3 (used to power the MCU as well, 3,3V)
Compiler Toolchain : XC8 (v1.11)
PIN 10 (RB7/TX/CK) connected to RX pin on CP2102
PIN 12 (RB5/AN11/RX/DT) connected to TR on CP2102
PIN 20 (Vss) connected to the GND on CP2102
I don't know how to draw schematics so here is a picture (just for completeness, I don't know If you can see anything from all the wires, but maybe you will see something else that I did wrong) :
Configuration :
Rich (BB code):
// CONFIG
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
Rich (BB code):
//Set the bits / registers
CREN = 1; //Enable Receiver
TXEN = 1; //Enable Transmitter
SPEN = 1;
TXIE = 0; //Disable interrupts
RCIE = 0;
TX9 = 0; //Transmission Type - 8 bit (Disable 9bit comm)
RX9 = 0;
SYNC = 0; //async
BRGH = 1; //hi speed
BRG16 = 0;
SPBRG = 0x19;//Page 165 / BaudRate 9600
TXREG = 0x00;//Clear Transmit register
//So Everything is set up, I hook this up to pc and get the
//50x times of "Hello World \r\n"
for(i=0;i<50;i++)
transmitMsg(greet_msg);
//Then this segment
while(!RCIF)
{
CLEAR_USART_ERRORS;
}
//Just SKIPS over the first time, even though nothing is being sent
//For some reason RCIF is set as the RCREG register is full / buffered.
//This just makes the invalid value and all 4 LEDs light up
//On the next iteration if the while(1) loop [2nd iter to be exact]
while(!RCIF) ...
//Waits, and waits, and waits ....
MPLABX Stimulus is a bit different from the older one in mplab 8.
The thing is in Stimulus here I can set the register RCREG, which apparently sets the RCIF interrupt bit as well. So it executes nicely.
However, a thing to notice is that it doesn't skip the while(!RCIF) at startup.
Somehow I think this is "cheating" and the actual transmission on the RX pin is skipped and the only way this works on Stimulus is because im directly setting the RCREG register.
For reference complete code :
Rich (BB code):
#include <stdio.h>
#include <stdlib.h>
#include <pic16f689.h>
#define ITER_DELAY 25000
#define LED1 0x10 //RC5
#define LED2 0x01 //RC0
#define LED3 0x02 //RC1
#define LED4 0x08 //RC4
#define CLEAR_USART_ERRORS \
if (OERR)\
{\
TXEN = 0;\
TXEN = 1;\
CREN = 0;\
CREN = 1;\
}\
if (FERR)\
{\
TXEN = 0; \
TXEN = 1; \
}
void initSystem()
{
//Oscilator Control
//Page 50 - IRCF -- Default 110 -> 4MHz
//Port C - Outputs for PNP Transisedr Control (LEDs atm)
TRISC = 0x00;
PORTC = 0x00;
/**********
* EUSART *
**********/
//AsyncMode (pg 150)
//Transmit Status And Control Register (pg 160)
//Receive Status And Control Register (pg 161)
CREN = 1; //Enable Receiver
TXEN = 1; //Enable Transmitter
SPEN = 1;
//Disable interrupts
TXIE = 0;
RCIE = 0;
//Transmission Type - 8 bit (Disable 9bit comm)
TX9 = 0;
RX9 = 0;
SYNC = 0;
BRGH = 1;
BRG16 = 0;
//Baud Rate
SPBRG = 0x19;//Page 165 / BaudRate 9600
//RCREG - EUART Receive Register
//TXREG - EUART Transmit Register
TXREG = 0x00;//Clear Transmit register
}
void transmitMsg(register const char *msg)
{
char c;
while(c = *msg++)
{
while(!TXIF)
{
CLEAR_USART_ERRORS;
}
TXREG = c;
delay();
}
}
/*
* Main Program Entry
*/
int main(int argc, char** argv)
{
initSystem();
char greet_msg[] = "Hello World \r\n";
unsigned char tmp_rcreg = 'A';
unsigned int i = 0;
PORTC = LED4;
for(i=0;i<50;i++)
transmitMsg(greet_msg);
//Bit Mask - 0001 1011
//Valid input mask (0x1B)
while(1)
{
transmitMsg("Waiting for Input : \r\n");
//Wait for some data
while(!RCIF)
{
CLEAR_USART_ERRORS;
}
tmp_rcreg = RCREG;
if(tmp_rcreg == 'A')
PORTC = LED1;
else if(tmp_rcreg == 'B')
PORTC = LED2;
else if(tmp_rcreg == 'C')
PORTC = LED3;
else if(tmp_rcreg == 'D')
PORTC = LED4;
else
PORTC = LED1 | LED2 | LED3 | LED4;
//TXREG=tmp_rcreg;//tmp_rcreg;
// while(TRMT==0);//Wait till all bytes transmitted
transmitMsg(tmp_rcreg);
for(i=0;i<ITER_DELAY;i++);
}
return (EXIT_SUCCESS);
}
You can probably by now notice that I am beginner at this.
I always liked electronics and decided it was about time to learn it.
After reading datasheets, comparing other C code from PIC site samples, comparing assembler code from other sources and reading datasheets from those MCUs used in the examples, tried finding the similarities .. blah blah ..
No success .. And I'm stuck
So please, any advice / tips / pointers / hints / solutions are more than welcome.