Converting from C+? to BASIC

Camerart

Member
Hi,
I can only read BASIC (Oshonsoft) and am faced with an example of CODE I need to use written in C+ I think.
Can anyone help, by converting this into BASIC please?
===============================================
CK_A = 0, CK_B = 0
For(I=0;I<N;I++)
{
CK_A = CK_A + Buffer
CK_B = CK_B + CK_A
}
==============================================
Cheers, Camerart.
 

Cromewell

Administrator
Staff member
Whatever it is it is not C. In fact, I am not sure exactly what language it is. Luckily, it is still fairly easy to read, but some things are missing. N and Buffer are unknown. I have added them in with made up values. Really the only difference from what you have to BASIC is the for loop structure, and really it is not that different. The C-like version you have posted uses this format: for(iterator variable and starting value, loop until, step) and the braces { } indicate what should be executed each time.

Code:
N = 10
Buffer = 1
CK_A = 0
CK_B = 0
For i = 0 to N step 1
  CK_A = CK_A + Buffer
  CK_B = CK_B + CK_A
Next i
 

Camerart

Member
Whatever it is it is not C. In fact, I am not sure exactly what language it is. Luckily, it is still fairly easy to read, but some things are missing. N and Buffer are unknown. I have added them in with made up values. Really the only difference from what you have to BASIC is the for loop structure, and really it is not that different. The C-like version you have posted uses this format: for(iterator variable and starting value, loop until, step) and the braces { } indicate what should be executed each time.

Code:
N = 10
Buffer = 1
CK_A = 0
CK_B = 0
For i = 0 to N step 1
  CK_A = CK_A + Buffer
  CK_B = CK_B + CK_A
Next i
Hi C2,
I have done a lot of programming for many years, and will never figure out how to do what you call simple :)
I can almost see what's happening, but one comma out of place makes mistakes, so this is why I ask the experts, thanks.
C
 

Camerart

Member
Hi,
I'm now trying to translate another program for use in my project. I use Oshonsoft BASIC.

Here are the 2x programs:

Any help will help me a lot, I'm stuck on things like [ | && += ] etc

Cheers, Camerart.
========================================================================
EXAMPLE:
#include <xc.h>
#include "config.c"
#include <stdint.h>
#define _XTAL_FREQ 32000000
#define NUM_SERVOS 6
#define FRAME 20000L //how long each frame is

uint16_t servoPos[NUM_SERVOS];
uint8_t servoCount=0;

void main(void) {
//setup timer 1 to count at 8MHz - the default
TMR1ON=1; //just turn it on
TMR1CLK=0b0001; //select Fosc/4
CCP1CON=0b10001001; //clear CCP1 output on match
CCP1IE=1; //enable CCP1 interrupts
for(uint8_t i=0;i<NUM_SERVOS;i++){
servoPos=1.5*8000;
}
TRISB=0;
LATB=0;
PEIE=1;
GIE=1;
while(1){
}
}


void __interrupt() inter(void){
if(CCP1IE && CCP1IF){
if(CCP1OUT){ //which phase of the output are we in?
//outputting the pulse
CCPR1+=servoPos[servoCount];
CCP1CON|=1; //is high phase, so clear on match
}else{
//outputting the gap
CCPR1+=FRAME-servoPos[servoCount];
CCP1CON&=0b11111110; //is low phase so set on match
unsigned char *point=(unsigned char *)&RB0PPS;
point[servoCount]=0; //release the old CCP1 output
if(++servoCount>=NUM_SERVOS){
servoCount=0;
}
point[servoCount]=9; //setup new CCP1 output
}
CCP1IF=0;
}
}
===================================================================
====================================================================
MINE:

'18F4431 32MHz XTL REMOTE_SLAVE POMMIE BASIC 160123 0900

'#include < xc.h >
'#include "config.c"
'#include < stdint.h >
'#define frame 20000l //how long each frame is#
Const frame = 2000

Define CONFIG1L = 0x00
Define CONFIG1H = 0x06 '8mHz x4 =32
Define CONFIG2L = 0x0c
Define CONFIG2H = 0x20
Define CONFIG3L = 0x04
Define CONFIG3H = 0x80
Define CONFIG4L = 0x80 'Set for HVP
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40

'OSH config
'#define _xtal_freq 32000000
Define CLOCK_FREQUENCY = 32
Define SINGLE_DECIMAL_PLACES = 2
Define STRING_MAX_LENGTH = 20

'SET PIN IN/OUT 18F4431
TRISA = %11011000 '7=OSC, 6=OSC, 4=QEIB 3=QEIA 2=TEMP SEROUT
TRISB = %00000000
TRISC = %01000100 '6=1-slave4431_cs, 2=MOSI
TRISD = %00000000 '6=led, 7=led, 5=synch
TRISE = %00000000

Define SIMULATION_WAITMS_VALUE = 1 'Comment in for SIM out for PIC

ANSEL0 = 0 '%00000000
ANSEL1 = 1 '%00000001'check 21.7?

Dim i As Byte

'#define num_servos 6
'uint16_t servopos[num_servos];
Dim servo_pos(8) As Word

'uint8_t servocount = 0;
Dim servocount As Byte

'void main(void) {
'//setup timer 1 to count at 8MHz - the default
'TMR1ON = 1; //just turn it on
'tmr1clk = 0b0001; //select Fosc/4
T1CON.7 = 0 '7=RD16 0 = Enables register read/write of Timer1 in two 8-bit operations
T1CON.6 = 1 'T1RUN 1 = Device clock is derived from Timer1 oscillator
T1CON.5 = 1 'T1CKPS.5-4 10 = 1:4 Prescale value
T1CON.4 = 0
T1CON.3 = 1 'T1OSCEN 1 = Timer1 oscillator is enabled
T1CON.2 = 0 'T1SYNC This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0.
T1CON.1 = 0 'TMR1CS 0 = Internal clock (F OSC/4)
T1CON.0 = 1 'TMR1ON 1 = Enables Timer1

'CCP1CON = 0b10001001; //clear CCP1 output on match
CCP1CON = %10001001 '7=Unimplemented 3-0=1001 = Compare mode; initialize CCPx pin high; on compare match, force CCPx pin low (CCPxIF bit is set)

'CCP1IE = 1; //enable CCP1 interrupts
PIE1.CCP1IE = 1 '1 = Enables the CCP1 interrupt

'For(uint8_t i = 0; i < num_servos; i++){
Dim num_servos(8) As Byte

'servopos = 1.5 * 8000;
servo_pos(i) = 1.5 * 8000
'}
'TRISB = 0;
TRISB = 0

'LATB = 0;
LATB = 0

'PEIE = 1;
INTCON.PEIE = 1 'PEIE/GIEL 1 = Enables all unmasked peripheral interrupts

'GIE = 1;
INTCON.GIE = 1 'GIE/GIEH1 = Enables all unmasked interrupts
'While(1){
While (1)
Wend
'}
'}
main_loop:


Goto main_loop

End
'void __interrupt() inter(void){
On High Interrupt
Save System

For i = 0 To num_servos - 1

'If(CCP1IE && CCP1IF){
If PIE1.CCP1IE And PIR1.CCP1IF Then


'If(ccp1out){ //which phase of the output are we in?
'//outputting the pulse
'???????????????????

'If Then
'CCPR1 += servopos[servocount];
'??????????????


'CCP1CON | = 1; //is high phase, so clear on match
'????????????????

'Endif

'}Else{
Else
'//outputting the gap
'CCPR1 += frame - servopos[servocount];
'???????????

'CCP1CON & = 0b11111110; //is low phase so set on match
'???????????????

'unsigned char * point = (unsigned char *) & rb0pps;
'????????????

'point[servocount] = 0; //release the old CCP1 output
'????????????????

'Endif
'If(++servocount >= num_servos){
'??????????????
'servocount = 0;
'}
'?????????????

'point[servocount] = 9; //setup new CCP1 output
'}
'???????????

'CCP1IF = 0;
'??????????

'}
'}
Endif
Next i

Resume
==========================================================
 

Cromewell

Administrator
Staff member
Any help will help me a lot, I'm stuck on things like [ | && += ] etc
[ is array/list indexing
Code:
Dim abc(10) as Byte ' make an array, with indexes 0-9
abc(3) = 32

|= is bitwise or with assignment in a single operator, x |= 5 is like saying x = x | 5

&& is logical and, all conditions must be true

For bitwise and logical operations, oshonsoft seems to make no distinction on operators, just the type of variables you pass. Presumably if you pass it bytes, you get a shift, and boolean you get logical.

+= is just addition, equivalent to value = value + 5

Hope that helps, I don't really speak oshonsoft, just MSBASIC. Most of it seems to be the same though
 

Camerart

Member
[ is array/list indexing
Code:
Dim abc(10) as Byte ' make an array, with indexes 0-9
abc(3) = 32

|= is bitwise or with assignment in a single operator, x |= 5 is like saying x = x | 5

&& is logical and, all conditions must be true

For bitwise and logical operations, oshonsoft seems to make no distinction on operators, just the type of variables you pass. Presumably if you pass it bytes, you get a shift, and boolean you get logical.

+= is just addition, equivalent to value = value + 5

Hope that helps, I don't really speak oshonsoft, just MSBASIC. Most of it seems to be the same though
Hi C,
I've been learning BASIC since the 80's, and still can't fully understand it, and I think these other languages are even more difficult especially as I'm not as young as I was.
I'll have to stop trying to convert this code, as these characters, go into infinite complication to me.
Thanks, C.
 

Camerart

Member
Hi,
Update:
I've been using AI, which does a pretty good job. Then the BASIC needs a little tweeking at compile.
C
 
Top