Showing posts with label EE. Show all posts
Showing posts with label EE. Show all posts

Monday, October 31, 2011

FT232H

FT232H - High speed USB 2.0 interface. Can communicate via high speed parallel FIFO interface at up to 40MBytes/s. Should be very easy to interface to an FPGA or MCU.

Thursday, September 29, 2011

UBW32 Generic USB demo

I ported and cleaned up the generic USB demo from the Microchip application libraries. The project is designed to work with the UBW32.

Requirements:
  • UBW32 or any other pic32 microcontroller board.
  • PICKIT/ICD/REALICE to program your board, or a linker script to load with the USB bootloader.
Download

Download the project and compile/flash the firmware onto your chip. Open the USB/Device - LibUSB - Generic Driver Demo host program for your operating system. Attach your device to the host and connect.

PIC32 Timer1 example code

The following code snippet opens and uses Timer1 as an interrupt. In your initialization sequence:
//Open Timer1 with 1:8 prescaler (80MHz -> 10MHz), with period of 10, therefore tick = 1MHz.
OpenTimer1(T1_ON | T1_PS_1_8 | T1_SOURCE_INT, 10);
ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
INTEnableSystemMultiVectoredInt();
And the interrupt handler:
void __ISR(_TIMER_1_VECTOR, ipl2) _Timer1Handler(void) {
	mT1ClearIntFlag();
	//Your code
}

PIC32 pin change interrupt

The following is a quick snippet for interrupt on change pin
void __ISR(_CHANGE_NOTICE_VECTOR, ipl2) ChangeNotice_Handler(void) {            
	mPORTDRead(); //Need to read the port (see PIC32 datasheet)
	mCNClearIntFlag();
	//Your code...
}

And, in your initializing code:

mCNOpen(CN_ON | CN_IDLE_CON, CN13_ENABLE | CN14_ENABLE | CN15_ENABLE, CN_PULLUP_DISABLE_ALL); //See plib documentation and PIC32 datasheet
ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_2);
INTEnableSystemMultiVectoredInt();	 

Monday, September 26, 2011

Crimping a modular connector without a cimp tool

So it turns out that the guy at the local electronics store near the university is too damn cheap to lend me a crimp tool for literally 10 seconds, and would rather charge me $5 to merely crimp a header onto the end of a cable. So I decided to crimp the modular header (for ICD) onto a the cable myself. I decided to instead use a flat ribbon cable (like the computer IDE cable) since they're thinner and much more flexible than the 6 wire telephone cable I bought. The wire-to-wire pitch is slightly larger than for a normal telephone type (6p6c) cable so you can't insert it directly, but if you pull the individual wires apart to about 5mm from the end, and then put them in, it works out well. Crimping without a tool was a snap - took only about a minute with nothing more than a flat head screwdriver to push in the tab as well as each of the contacts.

Friday, September 23, 2011

Simple USB Hello World for PIC32 (UBW32)

Here's a simple USB Hello World program that I wrote/modified/set up for the PIC32 (specifically UBW32 board, but any PIC32 will run it). It's a very quick, minimal, bare bones project, with minimal comments and clutter, formatted nicely.

Download project

Requirements:

Instructions:

1. Open the project in MPLAB (I'm using 8.76 with C32 v2.01).
2. Go to Build Options (green button on toolbar)
3. Go to Directories -> Include Search Path and edit the MAL line to your Microchip Applications Library Include folder. Go to Library Search Path and edit the line to your C32 libraries folder.
4. If you are NOT using the UBW32 board, edit HardwareProfile.h for your board (LEDs, etc). Also make sure to select your specific chip from Configure -> Select device...
5. Compile and program your board with a PICKIT/ICD/REAL ICE. This package does NOT support the bootloader because I use a REAL ICE to program my board, although you could probably very easily load it from the UBW32 bootloader by compiling it with the procdefs.ld file from the HelloWorldUSB package from the UBW32 website.
6. When the board is running the program, open a terminal program on the host computer (for example, Termite on Windows, or the "screen" command line tool on Linux/Mac OS X). Type something. You should see text sent back on the terminal.

Friday, September 16, 2011

Constant current source/sink

A very accurate MOSFET current source/sink I designed.




Number of MOSFETs is variable. I designed this for load testing, so the circuit is using 4 MOSFETs to be able to dissipate more power. As can be seen on the bottom graph (green), it works well down to very low voltages due to the use of MOSFETs instead of BJTs.

Friday, November 20, 2009

Electronics Supplier in Toronto

creatroninc.com

This store, located on 255 College st., sells Arduinos for $30 as well as a whole bunch of other stuff you'd expect to find at Sparkfun.

Saturday, August 22, 2009

Sonsivri

Sonsivri is a really great forum pertaining to the discussion of electronics and microcontrollers. Has a lot of other goodies as well :)

Thursday, August 13, 2009

UART Example for DSPIC33 (DSPIC33FJ128GP802)

#include <p33FJ128GP802.h>

_FOSC(OSCIOFNC_ON & FCKSM_CSDCMD & POSCMD_NONE);	//Oscillator Configuration (clock switching: disabled;
							// failsafe clock monitor: disabled; OSC2 pin function: digital IO;
							// primary oscillator mode: disabled)
_FOSCSEL(FNOSC_FRCPLL);					//Oscillator Selection PLL
//_FOSCSEL(FNOSC_FRC);					//Oscillator Selection no PLL
_FWDT(FWDTEN_OFF);					//Turn off WatchDog Timer
_FGS(GCP_OFF);						//Turn off code protect
_FPOR(FPWRT_PWR1);					//Turn off power up timer

void InitUART1() {
	// This is an EXAMPLE, so brutal typing goes into explaining all bit sets

	// The HPC16 board has a DB9 connector wired to UART2, so we will
	// be configuring this port only

	// configure U2MODE
	U1MODEbits.UARTEN = 0;	// Bit15 TX, RX DISABLED, ENABLE at end of func
	//U2MODEbits.notimplemented;	// Bit14
	U1MODEbits.USIDL = 0;	// Bit13 Continue in Idle
	U1MODEbits.IREN = 0;	// Bit12 No IR translation
	U1MODEbits.RTSMD = 0;	// Bit11 Simplex Mode
	//U2MODEbits.notimplemented;	// Bit10
	U1MODEbits.UEN = 0;		// Bits8,9 TX,RX enabled, CTS,RTS not
	U1MODEbits.WAKE = 0;	// Bit7 No Wake up (since we don't sleep here)
	U1MODEbits.LPBACK = 0;	// Bit6 No Loop Back
	U1MODEbits.ABAUD = 0;	// Bit5 No Autobaud (would require sending '55')
	U1MODEbits.URXINV = 0;	// Bit4 IdleState = 1  (for dsPIC)
	U1MODEbits.BRGH = 0;	// Bit3 16 clocks per bit period
	U1MODEbits.PDSEL = 0;	// Bits1,2 8bit, No Parity
	U1MODEbits.STSEL = 0;	// Bit0 One Stop Bit
	
	// Load a value into Baud Rate Generator.  Example is for 9600.
	// See section 19.3.1 of datasheet.
	//  U1BRG = (Fcy / (16 * BaudRate)) - 1
	//  U1BRG = (36850000 / (16 * 9600)) - 1
	//  U1BRG = 238.908854 //Round to 239
	
	U1BRG = 239;

	// Load all values in for U1STA SFR
	U1STAbits.UTXISEL1 = 0;	//Bit15 Int when Char is transferred (1/2 config!)
	U1STAbits.UTXINV = 0;	//Bit14 N/A, IRDA config
	U1STAbits.UTXISEL0 = 0;	//Bit13 Other half of Bit15
	//U2STAbits.notimplemented = 0;	//Bit12
	U1STAbits.UTXBRK = 0;	//Bit11 Disabled
	U1STAbits.UTXEN = 0;	//Bit10 TX pins controlled by periph
	U1STAbits.UTXBF = 0;	//Bit9 *Read Only Bit*
	U1STAbits.TRMT = 0;	//Bit8 *Read Only bit*
	U1STAbits.URXISEL = 0;	//Bits6,7 Int. on character recieved
	U1STAbits.ADDEN = 0;	//Bit5 Address Detect Disabled
	U1STAbits.RIDLE = 0;	//Bit4 *Read Only Bit*
	U1STAbits.PERR = 0;		//Bit3 *Read Only Bit*
	U1STAbits.FERR = 0;		//Bit2 *Read Only Bit*
	U1STAbits.OERR = 0;		//Bit1 *Read Only Bit*
	U1STAbits.URXDA = 0;	//Bit0 *Read Only Bit*


	U1MODEbits.UARTEN = 1;	// And turn the peripheral on

	U1STAbits.UTXEN = 1;
	// I think I have the thing working now.
}

int main() {
	ADPCFG = 0xFFFF;				//make ADC pins all digital	
	
	//PLL setup:
	CLKDIVbits.PLLPRE = 0;          // N1=2: PLL VCO Output Divider Select bits; 0 -> /2 (default)
	PLLFBDbits.PLLDIV = 42 - 2;     // M=40: PLL Feedback Divisor bits; 42 - 2 = 40 -> 40 x multiplier
					// (divisor is 2 more than the value)
	CLKDIVbits.PLLPOST = 0;         // N2=2: PLL Phase Detector Input Divider bits; 0 -> /2
	
	//40 x: 73.700000 MHz = 36.850000 MIPS
	//42 x: 77.385 MHz = 38.6925 MIPS
	//43 x: 79.2275 MHz = 39.61375 MIPS
	
	while(OSCCONbits.LOCK != 1);	// Wait for PLL to lock
	RCONbits.SWDTEN = 0;      // Disable Watch Dog Timer
	
	TRISA = 0b00000000;
	TRISB = 0b00000000;
	
	
	asm volatile ("mov #OSCCONL, w1  \n"
				  "mov #0x46, w2     \n"
				  "mov #0x57, w3     \n"
				  "mov.b w2, [w1]    \n"
				  "mov.b w3, [w1]    \n"
				  "bclr OSCCON, #6");
				  

	RPINR18bits.U1RXR = 7;
	RPOR3bits.RP6R = 0b00011;
	
	asm volatile ("mov #OSCCONL, w1  \n"
				  "mov #0x46, w2     \n"
				  "mov #0x57, w3     \n"
				  "mov.b w2, [w1]    \n"
				  "mov.b w3, [w1]    \n"
				  "bset OSCCON, #6");
	

	InitUART1();
	
	while(1) {

		U1TXREG = 'H';
	}
		
	return 0;
}

Saturday, June 27, 2009

FreeRTOS on the dsPIC33

I recently ordered a bunch of dsPIC33F samples – specifically the dsPIC33FJ128GP802. These devices are amazing. They’re fast, have lots of RAM, and very very easy to use with FreeRTOS.

Things to keep in mind when setting up a project for the dsPIC:

  • Ensure you define the MPLAB_DSPIC_PORT macro (Project options –> MPLAB C30 –> Add… (in the preprocessor macros box)
  • I prefer to use heap_3.c so that the compiler’s own malloc() and free() functions can be used. To use them, define a heap size (Project options –> MPLAB LINK30 –> Heap size: (text box)). I just put 5000. Adjust yours accordingly.
  • C30 optimization: (Project Options –> MPLAB C30 –> Categories: Optimization) – I selected –O3 level optimization. Adjust yours accordingly. The port page on freertos.org says to enable the Omit frame pointer checkbox. Strangely enough, the demo application does not have the checkbox enabled.
  • Also remember to tweak your FreeRTOSconfig.h file and set up all the include directories.

I had no trouble getting the project to work. I even made a simple LED blinker. No modifications to the linker script had to be made (what a relief). The sample project is provided below:

Download Project

Friday, June 19, 2009

Setting Up FreeRTOS for the PIC18 Using MPLAB C18

FreeRTOS is a really cool real-time operating system (preemptive scheduler) for various architectures. It has been ported to the PIC18 platform (using MPLAB C18 compiler). However, the port is not very good.

Isaac Marino Bavaresco (isaacbavaresco AT yahoo DOT com DOT br) of the PICLIST has greatly improved and optimized the port for PIC18. His PICLIST page is here.

It took me a long time to get everything set up and working properly, so I’m writing this tutorial as an easy quick-start guide on getting FreeRTOS running on the PIC18.

Things you’ll need to get started:

  • A PIC18 device
  • MPLAB & MPLAB C18 compiler
  • FreeRTOS
    • I have made a convenient FreeRTOS modified package available for download (FreeRTOS 5.3.0) for the PIC18 containing Isaac’s improved port. DOWNLOAD
  • My sample project. It’s the fastest way to get started. However, I recommend that you download this sample project just for the source files (don’t actually use the project file) and you set up your own project by reading this tutorial. DOWNLOAD

 

Step 1 – Setting up your directories

Configure your directories in this structure:

  • PIC_Projects
    • FreeRTOS
    • Project_1
    • Project_2
    • Project_3

Step 2 – Download FreeRTOS_mod for PIC18 from the link above.

Unzip in your projects directory and you will get a directory named “FreeRTOS”.

Step 3 – Create a project

  • Open MPLAB and click Project –> New…
    • Create a project with the name of your choice corresponding to the directory structure outlined above.
  • Click Configure –> Select Device… and select your PIC18 device
  • Click Project –> Build Options –> Project…
    • In the Directories tab
      • select Library Search Path from the combo box and add the lib folder from your C18 installation. For me, it was C:\MCC18\lib.
      • Next, select Include Search Path from the same combo box and add the following directories:
        • .
        • ..\FreeRTOS\Source\Portable\PIC18
        • ..\FreeRTOS\Source\include
    • In the MPASM/C17/C18 Suite tab
      • Enable the checkbox for Extended mode.
    • In the MPLAB C18 tab
      • In the General category, add the following macro: MPLAB_PIC18F_PORT
      • In the Memory Model category, select Large code model, Large data model, Multi-bank model
    • Apply the changes.
  • Add the following files to the project Source Files:
    • Everything in the FreeRTOS\Source directory
    • Everything in the FreeRTOS\Source\portable\PIC18 directory
    • My main.c file from my sample project
  • Add the following files to the project Header Files:
    • FreeRTOSConfig.h from my sample project
  • Add FreeRTOS\Source\portable\PIC18\18f2620_e_FreeRTOS.lkr file to the Linker Script section. If you are using a different device, you need to make your own linker script. Use the one provided as a reference and compare it to the original one.
  • You should now be ready to compile your project. Go ahead and click Project –> Build All and watch the magic happen.

Explanation

All that this project does is create two tasks that constantly print “abcd” and “1234” to the UART. It uses a mutex to ensure that the non-thread-safe puts function is only being used in one thread at a time.

The RTOS configuration file is FreeRTOSConfig.h. The only thing non-standard in there is the #define CUSTOM_HEAP macro. I created this little modification to replace the standard malloc()/free() calls in the RTOS source to instead use Isaac’s functions which are far more optimized.

FreeRTOS Finally Working!

With a lot of help from Isaac Marino Bavaresco (PICLIST), I finally got FreeRTOS up and running perfectly on my PIC18F2620. It’s a fun chip loaded with features and perfect for FreeRTOS. I’ll write a complete tutorial shortly to get the RTOS working on this chip. Hopefully, when I get my dsPIC33 and PIC32, it will be a whole lot easier to get the RTOS working.

Sunday, May 10, 2009

I2C for chip-to-chip communication

If you have several PIC microcontrollers and other I2C devices on the same PCB, the best bus to use would probably be the I2C bus. It’s a synchronous multi-master bus so the PICs can talk to each other while controlling the slave devices as well.

Here are some interesting sites on the I2C bus:

http://www.robot-electronics.co.uk/htm/using_the_i2c_bus.htm

http://www.i2c-bus.org/i2c-primer/

http://en.wikipedia.org/wiki/I%C2%B2C

http://www.esacademy.com/faq/i2c/

Sunday, May 3, 2009

CAN – It’s Awesome!

After further research, I learned more about the CAN bus (controller area network). It only requires three wires (high, low, ground) and terminating resistors (typically 110 ohms for CAT5 cable) and a CAN transceiver. Unlike RS485, the CAN bus specifications specify the physical layer as well as the protocol layer. The bus allows for multi master communication over long distances (1000 m) and sufficient data rates (1 Mbit/s). The details such as collision avoidance/detection error checking and so on are all taken care of at the chip level.

Maxim IC has some CAN transceivers and I just sampled them. I’m planning to use them over CAT3 or CAT5 cable (twisted pair). Let’s see how this goes.

More on this once I get the system up and running.

Saturday, May 2, 2009

Microcontrollers & Communication

I recently started a project which required several microcontrollers (placed around the house) to be able to communicate with each other. The difficult part is choosing a communications method and protocol. After doing some research, I found the following to be suitable options for inter-microcontroller communications.

The most interesting and suitable option I found was was the RS485 bus. This bus allows for communications over potentially long distances (greater than 1.3 km), tolerates high speed, allows for many protocol designs, is asynchronous and easy to use. Several protocols allow for multiple bus masters with collision detection. Overall, the bus should be pretty reliable. Further research suggested that either CAT3 (phone cable) can be used with 6 contact RJ11 type connectors. This allows for full duplex communication over 4 lines and one ground line. CAT 5 ethernet cable with RJ45 type connectors could also be used if desired. However, twisted pair cable is strongly recommended (such as CAT3 or CAT5).

Another option is using ethernet. The Microchip ENC26J60 transceiver can be used. However, I’m not sure how ethernet can be used as a bus (without a hub/switch). More on this later.

If wires prove to be too cumbersome to implement, one may chose to take the wireless route. Futurlec sells SPI radio transceiver modules at a low cost. Implement your own protocol.

Other busses such as CAN, LIN and USB are further discussed on the following site: http://www.ucpros.com/

Tuesday, April 21, 2009

Driving a Piezoelectric Speaker with a PIC

Piezoelectric speakers are really cool. You can get them inexpensively from most hobby electronics stores, take them out of PCs, and so on. You can directly connect them to your microcontroller (resistor recommended) and generate any tone you want.

Simply connect one end of the speaker to an output pin on your PIC and connect the other end to GND for the simplest configuration. You make the speaker “tick” by passing current through it. By rapidly turning the output pin on and off, you generate a tone. Here’s a very simple example program that turns the output pin on and off via software:

#include "main.h"
#define SLEEP 1000

void main() {
    setup_adc_ports(NO_ANALOGS|VSS_VDD);    
    setup_oscillator(OSC_8MHZ);
    setup_uart(9600);
    
    while(true) {
        output_high(PIN_B6);
        output_high(PIN_C0);
        delay_us(SLEEP);
        output_low(PIN_B6);
        output_low(PIN_C0);
        delay_us(SLEEP);
    }
}

Pin B6 (in my case) is the speaker output. Pin C0 is an output to an LED (for testing, it is not needed). You can change the value of the SLEEP define for varying frequency of sound.

However, there is another way that this can be done (more reliably) – using the PIC’s internal PWM (from CCP module). You can generate very precise frequencies that way.

Saturday, April 18, 2009

MAX233A

I just got my MAX233A samples from Maxim. They’re UART RS232 <—> TTL level computers so you can interface your PIC’s UART to your computer’s RS232 port. Makes development a lot quicker since it’s easy to program and view output.

On windows, you can use the termite terminal to communicate via UART on a windows machine. On a Linux machine, you have a lot more options – see Minicom. There are even floppy Linux distributions designed just for this purpose, so you can pull that old 386 out and put it to some use.

The great thing about the MAX233A is that it requires no external capacitors for its internal charge pumps (which are used to boost voltage levels to RS232 specifications). The only thing that’s required is a decoupling capacitor as described in the sample circuit in the datasheet.

I salvaged an old RS232 mouse for its cable. It had all the wires I needed (TX, RX, GND) to fully interface to my microcontroller.

So just go ahead and order some samples of the MAX233A – it’s a great little chip.

If you have a PIC that supports self writes to flash, you can even write (or download) a serial bootloader and development will go even faster as you’ll be able to program your chip directly over UART.

Saturday, April 11, 2009

DIY IPOD Battery Pack

So my IPOD Nano 1st G internal battery is dying. I can barely squeeze out a few minutes of play time using the normal Apple firmware. IPOD Linux and Rockbox are awesome; by setting the backlight brightness to a minimum, I can get a whole lot more playtime. One option is to buy a battery and installation kit (they’re pretty inexpensive). However, this method requires you to open up your IPOD (which is really not that big of a deal), but there must be a simpler way of getting the job done – using components you can find around the house.

What I’m talking about is an external power supply/battery pack that can fit onto any IPOD (through the USB cable) - http://www.ladyada.net/make/mintyboost/

But we can make one ourselves – a more versatile one.

The IPOD cable consists of one end that plugs into the device and the other end is a standard USB male connector. The USB connector consists of 4 pins - V+, GND, D+, D-. To supply power to the IPOD, we need to supply exactly 5 volts through the V+/GND pins. The D+ and D- should be tied to V+ via 100k resistors (I think – more on this later). And there you have it – your own power ipod power supply.

The actual circuit, however, can get a bit complicated. What regulator do we chose? The design I’m thinking of will be able to be powered from as little as 2 AA batteries to the 12 V output from your car. We need a regulator that can accept this high range of voltages. Obviously it needs to be a boost/buck (step up/step down) regulator. These tend to get a bit annoying as we need external capacitors and solenoids for the circuit. Take a look at the mintyboost above for inspiration.

Oh, and it would be cool to have it small enough so all of it fits on the back of an IPOD Nano 1st G.

 

Just a thought.

Sunday, April 5, 2009

Salvaging an RS232 serial mouse

I recently got some MAX232A samples from Maxim-IC. These chips convert the TTL RS232 signals from your PIC to the RS232 voltage levels required by your PC. They make it very easy to connect your PIC with your PC. I only needed one more thing: an RS232 serial cable. I noticed I had some old RS232 serial mice lying around. I decided to break one open and remove the cable. Perfect – the cable had all the wires I needed – RX, TX and more. Let’s put this aside.

Next, there were some more interesting components inside the mouse that may come in handy, such as buttons, phototransistors, IR LEDs and so on. The IR LEDs, phototransistors and the gears could be used for something interesting.

I found a site that describes the use of the IC inside the serial mouse to extract movement information: http://www.geocities.com/kellertuberg/telescope/serial_mouse.html

Let’s see where this goes…