Microprocessor

Discussion about project that uses other controller, or non controller based project such as using logic gates, FPGA, ARM, Mechanical ....etc. Welcome to post it here

Microprocessor

Postby krys » Thu Feb 16, 2012 7:01 pm

I would like to design a vehicles signal. When DIP switch 1 is turned on, the LED will move from left to right - LCD display will display turn right. DIP switch 2 will light up the LED from right to left - LCD display turn left. And when DIP switch 3 is turned on, all LED will light up and LCD will display stop.

I already do the coding for each part. My problem is that I don't know how to combine them. And I'm using HCS12 Dragon-Plus Board.

And how do I post my coding? It doesn't allow .txt or .asm file
krys
Novice
 
Posts: 23
Joined: Fri Dec 02, 2011 12:49 pm

Re: Microprocessor

Postby ABSF » Thu Feb 16, 2012 7:15 pm

You may post it using [code] tag. First open your asm file using Notepad. Copy your program text, press [code] tag and paste your text in between the 2 [code] tags.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Microprocessor

Postby robosang » Fri Feb 17, 2012 6:59 pm

Yup Allen is right. Anyway I am not familiar with HCS12, so cannot help much. Is Motorola MCU?
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Microprocessor

Postby krys » Mon Feb 20, 2012 9:19 pm

@robosang Yeah I think so

CODE: SELECT_ALL_CODE
 
   #include "C:\miniide\hcs12.inc"
   ORG $1500

   MOVB #$FF, DDRB            ;configure PORTB as output
   MOVB #$02, DDRJ             ;configure PTJ1 as output
   BCLR PTJ,$02                      ; send PTJ1 as logic 0 = 0 enable LED light on
      
LOOP    MOVB #$FF,PORTB            ;on led STOP
   LDY #5                                  ; Call Delay routine
   JSR Delayby100ms
   
   BRA LOOP
         
   #include "C:\miniide\delay.asm"
   end


   #include "c:\miniide\hcs12.inc" ; LCD
lcd_dat   equ   PTK
lcd_dir   equ   DDRK
lcd_E   equ   $02
lcd_RS   equ   $01
      
   org   $1500
   lds   #$1500
   jsr   openLCD
   ldx   #msg1
   jsr   putsLCD
   ldaa   #$C0
   jsr   cmd2LCD
   ldx   #msg2
   jsr   putsLCD
   swi   
      
;*********************************************************************************
;*cmd2LCD                                                                        *
;*performs desired operation, waits for 40us, then returns to the caller         *
;*all instructions except clear display adn cursor home will be completed        *
;*********************************************************************************
      
cmd2LCD   psha   
   bclr   lcd_dat,lcd_RS
   bset   lcd_dat,lcd_E
   anda   #$F0
   lsra   
   lsra   
   oraa   #lcd_E
   staa   lcd_dat
   nop   
   nop   
   nop   
   bclr   lcd_dat,lcd_E
   pula   
   anda   #$0F
   lsla   
   lsla   
   bset   lcd_dat,lcd_E
   oraa   #lcd_E
   staa   lcd_dat
   nop   
   nop   
   nop   
   bclr   lcd_dat,lcd_E
   ldy   #1
   jsr   delayby50us
   rts   
      

;*********************************************************************************
;*openLCD - contains 4 LCD instructions                                          *
;*1) entry mode set. Move cursor to the right AFTER reading or writing           *
;*2) display on/off. Turn on display, cursor and blinking                        *
;*3) function set. Sets the number of rows for display, font size and width      *
;*4)clear display. Clear LCD screen and move cursor to home position.            *
;*********************************************************************************
      
openLCD   movb   #$FF,lcd_dir
      
   ldy   #10
   jsr   delayby10ms
      
   ldaa   #$28
   jsr   cmd2lcd
      
   ldaa   #$0F
   jsr   cmd2lcd
      
   ldaa   #$06
   jsr   cmd2lcd
      
   ldaa   #$01
   jsr   cmd2lcd
      
   ldy   #2
   jsr   delayby1ms
      
   rts   
      
;*********************************************************************************
;*putcLCD                                                                        *
;*outputs a character to LCD                                                     *
;*makes sure write operation is complete                                         *
;*********************************************************************************
      
putcLCD   psha   
   bset   lcd_dat,lcd_RS
   bset   lcd_dat,lcd_E
   anda   #$F0
   lsra   
   lsra   
   oraa   #$03
   staa   lcd_dat
   nop   
   nop   
   nop   
   bclr   lcd_dat,lcd_E
   pula   
   anda   #$0F
   lsla   
   lsla   
   bset   lcd_dat,lcd_E
   oraa   #$03
   staa   lcd_dat
   nop   
   nop   
   nop   
   bclr   lcd_dat,lcd_E
   ldy   #1
   jsr   delayby50us
   rts   
      
;*********************************************************************************
;*putsLCD                                                                        *
;*outputs a NULL terminated string pointed to by index register X                *
;*********************************************************************************
      
putsLCD   ldaa   1,x+
   beq   donePS
   jsr   putcLCD
   bra   putsLCD
donePS   rts   
      
;*********************************************************************************
;*insert message(s) here                                                         *
;*********************************************************************************
      
msg1   dc.b   "hello world!",0
msg2   dc.b   "LCD is working!",0
   #include "c:\miniide\delay.asm"
   end   


   #include "C:\miniide\hcs12.inc" ; switch
   org $1500
   movb #$FF,DDRB
   movb #$0,DDRH
   bset DDRJ,$02
   bclr PTJ,$02
back   movb PTH,PORTB
   bra back
   end

   #include "C:\miniide\hcs12.inc" ; led
   org $1000
lp_cnt   ds.b 1
   org $1500
start   movb #$FF,DDRB
   bset DDRJ,$02
   bclr PTJ,$02
forever   movb #16,lp_cnt
   ldx #led_tab
led_lp   movb 1,x+,PORTB
   ldy #5
   jsr delayby100ms
   dec lp_cnt
   bne led_lp
   bra forever
led_tab   dc.b $80,$40,$20,$10,$08,$04,$02,$01 ; right
   dc.b $80,$40,$20,$10,$08,$04,$02,$01
   #include "C:\miniide\delay.asm"
   end


   #include "C:\miniide\hcs12.inc"
   org $1000
lp_cnt   ds.b 1
   org $1500
start   movb #$FF,DDRB
   bset DDRJ,$02
   bclr PTJ,$02
forever   movb #16,lp_cnt
   ldx #led_tab
led_lp   movb 1,x+,PORTB
   ldy #5
   jsr delayby100ms
   dec lp_cnt
   bne led_lp
   bra forever
led_tab   dc.b $01,$02,$04,$08,$10,$20,$40,$80 ; left
   dc.b $01,$02,$04,$08,$10,$20,$40,$80
   #include "C:\miniide\delay.asm"
   end
 
krys
Novice
 
Posts: 23
Joined: Fri Dec 02, 2011 12:49 pm

Re: Microprocessor

Postby ABSF » Mon Feb 20, 2012 10:30 pm

Never used HC12 before, but I have used HC11. Have to get the HC12 datasheet before I can understand some of the instructions.

But your first task is to convert the small pieces of programs into subroutine. Like this one

CODE: SELECT_ALL_CODE
#include "C:\miniide\hcs12.inc"
ORG $1500

MOVB #$FF, DDRB ;configure PORTB as output
MOVB #$02, DDRJ ;configure PTJ1 as output
BCLR PTJ,$02 ; send PTJ1 as logic 0 = 0 enable LED light on

LOOP MOVB #$FF,PORTB ;on led STOP
LDY #5 ; Call Delay routine
JSR Delayby100ms
BRA LOOP

#include "C:\miniide\delay.asm"
end


since the program ends with "BRA" it would always flash the LED and loop back forever. You have to make it light the LED in the pattern you want for a number of times and exit with a "RTS".

Same with the subroutine ending with SWI has to be converted to subroutine ended with RTS. Then you have to write the main loop that detect the left switch or right switch; something like this:

CODE: SELECT_ALL_CODE
main:
Detect switches
If left sw detected then
   JSR LCD display turn left message
   for (repeat=0;repeat<10;repeat++)
    {   JSR LED flashes R to L
       If left sw = false
           Clear LCD message
           Off all LED
           Exit
       endif }
endif
If right sw detected then
   JSR  LCD display turn right message
   for (repeat=0;repeat<10;repeat++)
   {    JSR LED flashes L to R
       If right sw = false
           Clear LCD message
           Off all LED
           Exit
       endif }
endif
BRA main


Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Microprocessor

Postby ABSF » Mon Feb 20, 2012 10:54 pm

This subroutine looks like the turn right LED subroutine

CODE: SELECT_ALL_CODE
org $1000
lp_cnt   ds.b 1           ;this is the loop counter
org $1500
start   movb #$FF,DDRB
bset DDRJ,$02
bclr PTJ,$02
forever   movb #16,lp_cnt
ldx #led_tab
led_lp   movb 1,x+,PORTB
ldy #5
jsr delayby100ms
dec lp_cnt
bne led_lp
bra forever
led_tab   dc.b $80,$40,$20,$10,$08,$04,$02,$01 ; right
dc.b $80,$40,$20,$10,$08,$04,$02,$01
#include "C:\miniide\delay.asm"
end


Changing it to a subroutine as follows:
CODE: SELECT_ALL_CODE
*************************************************************
* The code will flash the LED from L to R 10 times
*************************************************************
   org $1000
lp_cnt   ds.b    1                      ;define storage 1 byte
repeat       ds.b   1
   org $1500
start   movb #$FF,DDRB
   bset DDRJ,$02
   bclr PTJ,$02
   movb #10,repeat
rpt   movb #16,lp_cnt                       ;loop_cnt=16
   ldx #led_tab
led_lp   movb 1,x+,PORTB          ;X will be autoincremented on each loop
   ldy #5                    ;delay 500ms
   jsr delayby100ms
   dec lp_cnt                                    ;lp_cnt--
   bne led_lp                                    ;loop 16 times (16 patterns)
   dec repeat
   bne rpt                                        ;repeat 10 times
   rts
led_tab   dc.b $80,$40,$20,$10,$08,$04,$02,$01 ; turn right LED pattern
                dc.b $80,$40,$20,$10,$08,$04,$02,$01
#include "C:\miniide\delay.asm"


All the "end" have to be remove and left only one at the end of your program

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Microprocessor

Postby krys » Fri Feb 24, 2012 5:23 pm

CODE: SELECT_ALL_CODE


   #include "C:\miniide\hcs12.inc"
   #include "C:\miniide\delay.asm"
   
   
   org $2000      ;switch
   movb #$FF,DDRB
   movb #$0,DDRH
   bset DDRJ,$02
   bclr PTJ,$02

check   ldaa PTH
   cmpa #%00000001
   beq led_on

   cmpa #%00000000
   beq led_off

   bra check
   
led_on   movb #$FF,PORTB         
   ldy #5                 
   jsr Delayby100ms      ; all led ON - stop
   bra check
   
led_off   movb #$00,PORTB           
   ldy #5                   
   jsr Delayby100ms      
   bra check

   cmpa #%00000010
right   movb #$00,PORTB 
   ds.b 1
   ds.b 1
move   movb 1,x+,PORTB         
   ldy #5                   
   jsr Delayby100ms
   bra ledright      
   bra check
ledright dc.b $80,$40,$20,$10,$08,$04,$02,$01 ; turn right LED pattern
        dc.b $80,$40,$20,$10,$08,$04,$02,$01


   cmpa #%00000011
left   movb #$00,PORTB 
   ds.b 1
   ds.b 1
led_lp   movb 1,x+,PORTB         
   ldy #5                   
   jsr Delayby100ms
   bra ledleft      
   bra check
ledleft dc.b $01,$02,$04,$08,$10,$20,$40,$80 ; turn left LED pattern
        dc.b $01,$02,$04,$08,$10,$20,$40,$80


    end


You mean like that? Oh, how do I call the LCD coding? Is it by using label? If so, where do I put it?
krys
Novice
 
Posts: 23
Joined: Fri Dec 02, 2011 12:49 pm

Re: Microprocessor

Postby ABSF » Sat Feb 25, 2012 1:21 pm

Your program will not be able to branch to the turn right or turn left signals. It can only do "All ON" and "All OFF" jobs.

The turn right/left routines after modification are completely wrong and it wouldnt work. Lets take a look at the "turn right" subroutine....

cmpa #%00000010
right movb #$00,PORTB
ds.b 1 ;"ds.b 1" means define storage in byte for 1 byte. You cannot just put a data byte in the
ds.b 1 ; middle of a program, the mcu will become crazy hitting this non-instruction byte. It should
;be placed in front before the entering point of a subroutine.
move movb 1,x+,PORTB
ldy #5
jsr Delayby100ms
bra ledright
; ledright are just data bytes, they are not instruction. Branching there will also cause the mcu to go crazy.
bra check
ledright dc.b $80,$40,$20,$10,$08,$04,$02,$01 ; turn right LED pattern
dc.b $80,$40,$20,$10,$08,$04,$02,$01
;dc.b is define character in byte. So those are the byte pattern for the led
; 10000000 $80
; 01000000 $40
; 00100000 $20
; 00010000 $10
; 00001000 $08
; 00000100 $04
; 00000010 $02
; 00000001 $01
; As can be seen, the bit 1 (led on) is moving from left to right so it is the turn signal pattern



I hope you can study the original program and try to understand them first. The program is very simple and I know how to write the whole program for you but you'll learn nothing from it if you dont understand how the instructions work in the first place. :roll:

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia


Return to Misc Project

Who is online

Users browsing this forum: No registered users and 7 guests