Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

SPI ISP doesnt work

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-8051 code here
View previous topic :: View next topic  
Author Message
vinodkumar

Bascom Member



Joined: 18 Oct 2005
Posts: 7
Location: Jabalpur

india.gif
PostPosted: Sun Dec 25, 2005 4:11 am    Post subject: SPI ISP doesnt work Reply with quote

I have been trying to make a program to program a ISP chip (AT89S52) over SPI interface. I made a few routines for testing on the ELEKTOR board with PAULMON. the responce from the target chip is always FF. even though i can program the target chip using my usual parallel port ISP programmer. could someone please help me out. the program listing is in bascom51 demo version. PC interface is simply a terminal emulator
the SPI write (SHOUT) and read (SHIN) routines were taken from the ATmel's application note for programming AT89s8252, i have modified the routines to acomodate the four byte programming instruction set of the AT89S52. still i am not able to find whats wrong.

The new notebooks that the students got this semester in my department doesnot have any parallel port. and here we do not get a USB to parallel port convertor that easily ( I couldnt find one locally). So i searched and searched for a SPI programmer for ATMEL's AT89S52 using the serial port. but to no avail, all the serial port programmers catered to AT89S8252/53. Now one choice would be to get phillips or dallas chips with internal bootloader. but the thing is that our inventory is filled with ATMEL's AT89S52. and we are stuck with it. so i thought of making a serial port ISP programmer using a AT89C2051. for that i made some programs but to no avail.


'SPI ISP PRG Ver 1.0
'SPI ISP PROGRAMMER FOR AT89S51/52
'demo program only if getready,read and

$crystal = 11059200
$noinit 'for elekor board
$romstart=&H2000
$baud = 9600

'SOFT SPI PIN CONFIGURATION

Mosi Alias P1.4
Miso Alias P1.3
Sck Alias P1.5
Rst Alias P1.7

'DECLARE SUBROUTINE

Declare Sub Writebyte
Declare Sub Readbyte
Declare Sub Erasechip
Declare Sub Shout
Declare Sub Shin
Declare Sub Rdy


'VARIALBLE ASSINGMENT
Dim K As Byte



Waitms 500 'FOR THE ELEKTOR BOARD WITH PAULMON

'INITIALISATION OF SPI PINS
Init:
setb MISO
setb MOSI
clr SCK
clr RST

Main:

print "AT89S52 ISP SPI programmer Ver 1.0"
Print
Print "COMMANDS: G(etREADY), W(RITE),R(EAD),E(RASE)"
Print


K:
K = Waitkey()

If K = "G" Then Rdy Else Goto L 'put board into programming mode
Goto K

L:
If K = "W" Then Writebyte Else Goto M 'writing one byte at the required address
Goto K

M:
If K = "R" Then Readbyte Else Goto N 'read one byte from the iven address
Goto K

N:
If K = "E" Then Erasechip Else Goto O 'erase chip
Goto K

O:
If K = "Q" Then Goto Quit Else Goto P 'quit and go back to monitor
Goto K

P:
Print "INVALID COMMAND"
Goto K

Quit:
LJMP 00



'SERIAL PROGRAMMING ENABLE
Sub Rdy()

Print "Enabling Programming"
Set Rst
MOV A,#&HAC 'PGMEN COMMAND BYTE1
Shout
MOV A,#&H53 'PGMEN COMMAND BYTE2
Shout
MOV A,#&H55 'PGMEN COMMAND BYTE3
Shout
Shout
Shin 'OUTPUT SHOULD BE &H69

Printhex Acc
Print "Programming enabled"
Reset Rst

End Sub


'READING A BYTE FROM MEMORY
Sub Readbyte()

Print "Reading a byte from 0000"
Set Rst
MOV A,#&H20 'READBYTE COMMAND BYTE1
Shout
MOV A,#&H00 'ADDRESS MSB
Shout
MOV A,#&H00 'ADDRESS LSB
Shout
Shin 'OUTPUT SHOULD BE DATA AT MEMORY LOCATION 00FF
Reset Rst
Printhex Acc
Print " Read Complete"

End Sub



'READING SIGNATURE BYTE
Sub Readsign()

Print " reading signatue byte"
Set Rst
MOV A,#&H28 'READ SIGN BYTE COMMAND BYTE1
Shout
MOV A,#&H00 'READ SIGN BYTE COMMAND BYTE2
Shout
MOV A,#&HFF 'READ SIGN BYTE COMMAND BYTE3
Shout
Shin 'OUTPUT SHOULD BE THE SIGNATURE BYTE AT 00
Reset Rst
Printhex Acc
Print " Read Sign Complete"

End Sub



'ERASING THE CHIP
Sub Erasechip()

Print " Erasing Chip"
Set Rst
MOV A,#&HAC 'ERASE COMMAND BYTE1
Shout
MOV A,#&H80 'ERASE COMMAND BYTE2
Shout
MOV A,#&HFF 'ERASE COMMAND BYTE3
Shout
MOV A,#&HFF 'ERASE COMMAND BYTE4
Shout
Reset Rst
Print " Chip Erased"

End Sub


'PROGRAMMING THE CHIP IN BYTE MODE
Sub Writebyte()

Print " Programming a byte at 0000H"
Set Rst
MOV A,#&H40 'PROGRAM CHIP COMMAND BYTE1
Shout
MOV A,#&H00 'ADDRESS MSB
Shout
MOV A,#&H00 'ADDRESS LSB
Shout
MOV A,#&H25 'DATA TO BE PROGRAMMED
Shout
Reset Rst
Print " programmed 25H at 0000H"

End Sub




Sub Shout()
'Shift out a byte, most significant bit first.
'SCK expected low on entry. Return with SCK low.
'Called with data to send in A.
push b
mov b, #8 ' bit counter
X42:
rlc a ' move bit into CY
mov MOSI, c ' output bit
nop ' enforce data setup
nop '
setb SCK ' raise clock
nop ' enforce SCK high
nop '
nop '
nop '
clr SCK ' drop clock
djnz b, .x42 ' next bit
pop b
End Sub


Sub Shin()
'Shift in a byte, most significant bit first.
'SCK expected low on entry. Return with SCK low.
'Returns received data byte in A.
push b
mov b, #8 ' bit counter
X43:
setb SCK ' raise clock
mov c, MISO ' input bit
rlc a ' move bit into byte
nop ' enforce SCK high
nop '
clr SCK ' drop clock
nop ' enforce SCK low
nop '
djnz b, .x43 ' next bit
pop b
End Sub


'LOW LEVEL ASM ROUTINES TO SEND A BYTE OVER SPI

Sub Sendspi()

CLR SCK
MOV B,#8 ;Set bit count.
Loop1:
RRC A ;Send one data bit.
MOV MOSI,C ;Put data bit on pin.
SETB SCK
NOP
NOP
CLR SCK
NOP
DJNZ B,.Loop1 ;Repeat until all bits sent.
CLR MOSI ;Release data line for acknowledge.

End Sub



'LOW LEVEL ASM ROUTINE TO READ A BYTE OVER SPI

Sub Readspi()

MOV B,#8 ;Set bit count.

Rbloop:
SETB SCK
NOP
NOP
MOV C,MISO ;Get data bit from pin.
RLC A ;Rotate bit into result byte.
CLR SCK
NOP
DJNZ B,.RBLoop ;Repeat until all bits received.
End Sub

End
Back to top
View user's profile Yahoo Messenger
Abhishek_Kakkar

Bascom Member



Joined: 22 Dec 2006
Posts: 2

india.gif
PostPosted: Sun Dec 24, 2006 4:33 am    Post subject: Reply with quote

Don't use low- level assembly, do the program in BASIC.
ReadSPI And SendSPI Routines have'nt been used anywhere
Use the software SPI in BASCOM to do the dirty work

Also, try to debug the program from the COM Port by adding Print Statement after each statement.

Use an AT89C51 instead of AT89C2051

Keep reset high at initialization and low at the end of programming

Also the clock frequency is crucial.

The maximum serial clock (SCK) frequency should be less than
1/16 of the crystal frequency.

Build the programmer individually, not on the elektor board, using an at89c51.

remove the $noinit and $romstart directives and add this line-
$regfile = "reg51.dat"
Back to top
View user's profile AIM Address
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-8051 code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum