Step 5Implementing Our Library
At the top of the file add:
#include "epsnCart.h" // the name of the header file we created earlier
Since this microcontroller is pretending to be the host, it's controlling the SCK, SDA and RST signals. so make sure they're outputs, add this with the other TRIS registers inside main():
epsnCartInit();
The next bit of code is what I used inside of the while(1) {
It requests an address starting at 0x00 then increments by one after 32 read bytes:
void main(){
char addr = 0, i = 0;
char string[40];
...
epsnCartInit(); // Initialised the pins used
while(1){ // Loop forever!
sprintf(string, "Reading cartridge with addr: 0x%02X\r\n",addr);
putsUSART(string); // prints a messge like: Reading cartridge with addr: 0x03
epsnCartStart(addr,0); // Start by sending the address in read mode
i = 0;
while(i<32){ // keep looping until i is no longer less than 32
sprintf(string,"0x%02X,",epsnCartRead());
putsUSART(string);
i++;
}
epsnCartStop(); //brings RST back low
putrsUSART("\r\n");
addr++
if(addr>7){
addr = 0;
}
DelayMS(500);
}
}
You're probably looking at the line "sprintf(string,"0x%02X,",epsnCartRead());" and going "Huh?"
sprintf is a string formatting function, much like printf except saves the formatted string into the variable string.
"0x%02X," will return a string with a readable hexadecimal value eg: 0xFE and epsnCartRead() returns a value that was read from the ink cart
This was set up with a 3.3V power supply because the ink cartridges run on 3.3Volts
I programmed this to my microcontroller, disconnected the print head from the printer to prevent interference.
I then plugged in the 3 ink cartridges I had and turned it on.
Note: At this point, if you ran this code for the first time, I would expect you have problems. Like me, I've gone over the code dozens of times, changing it here and there to make it work. It's normal if it doesn't work the first time for you. It's a great learning experience figuring out what went wrong! :P
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|









































