Converting a piece of PIC code to Arduino

Hello there,

I found a piece of code in PIC language that I’ve tried to convert in arduino.

#include <xc.h>
#include <htc.h>

#pragma config BOREN = OFF, CPD = OFF, FOSC = INTRCIO, MCLRE = OFF, WDTE = OFF, CP = OFF, PWRTE = OFF
#define FOSC 4

extern void eeprom_write(unsigned char addr, unsigned char value);
extern unsigned char eeprom_read(unsigned char addr);

void delay_ms(unsigned int duration);

void main(){
        unsigned char val_GPIO;
        unsigned char eeprom_id;
        unsigned char counter_type;

	GPIO = 0; //init & hold reset low
	TRISIO = 0x8; //b001000 hold reset #GP2 as output
	  	
   	eeprom_id = eeprom_read(0x00); //read eeprom value
	
        counter_type = 3; //default 2/4 games

        if(GP3==1) counter_type = 2; //GP3 == VCC
        	
        if(eeprom_id >= counter_type){
            eeprom_id = 0; //init eeprom value if empty/new
            }else{
            eeprom_id++;
	}

    eeprom_write(0x00, eeprom_id);
    delay_ms(30);

    //GP0 = bankswitch EPROM
    //GP1 = bankswitch EPROM #2
    //GP2 = /RST
    //GP3 = - switch between 2/4 and 3 games config
    //GP4 = SRAM 1
    //GP5 = SRAM 2

     switch(eeprom_id){
       case 0:
       //000000
       val_GPIO = 0x0;
       break;
       
       case 1:
       //010001
       val_GPIO = 0x11;
       break;
       
       case 2:
       //100010
       val_GPIO = 0x22;
       break;
       
       case 3:
       //110011
       val_GPIO = 0x33;
       break;      
     }

    TRISIO = 0xC; //in GP2 & GP3
    GPIO = val_GPIO; //high-low
    
   while(1){
    //endless loop
    }
}


void delay_ms(unsigned int duration){
    // delay in miliseconds for 4.0MHZ crystal
    unsigned int i;
    for(;duration!=0;duration--){
        for(i=0;i<=50;i++){
            asm("nop");
            asm("nop");
            asm("nop");
        }
        asm("nop");
        asm("nop");
    }
}

Here is what I’ve tried :

#include <EEPROM.h>
unsigned char val_GPIO;
unsigned char eeprom_id;
unsigned char counter_type;
int GP3 = 13;
int GP2 = 12;
void setup() {
  // put your setup code here, to run once:



pinMode(GP2,LOW);

counter_type = 3;

}

void loop() {
  // put your main code here, to run repeatedly:
eeprom_id = EEPROM.read(0x00);

if(GP3==1) {
counter_type = 2;

}


if (eeprom_id >= counter_type) {
  eeprom_id = 0;
}else{
  eeprom_id++;
  
}

EEPROM.write(0x00, eeprom_id);
delay(30);

switch(eeprom_id){
      case 0:
       //000000
       val_GPIO = 0x0;
       break;
       
       case 1:
       //010001
       val_GPIO = 0x11;
       break;
       
       case 2:
       //100010
       val_GPIO = 0x22;
       break;
       
       case 3:
       //110011
       val_GPIO = 0x33;
       break;      
     }

//IM BLOCKED HERE

}

I didn’t manage to end the code, if anyone knows if it’s correct and can end the code that would be generous.

Thanks by advance

Regards

You never actually stated what you expect the code to do.
Or do we have to guess?

Thanks for your reply, sorry i forgot to mention it :
It's meant to bankswitch an eeprom.

The pic code is for a PIC12F629.

//IM BLOCKED HERE

Well, this part will certainly work for both the PIC and the Arduino:

  while(1){
    //endless loop
    }

This part sets pins GP2 and GP3 as input, and sets the data bits in val_GPIO to the output pins.

    TRISIO = 0xC; //in GP2 & GP3
    GPIO = val_GPIO; //high-low

Great, thanks for your help.

sets the data bits in val_GPIO to the output pins

How could I do that ?

Thanks again

How could I do that ?

Use digitalWrite(), or direct port access. It depends on your wiring, which you also forgot to post.

PS: SRAM1 and SRAM2 is for bankswitching the ram, it's in option.

jremington:
This part sets pins GP2 and GP3 as input, and sets the data bits in val_GPIO to the output pins.

    TRISIO = 0xC; //in GP2 & GP3
GPIO = val_GPIO; //high-low

If GP2 & GP3 are set as input then what are the other outputs because they aren't set.

What i did :

pinMode(GP2, INPUT);
pinMode(GP3, INPUT);

digitalWrite(?????? , val_GPIO);

Thanks

Do you actually understand what the PIC is doing?

As far as inputs and outputs are concerned, perhaps you should go back and review how to blink an LED and read a switch on the Arduino. The same principles apply to this project.

Not really.

The other pins of the PIC aren't set as output so how could It write then.

It would be like this to set the data_bits to the outputs ?

digitalWrite(GP0 , val_GPIO);
digitalWrite(GP1 , val_GPIO);
digitalWrite(GP4 , val_GPIO);
digitalWrite(GP5 , val_GPIO);

If you want to duplicate the operation of the PIC, it is absolutely essential to first understand what that program is doing, which is to set certain pins to certain values.

Consult the 12F679 data sheet, section 3.1, to learn about the function of the TRISIO register and how that sets up input and output pins.

Thanks,

I understand know but to be sure I got a question :

//010001
       val_GPIO = 0x11;
       break;

This code means that GP1 and GP5 will be "high" ?

But what about this ?

GPIO = 0; //init & hold reset low

It says that it hold reset low but =0 seems to be all pins.

Thanks

Regards

This code means that GP1 and GP5 will be "high" ?

No. Pins GP0 and GP4 will be high, if val_GPIO is output to the port.

It says that it hold reset low but =0 seems to be all pins.

It says init & reset. In fact all OUTPUT pins will be set low.

Thanks.

This is what i’ve done. I didn’t define the other pins as OUTPUT since there isn’t in the pic code, should i ?

#include <EEPROM.h>
unsigned char val_GPIO;
unsigned char eeprom_id;
unsigned char counter_type;
int GP5 = 13;
int GP4 = 12;
int GP3 = 11;
int GP2 = 10;
int GP1 = 9;
int GP0 = 8;
void setup() {
  // put your setup code here, to run once:



  pinMode(GP2,OUTPUT);


  counter_type = 3;

}

void loop() {
  // put your main code here, to run repeatedly:
  eeprom_id = EEPROM.read(0x00);

  if(GP3==1) {
    counter_type = 2;

  }


  if (eeprom_id >= counter_type) {
    eeprom_id = 0;
  }
  else{
    eeprom_id++;

  }

  EEPROM.write(0x00, eeprom_id);
  delay(30);

  switch(eeprom_id){
  case 0:
    //000000
    //val_GPIO = 0x0; 
    digitalWrite(GP0, LOW);
    digitalWrite(GP1, LOW);
    digitalWrite(GP2, LOW);
    digitalWrite(GP3, LOW);
    digitalWrite(GP4, LOW);
    digitalWrite(GP5, LOW);

    break;

  case 1:
    //010001
  //  val_GPIO = 0x11; // GP0 & GP4 = HIGH 
    digitalWrite(GP0, HIGH);
    digitalWrite(GP1, LOW);
    digitalWrite(GP2, LOW);
    digitalWrite(GP3, LOW);
    digitalWrite(GP4, HIGH);
    digitalWrite(GP5, LOW);

    break;

  case 2:
    //100010
   // val_GPIO = 0x22;
    digitalWrite(GP0, LOW);
    digitalWrite(GP1, HIGH);
    digitalWrite(GP2, LOW);
    digitalWrite(GP3, LOW);
    digitalWrite(GP4, LOW);
    digitalWrite(GP5, HIGH);
    break;

  case 3:
    //110011
//    val_GPIO = 0x33;
    digitalWrite(GP0, HIGH);
    digitalWrite(GP1, HIGH);
    digitalWrite(GP2, LOW);
    digitalWrite(GP3, LOW);
    digitalWrite(GP4, HIGH);
    digitalWrite(GP5, HIGH);
    break;      
  }

  pinMode(GP2, INPUT);
  pinMode(GP3, INPUT);


}

I didn't define the other pins as OUTPUT

In Arduino, all pins that are to be used for output must be declared OUTPUT.

The point is not to duplicate the PIC code, it is to make the ATMega processor act exactly like the PIC processor in the application.

To do that, you have to know exactly what the PIC is doing, and follow all the Arduino language rules to make the ATMega processor do exactly the same thing.

Why are you doing this? Why not just use a PIC?

jremington:
Why are you doing this? Why not just use a PIC?

Good question.

We await the answer with great interest …

I don't have one and I wanted to know how the code would be for an Arduino.
I got some attiny's so I wanted to use one of them.