View previous topic :: View next topic |
Author |
Message |
embedder
Joined: 11 Feb 2010 Posts: 19
|
PIC18F85K22 UART2 |
Posted: Fri Apr 08, 2011 9:27 am |
|
|
I am having a problem getting UART2 to work on PIC18F85K22. I can see valid characters getting to the RX2 pin (7 / RG2) on the 'scope, but no response or break from from kbhit() or fgetc(). I tried changing the port to a SW UART, but no luck. UART2 transmits OK, but no RX. UART1 works fine. Is there a bug or have I missed something "obvious"? Thanks for reading...
Code: |
#include <18F85K22.h>
#define DEBUG_MODE // comment-out for stand-alone operation
#ifdef DEBUG_MODE
#device ICD=TRUE
#endif
#ifdef DEBUG_MODE
#FUSES MCLR
#else
#FUSES NOMCLR
#endif
#device adc=12
#FUSES NOWDT // No Watch Dog Timer
#FUSES WDT128 // Watch Dog Timer uses 1:128 Postscale
#FUSES SOSC_DIG // Enables C0 & C1 as digital I/O
#FUSES NOXINST // Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PUT // Power Up Timer
#FUSES NOBROWNOUT // No brownout reset
#FUSES WDT_NOSLEEP
#FUSES NOEXTADDRSFT
#FUSES RTCOSC_INT
#FUSES HSM
#FUSES INTRC_HP
#FUSES PLLEN
#use delay(clock=16000000)
#use rs232(UART1, baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=COM_A,errors)
#use rs232(UART2, baud=9600,parity=N,xmit=PIN_G1,rcv=PIN_G2,stream=COM_B,errors)
// RS232
#define MDX_SHDN PIN_G0
#define MD1_TXEN PIN_C0
#define MD1_REN PIN_C1
#define MD2_TXEN PIN_G3
#define MD2_REN PIN_G4
char cmd;
void init()
{
// RS232
output_high(MDX_SHDN); // MAX3323 ON
output_high(MD1_REN); // Rx enable
output_high(MD1_TXEN); // Tx enable
output_high(MD2_REN); // Rx enable
output_high(MD2_TXEN); // Tx enable
delay_ms(20);
}
void main()
{
disable_interrupts(GLOBAL);
setup_adc_ports(ALL_ANALOG|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
setup_wdt(WDT_OFF);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_oscillator(OSC_16MHZ);
enable_interrupts(GLOBAL);
init();
fprintf(COM_A, "NYMPH COM_A\r\n");
fprintf(COM_B, "NYMPH COM_B\r\n");
while (TRUE) {
if(kbhit(COM_A)){
cmd=fgetc(COM_A);
fputc(cmd,COM_A);
}
if(kbhit(COM_B)){
cmd=fgetc(COM_B);
fputc(cmd,COM_B);
}
}
reset_cpu();
}
|
|
|
|
embedder
Joined: 11 Feb 2010 Posts: 19
|
|
Posted: Fri Apr 08, 2011 9:35 am |
|
|
Compiler V4.119 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 08, 2011 5:01 pm |
|
|
Quote: | void main()
{
disable_interrupts(GLOBAL);
setup_adc_ports(ALL_ANALOG|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
setup_wdt(WDT_OFF); |
The UART2 pins are set as analog pins by the line in bold.
They should be configured as digital pins.
You are setting ALL 24 pins that can be analog, as analog pins.
You should change it to select the specific analog pins that you need.
There are 24 analog-configurable pins on this PIC.
Also, in your program you are enabling/disabling Global interrupts.
There is no need for this. You have no interrupt routines. |
|
|
embedder
Joined: 11 Feb 2010 Posts: 19
|
|
Posted: Fri Apr 08, 2011 5:17 pm |
|
|
Excellent catch. Thank you!
(I plan to have some interrupt routines in the near future). |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Fri Apr 08, 2011 6:31 pm |
|
|
Ever since the design guys figured out you could 'multipurpose' a pin, it's been a battle to get the 'correct' configuration.
Things were easier back in the days of 8008s, teletypes and papertape ! |
|
|
embedder
Joined: 11 Feb 2010 Posts: 19
|
|
Posted: Mon Apr 11, 2011 8:18 am |
|
|
That was it PCM programmer. Thanks for your help. Caught-out by the doubled edged sword of multi-purpose pins. |
|
|
|