Electronics, Synth, Sound effects, Microcontrollers, , Digital signal processing,AVR, PIC, PIC32,ARM, etc
Wednesday, 7 November 2012
AVR MCU's C Course
Brilliant course by Brunce Land ECE - Cornell University
The link is for the whole playlist. I been using it to revise some stuff, as their website has some nice example codes as well !
He also has another one on FPGA's
Thursday, 1 November 2012
Cheat Sheet
@16 Mhz
Prescaler : 64 = Tick 4 uS
250 ticks = 1mSecond
PIC's with HITECH C Compiler...
On the newer version GODONE was changed to GO_DONE and the special function register OPTION was changed to OPTION_REG.
Decided too share as it got me stuck for a while, while studying someone else's code( which i do a lot, in order to better my programming skills and also to develop new ideas).
Prescaler : 64 = Tick 4 uS
250 ticks = 1mSecond
PIC's with HITECH C Compiler...
On the newer version GODONE was changed to GO_DONE and the special function register OPTION was changed to OPTION_REG.
Decided too share as it got me stuck for a while, while studying someone else's code( which i do a lot, in order to better my programming skills and also to develop new ideas).
Thursday, 11 October 2012
Thursday, 4 October 2012
Arduino Due is to finally be released on the 22nd of October
There are a few very neat features in the DUE, namely a USB On The Go port to allow makers and tinkerers to connect keyboards, mice, smartphones, etc...
A few DUE boards have already made it into the hands of important people in the Arduino community, it seems.
The PDF handed included:
Arduino DUE
The Arduino Due is the newcomer microcontroller board in the Arduino boards family. lt's the first board based on a 32 bit processor (Atmel SAM3X8E ARM Cortex-M3 N/CU), which improves all the standard Arduino functionalities and adds manv new features.
The arduino DUE offers 54 digital input/output pins (of which 16 can be used as PWM outputs, with selectable resolution), 12 analog inputs with 12 bits of resolution, 4 UARTs (hardware serial ports), two DAC (digitalto analog converter) outputs, an 84 MHz crystal oscillator, two USB connections, a power jack, an ICSP header, a JTAC header, and a reset button.
The Due has two micro USB connectors: one intended for debugging purposes and a second one capable of acting as a USB host, allowing external USB peripherals such as mouse, keyboards, smartphones, etc. to be connected to the Arduino Due.
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) : http://dubworks.blogspot.co.uk/2012/08/beyond-gpio-pins-peripherals-intro-to.html
Saturday, 8 September 2012
Serial Digital Data Networks
| 1-Wire | I²C* | SMBus™ | SPI™ | MicroWire/PLUS™ | M-Bus (EN1434) | CAN (ISO11898) | LIN Bus | |
| Network Concept | single master, multiple slaves | multiple masters, multiple slaves | multiple masters, multiple slaves | single master, multiple slaves | single master, multiple slaves | single master, multiple slaves | multiple masters, multiple slaves | single master, multiple slaves |
| Number of Signal Lines | 1 (IO) | 2, (SCL, SDA) | 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 signals | N/A | N/A | SMBSUS#, SMBALERT# | N/A | N/A | N/A | 2nd GND, Power, Shield | N/A |
| Network Size | Up to 300 m (with suitable master circuit) | Limited by max. 400pF buscapacitancerequirement | Limited by max. 400pF bus capacitance requirement | N/A (circuit board level) | N/A (circuit board level) | Max. 350m per segment of max. 250 slaves; max. 180nF | 40m @1M bps1000m @ 50k bps(example) | Up to 40m, max. 10nF total load |
| Network Interface | open drain, resistive or active master pull-up | open drain, resistive or active master pull-up | open drain, resistive or active master pull-up | Push-pull with tristate | Push-pull with tristate | M to S:voltage drive S to M: current load | Differential open drain/source or open coll./emitter | open drain, resistive master pull-up |
| Network Voltage | From 2.8 to 6.0 V, device specific | From 1.8 to 5.5V, device specific | 2.7V to 5.5V | From 1.8V to 5.5V, device specific | From 1.8V to 5.5V, device specific | ~40V | VDD-VD (diodedrop); ~4.5V max. | 8 to 18V |
| Logic Thresholds | Vary with network voltage | Fixed level: >1.5V, >3.0 V VDD-related level: <30>70% of VDD30> | <0 .8v=".8v">2.1V0> | VDD-related level: <20>70% of VDD(inconsistent)20> | Fixed level: <0 .8v=".8v">2.0V; VDD-related level: <20>70% (80%) of VDD (inconsistent)20>0> | Master to slave: 24V, 36V nominalSlave to master: <1 .5ma=".5ma">11mA1> | Differential: <50mv recessive="recessive">1.5V (dominant); driver specification50mv> | VDD-related level: <20>80% of VDD (driver spec.)<40>60% of VDD (receiverspec.)40>20> |
| Transmission | LS bit first, half-duplex | MS bit first plus Acknowledge bit, half-duplex | MS bit first plus Acknowledge bit, half-duplex | MS bit first, full-duplex | MS bit first, full-duplex | LS bit first,half-duplex, acknowledge response | MS bit first, half-duplex | LS bit first, half-duplex |
| Address Format | 56 bits | 7 bits, (10 bits defined but not implemented) | 7 bits, (10 bits defined but not implemented) | N/A | N/A | 8 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 Inventory | Automatic, supports dynamic topology change | N/A; slave addresses hard-coded in firmware | ARP, Address Resolution Protocol (Rev. 2.0 only) | N/A; slave select (active-low CS) hard-coded in firmware | N/A; slave select (active-low CS) hard-coded in firmware | Automatic | N/A; message-based protocol, not address based | N/A; message-based protocol, not address based |
| Gross Data Rate | Standard: ~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 bps | 10k 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 Time | Standard: ~ 5.4ms Overdrive: ~0.6ms (at maximum speed) | Standard: ~95µsFast: ~23µs(at maximum speed) | ~95µs @ 100k bps | N/A | N/A | Primary 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 bit | At 20k bps 1.7ms from start of frame to 1st data bit |
| Data Protection | 8-bit and 16-bit CRC | N/A | PEC Packet Error Code (Rev.1.1, 2.0) | N/A | N/A | Even parity, check sum, frames | 15-bit CRC, frames, frame acknowledge | Check sum, frames |
| Collision Detection | Yes, through non-matching CRC | Yes (multi-master operation only) | Yes (Rev. 2.0 only) | N/A | N/A | Yes ("medium" and "strong" collisions) | Yes: CSMA/CD | Yes, through check sum |
| Slave supply | Parasitic (typical), VDD(exception) | VDD only | VDD only | VDD only | VDD only | Parasitic and/or local supply | VDD only, local or remote source | Parasitic 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), /*
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>).2:0>2:0>
6. Set the TMRy prescale value and enable the time base by setting TON
(TxCON<15>) = ‘1’. */15>
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
Subscribe to:
Posts (Atom)


