View previous topic :: View next topic |
Author |
Message |
delpiero
Joined: 31 Aug 2016 Posts: 4
|
I2C and UART not working together |
Posted: Wed Aug 31, 2016 2:08 am |
|
|
Hi,
I'm trying to use rs232 and i2c together. They are working individually but are not working together. If i send a byte from i2c, rs232 doesn't work.
Can you help me?
My code:
Code: |
#include <18F46K20.h>
#device ADC=16
#FUSES WDT //No Watch Dog Timer
#FUSES WDT2048 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOSTVREN //Stack full/underflow will not cause reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PROTECT
#FUSES INTRC_IO
#use delay(internal=16MHz)
#use i2c(master, sda=PIN_C4, scl=PIN_C3)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
void main()
{
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);
output_float(PIN_C4);
output_float(PIN_C3);
I2C_WriteByte(0x1F, 0x1F);
while(TRUE){
RESTART_WDT(); //watch dog timer reset
delay_ms(1000);
putc(0x51);
putc(0x53);
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Aug 31, 2016 2:10 am |
|
|
Your code as posted will crash.
If you enable an interrupt (and global), you must have a handler for this interrupt. Otherwise the code will jump back 'into itself', without a handler to go to.....
Also you say 'No watch dog timer', but you have the watchdog enabled, and timing out a little after the bytes are sent. Then you have the code dropping off the end, so the chip will go to sleep before the last two bytes are sent.... |
|
|
delpiero
Joined: 31 Aug 2016 Posts: 4
|
|
Posted: Wed Aug 31, 2016 3:02 am |
|
|
Ttelmah wrote: | Your code as posted will crash.
If you enable an interrupt (and global), you must have a handler for this interrupt. Otherwise the code will jump back 'into itself', without a handler to go to.....
Also you say 'No watch dog timer', but you have the watchdog enabled, and timing out a little after the bytes are sent. Then you have the code dropping off the end, so the chip will go to sleep before the last two bytes are sent.... |
Hi Ttelmah, thanks for your reply. i already restart to watch dog with RESTART_WDT() code. i changed the pic with another 18f46k20. Now uart and i2c are working.
can there be another problem? Because they were working individually. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Aug 31, 2016 6:16 am |
|
|
No. You restart the watchdog _once_. It will trigger on this chip about 4seconds after this, restarting from the sleep. So what happens is first serial byte starts to send, and the chip then goes to sleep. About three seconds later it watchdog's and tries again. Each time it may send one bit from the serial, but no more, if it gets this far (the interrupt may stop it).
Then you call an I2C_WriteByte function that doesn't exist. However assuming this is defined as a synonym for I2C_write, you call it with two values, which means it'll treat the first as a stream name. Since this stream doesn't exist, I doubt if it'll even compile. |
|
|
delpiero
Joined: 31 Aug 2016 Posts: 4
|
|
Posted: Mon Sep 05, 2016 6:12 am |
|
|
The problem continues. I can send uart data. I can read and write I2C. But if i read the data package(16 byte series), the program doesn't run.
My interrupt code part
Code: |
int x = 0;
short reply = 0;
int buffer[20];
#INT_RDA
void RDA_isr(void)
{
buffer[x] = getc();
if (++x >= buffer[0])
{
x = 0;
reply = 1;
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Mon Sep 05, 2016 6:54 am |
|
|
That's going to depend on what the first byte sent is. If it is >=20 (ASCII space character), then the code is going to start overwriting the rest of memory.... |
|
|
delpiero
Joined: 31 Aug 2016 Posts: 4
|
|
Posted: Mon Sep 05, 2016 7:03 am |
|
|
You are right. Thank you. It is working now. |
|
|
|