I cannot make my usbasp programmer work on AT89S52. It makes me so frustrated that I decide to build my own with the ATMEGA328P at hand so I can learn 8051 programming (what an overkill!). The process of trying to make it work is painful, but nevertheless it finally worked (code is still buggy and I don't know why).
Here's the result:
(OK, it's a poor man's Arduino -- a minimal breadboard system connected to PC via a USB-RS232 adaptor. The AVR chip is on the right -- the 51 chip on the left is so much bigger!)
ATMEGA328P AT89S52 VCC ---- VCC ---- USB VCC GND ---- GND ---- USB GND Pin 15 (Arduino Pin 9) --- RST Pin 17 MOSI (Arduino Pin 11) -- Pin 6 MOSI Pin 18 MISO (Arduino Pin 12) -- Pin 7 MISO Pin 19 SCK (Arduino Pin 13) -- Pin 8 SCK ATMEGA328P Pin 16 (Arduino Pin 10) -- LED, error signal AT80S52 Pin 31 /EA -- VCC Pin 3 (P1.2) -- LED, testing
Both chips use 16MHz crystal oscillator -- this is hardcoded in the program.
The PC host program, needs Boost to compile, but it should be cross platform.
Following are a few commands to show the usage of the host program. Change -s parameter to the serial port used for communication.
./x51 [-s /dev/ttyUSB0] [--page] foo.ihx # Uploading. sdcc .ihx output of sdcc. ./x51 --dump foo.ihx # Dump the content of the hex file. ./x51 [-s /dev/ttyUSB0] [--page] --verify foo.ihx # read the file content from the chip and dump it. It needs the original .ihx file to determine the number of bytes to read.
Both upload and verify accept an optional "--page" parameter to do page-mode I/O. The serial communication code is still buggy in that the Arduino has to be reset before another upload/download can be carried out.
What's learned: 1. Arduino's SPI does't work for this purpose for unknown reason, haven't checked the source code. 2. Arduino's shiftOut works for programming purpose, but shiftIn doesn't, maybe due to timing reason. 3. Arduino is slow in processing serial input. I have to lower the baud rate to 9600 and add delay to the host code to make serial communication work, and it is not yet stable.