Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

16F877A serial communication

Status
Not open for further replies.

shaun_c_m

Junior Member level 2
Joined
Oct 12, 2007
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,518
16f877 serial

hello, i was wondering if anyone could help me with a pic arrangement using a serial protocol. im attempting to create a single master multiple slave combination using the 16F877A pic for all units.

what i would need is for the master device to be able to initiate a transfer to one of the slaves. i need it to address the slave which is to be transmit to, and also have a general call option.
the master will need to be able to send 10 bytes to the slave device(s) in question, and these will be stored into tempory memory as variables. i will alocate fixed locations for these.

the speed of transfer is not a major issue. i will be running at 20 Mhz crystal, and i need the transfer to complete in under one tenth of a second max. i was planning to write software for this part, but when i realised that there was hardware to perform it, i started trying to use it instead.

i have been trying to use the I2C function, but i am not limited to using this particular method.

the master should interupt the slave devices
the slave should ignore if address does not match
if address matches then the following 10 bytes should be recieved and stored
the communication will only be in 1 direction, master to slave
the distance between devices will be 30 cm (pcb track length) max
i can spare 2 pins (3 if absolutely neccesairy)
it should be a reliable, ie low corruption protocol
the devices will be running on independant clocks, but all at 20 MHz
there will be 1 master, 5 slaves
ports that are analogue capable are allready being used
i am using the 40 pin version of the chip

i am using assembler to program, but i dont know if relocateable code is working reliably due to using a strange programmer (velleman VM111). when i was playing about with PWM i eventually got it working by removing the relocation and hard coding it into specific banks, but as i only have limited experience with pics, im finding this difficult with I2C.

given the low speed requirement of the task, it would probably be easier to write software to perform it. however if anyone can give me any advice on using the hardware i would appreciate it. also if anyone has any code/ a step by step guide on how to implement such a function i would be appreciative.

thanks
(sorry if it was all a bit long winded)
 

16f877a usart

The PIC has a built in feature to handle this. Look at the datasheet and the USART section. In particular look at the 9 bit mode. You would need to use RS485 multidrop communication. The 9th bit is used to tell the slaves that an address is on the bus. This method allows only one master.
 

16f877a serial communication

The I2C mode of the 16F877A would be the way to go. It's a synchronous bus and the full MSSP (Multi master) hardware is supported.
 
16f877 usart

hi guys, thanks for replying. ive tried both methods.

USART

i managed to get usart running fairly easily. it is a fairly simple feature compared to the i2c method, unfortunately it is difficult to get it to work in a multiple slave environment.

im currently trying to design the code so that it can address the slaves, and think ive just about managd it, but there is a lot of code for something that i2c can handle automaticly.

(great idea about the 9th bit ! i was just going to trap all the other slaves in a loop until transmission was over!)

i am now trying to get a 2 way system working. nothing that complex, but just allowing the slave to request data, (on error and on startup). with usart its quite a challenge to avoid boards talking over each other. ive been mucking about trying to connect all the RB0 interupt pins of all the boards and using it as a way of avoiding two boards trying to talk symaltaniously.


I2C

with i2c mode, although it has all the features built as hardware, i cant get it running. i have yet to even manage to get a byte between them, data or address.

i think im going to have to read the help files for i2c again, now that i understand a little more. however if anyone has any bare bones code to transmit a byte between 2 16f877a pics via i2c, it would make it a lot easier. most of the examples on the internet are too complex for me to backwards enginneer, and without a starting point ive been thrown in at the deep end.



there is only one master
it transmits to 5 slaves
 

16f877a examples

can i use basic language to program it
 

16f877a i2c

Yes, U can use Basic . example given below.

Code:
 ' Interface to a 24LC32 serial eeprom
 DEVICE = 16F877                    ' Use a device with an MSSP module
 DIM Loop AS BYTE
 DIM Array[10] AS BYTE
 ' Transmit bytes to the I2C bus
 HBSTART                            ' Send a START condition
 HBUSOUT %10100000                  ' Target an eeprom, and send a WRITE command
 HBUSOUT 0                          ' Send the HIGHBYTE of the address
 HBUSOUT 0                          ' Send the LOWBYTE of the address 
 FOR LOOP = 48 TO 57                ' Create a loop containing ASCII 0 to 9
 HBUSOUT LOOP                       ' Send the value of LOOP to the eeprom
 NEXT                               ' Close the loop
 HBSTOP                             ' Send a STOP condition
 DELAYMS 10                         ' Wait for the data to be entered into eeprom matrix
 ' Receive bytes from the I2C bus
 HBSTART                            ' Send a START condition
 HBUSOUT %10100000                  ' Target an eeprom, and send a WRITE command
 HBUSOUT 0                          ' Send the HIGHBYTE of the address
 HBUSOUT 0                          ' Send the LOWBYTE of the address
 HBRESTART                          ' Send a RESTART condition
 HBUSOUT %10100001                  ' Target an eeprom, and send a READ command
 FOR Loop = 0 TO 9                  ' Create a loop
 Array[Loop] = HBUSIN               ' Load an array with bytes received
 IF Loop = 9 THEN HBSTOP : ELSE HBUSACK ' ACK or STOP ?
 NEXT                               ' Close the loop
 PRINT AT 1,1, STR Array            ' Display the Array as a STRING
 

16f877a as i2c slave

thanks nishal, but i dont know basic for pics. unfortunately i only use assembler, however i am greatful.

i done a search on microchip.com and found source code for i2c, but when i saw that it used 600 lines of code to transmit a byte i was more than a little worried.

i think i have a plan. as i have a working usart connection i think im going to go with it. i think the best way forward is to use the 9th bit to indicate an address as RINTHESUN suggested, but to implement an ACK feature aswell. i will probably have to use an extra pin for this, but that shouldnt be a problem, it might even be possible to multiplex it with the data bus.

pseudocode sequence


masterTX sends (address_n byte) + (1) ;send address byte plus address indication bit to get attention of slave
slave sends (ack) on separate pin

masterTX sends (data byte) + (0)
masterTX sends (data byte) + (0)
masterTX sends (data byte) + (0)
masterTX sends (data byte) + (0)

masterTX sends (address_n byte) + (1) ; repeat address to signal end transmission
slave sends (ack) on separate pin

if a nack is sent the master will repeat transmission

address byte b'00000000' is general call




also

every 1/10th second there is a poll of all slaves

masterTX sends (address_1 byte) + (1) ;get attention slave 1
slave sends (ack) on separate pin
masterTX sends (address_1 byte) + (1) ;end transmission
slave sends (ack) on separate pin


masterTX sends (address_2 byte) + (1) ;get attention slave 2
slave sends (ack) on separate pin
masterTX sends (address_2 byte) + (1) ;end transmission
slave sends (ack) on separate pin

etc

if a nack is sent the master will repeat transmission for that device. as there will only be one device transmitting there wont be a problem with bus collisions. if a slave needs data it will await a poll and then signal ACK, NACK.

("i hear you, i need data")


even with the slaves having to test every byte that comes in through usart it is still more than fast enough. at 20 Mhz clock im running at least an order of magnitude faster than i will need. i think this will be quite easy to code compared to the I2C, and has scope for expansion. it would be possible to modify it so it could be bi-directional (although not full duplex) and it could drive as many devices as the output can support.


i think i will leave I2C for another day

thanks everyone!


ps. could someone explain the difference between:

"0x00" and "00h"
 

16f877 userial

thank nishal can u help me to communucation i2c between pic to pic use basic language
 

Re: 16f877 userial

hi.
if i would send 8 bit such as 10000001 serially to other pic. How i should do that?
 

if i would send 8 bit such as 10000001 serially to other pic. How i should do that?
Transmit from one PIC & receive it from the other one. I think SPI or I2C would work better here than USART.
Regards,
Jerin. ;-)
 

Re: 16f877 userial

hi.
if i would send 8 bit such as 10000001 serially to other pic. How i should do that?

With your query, you must have to write two different programs say for PIC-1 and PIC-2 in which one for transmitter and another one for receiver.
Suppose if u would like to transmit an 8bit data via TXREG (USART's Transmitting Register), then, the same data received at PIC-2 will be at RXREG register. Take the value of RXREG and manipulate it as you like.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top