Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/14/05 22:38
Read: times


 
#101058 - Serial communication between 2 at89c52
I have 2 mcu at89c52 (8052 - 8051). i need to send from one to another some information. i'm using the serial port but when
i've tried to ensamble the circuit the results are not like i want. i've been using sdcc to make the code for the mcu.

here is the code for the pic #1 program, the master:

/***********************************************************************************************************/

#include <8052.h>
#include "serial.h"
#include <stdio.h>

//procedimientos del juego
void inicializar();
void visualizar_display(char,long,bit,bit);
void restraso();
void capturar_entradas() interrupt 0;
void descargar_salidas() interrupt 1;

#define V 1
#define F 0

#define direccion_local 0x01
#define display_credito 0x02
#define display_apuesta 0x03
#define display_doblada 0x04
#define display_noapostado 0x05
#define display_nopremio 0x06
#define display_premio 0x07

//variables del juego
long total_entradas, total_salidas;
long creditos, ganado;
float pozo_juego, pozo_doblada;
unsigned int numero_juego;
unsigned char porcentaje_pago[]={65, 70, 75, 80, 85, 90};
unsigned int tabla_premios[][]={{400, 50}, {500, 60}, {600, 60}, {700, 70}, {800, 80}};
unsigned char tabla_apuestas[]={1, 2, 3, 4, 5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100};
unsigned char creditos_por_pulsos[]={1, 2, 5, 10, 20, 25, 50, 100};
unsigned char apuesta, valor_credito, ind_apuesta;
unsigned char error;

sbit at 0x92 btnN1;
sbit at 0x93 btnN2;
sbit at 0x94 btnN3;
sbit at 0x95 btnDbl1;
sbit at 0x96 btnDbl2;
sbit at 0x97 btnDbl;
sbit at 0xA6 btnIniciar;
sbit at 0xA7 btnApuesta;
sbit at 0xB2 swEnt;
sbit at 0xB3 swSal;
sbit at 0xB4 swLlave;
sbit at 0xA0 swPago0;
sbit at 0xA1 swPago1;
sbit at 0xA2 swPago2;
sbit at 0xA3 swPremio0;
sbit at 0xA4 swPremio1;
sbit at 0xA5 swPremio2;
sbit at 0xB5 swCred0;
sbit at 0xB6 swCred1;
sbit at 0xB7 swCred2;

void main (void){
inicializar();
while(1){
while(error==0){
visualizar_display(display_premio,ganado,F,F);
visualizar_display(display_credito,creditos,F,V);
visualizar_display(display_apuesta,tabla_apuestas[ind_apuesta],F,V);
visualizar_display(display_doblada,0,F,F);
visualizar_display(display_noapostado,0,V,V); //cambiar el numero por la variable
visualizar_display(display_nopremio,0,V,V); //cambiar el numero por la variable
}
}
}

void retraso(){
unsigned char i;
P1=ind_trama;
for(i=0;i<255;i++){
}
}

void inicializar(){
char i;

//inicializacion de las interrupciones
ET0=0; //desactiva la interrupcion del TIMER 0
ET1=0; //desactiva la interrupcion del TIMER 1
ET2=0; //desactiva la interrupcion del TIMER 2
EX0=1; //activa la interrupcion externa 0 INT0
EX1=1; //activa la interrupcion externa 1 INT1
cfg_pto_serial(); //configura el puerto serial
EA=1; //activa las interrupciones


//inicializacion de las variables
total_entradas=0;
total_salidas=0;
creditos=0;
ganado=0;
ind_apuesta=0;
numero_juego=0;
pozo_juego=3000;
pozo_doblada=1500;
error=0;
apuesta=tabla_apuestas[ind_apuesta];

//inicializacion de los displays
for(i=0;i<10;i++){
visualizar_display(display_premio,i,V,V);
visualizar_display(display_credito,i,V,V);
visualizar_display(display_apuesta,i,V,V);
visualizar_display(display_doblada,i,V,V);
visualizar_display(display_noapostado,i,V,V);
visualizar_display(display_nopremio,i,V,V);
retraso();
}
}

void visualizar_display(char display, long numero, bit ceros, bit ultDig){
unsigned char bufferEnv[]={0xff, 0xff, 0xff};
unsigned char i,k,vartmp;

k=-1;
for(i=0;i<6;i++){
vartmp=(unsigned char)numero%10;
if(i%2!=0){
vartmp=vartmp<<4;
vartmp=vartmp&0xf0;
}
else{
vartmp=vartmp&0x0f;
k++;
}
if((numero==0)&&(ceros==F)){
vartmp=0xff;
}
bufferEnv[k]=bufferEnv[k]|vartmp;
numero=numero/10;
}
if((ultDig==V)&&(bufferEnv[0]==0xff)){
bufferEnv[0]=0;
}
enviar_comando(display,0,bufferEnv);
}

void capturar_entradas() interrupt 0{
unsigned char vartmp=P3>>5;
valor_credito=creditos_por_pulsos[vartmp];
creditos=creditos+valor_credito;
total_entradas=total_entradas+valor_credito;
}

void descargar_salidas() interrupt 1{
total_salidas=total_salidas+creditos;
creditos=0;
}


/***********************************************************************************************************/

this is the code for the pic #2, the slave:

/***********************************************************************************************************/

#include <8052.h>
#include " serial.h"
#include <stdio.h>

//procedimientos
void inicializar();

#define V 1
#define F 0

#define direccion_master 0x01
#define direccion_local 0x02

//variables
unsigned char error;

void main (void){
inicializar();
while(1){
while(error==0){
if(ind_trama==4){
//el comando es para mi
if(trama[0]==direccion_local){
P2=trama[2];
}
ind_trama=0;
}
P1=ind_trama;
}
}
}

void inicializar(){
//inicializacion de las interrupciones
ET0=0; //desactiva la interrupcion del TIMER 0
ET1=0; //desactiva la interrupcion del TIMER 1
ET2=0; //desactiva la interrupcion del TIMER 2
EX0=0; //activa la interrupcion externa 0 INT0
EX1=0; //activa la interrupcion externa 1 INT1
cfg_pto_serial(); //configura el puerto serial
EA=1; //activa las interrupciones


//inicializacion de las variables
error=0;
ind_trama=0;
}


/***********************************************************************************************************/

this is the include serial.h:

/***********************************************************************************************************/

#include <8052.h>

unsigned char ind_trama, trama[5];

void cfg_pto_serial(){
SCON=0xF0; //uart en modo 3 (9 bit), REN=1
TMOD=TMOD|0x20; //Timer 1 en modo 2
TH1=0xFD; //19200 Bds
TL1=0xFD; //19200 Bds
ES=1; //activa la interrupcion del PUERTO SERIAL
TR1=1; //correr el Timer 1
}

void puerto_serial_event() interrupt 4{
if(TI==1){ //si envia datos
TI=0; //limpia la bandera de transmision
}

if (RI==1){ //si recibe datos
RI=0; //limpia la bandera de recepcion
if(RB8){
SM2=0; //modo de datos 2
}
else{
ind_trama++;
if(ind_trama>4){
ind_trama=0;
}
trama[ind_trama]=SBUF;
SM2=1; //modo de datos 1
}
}
}

void enviar_comando(unsigned char direccion, unsigned char comando, unsigned char parametros[3]){
unsigned char i;
SBUF=direccion;
while(TI==1);
SBUF=comando;
while(TI==1);
for(i=0;i<3;i++){
trama[2+i]=parametros[i];
while(TI==1);
}
}


in the circuit i've put the pins 40 and 31 to 5v, 20 to GND, the pin 10 (RXD) of the master (pic #1) to the pin 11 (TXD) of

the slave (pic #2) and the pin 11 (TXD) of the master to pin 10 of the slave (RXD). the pins 21 to 28 are conecteds to 7447.

i'm using the SUPERPRO/Z to program the at89c52, i've opened the juego.ihx in intel hex format file and program the at89c52. the same for display.ihx.


i'll appreciate any help thanks.



List of 20 messages in thread
TopicAuthorDate
Serial communication between 2 at89c52            01/01/70 00:00      
   I had a quick look and since I am not            01/01/70 00:00      
   pic?            01/01/70 00:00      
      again            01/01/70 00:00      
         re your ISR            01/01/70 00:00      
            i've modified the ISR            01/01/70 00:00      
               get it going with a PC at one end that g            01/01/70 00:00      
                  i'll try 2 do that            01/01/70 00:00      
               transmit            01/01/70 00:00      
                  sorry            01/01/70 00:00      
                     No, do it in the interrupt            01/01/70 00:00      
                        must be like this?            01/01/70 00:00      
                           should be            01/01/70 00:00      
                              thanks            01/01/70 00:00      
                     NO            01/01/70 00:00      
   problems again, pls help me            01/01/70 00:00      
      still don't get it            01/01/70 00:00      
   with only two processors why are you con            01/01/70 00:00      
      i've said            01/01/70 00:00      
         then this is a bad title.            01/01/70 00:00      

Back to Subject List