The project mainly has two parts, a serial communication part and EEPROM accessing part. The data which should be written into the EEPROM is received from the serial port. The internal hardware module USART is used for this purpose. The data which is read from the EEPROM is also send back to the serial port using the same hardware module.
Hence two internal modules of the PIC18F4550 are used in this project namely EEPROM and USART. These hardware modules can be accessed and controller by simply writing into or reading from their corresponding registers.
The important control registers associated with the USART and their details are given below:
TXSTA: TRANSMIT STATUS AND CONTROL REGISTER
This register has the bits which controls the serial transmission features like enable or disable the transmission, whether it should be synchronous asynchronous etc.
For a simple asynchronous serial transmission only two bits are significant, which are TXEN and SYNC
TXEN is the transmission enable/disable bit and SYNC sets the synchronous/asynchronous mode.
TXEN: Transmit Enable
1 = Transmit enabled
0 = Transmit disabled
SYNC: EUSART Mode Select bit
1 = Synchronous mode
0 = Asynchronous mode
RCSTA: RECEIVE STATUS AND CONTROL REGISTER
This register has the bits which controls the serial reception features like enable/disable the serial port, reception should be 8 bit or 9 bit, whether it should be synchronous asynchronous, enable/disable continuous reception etc.
For a simple asynchronous serial reception the significant bits are SPEN, RX9 and CREN.
SPEN: Serial Port Enable bit
1 = Serial port enabled
0 = Serial port disabled
RX9: 9-Bit Receive Enable bit
1 = Selects 9-bit reception
0 = Selects 8-bit reception
CREN: Continuous Receive Enable bit
1 = Enables receiver
0 = Disables receiver
SPBRG: EUSART BAUD RATE GENERATOR REGISTER
This is a 16 bit register into which the value corresponding to the required baud-rate can be written into. The value according to a particular baud-rate can be calculated from the CPU clock frequency according to the following equation;
SPBRG = ((FOSC/Desired Baud Rate)/64) – 1
Where; FOSC is the CPU clock frequency
The UART hardware module can be initialized to a particular baud-rate with transmission and reception enabled can be done in the following steps:
Once the UART module has been initialized then the data can be read or write from the RCREG and TXREG registers respectively.
The steps required to receive data from the serial port is shown in the following figure:
The steps required to transmit data to the serial port is shown in the following figure:
The important control associated with the EEPROM is the EECON1register and its details of the important bits are given below;
EEPGD: Flash Program or Data EEPROM Memory Select bit
1 = Access Flash program memory
0 = Access data EEPROM memory
CFGS: Flash Program/Data EEPROM or Configuration Select bit
1 = Access Configuration registers
0 = Access data EEPROM memory
WREN: Flash Program/Data EEPROM Write Enable bit
1 = Allows data write into the EEPROM
0 = Inhibits data write into the EEPROM
WR: Write Control bit
1 = Initiates an EEPROM write
0 = When read as 0 it indicates that the write cycle to the EEPROM is complete
RD: Read Control bit
1 = Initiates an EEPROM read
0 = When read as 0 it indicates that the read cycle to the EEPROM is complete
The steps required to read a data from the EEPROM is shown in the following figure:
The exact steps which are shown in the below figure should be followed to get a successful EEPROM write;
Things to care:
· The baud rate of the serial communication depends on the clock frequency that the CPU receives. The calculation for the value to be written into the ‘SPBRG’ should be made based on that exact frequency.
SPBRG = ( ( FOSC / Desired Baud Rate) / 64 ) – 1
Always make sure that the frequency received by the CPU is a round figure, like 4 MHz, 8 MHz, and 12 MHz etc. If using an external crystal, a slight shift in the frequency or a bad connection with the crystal and decoupling capacitor etc. can cause a data error in transmission or reception. Hence it is recommended to use the internal oscillator of a microcontroller which will reduce the circuit and provide a stable round figured frequency; in case of PIC18F455 the maximum internal frequency is 8 Mhz.
· Disable all the interrupts of the microcontroller before initiating an EEPROM read
· The EEPROM writing is possible only when the register EECON2 is first written with a value of 0x55 and then with a value of 0xAA, before initiating an EEPROM write. No delay is necessary between these writing steps.
· The EEPROM writing will be successful only through the steps given in the flow diagram of EEPROM write.