Hola a todos/as.
Os pongo un programita para el 16f84, pasa de Irda a Rs232, el emisor receptor está hecho con un TFDS4500,
El único problema que tengo es que a veces pierdo caracteres cuando paso de Irda a RS232, pero cuando sólo está esa parte en el pic no pierdo ni uno. La parte de Rs232 a Irda va sin problemas.
Me gustaría que me dijéseis forma de mejorarlo o alguna solución al problema que os comento, ya que no soy muy bueno programando en C.
ahí va.:
#include <16F84.H>
#use delay( clock = 10000000 )
#fuses HS, NOWDT, NOPROTECT
#use fast_io(B)
#use fast_io(A)
// ------------------IRDA
#define RxIr PIN_A3
#define TxIr PIN_B2
// ------------------Rs232
#define RxRs PIN_A0
#define TxRs PIN_B1
#define BPS 10
#define CER 5
#define UNO 4
byte ContCero;
int b[8];
struct {
short int RBIF;
short int INTF;
short int T0IF;
short int RBIE;
short int INTE;
short int T0IE;
short int PEIE;
short int GIE;
} INTCON;
#byte INTCON = 0x0B
// -----------------------------------------------------------------------
// BLOQUE RS232 -> IRDA
// -----------------------------------------------------------------------
void EnviarIr(){
// Inicio bit Start
delay_us ( BPS );
if ( !input (RxRs) ) {
output_high( TxIr );
delay_us ( UNO );
output_low( TxIr );
} else delay_us ( CER );
delay_us ( BPS );
// Fin bit Start
// Inicio bit 1
delay_us ( BPS );
if ( !input (RxRs) ) {
output_high( TxIr );
delay_us ( UNO );
output_low( TxIr );
} else delay_us ( CER );
delay_us ( BPS );
// Fin bit 1
// Inicio bit 2
delay_us ( BPS );
if ( !input (RxRs) ) {
output_high( TxIr );
delay_us ( UNO );
output_low( TxIr );
} else delay_us ( CER );
delay_us ( BPS );
// Fin bit 2
// Inicio bit 3
delay_us ( BPS );
if ( !input (RxRs) ) {
output_high( TxIr );
delay_us ( UNO );
output_low( TxIr );
} else delay_us ( CER );
delay_us ( BPS );
// Fin bit 3
// Inicio bit 4
delay_us ( BPS );
if ( !input (Rxrs) ) {
output_high( TxIr );
delay_us ( UNO );
output_low( TxIr );
} else delay_us ( CER );
delay_us ( BPS );
// Fin bit 4
// Inicio bit 5
delay_us ( BPS );
if ( !input (RxRs) ) {
output_high( TxIr );
delay_us ( UNO );
output_low( TxIr );
} else delay_us ( CER );
delay_us ( BPS );
// Fin bit 5
// Inicio bit 6
delay_us ( BPS );
if ( !input (RxRs) ) {
output_high( TxIr );
delay_us ( UNO );
output_low( TxIr );
} else delay_us ( CER );
delay_us ( BPS );
// Fin bit 6
// Inicio bit 7
delay_us ( BPS );
if ( !input (RxRs) ) {
output_high( TxIr );
delay_us ( UNO );
output_low( TxIr );
} else delay_us ( CER );
delay_us ( BPS );
// Fin bit 7
// Inicio bit 8
delay_us ( BPS );
if ( !input (RxRs) ) {
output_high( TxIr );
delay_us ( UNO );
output_low( TxIr );
} else delay_us ( CER );
delay_us ( BPS );
// Fin bit 8
// Inicio bit Stop
delay_us ( BPS );
// Fin bit Stop
}
// ---------------------------------------------------------------------------
// BLOQUE IR->RS
// ---------------------------------------------------------------------------
int getIRDABit() {
set_rtcc(256-13);
ContCero = 1 ;
INTCON.T0IF = 0;
while (!INTCON.T0IF) { // Mientras no Expire el tiempo
if (!input(RxIr)){
ContCero = 0; // Bit
while (!input(RxIr)); // Espera a que acabe el pulso
}
}
}
void RecibirIrda() {
getIRDABit();
b[7] = ContCero;
getIRDABit();
b[6] = ContCero;
getIRDABit();
b[5] = ContCero;
getIRDABit();
b[4] = ContCero;
getIRDABit();
b[3] = ContCero;
getIRDABit();
b[2] = ContCero;
getIRDABit();
b[1] = ContCero;
getIRDABit();
b[0] = ContCero;
}
void EnviarRs(){
int dela;
dela = 25;
OUTPUT_LOW (TxRs); // Bit De inicio 0
delay_us(25);
OUTPUT_HIGH (TxRs);
if (b[7]==1) {
OUTPUT_HIGH (TxRs); // 1
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
ELSE
{
OUTPUT_LOW (TxRs); // 0
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
if (b[6]==1) {
OUTPUT_HIGH (TxRs); // 1
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
ELSE
{
OUTPUT_LOW (TxRs); // 0
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
if (b[5]==1) {
OUTPUT_HIGH (TxRs); // 1
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
ELSE
{
OUTPUT_LOW (TxRs); // 0
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
if (b[4]==1) {
OUTPUT_HIGH (TxRs); // 1
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
ELSE
{
OUTPUT_LOW (TxRs); // 0
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
if (b[3]==1) {
OUTPUT_HIGH (TxRs); // 1
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
ELSE
{
OUTPUT_LOW (TxRs); // 0
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
if (b[2]==1) {
OUTPUT_HIGH (TxRs); // 1
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
ELSE
{
OUTPUT_LOW (TxRs); // 0
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
if (b[1]==1) {
OUTPUT_HIGH (TxRs); // 1
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
ELSE
{
OUTPUT_LOW (TxRs); // 0
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
if (b[0]==1) {
OUTPUT_HIGH (TxRs); // 1
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
ELSE
{
OUTPUT_LOW (TxRs); // 0
delay_us(dela);
OUTPUT_HIGH (TxRs);
}
OUTPUT_HIGH (TxRs); // Bit de Parada 1
delay_us(25);
OUTPUT_HIGH (TxRs);
}
CondicionesIniciales(){
set_tris_B( 0b00000000 );
set_tris_A( 0b11111 );
setup_counters(RTCC_INTERNAL,RtCC_Div_4);
output_high (TxRs);
OUTPUT_LOW (PIN_B0);
OUTPUT_LOW (PIN_B2);
OUTPUT_LOW (PIN_B4);
OUTPUT_LOW (PIN_B5);
OUTPUT_LOW (PIN_B6);
OUTPUT_LOW (PIN_B7);
ouTput_high (PIN_B3); // Chequeo
DELAY_MS(2000);
OUTPUT_LOW (PIN_B3);
}
void main() {
int i;
CondicionesIniciales(); // Parámetros iniciales
do {
// ---------------------------------------------------
// PARTE IRDA -> RS232
// ---------------------------------------------------
// ------------------- // Inicio StarBit
if (!input(RxIr)){ // entra con 0
while (!input(RxIr)); // Espera a que acabe el pulso
// ------------------- // Fin StarBit
RecibirIrda(); // Guardamos los 8 Bits del Codigo ASCII
EnviarRs();
}
// ---------------------------------------------------
// PARTE RS232 -> IRDA
// ---------------------------------------------------
else
{
if (!input(RxRs))
{
EnviarIr();
}
}
} while (TRUE);
}