Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

embedded c and the 8051 microcontroller

Tags:

c

embedded

Upon reset of the 8051 microcontroller, all the port-pin latches are set to values of ‘1’. Now I am reading this book "Embedded C" and it states thr problem with the below code is that it can lull the developer into a false sense of security:

// Assume nothing written to port since reset
// – DANGEROUS!!!
Port_data = P1;

If, at a later date, someone modifies the program to include a routine for writing to all or part of the same port, this code will not generally work as required:

unsigned char Port_data;
P1 = 0x00;
. . .
// Assumes nothing written to port since reset
// – WON’T WORK BECAUSE SOMETHING WAS WRITTEN TO PORT ON RESET
Port_data = P1;

Anyone with knowledge of embedded c can explain to me why this code won't work? All it does is assign 0 to a char variable.

like image 482
JohnMerlino Avatar asked Nov 11 '22 16:11

JohnMerlino


1 Answers

Potential issues.

1) The data direction register (DDR) associated with the port may not be set as expected, thus on power-up, the DDR may be set to "input". So writing the port to 0 may unexpectedly not read read 0.

2) The data direction register associated with the port may have been set to "output" and "reading" the data may not have a clear meaning. Depending on architecture, phantom bits may be needed to shadow the output bits for read-back.

3) Power-up code may get entered via a reset command that is nothing more than a jump to "reset vector". So any hardware specific action associated with a "cold" power-up did not occur as this is a "warm" power-up.

Solution:

On power-up code, explicitly set the DDR and output values (and shadow bits as needed).

May not apply to 8051 - speaking to embedded processor in general.

like image 181
chux - Reinstate Monica Avatar answered Nov 14 '22 22:11

chux - Reinstate Monica