little querry to get an output to flip/flop - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: October 10, 2011, 11:58:52 am
content: So I have a project I am already using the PWM output but i need another oscillating output that is no available on the pic I'm using. The program will run in a cycle that coincides with each PWM cycle so I had a thought, using basic as the language, have a "bit" type variable and add 1 to it each time I get round to "that" line of the program, in this way I should get a 0 1 0 1 0 1 0 1..... output, right ? or is there another way of doing it with complicated program lines, I just need a toggling output, frequency and duty are not much of a problem.

author: Jimmy
author_rank: Regular Contributor
timestamp: October 10, 2011, 12:09:03 pm
content: You will have to turn it on and off you can turn it on at the beginning and off half way through your program to output what you want

author: Simon
author_rank: Unknown
timestamp: October 10, 2011, 12:14:52 pm
content: I could do, i just wondered if I had a single bit variable 0 = 0, 0+1=1, 1+1=0, 0+1=1 etc

author: joelby
author_rank: Frequent Contributor
timestamp: October 10, 2011, 12:19:19 pm
content: Can you use a byte type and invert it each time, and then use the lowest bit (foo & 0x01)? You could increment a byte variable too, but using an adder seems like overkill for just toggling a bit.

author: jimmc
author_rank: Frequent Contributor
timestamp: October 10, 2011, 12:44:24 pm
content: Exclusive OR with a '1' in the bit position you want to toggle will toggle the bit as you require.Jim

author: Simon
author_rank: Unknown
timestamp: October 10, 2011, 12:47:08 pm
content: I think the timed set or reset bit program lines will be simpler, silly thought I can up with over lunch

author: PeterG
author_rank: Frequent Contributor
timestamp: October 10, 2011, 12:59:55 pm
content: Quote from: Simon on October 10, 2011, 12:14:52 pmWich BASIC language are you using?Proton?Regards

author: Simon
author_rank: Unknown
timestamp: October 10, 2011, 01:00:46 pm
content: mikroe-basic, I keep vowing to learn C but just never find the time

author: PeterG
author_rank: Frequent Contributor
timestamp: October 10, 2011, 01:06:10 pm
content: ok, i am thinking you have a few options.Instead of using +1, i would use NOT to flip the bit.The timing can be handled by either the pics timer or use a pair of Delay statements.Regards

author: Simon
author_rank: Unknown
timestamp: October 10, 2011, 01:17:29 pm
content: ah yes that will do it nicely

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 10, 2011, 04:49:32 pm
content: Quote from: Simon on October 10, 2011, 01:00:46 pmwhy dont you go direct to asm? C is "harder" than asm, much things are out of your control, just like basic. and asm is much closer relation with the mcu. people may argue

author: Simon
author_rank: Unknown
timestamp: October 10, 2011, 04:51:52 pm
content: Quote from: Mechatrommer on October 10, 2011, 04:49:32 pmIt is because of a number of assholes on another forum insisting that asm which i could not master was the only way and that nearly put me off electronics all together and caused a 2 year delay in my achieving anything in microcontrollers !

author: cyberfish
author_rank: Regular Contributor
timestamp: October 10, 2011, 07:53:22 pm
content: QuoteASM is suitable for some jobs, but for 95% of other things, C is much more suitable.ASM makes introducing bugs easier, takes a lot longer to write, and the code a lot harder to read and maintain. Don't use ASM unless necessary.

author: IanB
author_rank: Super Contributor
timestamp: October 10, 2011, 08:23:14 pm
content: Quote from: Mechatrommer on October 10, 2011, 04:49:32 pmThis is acrazyopinion.QuoteIndeed they may.

author: IanB
author_rank: Super Contributor
timestamp: October 10, 2011, 08:26:18 pm
content: Quote from: Simon on October 10, 2011, 11:58:52 amThe simplest idiom in a language like BASIC is this:Code:x = 0while (forever)x = 1 - xend

author: Simon
author_rank: Unknown
timestamp: October 10, 2011, 08:51:58 pm
content: The usual argument is that asm is faster, well my little 12F micro @ 4MHz can keep up with a car engine and more running a basic program so I can't see why i need asm in there

author: Zero999
author_rank: Super Contributor
timestamp: October 10, 2011, 09:24:02 pm
content: On the PC, I found assembly easier to learn than C which just made me want to give up.I've used BASIC before but never on an MCU.I've only done a little programming on MCUs and used assembler which I found quite straightforward.

author: Bored@Work
author_rank: Super Contributor
timestamp: October 10, 2011, 10:00:45 pm
content: Quote from: Hero999 on October 10, 2011, 09:24:02 pmAnd why do you think that single data point, if true, is sufficient to generalize?

author: Zero999
author_rank: Super Contributor
timestamp: October 10, 2011, 10:32:18 pm
content: Quote from: BoredAtWork on October 10, 2011, 10:00:45 pmI'm not.

author: IanB
author_rank: Super Contributor
timestamp: October 11, 2011, 02:10:56 am
content: If you "understand" computer hardware then assembly is easy (if tedious); otherwise it is not. However, C is like high level assembly, so if you don't "understand" computer hardware at an intuitive level then C is also hard to learn. On the other hand if you are comfortable with assembly then programming in C should be like a walk in the park.BASIC is in a different language family, quite deliberately so. Computers are supposed to be labour saving devices, and if BASIC saves on labour compared to C then program in BASIC and be happy!

author: Psi
author_rank: Super Contributor
timestamp: October 11, 2011, 02:16:57 am
content: Programming windows apps in C is much harder than C programming for a microcontroller.The Windows API is full of objects, structures and weird callback routenes which can be very annoying to get your head arround when starting out.You end up writing lots of boring API code just to 'tell' windows to create you a form.Of course modern IDE's often generate a lot of that code for you, so it's not too bad, but the code is still visible in your editior and gets in the way of following your actual code logic.When coding a microcontroller in everything is so much simpler. There is no OS API to interface with (unless you create one).Microcontroller code is normally quite easy to read and understand, even if you have limited skills in the language.After all, an IF and a LOOP is pretty easy to figure out in almost any language.(The obvious exception is ASM, where you really have to know the language to be able to read it)One thing you do need to understand when working with microcontrollers is bitwise operations and how use them. They're used far far more in microcontroller programming than windows programming.

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 11, 2011, 02:23:20 am
content: here we go againQuote from: Simon on October 10, 2011, 04:51:52 pmi'm not insisting, its just only suggestion since the OP did mention vb vs c

author: Simon
author_rank: Unknown
timestamp: October 11, 2011, 05:40:22 am
content: well I'm not sure how C compares to BASIC, I thought it was about as easy/hard to learn, maybe not ?

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 11, 2011, 06:03:48 am
content: same

author: IanB
author_rank: Super Contributor
timestamp: October 11, 2011, 06:09:05 am
content: Quote from: Simon on October 11, 2011, 05:40:22 amBASIC was designed to make it easy to get results out of a computer--designed help make writing programs fast and convenient. There have been many varieties of BASIC since the first one but all of them have had the same philosophy, a classic example being Microsoft Visual Basic. Visual Basic is the archetypal "get results fast" language.On the other hand C was designed to get "close to the metal". Everything about the language was intended to give you complete and full control over the hardware at the level of pointers, memory and data bits. It was intended to let you write every program you could write in assembly, only more easily.So easy/hard to learn is relative, but I would have to say in general that BASIC is easier to learn than C.

author: joelby
author_rank: Frequent Contributor
timestamp: October 11, 2011, 06:17:16 am
content: 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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 11, 2011, 09:25:33 am
content: Quote from: joelby on October 11, 2011, 06:17:16 ami'm currently building my own asm library so my pic code will be portable to avr. yes its a PITA.Quote from: joelby on October 11, 2011, 06:17:16 ami 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.

author: Zero999
author_rank: Super Contributor
timestamp: October 11, 2011, 05:09:16 pm
content: Quote from: IanB on October 11, 2011, 02:10:56 amI 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.

author: Simon
author_rank: Unknown
timestamp: October 11, 2011, 05:22:36 pm
content: 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

author: IanB
author_rank: Super Contributor
timestamp: October 11, 2011, 05:23:26 pm
content: Quote from: Hero999 on October 11, 2011, 05:09:16 pmUh 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++.QuoteQuite. And that's exactly how things were for many years in the era of Z80 and 6502 based home computers.

author: jahonen
author_rank: Super Contributor
timestamp: October 11, 2011, 07:40:18 pm
content: Quote from: Hero999 on October 11, 2011, 05:09:16 pmI 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 fastRegards,Janne

author: Unknown
author_rank: Unknown
timestamp: October 11, 2011, 08:41:51 pm
content: Quote from: jahonen on October 11, 2011, 07:40:18 pmI'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.Quote from: jahonen on October 11, 2011, 07:40:18 pmHand-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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 11, 2011, 09:26:54 pm
content: 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.

author: Simon
author_rank: Unknown
timestamp: October 11, 2011, 09:39:45 pm
content: Quote from: Mechatrommer on October 11, 2011, 09:26:54 pmwhy not invent the compiler and then sell itwhy work for someone else when you can work for yourself

author: Unknown
author_rank: Unknown
timestamp: October 11, 2011, 11:08:01 pm
content: Great, another programming language, justwhat the world desperately needs.

author: PeterG
author_rank: Frequent Contributor
timestamp: October 12, 2011, 02:03:56 am
content: 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.

author: jahonen
author_rank: Super Contributor
timestamp: October 12, 2011, 05:27:06 am
content: Quote from: alm on October 11, 2011, 08:41:51 pmSorry, 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

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 12, 2011, 07:39:41 am
content: Quote from: Simon on October 11, 2011, 09:39:45 pmbecause its not fun. i can live with either asm, C or basic.Quote from: alm on October 11, 2011, 11:08:01 pmthe 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?

author: Zero999
author_rank: Super Contributor
timestamp: October 13, 2011, 06:15:02 pm
content: 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.

author: Simon
author_rank: Unknown
timestamp: October 19, 2011, 06:40:45 am
content: well you can put assembly inline into Mikroe basic

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 19, 2011, 07:28:51 am
content: Quote from: Simon on October 19, 2011, 06:40:45 amasm 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

author: Simon
author_rank: Unknown
timestamp: October 19, 2011, 06:12:12 pm
content: 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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 19, 2011, 07:00:46 pm
content: lmgtfy:http://www.gooligum.com.au/tutorials/baseline/PIC_Base_A_1.pdfhttp://www.pic18f.com/tutorial/2007/12/06/tutorial-2-hello-world/http://talkingelectronics.com/projects/Elektor/PIC10F222/IgnStarter.htmland most importantly, the chip datasheet, in it there's asm code list.similar to C, there are examples out there.

author: Simon
author_rank: Unknown
timestamp: October 19, 2011, 09:39:01 pm
content: yes the datasheet only lists the mnemonics, not how to write a program

author: IanB
author_rank: Super Contributor
timestamp: October 19, 2011, 10:00:06 pm
content: Quote from: Simon on October 19, 2011, 09:39:01 pmHow 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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 19, 2011, 11:34:01 pm
content: i did some exercise sometime ago with my students to program with punch/hole card, guess for what? blinking tungsten bulb

author: Zero999
author_rank: Super Contributor
timestamp: October 20, 2011, 05:00:10 pm
content: Quote from: IanB on October 19, 2011, 10:00:06 pmYes, 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.