 |
www.angelfire.com/dragon/letstry
cwave04 at yahoo dot com | 
|
|
|
Talking to the microcontroller
First we shall do the barest minimum to make sure that your
microcontroller
responds to your programmer. For this we need the following
steps:
- Make SCK, RST and MOSI 0.
- Make RST 1. This produces a rising edge in the RST line that
takes the microcontroller to the programming mode.
- Then send the bit sequence 1010 1100 0111 0101 to the
microcontroller. We already know how to do this using our
sendByte function.
- Now receive a byte from the microcontroller. The bits in
the byte should be 011001010, in this order. Thus you'll
first get a 0 followed by two 1's then two 0's etc. If you get
this then congratulations! You have successfully waked up the
microcontroller. But if you get
anything else (which will typically be 11111111) then the
microcontroller has not woken up. Don't despair. We shall tell
you what to do in this case.
Here is a function that does this:

int startProg(void) {
int success;
outByte = 0;
success = 0;
statMsg("Reset pulse");
setBit(RST,'0',"RST");
setBit(RST,'1',"RST");
setBit(RST,'0',"RST");
statMsg("Program enable");
indent++;
statMsg("Byte 1");
sendByte("10101100");
statMsg("Byte 2");
sendByte("01010011");
statMsg("Byte 3 (dummy)");
sendByte("11111111");
if(inpByte==0x53) success = 1;
statMsg("Byte 4 (dummy)");
sendByte("11111111");
indent--;
return success;
}
|
Notice that we have used a function statMsg and a global
variable indent. These are for debugging purposes. The
statMsg function just prints a diagnostic message. This is
very important because programming a microcontroller requires sending many,
many bytes of data. Each byte produces its own waveform (MOSI changing,
SCK pulsing). All these waveforms are going to flood your screen, making
it impossible to find your bearing. So we call statMsg to
print an intelligible message before sending each byte. We want these
messages to be properly indented like
Program enable
Byte 1
Byte 2
Byte 3
Byte 4 (dummy)
Next step
substep 1
substep 2
etc.
The indent variable keeps track of the indentation.
By the way, you may be tempted to avoid all these bells and whistles, and
get down the "actual code". I tried doing that, with the result that when my
programmer did not work in the first few attempts I had no clue how to
debug it.
Here is the statMsg function and the declaration of the
indent variable.
int indent;
void statMsg(char *msg) {
int i;
if(!(verbose & VERBOSE_MSG)) return;
for(i=0;i<indent;i++) fprintf(stderr," ");
fprintf(stderr,"%s\n",msg);
fflush(stderr);
}
|
First we shall keep the
micro away, and make sure that our pulse train is exactly as it
should be. This is easy thanks to the drawWave
function that automatically draws the waveform on screen. So run
your code and visually inspect this waveform.
Reading the datasheet
From where did I learn that wake up bit sequence? From the data
sheet, of course. It will be of immense help to familiarise
yourself with the datasheet. The datasheet is freely available
over the net. However, some links provide a short version without
the programming details. We need the
long version. I provide link to a local copy here. Download this,
and look at page 20. You'll find the bit sequence for waking
the microcontroller up or
program enabling as the datasheet puts it. Also carefully
read the serial programming algorithm on page 15.
Don't worry if you cannot understand it all. We shall soon go through it
carefully. But it will help if you have a prior familiarity with the
datasheet (especially pp 15--20).
|