Yet another SCI problem

Have a problem with a product? Ask here!

Moderator: cbarnes

Yet another SCI problem

Postby Jazake » Wed Apr 18, 2007 10:19 am

I am using the 9s12C32. Can you guys look at my code and tell me what I am missing. I wrote this all from scratch, so I think I am missing some vectors or something. I wrote this to recieve some wireless data from a reciever, the main idea of this is a portable truck scale for big rigs implemented using strain gages. I needed to transmit 12 bits of data so I was thinking 2 sequential transfers as I did on the SerialData subroutine. If you guys could just let me know what I need to get this working. Thank you.

ROM EQU $C000 ; start code execution at this address
RAM EQU $3850 ; place variables here
STACK EQU $3FC0 ; top of the stack


; registers
portb EQU $01 ; port b address
portb_ddr EQU $03 ; data direction register for Port B

; masks
enable EQU %00000010 ; enable bit for enable on/off pulses
output EQU %11111111 ; make a port an output

; constants
msec EQU $1770 ; 6000 (=$1770) loops = 1 msec in startx loop
scaleconstant EQU $4C7 ; equal to 12.22 * 100 to eliminate decimal
scaledivide EQU $64 ; constant to divide to return back to original weight

; SCI registers
scibr1 EQU $00C8 ; SCI0 Baud Rate Generator, high byte
scibr2 EQU $00C9 ; SCI0 Baud Rate Generator, low byte
scicr1 EQU $00CA ; SCI0 Control Register 1
scicr2 EQU $00CB ; SCI0 Control Register 2
scisr1 EQU $00CC ; SCI0 Status Register 1
scisr2 EQU $00CD ; SCI0 Status Register 2
scidr1 EQU $00CE ; SCI0 Data Register, high byte
scidr2 EQU $00CF ; SCI0 Data Register, low byte

; LCD initialization strings
LCD1 EQU $0C ; first init string we write (3 times) to LCD on startup
LCD2 EQU $08 ; set to 4 bit transfer (repeat twice)
LCD3 EQU $08 ; set to 2 lines, 5x7 dots, 4 msb of 8 bits
LCD4 EQU $20 ; set to 2 lines, 5x7 dots, 4 lsb of 8 bits
LCD5 EQU $00 ; display off, 4 msb
LCD6 EQU $20 ; display off, 4 lsb
LCD7 EQU $00 ; display clear, 4 msb
LCD8 EQU $04 ; display clear, 4 lsb
LCD9 EQU $00 ; entry mode 4 msb
LCD10 EQU $1C ; entry mode, 4 lsb (increment DDRAM, cursor moves)
LCD11 EQU $00 ; display on, 4 msb
LCD12 EQU $3C ; display on, 4 lsb (display on, cursor on, blink on)

;********************************************************************************

ORG RAM

portbVAR ds.b 1 ;variable for portb to write to LCD

;*******************************************************************************

ORG ROM ;start program execution


lds #STACK ; initialize stack pointer
cli ;enable interrupts
movb #output, portb_ddr ; make Port B an Output
movw #$0000, $3A00
jsr INITIALIZELCD ; initialize the LCD


;initialize SCI

movb #$2C, scicr2 ; enable SCI
movb #$1A, scibr2 ; set BAUD rate to 9600

Wait4Max: brclr scisr1, #$20, Wait4Max ; wait till RDRF is set


;***************************************************************************
;Algorithm to display axle weights on the 4 lines of the LCD
;***************************************************************************
LocMax:

cpy #0 ;cycle that checks which axle
lbeq Steer ;weight is currently being recieved
cpy #1
lbeq Drive1
cpy #2
lbeq Drive2
cpy #3
lbeq Trailer1
cpy #4
lbeq Trailer2
cpy #5
lbeq Total
cpy #6
lbge Finish


Steer:
; 1ST INPUT FROM SCALE = STEER
jsr SerialDATA
std $3800
cpd #$2EE0
lbhi Overweight


lbra Hex2ascii


Drive1:
;2ND INPUT FROM SCALE = DRIVE 1
jsr SerialDATA
std $3802

movb #$30, portb ; set LCD to next line
jsr EPulse
movb #$00, portb
jsr EPulse

iny
lbra Wait4Max

Drive2:
;3RD INPUT FROM SCALE = DRIVE 2
jsr SerialDATA
std $3804
addd $3802
cpd #$84D0 ;If Drive axle is overweight then finish
lbhi Overweight
lbra Hex2ascii

Trailer1:
;4TH INPUT FROM SCALE = TRAILER 1
jsr SerialDATA
std $3806


movb #$24, portb ; set LCD to next line
jsr EPulse
movb #$00, portb
jsr EPulse

iny
lbra Wait4Max

Trailer2:
;5TH INPUT FROM SCALE = TRAILER 2
jsr SerialDATA
std $3808
addd $3806
cpd #$84D0 ;If Trailer axle is overweight then finish
lbhi Overweight
lbra Hex2ascii

Total:

movb #$34, portb ; set LCD to next line
jsr EPulse
movb #$00, portb
jsr EPulse

ldd $3800 ; STEER AXEL
addd $3802 ; + DRIVE 1 AXEL
addd $3804 ; + DRIVE 2 AXEL
addd $3806 ; + TRAILER 1 AXEL
bcc NotOver
movw #$0001,$3A00
NotOver: addd $3808 ; + TRAILER 2 AXEL
; = SUM OF ENTIRE TRUCK
bcs Over66grand

lbra Hex2ascii

;******************************************************************************
; Subroutine to do needed multiplication to convert the A2D input into a usable weight
;******************************************************************************
SerialDATA:

pshy
pshx

; ldaa scidr2 ;sequence to receive high order byte, then lower
Nextbyte: brclr scisr1, #$20, Nextbyte
ldab scidr2


ldy #scaleconstant
emul ;multiplication to equal proper weight
ldx #scaledivide
ediv
tfr Y,D
pulx
puly
rts

;**********************************************************************************
; Subroutine for truck over 65536 lbs or FFFF
;*********************************************************************************
Over66grand:
movw #$0001,$3A00
lbra Hex2ascii
;**********************************************************************************
; Subroutine to reach when the truck is overweight
;*********************************************************************************
Overweight:

movw #$4F56,$3900 ;ASCII character codes for 'OV'
movw #$4552,$3902 ;ASCII character codes for 'ER'
movw #$5745,$3904 ;ASCII character codes for 'WE'
movw #$4947,$3906 ;ASCII character codes for 'IG'
movw #$4854,$3908 ;ASCII character codes for 'HT'
lbra WeightOver

;******************************************************************************
;* Converts a 16-bit Hex value to ASCII.
;* This function checks how many times 10^4 goes into the number, then 10^3,
;* then 10^2, then 10^1, then uses the number of times as the decimal number.
;******************************************************************************
Hex2ascii: pshy
ldy $3A00
cpy #$0001
blt normal_axle
ldx #$2710 ;divide D by 10^4
ediv
sty $3900
bra second_axle
normal_axle: ldx #$2710
idiv
stx $3900
second_axle: puly
ldx #$3E8 ;divide D by 10^3
idiv
stx $3902
ldx #$64 ;divide D by 10^2
idiv
stx $3904
ldx #$A ;divide D by 10^1
idiv
stx $3906
std $3908
ldd $3900
beq Set_num1
orab #$30
bra Skip1
Set_num1: ldab #$20
Skip1: stab $3900
ldd $3902
beq Set_num2
orab #$30
bra Skip2

Set_num2: ldab #$20
Skip2: stab $3901
ldd $3904
orab #$30
stab $3903
ldd $3906
orab #$30
stab $3904
ldd $3908
orab #$30
stab $3905

movb #$20,$3902
ldd $3900
cpd #$2020
ble No_comma
movb #$2C,$3902
cmpa #$30
blt No_comma
cmpb #$30
bgt No_comma
movb #$30,$3901
No_comma: movw #$206C,$3906
movw #$6273,$3908






WeightOver: iny ;increment counter for LCD screen line
jsr Write ;jumps to write LCD line
lbra Wait4Max ;go back and wait for another local max


Finish:
swi

;********************************************************************************
;* Subroutine to initialize the LCD display
;********************************************************************************

INITIALIZELCD:
ldd #$70

; wait 20 msec after power on (need delay > 15 msec)
jsr DELAY

; write first init string three times with various delays
movb #LCD1, portb

; write 1/delay 1
jsr EPulse ; pulse data in portb to write it
ldd #$05 ; wait 5 msec (need delay > 4.1 msec)
jsr DELAY

; write 2/delay 2
jsr EPulse ; pulse data in portb to write it
ldd #$05 ; wait 1 msec (need delay > 100 usec)
jsr DELAY

; write 3, no delay needed
jsr EPulse ; no delay needed here

; final initialization writes
; wait > 1 msec between each write ( > 40 usec delay needed)


; set to 4 bit transfers; only one 4-bit write needed
movb #LCD2, portb
jsr EPulse
ldd #$05
jsr DELAY

; set to 2 lines, 5x7 dot character display; 2 4-bit writes needed
movb #LCD3, portb
jsr EPulse
ldd #$05
jsr DELAY
movb #LCD4, portb
jsr EPulse
ldd #$05
jsr DELAY

; display off; 2 4-bit writes needed
movb #LCD5, portb
jsr EPulse
ldd #$05
jsr DELAY
movb #LCD6, portb
jsr EPulse
ldd #$05
jsr DELAY

; display clear; 2 4-bit writes needed
movb #LCD7, portb
jsr EPulse
ldd #$05
jsr DELAY
movb #LCD8, portb
jsr EPulse
ldd #$05
jsr DELAY

; entry mode = increment, cursor move; 2 4-bit writes needed
movb #LCD9, portb
jsr EPulse
ldd #$05
jsr DELAY
movb #LCD10, portb
jsr EPulse
ldd #$05
jsr DELAY

;display on, cursor on, blink on; 2 4-bit writes needed
movb #LCD11, portb
jsr EPulse
ldd #$05
jsr DELAY
movb #LCD12, portb
jsr EPulse
ldd #$05
jsr DELAY

rts

;*********************************************************************************
; Subroutine for EPulse
;********************************************************************************
EPulse:
pshd
bset portb,enable ; set portb enable bit high
nop ; hold enable high for > 230 ns
nop
nop
bclr portb,enable ; take enable back to low
ldd #$05
jsr DELAY
puld
rts

;********************************************************************************
; Subroutine for DELAY
;**********************************************************************************

DELAY:
pshd
pshx
pshy

tfr D,Y ; (D) -> (Y)
starty: ldx #msec ; start y loop and set x loop to 1 msec
startx: dex ; start x loop - 4 cycles per startx loop if branch
bne startx
dey
bne starty

puly
pulx
puld

rts
;********************************************************************************
; Subroutine WRITE, to display on the LCD
;**********************************************************************************
Write:

pshd ; save values for D and X
pshx


ldx #$3900
ldaa #10 ;counter for 10 charaters in string

begin:
ldab 0,X ; 4 msb nibble
lsrb ; center msb nibble
lsrb
stab portbVAR ; work on msb nibble
bset portbVAR, $01 ; set RS=1
bclr portbVAR, $C2 ; clear upper 2 bits and enable bit
movb portbVAR, portb
jsr EPulse
ldab 0,X ; 4 lsb nibble
lslb ; center lsb nibble
lslb
stab portbVAR ; work on lsb nibble
bset portbVAR, $01 ; set RS=1
bclr portbVAR, $C2 ; clear upper 2 bits and enable bit
movb portbVAR, portb
jsr EPulse
inx
deca
bne begin

pulx
puld
rts

;**********************************************************************************
; Reset Vector
;**********************************************************************************
ORG $FFFE
dc.w $c000 ; Address of Start of Code
;*********************************************************************************
Jazake
Newbie
Newbie
 
Posts: 2
Joined: Wed Apr 18, 2007 1:26 am

Return to Product Troubleshooting

Who is online

Users browsing this forum: No registered users and 1 guest

cron