Showing posts with label Olimex. Show all posts
Showing posts with label Olimex. Show all posts

Monday, 28 October 2013

Arduino Decimal to Roman numerals converter- The code for Olimex WPC #30

Ok, here is the story :
Friday i saw the "Olimex WPC #30 Problem" just posted and decide to participate...MORE: I was going to try to be the first one to apply a working project, regardless of the "cost".
So i rushed, cheated, took no notice of programming "rules", and VOILA !
Also, as i rarely(?!) see an "Arduino Language" entry ( if you can call it that) , i decided it was what id be applying with ( and because i had nothing else more to code with, at the time :) ).
Added my good mood to the vibes, and this is what came out !
Will only work up to 9999 numbers... If you do a calendar with it, they will probably think it is the end of the world in the year 9999, due to my laziness !!
It was the fastest, laziest way i could find at the time !
* Dont take this code too serious...

**See the results here !https://github.com/OLIMEX/WPC/tree/master/ISSUE-30


// Enjoy the random names for variables and function at moments. 
// and learn how to write down the year that Olimex was founded in Roman numerals
// For completion, i give you the foundation of their fraction system: A twelfth !
// DISCLAIMER : Any similarities with attempt at comedy is pure coincidence !
//        CHANGE THIS to test other numbers ( up tp 9999
//         | | | | |    
//         V V V V V
char plaintext[]="1991"; // Why a char[] array? More user friendly with both c and c++
// and because i can !! :)

int tocipher[sizeof(plaintext)];
int ciphered[sizeof(tocipher)];

boolean stringComplete = false;  // whether the string is complete
boolean done = false; // no relationship between the name and function ! ;)

void setup() {
  // DO NOT put your code here to run once ?!?!
  Serial.begin(9600);
  Serial.println("Roman Numeral History Lesson with Olimex Pt.1");
}

void loop() 
{ 
  while(Serial){
    if(stringComplete == true)  // How long is a piece of string ?!
    {
      calculate();
      Serial.println("string complete");
      fractions();
      //
    }
    if(stringComplete == false){
      Serial.println("string NOT complete");
      // put your debug code here, to find out where you was going wrong: 
      uint8_t sizeofarray = sizeof(plaintext);
      uint8_t i;
      //
      for(i=0;i<=sizeofarray;i++)
      {
        uint16_t retrieved;
        retrieved=plaintext[i];
        leap(retrieved,i);
        Serial.println(retrieved);

        if(retrieved==0){
          Serial.print(i);
          Serial.println(" characters in the string processed.END OF STRING "); // Thats how long a string is :)
          Serial.println("null CHARACTER");
          stringComplete = true; 
          // The use of goto's is highly frowned upon...
          // ...but can be quite handy as well !
          goto bailout; 
        }
      }
bailout: // " Bloody bankers taken all !"
      delay(1); // As the tradition says:" Rome and Pavia weren't made in one day!
    }  
    while(done==true){
      // Allowing time for Nero to play with fire !
    }
  }
}

int leap(int in,int il){
// tocipher[il]=in; would have been enough
  switch (in) {
  case '0':  
    //
    Serial.println("tocipher- 0 ");
    tocipher[il]=0;
    break;
  case '1':
    Serial.println("tocipher- 1 ");
    tocipher[il]=1;  
    //
    break;
  case '2': 
    Serial.println("tocipher- 2 ");
    tocipher[il]=2;  
    //
    break;
  case '3': 
    Serial.println("tocipher- 3 ");
    tocipher[il]=3;  
    //
    break;
  case '4':
    Serial.println("tocipher- 4 ");
    tocipher[il]=4;  
    //
    break;
  case '5': 
    Serial.println("tocipher- 5 ");
    tocipher[il]=5;  
    //
    break;
  case '6':
    Serial.println("tocipher- 6 ");
    tocipher[il]=6;  
    //
    break;
  case '7':
    Serial.println("tocipher- 7 "); 
    tocipher[il]=7;  
    //
    break;
  case '8':
    Serial.println("tocipher- 8 ");
    tocipher[il]=8;  
    //
    break;
  case '9': 
    Serial.println("tocipher- 9 ");
    tocipher[il]=9;  
    //
    break;
    //
  default:
    //
    break;
    //
  }
}

void calculate(){
  uint16_t sizeofarray = sizeof(tocipher);
  int i;
  //
  for(i=0;i<=sizeofarray;i++)
  {
    int retrieved;
    retrieved=tocipher[i];
    bigjump(retrieved,i);
    //
  }
}

int bigjump(int coming, int il){

  switch (il) { 
  case 0:
    int k;
    Serial.print("Year Olimex was founded in Roman numerals- ");
    for(k=0;k<coming;k++){
      Serial.print("M");
    }
    break;

  case 1:
    if(coming==9){
      Serial.print("CM");
    }
    if (coming==8){
      Serial.print("DCCC");
    }
    if (coming==7){
      Serial.print("DCC");
    }
    if (coming==6){
      Serial.print("DC");
    }
    if (coming==5){
      Serial.print("D");
    }
    if (coming==4){
      Serial.print("CD");
    }
    if (coming==3){
      Serial.print("CCC");
    }
    if (coming==2){
      Serial.print("CC");
    }
    if (coming==1){
      Serial.print("C");
    }
    break;

  case 2:
    if(coming==9){
      Serial.print("XC");
    }
    else if (coming==8){
      Serial.print("LXXX");
    }
    else if (coming==7){
      Serial.print("LXX");
    }
    else if (coming==6){
      Serial.print("LX");
    }
    else if (coming==5){
      Serial.print("L");
    }
    else if (coming==4){
      Serial.print("XL");
    }
    else if (coming==3){
      Serial.print("XXX");
    }
    else if (coming==2){
      Serial.print("XX");
    }
    else if (coming==1){
      Serial.print("X");
    }
    break;

  case 3:
    if(coming==9){
      Serial.print("IX");
    }
    else if (coming==8){
      Serial.print("VIII");
    }
    else if (coming==7){
      Serial.print("VII");
    }
    else if (coming==6){
      Serial.print("VI");
    }
    else if (coming==5){
      Serial.print("V");
    }
    else if (coming==4){
      Serial.print("IV");
    }
    else if (coming==3){
      Serial.print("III");
    }
    else if (coming==2){
      Serial.print("II");
    }
    else if (coming==1){
      Serial.print("I");
    }
    done=true;
    break;

  case 4:
    Serial.println("");
    break;  

  default:
    break;
  }
}


void fractions(){
  // Any similarites with attempt at comedy is pure coincidence !
  uint16_t sizeofarray = sizeof(tocipher);
  int i;
  //
  for(i=0;i<=sizeofarray;i++)
  {
    int retrieved;
    int accum;
    retrieved=tocipher[i];
    int result=0;
    switch (i) {
    case 0:  
      //
      accum=(retrieved*1000);
      Serial.print("result thou ");
      Serial.println(accum);
      break;
    case 1:
      accum+=(retrieved*100); 
      Serial.print("result with hundr");
      Serial.println(accum); 
      //
      break;
    case 2: 
      accum+=(retrieved*10); 
      Serial.print("result with decim ");
      Serial.println(accum); 
      //
      break;
    case 3: 
      accum+=retrieved; 
      Serial.print("total result ");
      Serial.println(accum);
      //
      break;

    case 4:
      Serial.print("Proverbial 1/12 (twelfth) Roman fracction ");
      Serial.print((float)(accum/12));
      break;

    default:
      break;
      //
    } 
  }
}
















Saturday, 26 October 2013

Problem : Write a decimal to Roman Numerals Converter


Decided to rush and be the first to enter a solution for Olimex's 30th Weekend Programming Contest (WPC #30).
Problem : Write a decimal to Roman Numerals Converter !
I sacrificed a lot on trying to be the first to apply( time will tell if it worked). But did finish a working converter for Arduino (read cumbersome ) !!
Even included the calculation of a twelfth, which was the backbone of their fraction system.

https://github.com/OLIMEX/WPC

http://olimex.wordpress.com/2013/10/25/weekend-programming-challenge-week-30-roman-numbers/

Wednesday, 5 September 2012

Pinguino 32 Olimex boards PWM pins info !



As i had trouble finding this information which will be the subject of this post myself, i decided to leave a note about it it here in case someone gets "lost" on it as well !

The Olimex boards PIC32 PWM pins info !

Digital Pins D0, D1 and D2 are the one on which you can use analogWrite ( and PWM as well as theres a PWM_set_frequency(u32 freq), /* 
PWM_set_frequency--------------------------------
@param: frequency in hertz (range 3kHz .. 12MHz)
PB is Peripheral Bus Clock
let's say p = TMR Prescale Value
PWM Period = (PR + 1) * TPB * p
so (PR + 1) = PWM Period / (TPB * p)
but PWM Period = 1 / PWM Frequency
so (PR + 1) = (1/PWM Frequency) / (1/TPB * p)
and (PR + 1) = FPB / (PWM Frequency * p)
then (PR + 1) = FPB / PWM Frequency / p --------------------------------                                             ------------------    PR3+1 calculation
_pr3_plus1 = GetPeripheralClock() / freq; // FOSC /  PWM Frequency
 Timer3 prescaler calculation
 PR3 max value is 0xffff, so PR3+1 max value is 0x10000 = 65536
 highest prescaler value is 256
 256 * 65536 = 0x1000000 = 16777216  :*/
 PWM_set_dutycycle(u8 pin, u16 duty), /* PWM_set_dutycycle
1. Set the PWM period by writing to the selected timer period register (PRy).
2. Set the PWM duty cycle by writing to the OCxRS register.
3. Write the OxCR register with the initial duty cycle.
4. Enable interrupts, if required, for the timer and output compare modules. The output
compare interrupt is required for PWM Fault pin utilization.
5. Configure the Output Compare module for one of two PWM Operation modes by writing
to the Output Compare mode bits, OCM<2:0> (OCxCON<2:0>).
6. Set the TMRy prescale value and enable the time base by setting TON
(TxCON<15>) = ‘1’. */
PWM_set_percent_dutycycle(u8 pin, u8 percent) /*  PWM_set_percent_dutycycle--Set a percentage duty cycle, allowing max 100 PWM steps.Allowed range: 0..100The duty cycle will be set to the specified percentage of the maximum for the current PWM frequency.Note: The number of available PWM steps can be lower than 100 with (very) high PWM frequencies-- */




Relying with my limited experience with the PIC16F family, i went digging for info on the code and found something that helped me, at least for now !

So to clear that ill leave an excerpt of the pwm.c, with some additional comments for those who might interest:



#if defined(PIC32_PINGUINO) || defined(PIC32_PINGUINO_OTG)
	switch (pin)
	{
		case 2:
			TRISDSET=0x10; /* D2 in the Board, RD4 pin which is also connected to the button (see schematics); 0b10000 in binary */
			TRISDCLR=0x01;
			OC1CON=0;
			OC1R=setpoint;
			OC1RS=setpoint;
			OC1CON=0x000E; /*Binary 1110 activates OCM's bit 2-0, where 1110 = PWM mode on OCx; and bit 3-0 1110 OCTSEL: Output Compare Timer Select bit where 1 = Timer3 as the clock source for this OCMP module */ 
			OC1CON|=0x8000; /* Binary 1000000000000000 is bit 15 ON: Output Compare Peripheral On bit(1), where 1 = Output Compare peripheral is enabled and 0 = Output Compare peripheral is disabled */
			return 1;
			break;
		case 1:
			TRISDCLR=0x08; /* D1 in the Board, RD3 pin, 0b1000 in binary . This Pin is also TX1*/
			OC4CON=0;
			OC4R=setpoint;
			OC4RS=setpoint;
			OC4CON=0x000E;
			OC4CON|=0x8000;
			return 1;
			break;
		case 0:
			TRISDCLR=0x04;  /* D0 in the Board, RD2 pin, 0b100 in binary where 0 is a number. This Pin is also RX1 */
			OC3CON=0;
			OC3R=setpoint;
			OC3RS=setpoint;
			OC3CON=0x000E;
			OC3CON|=0x8000;
			return 1;
			break;
		default:
			return 0;
	}		
#endif


I have had both this and the DIP DIY version with the PIC32MX250F128B, with 128k of flash and 32k of RAM, 17 I/O, 2 UART etc , that with a works with a 8 Mhz crystal for a finally system frequency of 40 Mhz.
As the  Pinguino board from Olimex  uses an IDE that is "Arduino Compatible" but with a  PIC32MX440F256H  , you can imagine how handy that can be ! The specs that Olimex got us used to makes it worth it all the more !
Things like DCDC power supply allow power supply voltage from 9 to 30V DC, Li-Ion rechargeable battery power supply option with BUILT-IN on-board charger, so when you attach battery it is automatically charged and kept in this state until the other power source is removed (and it AUTOMATICALLY will power the board); original Arduino design had flaw and the connectors were not spaced at 0.1" this make perfo board use impossible, So this has a connector on 0.1" !!; UEXT connector;  RTC - Real Time Clock.; NOISE IMMUNE design. and last but least the chip itself which is a PIC32MX440F256H @ 80 Mhz microcontroller with 256KB Flash and 32KB RAM

Of course you can use the Arduino C++ style with:


int led = 13;
void setup() {              
  pinMode(led, OUTPUT);  
}
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second


BUT ... Ill leave you also a more direct way of blinking leds, released by Olimex itself ( they have some good examples, though mainly related to their MODboards). The code been partially commented by me just to help in our quest !


/*
		PIC32-PINGUINO
		Blinks both LEDs (Green and Yellow)
   
    If you have any questions, email
    support@olimex.com
    
    OLIMEX, JULY 2012
    http://www.olimex.com
*/

#if ( defined(PIC32_PINGUINO_OTG) || defined(PIC32_PINGUINO) || defined(PIC32_PINGUINO_MICRO) )

	//Definitions for 
	// -- PIC32-PINGUINO
	// -- PIC32-PINGUINO-OTG
	// -- PIC32-PINGUINO-MICRO
		
	#define BUTTON1INIT TRISDbits.TRISD0 = 1
	
	/*YELLOW LED is PORTD1, which is connected directly and exclusively into a led on the board */
	#define YLEDINIT   TRISDCLR = 0x02;
	#define YLED1         PORTDSET = 0x02;
	#define YLED0         PORTDCLR = 0x02;
	#define YLEDSWITCH    PORTD   ^= 0x02;
	
	//GREEN LED is D13 in board , PORTG6 pin / 0b1000000 on chip
	#define GLEDINIT  	 TRISGCLR = 0x40;  /*  Clearing the Port/bit */
	#define GLED1 		    PORTGSET = 0x40; /* Setting the bit */
#define GLED0      PORTGCLR = 0x40; /* Clearing the bit */ #define GLEDSWITCH    PORTG   ^= 0x40; /* Toggling the bit/led */ #define YELLOW 0 #define GREEN  1 #define BOTH   2 #endif //Blinks an LED five times - mode is YELLOW, GREEN or BOTH void blinkled(unsigned char mode); void setup() {     // put your setup code here, to run once:     YLEDINIT; /* Clearing the Port/bit */     GLEDINIT; /* Clearing the Port/bit */     } void loop() {     // put your main code here, to run repeatedly:     blinkled(GREEN); /* Using the function below created ! */     blinkled(YELLOW);     blinkled(BOTH);           } void blinkled(unsigned char mode) { //Turn off all LEDs YLED0; /* Clearing the bit */ GLED0; /* Clearing the bit */ char i; // Blink switch(mode) { case YELLOW: for(i=0; i<10;i++) { YLEDSWITCH;/* Toggling the bit/led */ delay(100); } break; case GREEN: for(i=0; i<10;i++) { GLEDSWITCH;/* Toggling the bit/led */ delay(100); } break; case BOTH: for(i=0; i<10;i++) { YLEDSWITCH;/* Toggling the bit/led */ GLEDSWITCH;/* Toggling the bit/led */ delay(100); } break; } }





Reference :

- Pinguino's Website and Forum

http://www.pinguino.cc/




- Compatibility Pinguino vs. Arduino

http://wiki.pinguino.cc/index.php/Compatibility




- Pinguino's blog (Info about the DIY of the PIC32MX or the 8Bit with PIC18F)

http://blog.pinguino.cc/




- Documentation about the DIY of the PIC32MX And the IDE download

http://code.google.com/p/pinguino32/downloads/list




Olimex Industrial-Grade board

https://www.olimex.com/dev/pic32-pinguino.html