Wednesday, 12 September 2012

Analog to digital sample code

First i bring a simple sample code on an analog to digital conversion

#include <avr/io.h>
int ADC_Read;        //Variable used to store the value read from the ADC converter
#define PB5 5
int main(void){

  DDRB |= (1<<PB5);    ///PB5/digital 13 is an output

  ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0));    //Prescaler at 128 so we have an 125Khz clock source
  ADMUX |= (1<<REFS0);
  ADMUX &= ~(1<<REFS1);                //Avcc(+5v) as voltage reference
  ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0));    //ADC in free-running mode
  ADCSRA |= (1<<ADATE);                //Signal source, in this case is the free-running
  ADCSRA |= (1<<ADEN);                //Power up the ADC
  ADCSRA |= (1<<ADSC);                //Start converting

  for(;;){            //The infinite loop
    ADC_Read = ADCW;    //Read the ADC value, really that's just it
    if(ADC_Read > 512){
      PORTB |= (1<<PB5);    //If ADC value is above 512 turn led on
    else {
      PORTB &= ~(1<<PB5);    //Else turn led off

  return 0;

This example demonstrates the use of the ADC of a ATmega328 
using the internal reference voltage  
To adapt to other AVR and / or other reference voltages 
see comments in this tutorial and in the data sheet

// This example demonstrates the use of the ADC of a ATmega169 
// using the internal reference voltage of nominally 1.1 V 
// To adapt to other AVR and / or other reference voltages 
// see comments in this tutorial and in the data sheet

/* Initialize the ADC */
void ADC_Init ( void )  {

  uint16_t result;

  // Select voltage reference for the ADC=> Avcc(+5v) 
  ADMUX = (1<< REFS0);
  ADMUX &= ~(1<<REFS1);                

  // Bit ADFR ("freerunning") in ADCSRA stands at power 
  // already set to 0, ie single conversion 
  ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0) ;      // frequency prescaler 
  ADCSRA |= (1<<ADEN) ;                   // enable ADC

  /* After activating the ADC is a "dummy readout" recommended reading
   So a value and rejects this in order to "warm up" the ADC */

  ADCSRA |= ( 1 << ADSC ) ;                   // an ADC conversion 
  while  ( ADCSRA & ( 1 << ADSC )  )  {          // wait for the conversion is complete 
  /* ADCW must be read once, otherwise the result of the next
   Conversion is not taken. */
  result = ADCW;

/* ADC single measurement */
uint16_t ADC_Read (uint8_t channel) 
  // channel choose to influence without other bits 
  ADMUX = (ADMUX &~ (0xF0))|(channel &0x0F) ;
  ADCSRA |= (1<<ADSC) ;             // a  "single conversion" 
  while  (ADCSRA & (1<<ADSC) ) {    // wait for the conversion is complete 
  return ADCW;                     // ADC read and return 

/* ADC with multiple measurement  */
/* Note: Range of sum variables */
uint16_t ADC_Read_Avg (uint8_t channel, uint8_t nsamples) 
  uint32_t sum = 0 ;

  for  (uint8_t i = 0; i< nsamples;  i++ )  { 
    sum += ADC_Read (channel) ;

  return  ( uint16_t ) ( sum/nsamples ) ;

/* Example calls: 

int main ( ) 
  uint16_t adcval;
  uint16_t adcval1;
  ADC_Init ( ) ;

  while (  1  )  { 
    adcval= ADC_Read(0) ;   // channel 0 
    // do something with adcval

    adcval1= ADC_Read_Avg(2,4) ;   // Channel 2, mean of 4 measurements 
    // do something with adcval 

Reference (Part. 1) :

Saturday, 8 September 2012

Serial Digital Data Networks


M-Bus (EN1434)
CAN (ISO11898)LIN Bus
Network Conceptsingle master, multiple slavesmultiple masters, multiple slavesmultiple masters, multiple slavessingle master, multiple slavessingle master, multiple slavessingle master, multiple slavesmultiple masters, multiple slavessingle master, multiple slaves
Number of Signal Lines1 (IO)2, (SCLSDA)2, (SMBCLK, SMBDAT)4, (active-low CS, SI, SO, SCK)4, (active-low CS, DI, DO, SK)2 (lines can be swapped)2 (CAN_H, CAN_L, terminated)1 (LIN)
Optional signalsN/AN/ASMBSUS#, SMBALERT#N/AN/AN/A2nd GND, Power, ShieldN/A
Network SizeUp to 300 m (with suitable master circuit)Limited by max. 400pF buscapacitancerequirementLimited by max. 400pF bus capacitance requirementN/A (circuit board level)N/A (circuit board level)Max. 350m per segment of max. 250 slaves; max. 180nF40m @1M bps1000m @ 50k bps(example)Up to 40m, max. 10nF total load
Network Interfaceopen drain, resistive or active master pull-upopen drain, resistive or active master pull-upopen drain, resistive or active master pull-upPush-pull with tristatePush-pull with tristateM to S:voltage drive
S to M: current load
Differential open drain/source or open coll./emitteropen drain, resistive master pull-up
Network VoltageFrom 2.8 to 6.0 V, device specificFrom 1.8 to 5.5V, device specific2.7V to 5.5VFrom 1.8V to 5.5V, device specificFrom 1.8V to 5.5V, device specific~40VVDD-VD (diodedrop); ~4.5V max.8 to 18V
Logic ThresholdsVary with network voltageFixed level: >1.5V, >3.0 V VDD-related level: <30>70% of VDD<0 .8v=".8v">2.1VVDD-related level: <20>70% of VDD(inconsistent)Fixed level: <0 .8v=".8v">2.0V; VDD-related level: <20>70% (80%) of VDD (inconsistent)Master to slave: 24V, 36V nominalSlave to master: <1 .5ma=".5ma">11mADifferential: <50mv recessive="recessive">1.5V (dominant); driver specificationVDD-related level: <20>80% of VDD (driver spec.)<40>60% of VDD (receiverspec.)
TransmissionLS bit first, half-duplexMS bit first plus Acknowledge bit, half-duplexMS bit first plus Acknowledge bit, half-duplexMS bit first, full-duplexMS bit first, full-duplexLS bit first,half-duplex, acknowledge responseMS bit first, half-duplexLS bit first, half-duplex
Address Format56 bits7 bits, (10 bits defined but not implemented)7 bits, (10 bits defined but not implemented)N/AN/A8 bits (primary address), 64 bits (secondary address)Message identifier 11 bits (standard format), 29 bits (extended format)Message identifier 8 bits, including 2 parity bits
Network InventoryAutomatic, supports dynamic topology changeN/A; slave addresses hard-coded in firmwareARP, Address Resolution Protocol (Rev. 2.0 only)N/A; slave select (active-low CS) hard-coded in firmwareN/A; slave select (active-low CS) hard-coded in firmwareAutomaticN/A; message-based protocol, not address basedN/A; message-based protocol, not address based
Gross Data RateStandard: ~0 to 16.3k bps Overdrive: ~0 to 142k bps)Standard: ~0 to 100k bps; Fast: ~0 to 400k bps; High-Speed: ~0 to 3.4M bps10k to 100k bps~0 to ~10 M bps (device specific)~0 to ~5 M bps (device specific)300, 2400, 9600 bps~0 to 1M bps~1k to ~20k bps
Access TimeStandard: ~ 5.4ms Overdrive: ~0.6ms (at maximum speed)Standard: ~95µsFast: ~23µs(at maximum speed)~95µs @ 100k bpsN/AN/APrimary address, 2400 bps: 13.75ms (short frame), 27.5ms (long frame)At 1M bps 19µs (standard) or 39µs (extended) from start of frame to 1st data bitAt 20k bps 1.7ms from start of frame to 1st data bit
Data Protection8-bit and 16-bit CRCN/APEC Packet Error Code (Rev.1.1, 2.0)N/AN/AEven parity, check sum, frames15-bit CRC, frames, frame acknowledgeCheck sum, frames
Collision DetectionYes, through non-matching CRCYes (multi-master operation only)Yes (Rev. 2.0 only)N/AN/AYes ("medium" and "strong" collisions)Yes: CSMA/CDYes, through check sum
Slave supplyParasitic (typical), VDD(exception)VDD onlyVDD onlyVDD onlyVDD onlyParasitic and/or local supplyVDD only, local or remote sourceParasitic only

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), /* 
@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 */
			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;
		case 1:
			TRISDCLR=0x08; /* D1 in the Board, RD3 pin, 0b1000 in binary . This Pin is also TX1*/
			return 1;
		case 0:
			TRISDCLR=0x04;  /* D0 in the Board, RD2 pin, 0b100 in binary where 0 is a number. This Pin is also RX1 */
			return 1;
			return 0;

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 !

		Blinks both LEDs (Green and Yellow)
    If you have any questions, email
    OLIMEX, JULY 2012

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

	//Definitions for 
	#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

- Compatibility Pinguino vs. Arduino

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

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

Olimex Industrial-Grade board