CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Int_RB/I2C - Software serial problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ric_vas
Guest







Int_RB/I2C - Software serial problem
PostPosted: Fri Jul 25, 2003 8:21 am     Reply with quote

People,

I´m not able to use the following code to get the 2nd serial working (using Int_rb). I need to keep calling a rotine in main() all the time, and this is apparently causing me trouble. I'm missing the first bits on the characteres received.

Any help will be much appreciated!

Ric

#include <16F876.h>
#device *=16
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP,NOWRT
#define EEPROM_SCL PIN_C3
#define EEPROM_SDA PIN_C4
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3,FORCE_HW)
#use fast_io(B)
#use fast_io(A)
#use fast_io(C)

// configurações das portas de IO
#define PortAConfig 0b00011110
#define PortBConfig 0b11100000
#define PortCConfig 0b10111000

//Prioridade de Interrupcoes
#priority rb,rda,timer1

// PORTAS DE IO
#byte PORTA = 5
#byte PORTB = 6
#byte PORTC = 7

//#define BUFFER_SIZE 32
#define BUFFER_SIZE 2048

/////////////////////////////////////////////////Hardware
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7,bits=8,ERRORS)

int16 next_in1 = 0;
int16 next_out1 = 0;

//Interrupcao Serial Interna
#int_rda
void serial_isr1()
{
int16 t;
char caracter;
if(kbhit())
{
caracter = getc();
write_ext_eeprom(next_in1, caracter);
t=next_in1;
next_in1=(next_in1+1) \% BUFFER_SIZE;
if(next_in1==next_out1)
next_in1=t; // Buffer full !!
}

}

//Define Kbhit por Software
#define bkbhit1 (next_in1!=next_out1)

//Envia Caracter Serial Interna
void putc1(char c)
{
putc(c);
}

/////////////////////////////////////////////////Software#use rs232(baud=9600, parity=N, xmit=PIN_B0, rcv=PIN_B5,bits=8,ERRORS)

int16 next_in2 = 2048;
int16 next_out2 = 2048;

//Envia caracter Serial Externa
void putc2(char c) {
putc(c);
}

//Interrupcao Porta Serial Externa
#int_rb
void serial_isr2()
{
int16 t;
char caracter, temp;
if(!bit_test(PORTB,5))
{
caracter = getc();
write_ext_eeprom(next_in2, caracter);
putc(caracter); //just to check
t=next_in2;
next_in2=(next_in2+1) | (BUFFER_SIZE);
if(next_in2==next_out2)
next_in2=t; // Buffer full !!
}
temp = PORTB;
}

//Define Kbhit por Software
#define bkbhit2 (next_in2!=next_out2)

/////////////////////////////////////////////////Interrupcao Timer 1
#int_TIMER1
TIMER1_isr()
{
set_timer1(0x0DBD-get_timer1());
TimeCounter++;
if (TimeCounter > 10)
{
TimeCounter = 0;
if (FlagTypeInterna)
FlagTimeInterna = 1;
}
}

///////////////////////////////////////////////////////////////////////////////////////

void main()
{

int16 temp;

SET_TRIS_A( PortAConfig );
SET_TRIS_B( PortBConfig );
SET_TRIS_C( PortCConfig );

// 100ms
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
set_timer1(0x0DBD);

// interrupções
disable_interrupts(INT_RTCC);
disable_interrupts(INT_TIMER0);
disable_interrupts(INT_TIMER2);
disable_interrupts(INT_AD);
disable_interrupts(INT_TBE);
disable_interrupts(INT_SSP);
disable_interrupts(INT_CCP1);
disable_interrupts(INT_CCP2);
disable_interrupts(INT_EEPROM);
enable_interrupts(INT_RB);
enable_interrupts(INT_RDA);
enable_interrupts(INT_TIMER1);
enable_interrupts(global);

// FRAM
init_ext_eeprom();

// loop
while(1)
{
// Main State Machine
StateMachine();

//Check RAM(I2C)
ReadRAM(int16 add1, int16 add2);
}
}


void StateMachine()
{
....

}

void ReadRAM(int16 add1, int16 add2)
{
int16 temp;
char c;
temp = add1;
for (temp;temp<=add2;temp++)
{
c=read_ext_eeprom(temp);
...
}
}

byte read_ext_eeprom(long int address) {
byte data;
i2c_start();
i2c_write(0xa0);
i2c_write(hi(address));
i2c_write(address);
i2c_start();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516353
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Int_RB/I2C - Software serial problem
PostPosted: Fri Jul 25, 2003 1:45 pm     Reply with quote

:=People,
:=
:=I´m not able to use the following code to get the 2nd serial working (using Int_rb). I need to keep calling a rotine in main() all the time, and this is apparently causing me trouble. I'm missing the first bits on the characteres received.
---------------------------------------------------------

There are a few problems with your design.

Software RS232 uses the full resources of the PIC.
To do a putc(), the PIC must sit there and toggle the Tx
pin high and low, with software delays in between each bit
of the transmission. The PIC cannot do anything else during
this time. A similar process must occur with getc().

So, in fact, if you are doing anything else with your code,
which could delay or interrupt this process, then you will
lose characters. That's why people prefer to use the hardware
USART.

Also, you must disable interrupts when you are doing a putc(),
getc(), printf(), etc., with a software USART. If you don't
disable interrupts, the character bits will be corrupted.

When most people use a software and a hardware USART, they
use the hardware USART to receive (and maybe transmit), and
they use the software USART to transmit only. Then disable
interrupts during those transmissions. At least, that's how
I would do it.

Also, I have a comment on your software USART. Most people
would use Pin B0 as the Rx pin. Then, you can use the INT_EXT
interrupt to start receiving a character. For some reason,
you are not doing it this way. I suggest that you change your
code.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516369
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group