Author Topic: little querry to get an output to flip/flop  (Read 11486 times)

0 Members and 1 Guest are viewing this topic.

Offline joelby

  • Frequent Contributor
  • **
  • Posts: 634
Re: little querry to get an output to flip/flop
« Reply #25 on: October 11, 2011, 06:17:16 am »
The main good thing about C is that it is usually very portable, whereas BASIC isn't standardised and varies between implementations.

If you're careful about how you code (generally by abstracting device-specific functions), the same C can run on a PIC, an AVR, an MSP430, and a desktop computer. It can be much faster to prototype an algorithm on a PC, since there's no upload step, and you can use advanced debuggers.

Recently I was writing a small program for an MSP430 Launchpad and started to run in to size limitations with the device I had on hand, and it was only about a 30 minute job to convert the lot for a PIC, and most of this was looking at the data sheets and figuring out how to set up peripherals the same way. The bulk of the code didn't require any changes at all. Try doing that with assembler!

If speed is your primary concern, pretty much all C compilers will allow you to add inline assembly.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9710
  • Country: my
  • reassessing directives...
Re: little querry to get an output to flip/flop
« Reply #26 on: October 11, 2011, 09:25:33 am »
The bulk of the code didn't require any changes at all. Try doing that with assembler!
i'm currently building my own asm library so my pic code will be portable to avr. yes its a PITA.

If speed is your primary concern, pretty much all C compilers will allow you to add inline assembly.
i still dont have full control of what register should i use if say, i'm inlining from inside a "for" loop. since the loop will use some registers in the chip for background task such counting etc. if the IDE documentation is detailed about that, then no problem.
It's extremely difficult to start life.. one features of nature.. physical laws are mathematical theory of great beauty... You may wonder Why? our knowledge shows that nature is so constructed. We simply have to accept it. One could describe the situation by saying that... (Paul Dirac)
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 14173
  • Country: gb
  • 0999
Re: little querry to get an output to flip/flop
« Reply #27 on: October 11, 2011, 05:09:16 pm »
On the other hand if you are comfortable with assembly then programming in C should be like a walk in the park.
I hated the way C used braces all the time, the way it's case sensitive and missing odd characters out stopped it from compiling. I couldn't stand the string functions and the way it insists on zero terminated strings. If I want to worry about low level stuff then I'd use assembly, if I just want an easy to use high level language I'd use BASIC.

I haven't had any experience with high level languages on MCUs so I can't comment. I may even give C another go.
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 14961
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: little querry to get an output to flip/flop
« Reply #28 on: October 11, 2011, 05:22:36 pm »
Well I tyink for the minute I'm stuck on BASIC, I'm not the best at programming so I suppose it meets my inabilities best at the moment, I spend so much time on projects that I have no time to learn a massive chunk like another programming language
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9692
  • Country: us
Re: little querry to get an output to flip/flop
« Reply #29 on: October 11, 2011, 05:23:26 pm »
I hated the way C used braces all the time, the way it's case sensitive and missing odd characters out stopped it from compiling. I couldn't stand the string functions and the way it insists on zero terminated strings.
Uh huh. You have to get the "logic" behind the design of C before you can feel comfortable with it, and that does take a bit of time to learn. The raw and low level nature of C is one of the things that motivated the introduction of C++.

Quote
If I want to worry about low level stuff then I'd use assembly, if I just want an easy to use high level language I'd use BASIC.
Quite. And that's exactly how things were for many years in the era of Z80 and 6502 based home computers.
I'm not an EE--what am I doing here?
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1046
  • Country: fi
Re: little querry to get an output to flip/flop
« Reply #30 on: October 11, 2011, 07:40:18 pm »
I hated the way C used braces all the time

I guess you don't then want to try LISP (Lots of Irritating Superfluous Parenthesis) :) Not exactly a microcontroller programming language but anyway. Curly braces of C (or C++) is nothing in comparison.

I wonder, what fundamentally prevents BASIC being equally efficient than C if both are compiled code. After all, many equivalent constructs can be done with either language. ASM is more efficient if you know the processor better than the compiler.

But then, why use ASM when you can code the whole application using HDL and do everything in parallel, now that is really efficient and fast ;)

Regards,
Janne
 

alm

  • Guest
Re: little querry to get an output to flip/flop
« Reply #31 on: October 11, 2011, 08:41:51 pm »
I wonder, what fundamentally prevents BASIC being equally efficient than C if both are compiled code. After all, many equivalent constructs can be done with either language. ASM is more efficient if you know the processor better than the compiler.
I'm not that familiar with embedded BASIC dialects, but there may be some minor issues. Like the non-shortcircuiting behavior of boolean operators (leading to cludges like AndAlso/OrElse in some dialects). Or bounds checking. Or if it uses Pascal strings (Hero999 complained about null-terminated strings, so I'm just guessing), most strings operations carry an overhead because the length has to be updated after the fact. The main thing is probably the amount of money and time the industry spends optimizing the compilers. There's a lot of money behind embedded C, every MCU vendor tries to get good compiler support. BASIC appears to be mainly confined to hobbyists, I've never seen a micro vendor advertise a new line as being supported by some BASIC compiler.

But then, why use ASM when you can code the whole application using HDL and do everything in parallel, now that is really efficient and fast ;)
Hand-optimized transistor layouts avoids the overhead of HDL, allows you to fit more functionality on the same piece of silicon and is easier because you're dealing directly with hardware, and avoid learning the abstraction of a HDL.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9710
  • Country: my
  • reassessing directives...
Re: little querry to get an output to flip/flop
« Reply #32 on: October 11, 2011, 09:26:54 pm »
basic is good at language (human friendly) but poor at mathematics. i think its possible to optimize basic performance, a new language maybe, hybrid of basic and c. if someone pay me a very good money, i will invent the compiler.  :-X
« Last Edit: October 11, 2011, 09:33:29 pm by Mechatrommer »
It's extremely difficult to start life.. one features of nature.. physical laws are mathematical theory of great beauty... You may wonder Why? our knowledge shows that nature is so constructed. We simply have to accept it. One could describe the situation by saying that... (Paul Dirac)
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 14961
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: little querry to get an output to flip/flop
« Reply #33 on: October 11, 2011, 09:39:45 pm »
basic is good at language (human friendly) but poor at mathematics. i think its possible to optimize basic performance, a new language maybe, hybrid of basic and c. if someone pay me a very good money, i will invent the compiler.  :-X

why not invent the compiler and then sell it ;) why work for someone else when you can work for yourself
 

alm

  • Guest
Re: little querry to get an output to flip/flop
« Reply #34 on: October 11, 2011, 11:08:01 pm »
Great, another programming language, just what the world desperately needs ;).
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 815
  • Country: au
Re: little querry to get an output to flip/flop
« Reply #35 on: October 12, 2011, 02:03:56 am »
I have been using Proton BASIC for PIC for years. It allows me to rapidly develop a simple pic app with the flexibility of inline ASM if i need it. I view C compilers as midway between BASIC and ASM, its not as easy as BASIC but its not as tight as ASM either.

Just my 2 cents.
Testing one two three...
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1046
  • Country: fi
Re: little querry to get an output to flip/flop
« Reply #36 on: October 12, 2011, 05:27:06 am »
But then, why use ASM when you can code the whole application using HDL and do everything in parallel, now that is really efficient and fast ;)
Hand-optimized transistor layouts avoids the overhead of HDL, allows you to fit more functionality on the same piece of silicon and is easier because you're dealing directly with hardware, and avoid learning the abstraction of a HDL.

Sorry, I completely forgot that one. It is just AFAIK large majority of digital ASICs are designed using a HDL. But of course, I guess one can draw own transistors if HDL feels too bloated.

It reminds me that I must respect the pioneers who designed 6800, 6502 etc. by pencil and paper, without any kind of HDL. They must have known their stuff.

Regards,
Janne
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9710
  • Country: my
  • reassessing directives...
Re: little querry to get an output to flip/flop
« Reply #37 on: October 12, 2011, 07:39:41 am »
basic is good at language (human friendly) but poor at mathematics. i think its possible to optimize basic performance, a new language maybe, hybrid of basic and c. if someone pay me a very good money, i will invent the compiler.  :-X
why not invent the compiler and then sell it ;) why work for someone else when you can work for yourself
because its not fun. i can live with either asm, C or basic.

Great, another programming language, just what the world desperately needs ;).
the idea is to combine the strong points of every languages and put the weaknesses aside. not creating a new language. maybe the language is still looks like basic, but the way it compiles is like C.

anyway. have you guys use a feature in C/C++ IDE for mcu where when you compiled your c code, you are able to see the generated asm for which line in the c code?
It's extremely difficult to start life.. one features of nature.. physical laws are mathematical theory of great beauty... You may wonder Why? our knowledge shows that nature is so constructed. We simply have to accept it. One could describe the situation by saying that... (Paul Dirac)
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 14173
  • Country: gb
  • 0999
Re: little querry to get an output to flip/flop
« Reply #38 on: October 13, 2011, 06:15:02 pm »
One idea is to start with a very high language such as BASIC, then converting it to C, which is in turn compiled to assembly and assembled. You could then use either of the three in the same program. I can't remember the compiler but I know this has been done before for the PC. I don't know about MCUs.
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 14961
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: little querry to get an output to flip/flop
« Reply #39 on: October 19, 2011, 06:40:45 am »
well you can put assembly inline into Mikroe basic
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9710
  • Country: my
  • reassessing directives...
Re: little querry to get an output to flip/flop
« Reply #40 on: October 19, 2011, 07:28:51 am »
well you can put assembly inline into Mikroe basic
asm will do its magic if your doing loop'ed routine. but if you just want to turn on and off something and go back to (basic or C) loop, then not much different there. and maybe other misunderstood, asm is all about manipulating registers, gathered together to create one logical statement or task. we have to think "procedurally", not "logically". so as we usually take for granted the formula such as a = b + c, in asm we need to do it in several lines. eg into something like mov a, reg1 ... mov b, reg2 ... add reg1, reg2 ... mov reg2, a. so all those lines will have the same meaning as a single line statement in C/basic, infact this is what happen in reverse once our C/basic code is translated into machine code (except more bloated if the compiler is not as intelligent as its creator). once we grasp the idea of asm is about manipulating registers (and memory etc), other than difficulty understanding the mcu datasheet and its memory structure, i dont see any reason why asm is so difficult.

but from proffesional point of view, the issue like portability and development time will be an issue, esp for large program. but sure there's a way. i've just completed my simple asm library that can work on both pic and avr. so when i code my app in asm, it will closely resemble higher level language (by encapsulating weird keywords/opcodes) but with the efficiency of raw asm, and work for both pic and avr with minor modification. well since i've coded for pic and i've found out the chip cannot do the job, i've to port my app without having to rebuild the logic again from scratch in avr. but i dont say asm is the only way. if in the future i will have to do more advance/complex project, i will certainly pick C or basic in the 1st place, esp when its involving device library from 3rd party or more complex logic flow. but when it comes to only generating PWM or LED on off, asm makes wonder.... for me ;)
It's extremely difficult to start life.. one features of nature.. physical laws are mathematical theory of great beauty... You may wonder Why? our knowledge shows that nature is so constructed. We simply have to accept it. One could describe the situation by saying that... (Paul Dirac)
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 14961
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: little querry to get an output to flip/flop
« Reply #41 on: October 19, 2011, 06:12:12 pm »
well really it needs a proper resource to actually explain how to put a program together step at a time. I have never found that, all tutorials do is explain some limited code and leave you in the dark about the rest.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9710
  • Country: my
  • reassessing directives...
Re: little querry to get an output to flip/flop
« Reply #42 on: October 19, 2011, 07:00:46 pm »
It's extremely difficult to start life.. one features of nature.. physical laws are mathematical theory of great beauty... You may wonder Why? our knowledge shows that nature is so constructed. We simply have to accept it. One could describe the situation by saying that... (Paul Dirac)
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 14961
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: little querry to get an output to flip/flop
« Reply #43 on: October 19, 2011, 09:39:01 pm »
yes the datasheet only lists the mnemonics, not how to write a program
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9692
  • Country: us
Re: little querry to get an output to flip/flop
« Reply #44 on: October 19, 2011, 10:00:06 pm »
yes the datasheet only lists the mnemonics, not how to write a program
How to write a program is "deeper knowledge" that is outside the scope of a datasheet.

Ironically, how to write a program at the machine code level was a standard part of O-Level (later GCSE) computer science when I was at school in the 70's. At the time the world of computers was a different place, and what we today would think of as a microcontroller was just an ordinary computer back then with similar power. The teaching syllabus went through all the basics of program counter, registers, memory addressing, stack operations and other hardware details, and there were a whole variety of simple machine emulators to write and test programs on. This was of course complementary to writing bigger and more functional programs in BASIC, which was the standard educational language in schools at the time.

These days I don't know whether GCSE computer science even touches much on programming in high level languages, let alone machine code. I suspect it's based much more around PCs and desktop applications along with more general theory. There were of course no PCs in the 70's unless you count machines like the Commodore PET or Tandy TRS-80, and applications like word processing or spreadsheets were barely emerging concepts.

Getting back on subject, try looking for some basic machine code or assembly programming tutorials on the web. Once you get the idea in general, you can transfer the concepts to any particular machine you come across, be it a PIC or an AVR.
« Last Edit: October 19, 2011, 10:01:38 pm by IanB »
I'm not an EE--what am I doing here?
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9710
  • Country: my
  • reassessing directives...
Re: little querry to get an output to flip/flop
« Reply #45 on: October 19, 2011, 11:34:01 pm »
i did some exercise sometime ago with my students to program with punch/hole card, guess for what? blinking tungsten bulb :D
It's extremely difficult to start life.. one features of nature.. physical laws are mathematical theory of great beauty... You may wonder Why? our knowledge shows that nature is so constructed. We simply have to accept it. One could describe the situation by saying that... (Paul Dirac)
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 14173
  • Country: gb
  • 0999
Re: little querry to get an output to flip/flop
« Reply #46 on: October 20, 2011, 05:00:10 pm »
These days I don't know whether GCSE computer science even touches much on programming in high level languages, let alone machine code. I suspect it's based much more around PCs and desktop applications along with more general theory. There were of course no PCs in the 70's unless you count machines like the Commodore PET or Tandy TRS-80, and applications like word processing or spreadsheets were barely emerging concepts.
Yes, when I studied GCSE IT back in the 90s it was mostly geared to databases with some spreadsheets. There was a little BASIC programming but it wasn't a module in itself, we just had to write fragments of code as part of the database or spreadsheet.

I can't speak for today's GCSE course as I did it over 12 years ago. I imagine there's probably more web design nowadays.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf