Assembly_7segment_counter

Embedded Systems Topics
User avatar
Hasula
Corporal
Corporal
Posts: 7
Joined: Sat May 05, 2012 11:58 am
Location: Gampaha,Srilanka

Assembly_7segment_counter

Post by Hasula » Sat May 05, 2012 1:48 pm

Code: Select all

status	equ				0x03
pcl			equ				0x02
portb			equ				0x06
portc			equ				0x07
portd			equ				0x08
trisb			equ				0x86
trisc			equ				0x87
trisd			equ				0x88
ssd1 			equ				0x23
ssd2 			equ				0x24
ssd3 			equ				0x25

start				
			call				sys_init
			goto			        main
main			call				ssd_1
			call				ssd_2
			call				ssd_3
			goto			        main

table 		addwf			pcl,f
			retlw			0x3f
			retlw			0x06
			retlw			0x5b 
			retlw			0x4f
			retlw			0x66
			retlw			0x6d
			retlw			0x7d
			retlw			0x07
			retlw			0x7f
			retlw			0x6f

ssd_1			clrf			portb	
			movlw		b'00000001'
			movwf		portc
			movf			ssd1,w
			call			table
			movwf		portb
			;movlw		b'00000001'
			;movwf		portc
			incf			ssd1
			movf			portb,w
			xorlw			0x6f
			btfss			status,2
			return
			incf			ssd1
			return

ssd_2			clrf			portb
			movlw		b'00000010'
			movwf		portc
			movf			ssd2,w
			call			table
			movwf		portb
			;movlw		b'00000010'
			;movwf		portc
			movf			portb,w
			xorlw			0x6f
			btfss			status,2
			return
			;incf			ssd2
			clrf			ssd1
			return

ssd_3			clrf			portb
			movlw		b'00000100'
			movwf		portc
			movf			ssd3,w
			call			table
			movwf		portb
			;movlw		b'00000100'
			;movwf		portc
			movf			portd,w
			xorlw			0x7d
			btfss			status,2
			return
			incf			ssd2
			clrf			ssd1
			clrf			ssd2
			return



sys_init			
			bsf			status,5
			clrf			trisb
			clrf			trisc
			clrf			trisd
			bcf			status,5
			clrf			portb
			clrf			portc
			clrf			portd
			clrf			ssd1
			clrf			ssd2
			clrf			ssd3
			return

end
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Assembly_7segment_counter

Post by SemiconductorCat » Mon May 07, 2012 5:23 am

Good work by the way, I review your code and it's nice. One application of a lookup table.
User avatar
Hasula
Corporal
Corporal
Posts: 7
Joined: Sat May 05, 2012 11:58 am
Location: Gampaha,Srilanka

Re: Assembly_7segment_counter

Post by Hasula » Tue May 08, 2012 9:39 pm

Thanks dude.I'll post more.
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Assembly_7segment_counter

Post by Nandika » Wed May 09, 2012 8:53 am

Hi Friends,
I can understand some asm commands of this.but,some others are difficult.i already red asm code like this.
Actually,i want to know about lookup tables of this.
I have commented for i already known commands.
please explain mark with ??

Code: Select all

    //Define memory location that we will use.
    status   equ            0x03
    pcl         equ            0x02
    portb         equ            0x06
    portc         equ            0x07
    portd         equ            0x08
    trisb         equ            0x86
    trisc         equ            0x87
    trisd         equ            0x88
    ssd1          equ            0x23
    ssd2          equ            0x24
    ssd3          equ            0x25

    start
             call            sys_init  //go to sys_int
             goto                 main  //go to main
    main         call            ssd_1   //go to ssd1
             call            ssd_2
             call            ssd_3
             goto                 main

    table //table contains 0..9 bit order to give port b,c,d
             addwf         pcl,f  //???
             retlw         0x3f   //???
             retlw         0x06
             retlw         0x5b
             retlw         0x4f
             retlw         0x66
             retlw         0x6d
             retlw         0x7d
             retlw         0x07
             retlw         0x7f
             retlw         0x6f

    ssd_1         clrf         portb      //all output of portb=0
             movlw      b'00000001'       //write b'00000001=d'1 to w register
             movwf      portc             //write w register to portc
             movf         ssd1,w          //???
             call         table           //go to table
             movwf      portb             //??
             ;movlw      b'00000001'
             ;movwf      portc
             incf         ssd1            //??
             movf         portb,w         //??
             xorlw         0x6f           //do xor between w register and 0x6f
             btfss         status,2       //if set bit2 of status register,skip next command
             return
             incf         ssd1             //??
             return

    ssd_2         clrf         portb
             movlw      b'00000010'
             movwf      portc
             movf         ssd2,w
             call         table
             movwf      portb
             ;movlw      b'00000010'
             ;movwf      portc
             movf         portb,w
             xorlw         0x6f
             btfss         status,2
             return
             ;incf         ssd2
             clrf         ssd1
             return

    ssd_3         clrf         portb
             movlw      b'00000100'
             movwf      portc
             movf         ssd3,w
             call         table
             movwf      portb
             ;movlw      b'00000100'
             ;movwf      portc
             movf         portd,w
             xorlw         0x7d
             btfss         status,2
             return
             incf         ssd2
             clrf         ssd1
             clrf         ssd2
             return



    sys_init  //lable
             bsf         status,5  //set(=1) bit5 of status register
             clrf         trisb     //portb as output
             clrf         trisc     //portc as output
             clrf         trisd     //port d as output
             bcf         status,5  //clear(=0) bit5 of status registerk
             clrf         portb    //portb output low
             clrf         portc    //portc output low
             clrf         portd    //portd output low
             clrf         ssd1     //ssd1 variable=0
             clrf         ssd2     //ssd2 variable=0
             clrf         ssd3     //ssd3 variable=0
             return                //go to calling line and continue

    end     //command to compiler for indicate end of codeing
i want to know about this command.
1.

Code: Select all

movf         ssd1,w
2.

Code: Select all

movwf      portb
3.

Code: Select all

movf         portb,w
4.

Code: Select all

btfss         status,2
5.Why use this command for all ssd blocks?is my definition correct? :roll:

Code: Select all

             ......
             ......
             btfss         status,2       //if set bit2 of status register,skip next command
             ......
             ......
thanks
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Assembly_7segment_counter

Post by SemiconductorCat » Tue May 15, 2012 2:34 pm

Don't you have a manual.
that's why I told you to print it out. At least those pages that you turn back again and again.

The variable ssd1 keep the current counter value.

ssd1 ssd2 ssd3 are simple functions that check the switch and increase the value.
It's not simple for a example, switch have two states. So you need to keep track.



w f are simply registers.Keep reading the code , good luck.
I don't need to discourage you on reading the code. It's good.


This is the lookup table.

Code: Select all


    table //table contains 0..9 bit order to give port b,c,d
             addwf         pcl,f  //???
             retlw         0x3f   //???
             retlw         0x06
             retlw         0x5b
             retlw         0x4f
             retlw         0x66
             retlw         0x6d
             retlw         0x7d
             retlw         0x07
             retlw         0x7f
             retlw         0x6f

So your calling this function with different values of f, 0 to 9,
it will return the correct bit pattern on 7 segment to display which we are going to put on
the port.

clear ?
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Assembly_7segment_counter

Post by Nandika » Tue May 15, 2012 3:55 pm

Thank you,
SemiconductorCat wrote:Don't you have a manual.
Actually,I don't have a manual.I will start it now.

Your small explanation about this code is good.I can refer some source and I can get more details.it's good.
But,I want a help of you now and feature.
At now,
Can you give me a book about Assembly? Because,I cant remember things that hasn't some meaning.I can say it you from example.

Code: Select all

movlw 0x25
This code has some meaning that i know.it's ,move literal number to w register.
But,

Code: Select all

addwf pcl,f
No any meaning in my mind about this command. :(
So,Can you introduce a book or link about this commands?

thanks..I salute to you for dedicate your tine for me. :|
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Assembly_7segment_counter

Post by SemiconductorCat » Tue May 15, 2012 6:00 pm

Thank you for your effort of reading the assembly language instructions.

A Book? Your refering to this : Instruction set manual. This is embedded in your datasheet for sure.
Probably in appendix.
http://ww1.microchip.com/downloads/en/D ... 31029a.pdf
page 29-3 chart. table 29-1.


No need to print the who book, print this page. or at least keep a written copy of it.

Code: Select all

addwf pcl,f
it says add the register f to the pcl.
where pcl is the program counter. you need to read the core PIC16FXXX core architecture.
As in general processors program counter is the register where we keep the address to the
next instruction. So next instruction will be somewhere in the table according the the value
of f.Note that f=0xF so it's will be interpreted by compiler as '1'.

See that's how it generate it.[page 29]
Add the contents of the W register with register 'f'. If 'd' is 0 the result is stored in the
W register. If 'd' is 1 the result is stored back in register 'f'
So could you need to read it.



Code: Select all

movlw 0x25
Again take the chart and seek for movlw, it says
move literal to W. Where literal is 0x25.So after executing that instruction the w will be 0x25.


Just don't try to take everything to your head like your programming your microcontroller.
Experts even referring to manuals. Let it happen automatically. A one who wrote a PIC assembler
in SDCC IRC channel at freenode told me "please wait till I refer the docs".

So be very straightforward to refer it. It will automatically go to your brain.




NOTE: With C a lookup table can be implemented very easily using a array. But I need you to
read the assembly code here. It makes you smart.

Code: Select all

const char up[256] = {  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
                        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16};

void main()  {
 TRISB = 0x00;
 CMCON = 0x07;
 while (1)
   PORTB = up[TMR0];
}
So it's nothing isn't it? But that's how your compiler works.
Source: Example C code stolen somewhere from the internet just googling.


Keep Reading
Good Luck.
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Assembly_7segment_counter

Post by Nandika » Wed May 16, 2012 11:22 pm

Thank you Semi.., :)
It's good book as i think.And your explanation is very good.Simple to understand.Also you given book.
Actually,I very like to Assembly. :)

I have some problems.
1.What is PICmicro MID-RANGE MCU FAMILY?
2.Explain this 4 and 5 columns in table 29-1.
Table 29-1 _ Midrange Instruction Set.PNG
Table 29-1 _ Midrange Instruction Set.PNG (98.06 KiB) Viewed 9420 times
3.What is this STATUS Register's Z, C, DC and OV bits?(I could understand some,using some example code in Vidusara artistic.But,I haven't more clear.)

Thank you...
User avatar
Hasula
Corporal
Corporal
Posts: 7
Joined: Sat May 05, 2012 11:58 am
Location: Gampaha,Srilanka

Re: Assembly_7segment_counter

Post by Hasula » Thu May 17, 2012 9:25 am

@nandika
z = Zero bit ;can use if u wanna check the is the zero bit is set?
DC=Digit carry bit ; consider if there's a 8 bit (4lower bits & 4higher bits;each are named as nibbles).If there's a bit which wanna move to lower nibble to higher nibble, u can use this as a flag
C=Carry bit;If there's a remainder when sum has done,u can use this

Hope this is oky.I advice you to refer on binary operations.
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Assembly_7segment_counter

Post by SemiconductorCat » Thu May 17, 2012 10:08 am

as they said mid range means 16FXXX MCUs.


2. As it described in the manual, before the chart , 'f' refers to a register or a memory location.
So the bits marked with f will contain the address of the register. [Since the register file also
located in general purpose ram ]. So there are 7 bits for register/ram filed. Which means this
PIC16FXXX micros could access 0h-0x7F , but I'm pretty sure your micro will have limits ,because
SRAMS are so expensive.

k is used to represent a literal ,refereed to a constant in C.

d represents a single one bit. It will decide the direction of the data flow.
if d=1 then result will written back to the register.
if d=0 then result will written back to the W register [working register]

Is these things so hard to read and understand ?

X refereed as non used. So it's value will be ignored.It does not represent any
meaning or affect's anyway to the instruction's operation.Microchip assembler
will generate logic 0 on that place, but as it says it could be 1 or 0 don't care.



3. Status Register.
come on read the section 2.3.1 , under memory organization->special registers->status register.
Simply Z means zero, if your arithmetic operation will generate a zero result this will be 1.
C means carry bit, if your arithmetic instruction will carry [for a example add 0xFF to 1,
example

Code: Select all

movlw 0xff
addlw 0x01
Then carry bit will be one.


Overlfow [OV bit]
Where is the OV bit in your microcontroller. However there is a OV bit commonly used in other
microcontrollers/microprocessors. OV bit is not related this micro. However I'll discuss it.
Generally we use 2's complement notation to keep signed numbers in memory. Where the MSB
[Most significant bit] will keep the value of the sign. For a example -1 is denoted as
-1= 0xFFFF in 16 bits.
where higher MSB bit is equal to -65536 [-2^16]
and other bits [ 1+2+4+ ...+ 2^15 = 6555 (http://www.wolframalpha.com/input/?i=su ... 15+%2C+2^x)]
represent positive. So that's how -1 comes into.

So the case is when there was an overflow from the 15th bit to the MSB bit, the result will be wrong !
It signal that , microprocessors implement this bit. So you need to check for Overflow flag after every
signed arithmetic instruction.


Anyway love reading your manual, it will make you smart, then you could read other manuals with
no exercise, like I did for you, Where I originally coming from 8051 , I have very less experience
with PICS. You can see most of the things in here like Z, C, OV are eveywhere in computing not
only limited to your specific microcontroller. So read it and partice reading through it.
It will make you smart. You many read this manual hard but next time you are reading a new manual
of complete different microcontroller, you'll figure the things out within seconds. So take my word
on this.
Post Reply

Return to “Embedded Systems”