Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic to pic spi and uart communication using microc and proteus

Status
Not open for further replies.

Jack_Rider

New Member
Hello guys. I've done this master and slave code for two 16f877a microcontrollers. The project is based on the virtual terminal receiving input from the user and sending it to the master by uart. The master then sends the received letter by spi to the slave, which displays it on the second virtual terminal by uart communication. the only thing left now is--i want the slave to display exactly what i type in and not display new letters when i'm not typing but from what i see here, once i type a letter in the first virtual terminal then i see the second virtual terminal keep repeating an unstoppable sequence of that letter, and when i press another letter it changes the sequence to that letter. I've done some tests on the slave select input and i've figured the problem is with the master. It keeps sending even when i'm not typing. Can anyone find the solution to this puzzle? Thanks. And also, i'm gonna upload the code and proteus as well.
 
I put it there cos it contained the proteus design as well. Nevertheless here's the master code:
C:
sbit master_sck_direction at trisc3_bit;
sbit master_sdi_direction at trisc4_bit;
sbit master_sd0_direction at trisc5_bit;
sbit master_ss_direction at trisd1_bit;

sbit master_ss at rd1_bit;

void main() {
char send=3;
master_sck_direction=0;
master_sdi_direction=1;
master_sd0_direction=0;
master_ss_direction=0;

master_ss=1;//slave deselected at start

uart1_init(9600);
spi1_init_advanced(_SPI_MASTER_OSC_DIV4,_SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_HIGH,_SPI_HIGH_2_LOW);

while(1)
{
   send=uart1_read();
   if(send>3)
   {
      master_ss=0;//select slave
      spi1_write(send);
      master_ss=1;//deselect slave
   }
   else
   {
      master_ss=0;
      spi1_write(3);
      master_ss=1;
   }
   send=3;
   delay_ms(50);
}

}
And here's the slave code:
C:
char received=3;

sbit slave_clk at trisc3_bit;
sbit slave_sdi at trisc4_bit;
sbit slave_sd0 at trisc5_bit;
sbit slave_ss at trisa5_bit;

void main() {
slave_clk=1;
slave_sdi=1;
slave_sd0=0;
slave_ss=1;
uart1_init(9600);

spi1_init_advanced(_spi_slave_ss_enable,_spi_data_sample_middle,_spi_clk_idle_high,_spi_high_2_low);
while(1)
{
   received=spi1_read(0);
   if(received!=3)
   {
      uart1_write(received);
   }
   received=3;

}

}
 
Last edited by a moderator:
C:
 received=spi1_read(0);
   if(received!=3)
   {
      uart1_write(received);
   }
   received=3;

The last statement is redundant as the next line will change when the next spi_read is called.. What's the purpose ( other than trying to control thoughput ) of the value 3?

I'm pretty sure that the RCREG will contain the same value on each read...

To do this properly, you need to poll the RCIF ( even if you are not using interrupts ) you wait for this flag, then you can act on the RCREG and send the value only once...
 
Thanks a lot for your direction. I tried it out and everything's now PERFECT.
Here's the new master code with only a little change, even simpler than before:

sbit master_sck_direction at trisc3_bit;
sbit master_sdi_direction at trisc4_bit;
sbit master_sd0_direction at trisc5_bit;
sbit master_ss_direction at trisd1_bit;

sbit master_ss at rd1_bit;

void main() {
char send=3;//could be anything, prefarably '\0' instead.
master_sck_direction=0;
master_sdi_direction=1;
master_sd0_direction=0;
master_ss_direction=0;

master_ss=1;//slave deselected at start

uart1_init(9600);
spi1_init_advanced(_SPI_MASTER_OSC_DIV4,_SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_HIGH,_SPI_HIGH_2_LOW);

while(1)
{
if(rcif_bit)//VERY IMPORTANT IF NOT AFTER FIRST KEYBOARD PRESS, TRANSFER WOULD CONTINUE INDEFINITELY. IF REPLACED BY (!rcif_bit), NOTHING WOULD EVER BE SENT.
{
send=uart1_read();
master_ss=0;//select slave
spi1_write(send);
master_ss=1;//deselect slave
delay_ms(50);
}
}

}

/*
Here's a very important piece from the datasheet of 16f877a explaining why i encountered the problem. It states:
"If enable bit SREN is set, then only a single
word is received. If enable bit CREN is set, the reception is continuous until CREN is cleared. If both bits are
set, CREN takes precedence. After clocking the last bit,
the received data in the Receive Shift Register (RSR)
is transferred to the RCREG register (if it is empty).
When the transfer is complete, interrupt flag bit, RCIF
(PIR1<5>), is set. The actual interrupt can be enabled/
disabled by setting/clearing enable bit, RCIE
(PIE1<5>). Flag bit RCIF is a read-only bit which is
reset by the hardware. In this case, it is reset when the
RCREG register has been read and is empty."*/
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top