View Feed
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12913 Members
Join this group to post and comment.
el3ktr1k • Jun 23, 2008

Reading in Data using a microcontroller

I'm not sure how digital serial input is read into the PIC. This is how I would guess it's done. You have a pin configured for input. You then have the first bit of data generate an interrupt. You then poll the input pin to see if the bit is a 1 or 0. You then store this value as the LSB at some location in memory. Then you wait for the next bit to come in. For example if the data is coming in at a rate of 10k bits/s then you would wait 0.1 ms and poll the input pin again and so forth until the MSB bit is read. Is this basically the general idea of how data is read in or does the PIC have a better way of doing it? Thanks for any help. I'm new at this stuff.
just2rock • Jun 24, 2008
Serial Input data is read in PIC as regard to the programing done into the microcontroller.Suppose you provide a data of Hex 12C8,then the controller will choose accordingly for its input.
el3ktr1k • Jun 24, 2008
I still don't understand how the PIC is able to capture serial data and store it somewhere in memory. For example if I'm sending the binary byte, 01011001, from some to device to the input pin of a PIC, how would the PIC capture this value and store it in memory where I can call upon this value later?
Ah, heres the thing. I think you can't really send serial data directly into the input pins of the PIC easily (assuming you are talking about the Microchip variants). You'd need to use the Rx/Tx (recieve and transmission) pins to do all serial data communications.

In brief, microcontrollers are pretty much miniturized microprocessors, thus some of the architecture is similar. You have registers, interrupts, flags, and memory locations, etc. Whatever data is sent to the microcontroller, they will be stored in memory locations that have their own addresses. Each address location can store 8 bit of data (if you talking about an 8 bit microC). Thus, when you send the binary byte of 01011001, the microC will store it at a particular address in which the address register is pointing at. Whenever it wants to recall the byte, it'll just find the address, just like microprocessors.

So how do you recall the data? This depends on how you program the PIC. Using assembly language, you'd need to directly enter the address in which the data is stored into the address register, copy the data into a general purpose register, etc. However, if you use C programming, you only need to initialize a variable with a name of your choice, store the data, and then simply recall the variable (whether you want to print the data, add something, etc).

A note about polling it. PIC's (and perhaps other microcontrollers) have very limited memory to store instructions. Using the polling method to store serial data will bloat the code it for sure. Which ever way you program it, the final assembly code will contain lots of loops, delays, instructions to add bits, conditions to check the start and end bits, etc.

Use the dedicated Rx/Tx pins for those stuff 😀 Just set the required baud rate, inform the PIC that you are going to use serial, and you are good to go.
el3ktr1k • Jun 25, 2008
Thank you Ash. I plan on using the PIC16F877 by Microchip. I looked at the data sheet and found the information about the Tx and Rx pins. I just wanted to make sure that I'm the right track with what I'm doing. I plan on using the standard 9600 baud rate with a 4 MHz crystal so it looks like the first thing I would do is give the SPRG register a value of 25 and set TXSTA<2>. It looks like I also need to clear bits 6 and 4 and set bit 5 of the TXSTA register to enable 8 bit asynchronous communication. It looks like I can then begin transmitting by moving data onto the TXREG register. For receiving data it looks like I set bits 7 and 4 and clear bit 6 of the RCSTA register to enable 8 asynchronous communication. It looks like the data would come in on the RCREG register and then i can move this to some location in file memory if I choose. I'm guessing most of this is a background process once the registers are set and I would just have to periodically check the RCREG register for incoming data. I'm not exactly sure how to tell the transmitter when the receiver's buffer is full or visa versa. I guess I poll the OERR bit and tell send a control byte to the transmitter to tell it to stop transmitting. Thank you for your help and any more insight would be appreciated.
el3ktr1k • Jul 7, 2008
There must still be something I'm doing wrong since I still can't figure out to get the microcontroller to communicate with my PC. Is there maybe some other register that I forgot about? I'm not sure. I'm using the RS232 port on my computer and converting that signal into the correct logic for the microcontroller by using a Max232 chip but it still doesn't seem to work.
I tried writing a simple program as a test that would take the data it receives and then just transmit it back but my program doesn't work. Thanks for any help.
Ashraf HZ
Ashraf HZ • Jul 8, 2008
How are you communicating to the microcontroller in the computer? Hyperterminal? Have you loaded the code into the microcontroller?
el3ktr1k • Jul 8, 2008
I am using Matlab to send to the data the serial port and I have loaded the loaded the code to the microcontroller.
el3ktr1k • Jul 8, 2008
I think I may have the the wrong configuration for the type of oscillator that I'm using. This what I have in the beginning of my program:

list P=PIC16F877, F=INHX8M, C=160, N=80, ST=OFF, MM=OFF, R=DEC
include "P16F877.INC"
__config ( _CP_OFF & _PWRTE_ON & _XT_OSC & _WDT_OFF & _BODEN_OFF )
errorlevel -302 ;Ignore "error" when storing to Bank1

Like I mentioned before I'm trying to use a 4 MHz external crystal oscillator that has 4 pins. I would think that _XT_OSC would be the correct configuration for the type of oscillator that I'm trying to use but I guess I could be wrong.
el3ktr1k • Jul 9, 2008
I think I figured out what I was doing wrong. I noticed on the programmer board there was a jumper wire that was using the RC oscillator instead of the crystal oscillator. I still don't know how to make the PIC work on a bread board though. I basically wire up the 2 VDD pins to +5V and the two VSS pins to ground. I hook up pin 13 to the clock and I give pin 1 +5V.
Ashraf HZ
Ashraf HZ • Jul 9, 2008
If you'd like to construct in on a bread board, you may refer to a schematic here:

Its for a line follower robot, but you should be able to follow it without problems. Just ignore the motor driver schematic 😉
Karthik S
Karthik S • Jul 10, 2008
Let me tell something..

While using the External Oscillators, we need to set the Oscillator Registers for Type of Oscillator we are using, Baud Rate setting etc..

This should be done in the Program while initializing...

Check out the Transceiver connections and Rx/Tx pin settings in Controller.

el3ktr1k • Jul 10, 2008
I'm still having trouble getting the PIC to work after following your diagram. I connected pins 11 and 32 to +5V like it shows. I also tried connecting pins 12 and 31 to ground. My clock circuit is a bit different though. I'm using a 4 pin crystal oscillator. One pin on the crystal oscillator gets connected to +5 V and one pin is connected to ground. Then there's one pin that isn't connected and one pin that gives the clock signal. I ran a wire from the output of the oscillator to pin 13 of the PIC. I left 14 unconnected. For Pin 1 I tried both connecting it directly to the +5 V source and using a 10K ohm resistor. It's really strange. The PIC works fine when it's in the programmer but I can't get it to work when I use it in a breadboard.
Oh, I'm not sure about 4 pin oscillators. The pin thats not connected is the "enable", yea? You either float it or connect it to VDD to enable the oscillator. Does connecting it to the +5V make a difference?

If all else fails, try getting a 2 pin oscillator.

Share this content on your social channels -