Convert my Picaxe Basic code?

drewbagd

New Member
Hello!

I've made a small program for my picaxe 08M chips that works pretty well. However, I want to be able to put it into different programming languages (C, Basic, etc) and I'm not sure the best way to go about it. I've only learned Picaxe Basic, so I'm not sure what kind of work the basic interpreter does. I suppose if I knew that, I would know how to write my program in regular Basic code. Any help would be greatly appreciated!

Thank you

Code:
symbol counter1 = b1         
symbol counter2 = b2
symbol counter3 = b3
symbol counter4 = b4
symbol counter5 = b5
symbol math = b6
symbol RED = B.4            
symbol GOLD = B.2
main:
    if pin3 = 0 then pause 3000 endif
    if pin3 = 1 then
	for counter5 = 0 to 255
		low RED
		low GOLD 
		math = 255-counter5
		Pause math
		high RED
		high GOLD 
		Pause math
		if pin3 = 0 then exit
		next counter5
	for counter5 = 255 to 0 step - 1
		low RED
		low GOLD 
		math = math + 1
		Pause math
		high RED
		high GOLD 
		Pause math
		if pin3 = 0 then exit
		next counter5
	else goto game
	endif		
	goto main
game:   
    for counter1 = 1 to 25     ; start a for...next loop
        high RED                   ; switch pin 4 high
        low GOLD
        pause 100                  ; wait for 0.5 second
        high GOLD
        low RED                   ; switch pin 4 low
        pause 100                 ; wait for 0.5 second
    next counter1
    for counter2 = 1 to 59     ; start a for...next loop
        low RED                   ; switch pin 4 high
        low GOLD
        pause 58000 
         for counter4 = 1 to 50     ; start a for...next loop
     		high RED                   ; switch pin 4 high
        	low GOLD
        	pause 50                  ; wait for 0.5 second
        	high GOLD
        	low RED                   ; switch pin 4 low
        	pause 50                 ; wait for 0.5 second
    	    next counter4      			 ; wait for 0.5 second
    next counter2                  ; end of for...next loop
    do				     ; start a for...next loop
        high RED                   ; switch pin 4 high
        low GOLD
        pause 750                  ; wait for 0.5 second
        high GOLD
        low RED                   ; switch pin 4 low
        pause 750                 ; wait for 0.5 second
    loop
    low RED
    low GOLD
end
 

bluejets

Senior Member
It's not simply a matter of conversion.
First you would need a reason to change, either the existing code is not fast enough or won't address certain problems.
Then you would need to select the chip to use.
Then select a compiler and code type.
Then read the existing code to see what you intend the code to do.
Then start to write the new code by configuring the new chip
Write and then test the new code in a simulator and fix any bugs.
 

jedynakiewicz

Senior Member
I've only learned Picaxe Basic, so I'm not sure what kind of work the basic interpreter does. I suppose if I knew that, I would know how to write my program in regular Basic code.
Firstly, a warm welcome to the PICAXE forum as this is your first post. I am sure that you will find this to be a most affable and helpful group.

PICAXE BASIC is a pretty straightforward dialect of BASIC. There is really no "regular basic" as you describe it; unless you mean Kemeny & Kurtz's Dartmouth College BASIC which was the original iteration of the language. BASIC is a high level programming language which is then run through a compiler to create code that can be run by a computer, in our case a microcontroller. The type of computer and its functions are often reflected in the dialect of BASIC that is used to create programs for it. Therefore there are many versions or dialects of BASIC.

If you are becoming competent in the PICAXE dialect of BASIC then you can comfortably claim to be competent in BASIC per se. PICAXE BASIC is really a very effective, well-formed and versatile dialect of BASIC, specific to the operation of microcontrollers; therefore it contains a range of commands that are not available in other dialects of BASIC. For example, you would not find a command to control pulse-width modulation in other versions that are not specific to microcontrollers. If anything, therefore, PICAXE BASIC is something of an extended dialect of BASIC.

It is difficult to follow what your real intention is; if you would like to program microcontrollers other than PICAXE microcontrollers then there a wide range of alternative languages and compilers available; for example you mention C - you will find these pretty easily with an internet search. You will then need to purchase a microcontroller programmer such as the Microchip PICKit in order to program the PIC. There is, of course, a PICAXE version of this, the BAS800 from Techsupplies but this is more limited in its range of application than the PICKits.

Computer languages are very much a question of "horses for courses" - if the programming system you are using is working for you there is little reason to change it. If, however, you are looking to widen your computer skills in general then there is plenty out there to choose from. As far as "translating" your program from one language to another, that can be less than straightforward; because the languages vary not only in commands but also in structure, you would be better to consider the algorithm that lies behind your program and then apply the algorithm to another language. You may regard an algorithm as the underlying logic that dictates what you wish your program to do.

To improve your skills with the existing version of BASIC that you are using here, why not look at the blog on this website http://www.picaxeforum.co.uk/entry.php?29-Good-Picaxe-programming-practices. It makes some very good points on good practice.

I do hope that this goes some way to answering your question and, once again, welcome to the forum and have many happy hours of microcontroller programming.
 

drewbagd

New Member
Wow what a great response, thank you jedynakiewicz! Firstly thank you for the warm welcome--I've been lurking around these forums for a while now, this is my first real challenge that I was hoping to get some help with. I was pretty confused initially when learning about the different 'dialects' as you put it so your response really clears up a lot of my confusion.

Basically I have been using strictly Picaxe chips for my projects so I was feeling pretty comfortable and confident in that. However, I have since earned myself some free PIC chips from a friend, as well as another chip from a free giveaway, and I wanted to see if I could make this current project work on the PIC chip. I don't want to stop using my Picaxe chips because they are just so versatile and I'm actually getting the hang of them! But, I would like to remain versatile myself and be able to do projects on both my picaxe chips and my pic chips (fyi the pic chips I have are PIC10F200).

I have no idea how to use the new additions to my hobby, and while I want to be able to try this program on this new pic chip (it's much smaller so I can run the project in a much smaller container, which I'm excited about), I dont really have the time to learn an entirely different language. That's were the part of my question regarding how the boot loader works came from: I was thinking maybe I could continue to work on my picaxe projects and when I wanted to utilize the PIC chip I just would have to change a few commands or tweak a few lines to get the picaxe dialect into an understandable one for the pic chips. Does that make more sense?

Thank you again for all your help!
 

jedynakiewicz

Senior Member
I have no idea how to use the new additions to my hobby, and while I want to be able to try this program on this new pic chip (it's much smaller so I can run the project in a much smaller container, which I'm excited about), I dont really have the time to learn an entirely different language. That's were the part of my question regarding how the boot loader works came from: I was thinking maybe I could continue to work on my picaxe projects and when I wanted to utilize the PIC chip I just would have to change a few commands or tweak a few lines to get the picaxe dialect into an understandable one for the pic chips. Does that make more sense?
My dear Sir, that does indeed make a lot of sense. I can quite recognise the situation, having been in a similar frame of mind several years ago when I was new to PICAXE programming. So permit me a few lines in order to explain to you why, perhaps, you are best just remaining with PICAXE.

Firstly, microcontroller chips are, relatively speaking, very cheap indeed. PICAXE microcontrollers enjoy a slight price premium because they have extra firmware that enables them to be programmed directly via a simple cable from the computer. It is extraordinary that the programming software is available free of charge, but just because it is free does not mean that it is not top class - I regard it as being so. The PICAXE microcontrollers are standard silicon but with the additional firmware that enables easy and direct programming. Overall, they do what they say on the tin - and do it very well indeed!

Now the purchase of the BAS800 programmer that I mentioned, and later as JimPerry advised, enables you to use the PICAXE BASIC programming software and then to compile the program down to the Assembler language/Hex Code that can be written directly to a "raw" PIC. I bought one of these some years ago and it does, like other PICAXE products, work as intended. The drawback is that it has a rather limited range of PICs that it will program. If you divide the cost of the BAS800 across the small premium that you pay additionally for each MicroChip PIC that has the installed PICAXE firmware, you will find that you need to buy an awful lot of "empty" PICs to make the cost of the programmer worthwhile. Furthermore, there is little to be gained in any operational manner; in fact there is a loss in that you will have to remove the chip from your circuit each time you wish to tweak the code.

To program the chips that you mention, you will need one of Microchip's PICKit programmers. - I wouldn't go for any other type; Microchip make the PICs, buy their programmer. The PICKits are available in several versions; as the price rises so does the range and sophistication of the chips that they will program. But you are getting into costs of many tens of dollars in order to use chips that cost a few cents. Economically, it makes no sense at all - intellectually though it is great fun to learn more. You will also find that you will need a programming language and again BASIC is very popular. But you will find that this needs to be purchased and most dialects offer little advantage over PICAXE BASIC for most hobby purposes (although there is a free version called GCBASIC, but I think you will find it not to be as versatile a dialect as PICAXE BASIC. One other factor to remember is that PICAXE is a professionally and commercially-supported system that is upgraded regularly, has excellent support and there is a new version of the Programming Editor on the way, so we are promised. (I am very much looking forward to this upgrade).

With regard to the small size of the PICS that you have, please remember that the PICAXE chips are also available as surface-mount versions where space is at a premium. I have squeezed the 08M2 surface mount into some very small models indeed.

So in conclusion; if you are looking for a challenging intellectual exercise and the cost of it is of no importance then buy yourself a PICKit programmer and start building up your portfolio of programming software. If you are more concerned about cost, then stick the free PICs in a drawer and forget about them and develop your projects with PICAXE. If you want to miniaturise a project, look to the 08M2 surface mount PIC https://www.techsupplies.co.uk/epages/Store.sf/sece21e236ee4/?ObjectPath=/Shops/Store.TechSupplies/Products/AXE007M2 and stick with the programming that you know.

(Incidentally, my BAS800 has been idle for many years, as have the several other PIC Programmers that I have bought along the way at various times -though I do not regret the learning experience. PICAXE is such a convenient, powerful and very clever way to use microcontrollers that I bother with little else for my projects- so perhaps you may just wish to keep improving your programming skills within this arena. And look forward to the release of Programming Editor 6.0!)
 

mrburnette

Senior Member
<...>Basically I have been using strictly Picaxe chips for my projects so I was feeling pretty comfortable and confident in that. However, I have since earned myself some free PIC chips from a friend, as well as another chip from a free giveaway, and I wanted to see if I could make this current project work on the PIC chip. I don't want to stop using my Picaxe chips because they are just so versatile and I'm actually getting the hang of them! But, I would like to remain versatile myself and be able to do projects on both my picaxe chips and my pic chips (fyi the pic chips I have are PIC10F200).
<...>
The path is somewhat complicated and somewhat expensive even at the BAS800 reasonable pricing. Certainly is much more complex than using the PICAXE. As an overview:

If you know PICAXE BASIC and are comfortable with your projects and are not hitting a 'glass ceiling' due to speed/performance issues, I would suggest that you follow a slightly different course for brain exercise. BASIC is, well it is basic... a teaching language that as implemented in the PICAXE is a kind of scripting; one takes pre-defined commands and these are turned into tokens that then execute within the PICAXE chip state machine. The "real" code is the firmware, your BASIC is simply exercising many pieces of RevEd written code which is likely C or assembler (or both.) Since RevEd is very secret about their firmware, I will not venture into that area.

With a naked PIC, if you write in BASIC or in C/C++ or mostly any language dialect, the compiler will take your code and use a pre-written library of routines to match your commands to the library. So, this is different than the PICAXE approach with the routines already in the chip, but it results in the compiled code being your orchestration and most of the dirty work being library code. The exception to this comes with Assembly programming where the programmer dictates every single process the uC registers must perform (even then, some compiler optimizations can substitute "other" code for yours!)

So, my suggestion is to stick with BASIC and continue gaining proficiency in this area for PICAXE. For those times you really wish to exercise those gray cells and energize a few neurons, then learn a structured language... be it a microcomputer that runs a structured BASIC (functions/subroutines/public and private variables/arrays, etc.) or an ??duino ... of which there are many inexpensive boards that run the C/C++ that utilizes the GNU tool chain.

My point is, you may be able to conceive and execute every conceivable project you wish with a PICAXE (they are capable little chips and inexpensive) but you will never challenge yourself to move into the area of advanced coding concepts because the PICAXE concept/implementation does not (at present) have such language capabilities as functions, variable visibility (scope other than Public), user defined objects, or true arrays. Such concepts are core concepts in evolving your thinking about programming. In my experience, many here on the forum program in higher level languages, but find the PICAXE a mainstay for projects... like the comfort of an old friend. However, even though the PICAXE does not embrace advanced concepts, I find that knowing how I would do something in C/C++ helps me with programming in PICAXE BASIC; because I understand the big picture, I can script a workable solution with the PICAXE tools.

Feed your brain - explore beyond your comfort level - learn.

- Ray
 

drewbagd

New Member
http://www.techsupplies.co.uk/epages/Store.sf/en_GB/?ObjectPath=/Shops/Store.TechSupplies/Products/BAS800 is one way of converting Basic to Assembler / Hex code - it does't support all PICs natively but will convert from within the Programming Editor to the code needed to run on a standard PIC not Picaxe.
I guess I'm still confused--so it converts from within the software to hex code that runs on the chips? Could we feasibly just get that software then instead of the programmer (obviously, the chip needs to be programmed somehow but ignoring that fact)?
 

mrburnette

Senior Member
I guess I'm still confused--so it converts from within the software to hex code that runs on the chips? Could we feasibly just get that software then instead of the programmer (obviously, the chip needs to be programmed somehow but ignoring that fact)?
My limited understanding is that he RevEd software is "married" to the BAS800, so you cannot actually create the assembly code unless you physically have the BAS800.

- Ray
 

mrburnette

Senior Member
@drewbagd:

The PICAXE was created (see PICAXE history) in late 1999 or early 2000 and used a PIC16F872 as the host microcontroller. The BASIC Stamp preceded the PICAXE by many years (BASIC_Stamp) and while it was not a one-chip solution, there are similarities in the BASIC interpreter and command structure. Much of the BASIC Stamp information is widely known due to code disassembles and competitive/parallel technology. If you are really interested in how a BASIC command can be linked to Assembly code, in the context of the PIC microcontroller, this may be a good read: The PIC Source-Book

-Ray
 

nick12ab

Senior Member
Yup - it has a Picaxe on board and is programmed to respond to the PE as a Rev Ed product.
Are you sure? Although it has a PIC16F627, it's not necessarily a PICAXE and the pin that's used as Serial In on PICAXE appears to be floating and certainly isn't connected to ground through a resistor.
 

JimPerry

Senior Member
Are you sure? Although it has a PIC16F627, it's not necessarily a PICAXE and the pin that's used as Serial In on PICAXE appears to be floating and certainly isn't connected to ground through a resistor.
No you are correct - I made an assumption :rolleyes:
 

Mark Harrington

New member
Hello!

I've made a small program for my picaxe 08M chips that works pretty well. However, I want to be able to put it into different programming languages (C, Basic, etc) and I'm not sure the best way to go about it. I've only learned Picaxe Basic, so I'm not sure what kind of work the basic interpreter does. I suppose if I knew that, I would know how to write my program in regular Basic code. Any help would be greatly appreciated!

Thank you

Code:
symbol counter1 = b1        
symbol counter2 = b2
symbol counter3 = b3
symbol counter4 = b4
symbol counter5 = b5
symbol math = b6
symbol RED = B.4           
symbol GOLD = B.2
main:
    if pin3 = 0 then pause 3000 endif
    if pin3 = 1 then
    for counter5 = 0 to 255
        low RED
        low GOLD
        math = 255-counter5
        Pause math
        high RED
        high GOLD
        Pause math
        if pin3 = 0 then exit
        next counter5
    for counter5 = 255 to 0 step - 1
        low RED
        low GOLD
        math = math + 1
        Pause math
        high RED
        high GOLD
        Pause math
        if pin3 = 0 then exit
        next counter5
    else goto game
    endif       
    goto main
game:  
    for counter1 = 1 to 25     ; start a for...next loop
        high RED                   ; switch pin 4 high
        low GOLD
        pause 100                  ; wait for 0.5 second
        high GOLD
        low RED                   ; switch pin 4 low
        pause 100                 ; wait for 0.5 second
    next counter1
    for counter2 = 1 to 59     ; start a for...next loop
        low RED                   ; switch pin 4 high
        low GOLD
        pause 58000
         for counter4 = 1 to 50     ; start a for...next loop
             high RED                   ; switch pin 4 high
            low GOLD
            pause 50                  ; wait for 0.5 second
            high GOLD
            low RED                   ; switch pin 4 low
            pause 50                 ; wait for 0.5 second
            next counter4                   ; wait for 0.5 second
    next counter2                  ; end of for...next loop
    do                     ; start a for...next loop
        high RED                   ; switch pin 4 high
        low GOLD
        pause 750                  ; wait for 0.5 second
        high GOLD
        low RED                   ; switch pin 4 low
        pause 750                 ; wait for 0.5 second
    loop
    low RED
    low GOLD
end

Hi the device is similar to the 12F675

Try this code using xc8 compiler MPlabx with pickit3 as programmer

I cant see why this wont work but try this and see


C:
/*
 * File:   test2.c
 * Author: mark
 *
 * Created on 30 August 2023, 16:40
 */


// PIC12F675 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF         // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#define _XTAL_FREQ 4000000  // Assuming a 4MHz oscillator frequency

#define RED GP4
#define GOLD GP2

unsigned char counter1;
unsigned char counter2;
unsigned char counter4;
unsigned char counter5;
unsigned char math;

void pause(unsigned int ms) {
    for (unsigned int i = 0; i < ms; i++) {
        __delay_ms(1);
    }
}

void main(void) {
    TRISIO = 0b000010; // Set GP3 as input, others as output

    while (1) {
        if (GP3 == 0) {
            pause(3000);
        } else {
            for (counter5 = 255; counter5 != 255u; counter5--) {
                RED = 0;
                GOLD = 0;
                math = 255u - counter5;
                pause(math);
                RED = 1;
                GOLD = 1;
                pause(math);
                if (GP3 == 0) {
                    break;
                }
            }

            for (counter5 = 255; counter5 !=255 ; counter5--) {
                RED = 0;
                GOLD = 0;
                math = (unsigned)math + 1;
                pause(math);
                RED = 1;
                GOLD = 1;
                pause(math);
                if (GP3 == 0) {
                    break;
                }
            }
        }

//game
        for (counter1 = 1; (unsigned)counter1 <= 25; counter1++) {
            RED = 1;
            GOLD = 0;
            pause(100);
            GOLD = 1;
            RED = 0;
            pause(100);
        }

        for (counter2 = 1; (unsigned)counter2 <= 59; counter2++) {
            RED = 0;
            GOLD = 0;
            pause(58000);
            for (counter4 = 1; (unsigned)counter4 <= 50; counter4++) {
                RED = 1;
                GOLD = 0;
                pause(50);
                GOLD = 1;
                RED = 0;
                pause(50);
            }
        }

        while (1) {
            RED = 1;
            GOLD = 0;
            pause(750);
            GOLD = 1;
            RED = 0;
            pause(750);
        }

        RED = 0;
        GOLD = 0;
    }
}
Here is the hex file to upload to device

Code:
:02000000F72BDC
:1006D2008312A301A4017B2BC730A200702BA20BB3
:1006E2006F2B732B742B01308312A3070318A40AF8
:1006F2000030A40721082402031D812B20082302B5
:10070200031C842B852B6D2B080002308316850079
:10071200831285198D2B8E2B942BB830A0000B30B1
:10072200A1006923982BFF30A700FF30A700831296
:10073200AA01AA0A051605116430A0000030A10022
:1007420069238312051505126430A0000030A10050
:10075200692301308312A5002508AA071A302A024C
:10076200031CB42BB52B9B2BA901A90A0512051159
:100772009030A000E230A10069238312A801A80AE8
:10078200051605113230A0000030A1006923831242
:10079200051505123230A0000030A1006923013096
:1007A2008312A5002508A80733302802031CDA2B80
:1007B200DB2BC12B0130A5002508A9073C302902FB
:1007C200031CE42BE52BB72B831205160511EE3023
:1007D200A0000230A1006923831205150512EE3034
:1007E200A0000230A1006923E52B00288312A60194
:0C07F200A701A801A901AA018301862B20
:08400000FF3FFF3FFF3FFF3FC0
:02400E00D431AB
:00000001FF
Let me know of this works for you

Fingers crossed I think it will
 

Mark Harrington

New member
Thanking you Erco Very nice to meet some of you by the way and thanking you for your kind comments of appreciation
Mark, the prior post was from 2012, so the OP may be long gone. But a good effort nonetheless!


I chanced over some of this purely by accident but I thought well no one seems to have answered this or have found a solution so even though late perhaps , my thinking was some one will be interested to see how this is done I also looked at the questions of new code compiler IDE similar to axe-pad and whilst on this topic even though this is no longer available apart from old distro's Im making this my next project to re produce for all operating systems

Ive always found the terms or syntactical use of basic a very good starting point for embedded systems I hope no one tells me off for using this description (Not deliberate by the way and no offense meant to anyone )

Im also working on producing along with many other ideas I have a really nice development board for pic axe which obviously I would market This could in itself open many doors for people from all sectors beginners inclusive and open educational doors as well for training etc

Ive been toying with this for years now but due to the levels which I have had to teach myself to finally reach a possibility if now being able to do this has been a very challenging lengthily process because there is just so much to take in and learn on all aspects or IT embedded , design , cad , compilers , languages etc which i wont bore any one with

So hopefully who knows from not to many aware of pic axe and its tremendous capabilities to far more interest

To the future and enhance ment of pic-axe then

Have a great evening
 
Top