Showing posts with label RF. Show all posts
Showing posts with label RF. Show all posts

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();	 

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.

Monday, March 29, 2010

CC1101 and CC1111 ISM tranceivers

I've recently ordered a few samples of the CC1101 and CC1111 ISM band transceivers from Texas Instruments. The CC1101 is the transceiver itself while the CC1111 is a full system on chip with a built in 8051 microcontroller core. Full details on this chip are here. I have yet to figure out how to use these chips. Google searching hasn't revealed any useful hobby schematics or example projects with this chip.

I did, however, find this: CC1101 RF modem + 250 mW amplifier. The modem seems to achieve communication over long distances (greater than 1 km).

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.

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/

Saturday, March 21, 2009

More Hobby PIC18 Devices

The best hobby chips are: (spaces added in part numbers for readability)

  • PIC 18 F 26/46 20 – Very robust, but a bit more expensive ($5.50), 100 K flash write cycles (the two below only have 10 K flash write cycles)
  • PIC 18 F 26/46 K 20 – Low power
  • PIC 18 F 26/46 J 50 – Low cost ($3.30), 2 – 3.3 V, 5.5 V tolerant digital inputs, USB, peripheral pin select, RTCC
  • PIC 18 F 2550 – USB

The 18F26J50 definitely seems cool. Note that the 46 models (as opposed to the 26 models) have higher pin counts and therefore, more ADC channels. Both come in DIP packages – very hobby friendly. The only trouble with the 18F26J50 is that it has a maximum voltage of 3.3 V.

Wednesday, February 18, 2009

Two more radio transceivers from Futurlec

Radio Data Transceiver - 433MHz - Priced at $7.90 and $8.90. SPI interface - easy interface with microcontrollers. http://www.futurlec.com/Radio.shtml - The 10th and 11th products on that page.

Sunday, February 15, 2009

Turbocharge Your PIC with PLL

The PIC18 series have a maximum internal oscillator frequency of 8 MHz, which translates to 2 MIPS. However, the PIC can run at much higher speeds (especially when using an external clock source). Using the PIC's PLL, one is able to multiply the clock source by up to 4 times. This is even software-controllable if using the internal clock source. Using the maximum speed of the internal oscillator, the PIC can run at up to 32 MHZ with PLL enabled. This yields blazing fast performance of 8 MIPS. Best of all, you can turn this feature on or off as you please to control power consumption. Using the PLL is very easy.

We must first ensure that we are using the internal oscillator. So, in C18, declare a configuration option like this:

#pragma config OSC = INTIO67

Next, we must either configure the internal oscillator to run at 4 or 8 MHz. The PLL will not be enabled on any other configuration. Since we're opting for speed here, let's set it at 8 MHz. Put the following as the first line in your main() function:

OSCCON = 0b01110000;

Note that the last two bits of OSCCON must be 00. And finally, to enable the PLL, add the next line right after:

OSCTUNE = 0b01011111;

This enables the PLL and runs the PIC at the maximum possible frequency using only the internal oscillator - 8 MIPS @ 32 MHz. To toggle the PLL on or off, simply set OSCTUNEbits.PLLEN to either 1 or 0, respectively. Refer to the datasheet for information on how to fine-tune the PLL.

Friday, February 13, 2009

XBee

http://www.sparkfun.com/commerce/product_info.php?products_id=8664 Another way to interface microcontrollers wirelessly.

Wednesday, February 4, 2009

Delays

Delays are used in code to pause for a specific amount of time, usually to allow humans to read output. But they are also important when interfacing peripherals. For example, when interfacing a microcontroller to an HD44780 LCD, enough time must be given for the LCD module to complete its operation. For this purpose, delays are used to waste time. There are numerous ways to implement delay functions. For example, one may chose to use the built in delay functions in C18, impliment their own using inline assembler for greater timing precision or use the timer module. The easiest way to use delays are to use the built in delay functions in C18. All you have to do is #include to use the delay functions. However, the delay functions are not a function of time - that is, they do not accept units of time as arguments. Instead, they delay for multiples of instructions. Therefore, to create times delays (1 second, 500 milliseconds, 45 microsecodns), it is important to calculate the number of instructions based on your clock speed. In the previous post, we configured the internal clock source to its highest setting for the PIC18F2620 - 8 MHz. PIC18 chips take 4 clock cycles for each instruction. So the frequency of executed instructions is 8 MHz / 4 = 2 MHz. The instructions execute at a frequency of 2 MHz. Taking the inverse of this value, we get the period (the time each instruction takes to execute). The value is 1/2000000 s per instruction. This is equal to 0.5 microseconds per instruction. We now know that each instruction takes 0.5 microseconds (a millionth of a second). We can now easily create our delay functions. By including delays.h, you now have access to the following built-in delay functions: Nop(); //Delay one instruction cycle Delay10TCYx(unsigned char N); // Delay in multiples of 10 instruction cycles. Delay100TCYx(unsigned char N); // Delay in multiples of 100 instruction cycles. Delay1KTCYx(unsigned char N); // Delay in multiples of 1000 instruction cycles. Delay10KTCYx(unsigned char N); // Delay in multiples of 10000 instruction cycles. The above functions delay in multiples of instruction cycles. If you want to create a function to delay 1 second (1 million microseconds), you need to delay 2 million instruction cycles (since each instruction cycle takes half a microsecond at our specified clock frequency). Here's a function that delays one second: void delay1s() { Delay10KTCYx(200); //Delay 2 million instruction cycles } As you can see, it is trivial to create your own custom delay functions using the delays.h funcions provided.

Basic Port I/O

In this post, we will examine basic IO port control for the PIC18. In C18, it is very straightforward to control I/O pins of your chip.

To set the data direction, use the TRISX register like you normally would. For example, if I want to make my PORTC all output, I would use:

TRISC = 0x00; In hexadecimal notation

or

TRISC = 0b00000000; In binary notation.

Once the tristate registers have been set, you can begin to send data through the I/O pins. You can do this directly by writing to the PORTX register or control the individual bits with PORTXbits.RXn.

For example, to make pin 6 of PORTC a logic "1", you would do:

PORTCbits.RC6 = 1;

Reading the data from the ports follows the same pattern. Remember to set the TRISX variable to the right value in order to read data.

Setting Configuration Bits and Configuring Clock Speed

Pragmas are preprocessor direrectives which are used in C18 to set configuration options for your chip. They are usually set at the top of the main source file, just under the includes.

They are declared like this:

#pragma [option] = [value]

For example, my usual options are:

#pragma config OSC = INTIO67
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = ON
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF

Declare them just under your #includes.

Another essential thing to do when developing with your chip is to define a clock source and set the frequency of oscillation. In the above configuration settings, the OSC has been declared to use the internal clock source. This is advantageous because no external circuitry is required.

The OSCCON register controls the clock frequency of the inter clock source. The following code will set it to the maximum frequency of the PIC18F2620 - 8 MHz.

OSCCON = 0b01110010;

The above code should be the first line in your main() function - more on this later.

Tuesday, February 3, 2009

PIC 18 Microcontroller Programming in C- Introduction

Introduction

I believe that the PIC18 series is the best family when diving into the world of microcontrollers. Myself, I have a PIC18F2620. It's a solid, all round, feature-rich chip with lots of flash and ram (64K, 3K). It's really easy to work with, very reliable, comes in a 28 pin DIP package and is compatible with the Microchip C18 compiler.

Assumptions

This tutorial assumes that you know basic electronics and how to wire up a breadboard or solder your components prototyping board. It is also assumed that the reader knows basic C programming and is able to understand and modify basic C code to his/her requirements.

Requirements

To get started, you'll need a PIC18 series microcontroller (such as the PIC18F2620). To program the chip, you'll need an ICSP programmer. I recommend the PICKIT2 as it is Microchip's own development programmer, is compatible with a wide range of chips, can do very easy UART communication between the PIC and PC and can even act as a mini logic analyzer. You'll also need a small development board (a breadboard or prototyping board, doesn't matter). And of course, you'll need a computer to use as a development machine. Your operating system doesn't matter, as long as you know your way around it well enough.

Setting Up The Circuit

To begin, wire up your circuit as follows:

(to do)

Then connect the PICKIT2 and load the "PICKIT2 Operating System" onto the chip.

Writing Your First Program

Will be in the next post...