Timers, Counters and Displays

tshuck

Joined Oct 18, 2012
3,534
Don't know about that.
Assembly helps the user understand why a multiply instruction in C for a 16-bit number takes much longer than a 16-bit addition. An assembly user would know the device has no hardware multiplier and understands this is an algorithmic approach, the C user doesn't know the difference.
 

takao21203

Joined Apr 28, 2012
3,702
Assembly helps the user understand why a multiply instruction in C for a 16-bit number takes much longer than a 16-bit addition. An assembly user would know the device has no hardware multiplier and understands this is an algorithmic approach, the C user doesn't know the difference.
There is no way around to study compression algorithms, lists, file formats for instance, if you want to refer to yourself as a programmer.

If you were able to write such things from scratch, what language you use is secondary.

You imply the C programmer (for embedded chips) is a OOP/GUI script kiddie clicking together a game with a graphical editor.

Yes true they will not know much about registers or hardware multiplier.

Assembler only clobbers up the mind especially RISC assembler.
C is an automated way to produce well defined sequences of assembler.
 

tshuck

Joined Oct 18, 2012
3,534
There is no way around to study compression algorithms, lists, file formats for instance, if you want to refer to yourself as a programmer.

If you were able to write such things from scratch, what language you use is secondary.
No, the language you use should be one of the first decisions. What is simple in one language may take much longer in another. This is the basic argument to using C in the first place!

You imply the C programmer (for embedded chips) is a OOP/GUI script kiddie clicking together a game with a graphical editor.

Yes true they will not know much about registers or hardware multiplier.
uhhh... when did I say that?
I implied nothing considering everything I said, minus the reply to Brownout, was generalized. A C instruction is compiled into a series, if applicable, of assembly instructions, that's why there is a dissassembly. That being said, a single assembly instruction does not guarantee a C user to understand what that single instruction is meant to accomplish.

Assembler only clobbers up the mind especially RISC assembler.
C is an automated way to produce well defined sequences of assembler.
I assume you are using a free C compiler, tell me, how efficient is the code generated from that?:rolleyes:
 

takao21203

Joined Apr 28, 2012
3,702
No, the language you use should be one of the first decisions. What is simple in one language may take much longer in another. This is the basic argument to using C in the first place!
Hmm. I should think about that. I have an old book here which is totally out of date, 1970s, but it is using a pseudo-code to describe the algorithms. Not tied to BASIC and relying on language specifics (basically collections of code in the ROM) to implement things.

It is highly abstract, but allows to implement about anything contained in it in any language.

uhhh... when did I say that?
I implied nothing considering everything I said, minus the reply to Brownout, was generalized. A C instruction is compiled into a series, if applicable, of assembly instructions, that's why there is a dissassembly. That being said, a single assembly instruction does not guarantee a C user to understand what that single instruction is meant to accomplish.
You did not. But it reads a bit like that, a C user writing a complex software, but being very naive about what's going on.

It is actually correct, looking at the disassembly can teach a lot about how the C compiler works.

Learning assembler is not so hard, but using it is uncomfortable.

I assume you are using a free C compiler, tell me, how efficient is the code generated from that?:rolleyes:
Not so effective. It is a tradeoff. The solution to that was increased clock frequency, large memories, and parallel computing. No one would want to program a large commercial 3D game these days in assembler. It is not even used anymore for critical components.

Get a book about the new DirectX11 and see yourself. Look at the Nvidia graphics card computing. Is this done in assembler relating to linear 1:1 instruction flow?

Intel 4004, that was assembler. :)

Trivial assembler programs are not so hard to create or to understand. Asembler programs which do anything meaningful are quite cryptic for younger people, even if they have a book at hand explaining the instructions.

Take the protracker replay routine (from the internet), show it to a young college student, see if he/she is able to change the way it works in an hour in a meaningful way. See if he or she is even able to understand on the margin how it works.

There is a lot of assembler stuff around on the net to look at. People really wrote 1 MByte sized sources. It is a dead end, a one way road which contains more and more potholes as you move along.

A small selection here: http://dhs.nu/files.php?t=democreation
DNT DMA-Player v2.7 - for instance would be a representative assembler source.
This package by the way is well documentated and commented.
 
Last edited:

takao21203

Joined Apr 28, 2012
3,702
Let's take a look at a smaller assembler source code.
It is not PIC RISC. It is a 16bit CPU including multiplication instructions and good addressing modes.

It is a code to generate a sine table.

How much would you be able to see, learn or understand how to generate a sine table?

Not at all unless you spend hours stepping through each line.

A programming teaching book should contain the pseudo-code, at first in unoptimized form. With discussion, including ways to optimize it.

Then you'd be able to implement it in any language.

Pseudo-code is very similar to C, yet it does not use special characters for binary or bit field operations.

Rich (BB code):
**********************************************
*                                            *
* Gen_cos :                                  *
* ---------                                  *
*                                            *
* This rout, gen the Cosinus and Sinus table *
*                                            *
*                                            *
*                   << Azrael (C.V.) >>      *
*                                            *
*                                            *
* Little note, the table are 4096 multiplied *
*                                            *
*                                            *
* HTTP://CEREBRAL-VORTEX.NET                 *
*                                            *
*                                            *
**********************************************


Gen_cos:	movem.l 	d0-d7/a0-a6,-(a7)

	moveq	#12,d5

	lea	Cosinus(pc),a0
 	lea 	Sinus(pc),a1

 	move.w 	#2896,90(a0)	* Cos 45*4096
 	move.w 	#2896,90(a1)	* Sin 45

 	moveq 	#44,d0
Boucle0:
 	move.w 	d0,d1
 	add.w	d1,d1
	move.w	2(a0,d1.w),d2
 	asl.l	d5,d2
	move.w	2(a1,d1.w),d3
 	muls 	#71,d3	* Sin 1
 	add.l 	d2,d3
	asr.l	d5,d3
	move.w	d3,0(a0,d1.w)
	move.w	2(a1,d1.w),d2
	asl.l	d5,d2
	move.w	2(a0,d1.w),d3
 	muls 	#71,d3	* Sin 1
 	sub.l 	d3,d2
	asr.l	d5,d2
	move.w	d2,0(a1,d1.w)
 	dbra 	d0,Boucle0
 
 	moveq 	#44,d0
Boucle1:
 	move.w 	d0,d2
 	add.w   	d2,d2
 	moveq 	#90,d1
 	sub.w 	d0,d1
 	add.w	d1,d1
 	move.w 	0(a0,d2.w),0(a1,d1.w)
 	move.w 	0(a1,d2.w),0(a0,d1.w)
 	dbra	d0,Boucle1 

 	moveq 	#89,d0
 	move.w 	#180,d1
Boucle2: 
 	move.w 	d1,d2
 	add.w	d2,d2
 	move.w 	#360,d3
 	sub.w 	d2,d3
 	move.w 	0(a1,d3.w),0(a1,d2.w)
 	move.w 	0(a0,d3.w),d4
 	neg.w 	d4
 	move.w 	d4,(a0,d2.w)
 	subq.w 	#1,d1
 	dbra	d0,Boucle2

 	move.w 	#178,d0
 	move.w 	#181,d1
Boucle3:
 	move.w 	d1,d2
 	add.w	d2,d2
 	move.w 	d2,d3
 	sub.w 	#360,d3
 	move.w 	0(a1,d3.w),d4
 	neg.w 	d4
 	move.w 	d4,(a1,d2.w)
 	move.w 	#720,d3
 	sub.w 	d2,d3
 	move.w 	0(a0,d3.w),0(a0,d2.w)
 	addq.w 	#1,d1
 	dbra	d0,Boucle3

 	movem.l 	(a7)+,d0-d7/a0-a6
 	rts

Cosinus:	ds.w	360
Sinus:	ds.w	360
 

tshuck

Joined Oct 18, 2012
3,534
Hmm. I should think about that. I have an old book here which is totally out of date, 1970s, but it is using a pseudo-code to describe the algorithms. Not tied to BASIC and relying on language specifics (basically collections of code in the ROM) to implement things.

It is highly abstract, but allows to implement about anything contained in it in any language.
Yes, and the language used to implement a certain algorithm may be more intensive than another...

You did not. But it reads a bit like that, a C user writing a complex software, but being very naive about what's going on.
I was discussing the merit of learning assembly in that it would offer the student to understand that a single line in C may take many assembly instructions, but would reflect the hardware present in the device used.
Learning assembler is not so hard, but using it is uncomfortable.
Agreed. It offers an insight to the fundamental inner workings of the device itself that C obfuscates.

Not so effective. It is a tradeoff. The solution to that was increased clock frequency, large memories, and parallel computing.
Those are some solutions...
No one would want to program a large commercial 3D game these days in assembler. It is not even used anymore for critical components.
[...]
There is a lot of assembler stuff around on the net to look at. People really wrote 1 MByte sized sources. It is a dead end, a one way road which contains more and more potholes as you move along.
There are some instances where assembly is the preferred language. A computer's BIOS is typically done is assembly. Extremely strict specifications will have a programmer writing in assembly, though, those strict of specs are few and very far between.

My part in this conversation is over. I jumped in to point out the misleading statement before so the OP could form an informed decision. I have stated I agree with using and learning C, and that there are benefits to be garnered from the use of assembly. Takao(or anyone, for that matter), if you'd like to discuss more about this, a new thread, or PM is in order; as this has sidetracked this thread too much already.
 

thatoneguy

Joined Feb 19, 2009
6,359
I assume you are using a free C compiler, tell me, how efficient is the code generated from that?:rolleyes:
With BoostC (free), optimization is enabled. I'd have a hard time trying to improve the code it produces for performance enhancements.

As far as the question of C vs. Assembly:
C compilers do produce a .ASM listing, the contents of which show each C function in the source, followed by the assembly instructions needed for that command to operate. I believe beginners would learn far more by programming in C, then looking at the listing, than it would be to "hit the wall" of midrange memory banking, register banks, etc. with assembly instructions.

Less than 1 in 100 programmers can produce tighter code for a large project in assembly than in C. Those programmers need to know all types of shortcuts, which are different for each microcontroller. When a user learns C, their program can be re-targeted to different microcontrollers without much of a headache, while the assembly programmer essentially needs to start from scratch.

So, that all aside, for the OP:
Take a look at MikroC, BoostC, and Microchip C. See which you prefer in a price/performance. On the BASIC side, Oshonsoft Basic simulator is an excellent tool, as is PICBasic Pro (much more expensive, the 'heart' of the BASIC Stamps). BoostC and Oshonsoft are the only languages I know of with low cost, ease of use, simulation, and optimization enabled.
 

takao21203

Joined Apr 28, 2012
3,702
I am only trying to save people from putting efforts into learning or using PIC assembler, or even to save memory space. And in the end, not getting much out of it, and thinking PICs are not so powerful + hard to program.

I am not having a benefit from writing this stuff, and I am not pushing any specific agenda.

I have also used PIC assembler for a few years, as the tutorials for the PIC Velleman kit were assembler based. Back then, C compilers were not so easy to use. It is quite easy now with XC8. There are code templates, get you started in a minute.
 

takao21203

Joined Apr 28, 2012
3,702
With BoostC (free), optimization is enabled. I'd have a hard time trying to improve the code it produces for performance enhancements.

As far as the question of C vs. Assembly:
C compilers do produce a .ASM listing, the contents of which show each C function in the source, followed by the assembly instructions needed for that command to operate. I believe beginners would learn far more by programming in C, then looking at the listing, than it would be to "hit the wall" of midrange memory banking, register banks, etc. with assembly instructions.

Less than 1 in 100 programmers can produce tighter code for a large project in assembly than in C. Those programmers need to know all types of shortcuts, which are different for each microcontroller. When a user learns C, their program can be re-targeted to different microcontrollers without much of a headache, while the assembly programmer essentially needs to start from scratch.

So, that all aside, for the OP:
Take a look at MikroC, BoostC, and Microchip C. See which you prefer in a price/performance. On the BASIC side, Oshonsoft Basic simulator is an excellent tool, as is PICBasic Pro (much more expensive, the 'heart' of the BASIC Stamps). BoostC and Oshonsoft are the only languages I know of with low cost, ease of use, simulation, and optimization enabled.
This is also useful advice. C code can well be optimized a lot. Adapt a non-PIC C code to write a string on a TFT (kind of a graphical LCD). You can improve the speed by factor ten only by modifying the C code to a form which is optimal for PICs.

True, knowing the assembler a bit, for instance it can only do one bit-wide shift each instruction and things like that.

You can write small assembler programs, and run them in the simulator, see how they modify the memory.

In C however you can spell stuff which would be highly cryptic in assembler, and often that will be close to the optimum you can get out of the hardware.

Program optimization is not the first step when you want to learn the technology. It should be OK for the time being if the font display isn't so fast after all.
 

tshuck

Joined Oct 18, 2012
3,534
I will however agree that C is easier for a beginner, though a true understanding of the mechanics of the device can be attained from learning assembly first.
I seem to need to reiterate....

@thatoneguy: Yes, having someone learning C may benefit from looking at the dissasembly, however, the desire to know what it means is gone. It may work, but many people are too lazy since the device already would do what is desired.
 

takao21203

Joined Apr 28, 2012
3,702
There are tutorials for the PIC fundamentals, so called Gooligum, which shows the website from search.

Unfortunately they are lengthy, and based on assembler.

I'd know how to build and program the devices OP is interested to construct, however not having PDFs or web pages about that right now.

One day I want to write a book about PICs.

I have built more than 100 circuits, maybe even 200. I have sold PICs and PIC based PCBs, so I consider myself as advanced PIC user not so much a hobbyist.

I never tried any of these, but relying on one of the Microchip evalution/demo boards is a starting point to recommend. They may cost 100 or 200 dollar, but that is nothing compared to the amount I used after all, building boards from scratch. This approach did anything but to save me money.

Visit http://microchip.com and examine the different product lines.

Edit: There is actually a surprise, a brand new product. You'll see straight away.
 

Attachments

Last edited:

Thread Starter

IGill

Joined Jan 5, 2013
27
It seams that the consensus is that C is easiest to learn, and to be honest I want to start with the simplest language especially since it appears that there is a different assembler for different PICs.

I have opted for the PICKit 2 starter which comes with a tutorial disk. What I find a little confusing about the starter kit contents is that it describes lessons in Assembly but also indicates that it comes with a LITE C compiler.

Is this a contradiction or is my ignorance showing?

http://ww1.microchip.com/downloads/en/DeviceDoc/51570a.pdf
 

takao21203

Joined Apr 28, 2012
3,702
I used the PICKIT2 for a while, for sure a good starting point. It is dated, 5 years old or something.

Always look at the Microchip website, and see you have the latest MPLABX (or MPLAB legacy), and as well the latest compilers installed.

XC8 is at revision 1.12 now.

I have never encountered any advantage compared to PICKIT3. Never using hardware debugging. PICKIT3 seems to be better technology, and is more stable on my 64bit Windows PC.

All softwares, documents and demo sources for the Microchip programmers/boards are also downloadable, not only available from supplied CD ROM.

So you could examine them immediately.
 

thatoneguy

Joined Feb 19, 2009
6,359
The "Lite" version is the non-optimizing compiler. You can download a trial version of MikroC which has some limitations, or BoostC, which has a RAM limitation, but simulation and optimization is enabled.

Each one will come with examples in addition to the ones that are included with the board.

Once you walk through the code, see how it performs in simulation, compare how it works on the microcontroller. There ARE slight differences between the compilers, such as built in/included library functions, as well as changes in definitions for register names, configuration, etc.

The best advice is to see what you'd like to do, install several compilers and try your project out on each one, simulate it, and you'll end up with a favorite. I have many installed (except Microchip C) here simply to re-produce problems when the code and goal is posted and I'm feeling energetic.

All of the compilers produce a .hex file that you can program to your chip with the PICKit 2/3, so that part is the same no matter your choice.

Just like picking a favorite text editor or word processor, try them out to see if they have the features you want at a price you can easily live with. :)
 

ErnieM

Joined Apr 24, 2011
8,377
It seams that the consensus is that C is easiest to learn, and to be honest I want to start with the simplest language especially since it appears that there is a different assembler for different PICs.

I have opted for the PICKit 2 starter which comes with a tutorial disk. What I find a little confusing about the starter kit contents is that it describes lessons in Assembly but also indicates that it comes with a LITE C compiler.

Is this a contradiction or is my ignorance showing?

http://ww1.microchip.com/downloads/en/DeviceDoc/51570a.pdf
Good lord, you got a consensus out of that pure and simple hijack of your thread?

C is easier then assembly, but Basic is easiest to learn, as Basic was designed to be a learning tool, sort of a practice language that turned out to be pretty useful. It's hard to shoot yourself in Basic, damn easy to put your eye out in C.

But C is a vast improvement over assembler.

Before you spend a cent on this you can download all the tutorials, compilers and other software tools and try these out. The MPLAB (a windows program used to write PIC programs) has a PIC simulator to run your code without any hardware at all.

However, bottom line I don't see any formal tutorials in C for any of the PICkit 2 kits. They are only with the PICkit 3 kits.
 

Brownout

Joined Jan 10, 2012
2,390
BASIC might be easier to learn, but C is universal, very easy and pays bigger dividens in the long run. It's also a good platform for C++, which a bugging programmer might eventually run across. There is some use for BASIC, but I've gotten along great without it for the last 30 years. Also, I think C helps you write functional code, while BASIC can get you into trouble. I got a PICKIT3 and was writing apps in just a couple days. That's the way I would go.
 

Neosec

Joined Feb 25, 2013
36
Clearly many here favor C. I have no experience with C and have felt I'm missing out on something for sure. I too started with Basic on a TRS-80 and Commodore 64 and never had much need to move on since I always worked on the hardware side of things (repair not design). My first experience with micro-controllers was the BasicStamp. It didn't take long to move on to PICs even with very limited tinkering, mostly because of PWM (Pulse-width modulation) from the Stamp.
I've noticed that no one here has mentioned PICBasic Pro. Since the OP has experience with Basic wouldn't it make some sense? On the other hand if the OP is up to learning C I think it will serve him well going forward.
As for starting with micro-controllers vs timers, counters, 7-segment displays, etc. I'd jump right into the PIC and learn the rest as you go.
Just m2c.

PS here's a simple bit of PICBasic Pro code for driving an LCD display. I remember the first time I got an LCD working, never even consider 7-segment any more.

Rich (BB code):
' Name        : LCD.pbp
' Compiler    : PICBASIC PRO Compiler 2.6
' Assembler   : PM or MPASM
' Target PIC  : 16F or 18F
' Hardware    : LCD display
' Oscillator  : Any internal or external
' Keywords    : LCDOUT
' Description : PICBASIC PRO program to demonstrate operation of an LCD in 4-bit mode.
'
 
' LCD should be connected as follows:
' LCD   PIC
' DB4   PortA.0
' DB5   PortA.1
' DB6   PortA.2
' DB7   PortA.3
' RS    PortA.4 (add 4.7K pullup resistor to 5 volts)
' E     PortB.3
' RW    Ground
' Vdd   5 volts
' Vss   Ground
' Vo    20K potentiometer (or ground)
' DB0-3 No connect
 
   Pause 500       ' Wait for LCD to startup
 
mainloop:
   Lcdout $fe, 1   ' Clear LCD screen
   Lcdout "Hello"  ' Display Hello
   Pause 500       ' Wait .5 second
 
   Lcdout $fe, 1   ' Clear LCD screen
   Lcdout "World"
   Pause 500       ' Wait .5 second
 
   Goto mainloop   ' Do it forever
 
   End
 
Last edited:

thatoneguy

Joined Feb 19, 2009
6,359
I've noticed that no one here has mentioned PICBasic Pro. Since the OP has experience with Basic wouldn't it make some sense? On the other hand if the OP is up to learning C I think it will serve him well going forward.
I mentioned it in Post 27 as an option, it is the compiler that the Parallax used to make the BASIC Stamp. However, the price is a couple hundred dollars.

Oshonsoft PICSimulator is programmed in BASIC and is even easier than PICBasic Pro, has a "visual simulator" which makes debugging very easy, is only $50.

For BASIC, from a Price/Performance/Features perspective, PICBasic Pro is overpriced. The compilers from Microchip are as well, affordable only if going into mass production on a product. BoostC, and Oshonsoft are the only two I know of that provide full optimization and simulation for $50 or less. One is C, one is BASIC.

I don't disagree that BASIC has uses, but is somebody plans to get further into electronics, I'd go so far as to suggest an Arduino rather than spending a couple hundred dollars on a Basic compiler. By the same token, the Arduino is strictly a "hobbyist system", and not fit for mass production.

There is no "One Size Fits All" dev system, though for beginners, the PICAXE (BASIC) and Arduino (C) are the easiest choices. The PICAXE environment can do simulation and debugging, which is a major disadvantage with the Arduino platform. Beyond those two "intro systems" (BASIC Stamp left out due to cost of a unit), the more the user knows, the more they are equipped to decide what controller, compiler, and language they will be the most productive in.

I suggest trying all of them before spending money on any single one, personally.
 

Neosec

Joined Feb 25, 2013
36
Sorry Thatoneguy, I missed it in your earlier post. I know what you mean about the cost of the PBP software, especially if it's just for hobby use as some do.
 
Last edited:

takao21203

Joined Apr 28, 2012
3,702
PICs are not geared for hobby use.

The older baseline is so simple it can serve many purposes without complicated documentation neccessary.

The new 16bit/32bit PICs were designed for hitech medical and industrial devices, used in production lines. Smaller 8bit PICs are found in cars and power supplies.

If you only want to build hobby circuits, never duplicate your design as PCB, small PICs don't give you so much power and memory like an Arduino.

They are cost optimized and used for specific applications.

Arduino chip has a large memory.

You'd need a PIC32 or PIC24, and these have a lot of documentation to read. There are demo boards for them, containing displays.

There are many circuits on the web using old PICs (8bit), and which are largely hobby circuits.

I did not see many for new PICs.

These are not much favoured among hobbyists as there are not many books or webpages about them and smaller distributors don't have them.

So, if you use these, you should use them professionally, not mix them with an outdated circuit.

Using an Arduino, you can build almost anything you like and show it off. It was made for students and beginners and for hobby use.

Using a PIC, you should always try to use them in the best possible way or in a way it looks smart.

Maybe you see the Microchip C compilers are used for chips used in PCB designs. The compiler was not designed for educational purpose.

Writing the optimizer was a lot of additional work, simply it has a certain cost to make it working properly.

Using the GCC on the Arduino, it makes not much difference if it optimizes or not there is so much FLASH memory available.
 
Last edited:
Top