Cool Cap Engineer

Engineering by an anime nerd


Leave a comment

Tutorials: Simple Electronic Load Circuit

wpid-20140810_175109.jpg

Electronic load on bench. Drawing close to 1.20A at 21V

A couple weeks ago, I had a discussion with one of my co-workers about electronic equipment. I complained that alot of the equipment that I want is very expensive. I told him some of the requirements that I’m looking for. After a couple minutes talking, he said to me “Why not build it yourself? The requirements are not that stiff.” “Crap, he’s right”, I said. After couple weeks researching the topic and brushing up on Mosfets, I built a very simple one. There is a alot of refinement that could be done on the circuit, but it demonstrates the topic nevertheless.Today’s post will talk about how this electronic load circuit works and how to build one yourself.

What is an electronic load?
An electronic load is a device design so a power supply can draw a certain amount of current without desipating tons of heat. Electronic loads are useful for testing a power supply’s efficiency, current limit, etc.To understand how the circuit works, I’ll talk about two crucial things involving mosfets and op amps. Let’s talk about the mosfet aspect first.

buz11_dataseet

VDS vs ID characteristics. Shows the mode we want to operate to behave as an electronic load.

Although the picture above looks like a bunch of lines for hobbyists, this VDS vs Id characteristics shows how the mosfet will behave depending on the voltage applied at the gate and source (VGS for short) Mosfets have 3 modes of operations: cutoff, active, and saturation. When the mosfet is in cutoff operation, it does not turn on. This is due to the voltage applied to the gate and source  not high enough to turn it on. Active is a state in which the mosfet’s gate has enough voltage to allow the mosfet to behave as a variable resistor. Finally, saturation is when the mosfet behaves like a switch. For this load to work, we need it to enter active mode since we can set the mosfet to draw a certain amount of current.

 

Now let’s talk about one common configuration using an op amp: a unity amplifier. The sole purpose of a unity amplifier is to make sure that the voltage seen at the non inverting pin of the op amp (+ pin) appears at the output. Although it sounds simple, these amplifiers are useful to prevent the input source from getting affected from output impedance. However, we will be using this configuration for another reason.

electronic_load

Electronic load schematic

Now let me explain how the circuit works. First the user sets the voltage using a potiemeter (R2 in the schematic) and gets set at the inverting pin (- pin).  Although the op amp looks like its configured as a comparator, it’s actually set as unity amplifier due to the mosfet. To make sure the voltage at the non inverting pin appears at the inverting pin, the op amp must set a certain voltage to the mosfet gate. Depending on the gate voltage, the mosfet will draw a certain current until the voltage at R1 is equal to the voltage at the non-inverting pin.

 

How to select your mosfet?
Unlike my other tutorials, there’s a high possibility you’ll damage the mosfet in this tutorial, if you’re not careful. When looking at the datasheet for a mosfet, remember to pay attention to the drain current (ID) and drain to source (VDS) ratings. ID determines how much current the mosfet can handle before exploding and VDS determines the maximum voltage that can be applied for safe operation.

SOA

BUZ11 safe operating area per datasheet

Now for the little known parameter you should REALLY look at when reading mosfets datasheets: the safe operating range (SOA). Keep in mind, mosfets liked to be switched on and off. Rarely do you want the mosfet to work with analog voltages. The SOA tells you the recommended voltage and current that the mosfet can handle before exploding.

Build Your Own!

The following circuit is designed to handle 20V from 0A-5A. You’ll need the following to build this circuit

1x 10k Poteimeter

1x Buz11 Mosfet

1x 10Watt TO-220 Resistor

1x 741 Op Amp

2x .1uF Capacitors

2x Heatsinks

1x breadboard

Misc. wires

1x Cooling Fan (optional)

Consequentially, you’ll need the following tools for this circuit

1x +15V/-15V power supply

1x +32V/5A Power Supply

1x Mulitmeter

 

Now assemble the circuit as shown in the picture below. Remember to attach the heatsinks to the resistor and mosfets! THEY WILL GET DAMAGED WITHOUT THE HEATSINKS! If you have a cooling fan lying around, turn it on and direct it towards the resistor and mosfet. The colder these components run, the less likely they will get damaged.

simpleloadcircuit

Before Turning The Circuit On.
Do not apply 20V to the mosfet just yet.You want to make sure the voltage at the poteimeter is 0V, otherwise your power supply will go into current limit.  Apply power to the op amp/poteimeter. Make sure the voltage at the poteitmeter is 0V. Now apply power to the mosfet and start turning the knob. You should start seeing the circuit consuming a certain amount of current will keeping the voltage of the power supply the same.

 

Well that’s it for today! Thank you guys for reading this post and if you have any questions, comments or concerns, please post a comment. Also, be free to follow me on twitter. Have a wonderful week!


Leave a comment

Tutorials: Op Amp Relaxation Oscillator

wpid-20140803_112017.jpg

Relaxation oscillator on the bread and waveform taken on oscilloscope.

So lately I’ve been messing around with op amps. Why? Because I want to learn more about oscillators and wanted to build one from scratch. One of the simplest oscillator to build using an Op Amp is a relaxation oscillator. Today, I’ll talk about how it works and a small experiment using the oscillator.

What is an oscillator?
An oscillator is a circuit designed to output a repetitive signal over and over again based on a certain frequency. Oscillators are often use for devices such as switching regulators and making sure your PC’s CPU operates correctly. Some of the waveforms an oscillator can output includes square waves, sine waves, sawtooth waves, triangle waves, etc. Today’s relaxation oscillator will output a square pulse with a 50% duty cycle.

How does A Relaxation Oscillator work?

Relaxation Oscillator Schematic

Relaxation Oscillator schematic

An Op-Amp relaxation oscillator is comprised of two parts: a schmitt trigger and a RC  circuit (R3 and C1). When the circuit is powered, it charges the capacitor in the RC network. Keep in mind that whatever voltage appears at the capacitor will appear at the non-inverting pin of the op-amp (pin 3). The schmitt trigger (R1 and R2) determines when the output will swing from high to low or low to high.  In other words, the schmitt trigger determines when the capacitor will start charging or discharging.

pic_44_1

Output waveform (yellow) and the charging voltage of the capacitor (blue)

The figure above was taken from an relaxation oscillator with 15V peak to peak voltage. Although it shows 30V peak to peak, the DC offset of the waveform was set around 15V. Nevertheless, you can see that when the capacitor is charging, the relaxation oscillator output goes high, and goes low while the capacitor is discharging.

The frequency of the waveform can be set using the following equation:

 

Keep in mind, these equations work under the assumption that the voltage on pin 8 and 4 of the op amp are symmetrical. For example, if you apply 9V to pin 8, then -9V should be applied to pin 4. Also, if you apply 15V to pin 8, then -15V should be applied to pin 4.

Build Your Own!
Although I now have an oscilloscope – a crappy one, but still an oscilloscope- I realized some of you guys probably do not have one. So, I devised a small circuit that will turn on flash two LEDs on and off every 250ms. You will need the following materials:

1x DC Power Supply (Able to supply +15V and -15V)
1x LM741 Op Amp
3x 1K Ohm Resistors
2x .1uF Capacitors
1x  10K Resistors
1x 1uF Capacitors
2x LEDs
1x Breadboard
Misc. Wires

Now wire the circuit like in this picture.

relaxation oscillator circuitNow power up your DC power supply! You should see the LEDs flash on and off. The purpose of the LEDs is to show that the oscillator is outputting a square pulse with a positive and negative amplitude.

wpid-20140803_114107.jpg

 

Well that’s it for today’s post! If you have any questions, comments, or concerns about today’s post, let me know. Thanks for reading, and see you guys next week.

 


Leave a comment

Controlling A Seven Segment Display Using Mojo

20130519_200640

Remember a couple weeks ago when I said that I will be working on tutorials for the Mojo? Today’s post will be the first post that will be a Mojo tutorial. Although I originally planned to show you guys how to create a simple half adder with a 7 segment display, I realized we have to learn how to control the seven segment display first.  Today, I will show you not only how to connect your seven segment display to the Mojo, but how to implement a digital design to control the display on the Mojo.

What Are Seven Segment Displays?

LSHD-5503

A seven segment display is just seven LEDs arranged to display a number ranging from 0-9.  However, seven segment displays have two different connections: common anode and common cathode.Seven segment displays with a common anode connection means that all of the LEDs have their anodes connected together.  However, seven segment displays with common cathode connection means all of the LEDs cathodes are connected to each other. The picture on the bottom shows common anode and common cathode seven segment displays.

7segmentdisplay
When you have a common anode display, you just need one current limiting resistor, but 7 devices to sink the current for each LED. A common cathode display requires multiple current limiting resistors, but one ground connection to sink the current for all of the LEDs. For today’s tutorial, we will use a common cathode seven segment display.

What Will You Need?

1) (1x) Mojo Board

2) (1x) LTS-4301JR 7 Segment Display

3) (7x) 330 ohm resistor

4) (1x) Micro USB cable

Wiring Connection

The picture below shows how to connect the 7 segment display to the Mojo.

mojopic

How To Implement Your Digital Design Onto The Mojo?

For the next part, you’ll probably need to visit my getting started with FPGAs guide as alot of the steps from that tutorial can be applied for this one such as downloading the Xilnix Web ISE.
Afterwards, you’ll need to download the Mojo project base.

After downloading and extracting the Mojo project base, open Xilnix Web ISE. Once you open up the Xilnix ISE, go to file->open project. Navigate to your Mojo Project base and select the Xilnix ISE Project file inside the folder.
mojoproject

From the hierarchy menu in the Xilnix Web ISE, double click on the Mojo Top.v file, and edit it to look like this.

module mojo_top(
    input clk,
    input rst_n,
    input cclk,
    output[7:0]led,
    output spi_miso,
    input spi_ss,
    input spi_mosi,
    input spi_sck,
    output [3:0] spi_channel,
    input avr_tx,
    output avr_rx,
    input avr_rx_busy,
	 output [6:0] display
    );

wire rst = ~rst_n;

assign spi_miso = 1'bz;
assign avr_rx = 1'bz;
assign spi_channel = 4'bzzzz;
	 
assign led = 8'b0;
seven_segment_display_decoder(.clk(clk),.number(9),.seven_segment_display_control_lines(display));
endmodule

Don’t worry about line 24. We’re going to add the seven_segment_display_decoder module right now. Right click on Mojo Top.v and add a new verilog file. We’re going to call it “seven_segment_display_decoder.” After creating the file, add the following code to the file.

`timescale 1ns / 1ps
module seven_segment_display_decoder(clk,number,seven_segment_display_control_lines);
input clk;
input [8:0] number;
output reg [6:0] seven_segment_display_control_lines;

always @ (posedge clk)
begin
	case(number)
	1:seven_segment_display_control_lines = 7'b0110000;
	2:seven_segment_display_control_lines = 7'b1101101;
	3:seven_segment_display_control_lines = 7'b1111001;
	4:seven_segment_display_control_lines = 7'b0110011;
	5:seven_segment_display_control_lines = 7'b1011011;
	6:seven_segment_display_control_lines = 7'b1011111;
	7:seven_segment_display_control_lines = 7'b1110000;
	8:seven_segment_display_control_lines = 7'b1111111;
	9:seven_segment_display_control_lines = 7'b1110011;
	default:seven_segment_display_control_lines = 7'b1111110;	
	endcase
end
endmodule

Finally, select your mojo.ucf file. Edit it to make sure it look like this.

#Created by Constraints Editor (xc6slx9-tqg144-3) - 2012/11/05
NET "clk" TNM_NET = clk;
TIMESPEC TS_clk = PERIOD "clk" 50 MHz HIGH 50%;

# PlanAhead Generated physical constraints 
NET "clk" LOC = P56;
NET "rst_n" LOC = P38;

NET "cclk" LOC = P70;

NET "led<0>" LOC = P134;
NET "led<1>" LOC = P133;
NET "led<2>" LOC = P132;
NET "led<3>" LOC = P131;
NET "led<4>" LOC = P127;
NET "led<5>" LOC = P126;
NET "led<6>" LOC = P124;
NET "led<7>" LOC = P123;

NET "spi_mosi" LOC = P44;
NET "spi_miso" LOC = P45;
NET "spi_ss" LOC = P48;
NET "spi_sck" LOC = P43;
NET "spi_channel<0>" LOC = P46;
NET "spi_channel<1>" LOC = P61;
NET "spi_channel<2>" LOC = P62;
NET "spi_channel<3>" LOC = P65;

NET "avr_tx" LOC = P55;
NET "avr_rx" LOC = P59;
NET "avr_rx_busy" LOC = P39;
NET "display<0>" LOC =P87;
NET "display<1>" LOC =P85;
NET "display<2>" LOC =P84;
NET "display<3>" LOC =P83;
NET "display<4>" LOC =P82;
NET "display<5>" LOC =P81;
NET "display<6>" LOC =P80;

So we finished all of the code for the Mojo. Now we need to create the bit file. Before we create the bit file, make sure you save everything. Once you save everything, select mojo_top.v. Underneath the hierarchy menu is the processes menu. Look for “Generate Programming File” in the Processes menu. Click on it. Xilnix will now try to create a bitfile aka your digital design.

mojofpga1

Now we need to burn the bitfile onto the Mojo board. If you have not done so, download the Mojo Loader. Once your download and extract the mojo loader and configure your computer to recognize the Mojo, open the application file inside the Mojo Loader folder. From the Mojo Loader select “Open Bin File.” Navigate to your Mojo Project Base and then to Mojo-Base->syn. Select the mojo_top.bit file.
mojofpga2

Finally, select load file. When the bit file is uploaded to your Mojo, you should see a 9 on the seven segment display.

Well that’s it for today you guys! I’ll leave you to figure out how to change the number displayed on the display. If you guys have any more questions or concerns, then please leave a comment!


8 Comments

Implementing PWM On PIC18F Microcontroller

pwm50

PWM output with 20% duty cycle.

What is PWM?

As you guys know, microcontrollers, FPGAs, and other embedded processors can only output a high or low signal based on the power supplied to it. In other words, embedded processors cannot output a variety of voltages. Despite this, we have the processor “pretend” to output different voltages using a series of pulses and varying the width of the pulses. The technique of varying the width of a series of pulses is called Pulse Width Modulation (PWM).

pwm

The picture above shows an example of a simple pulse train.  The time it takes for each pulse to start again is referred as its period (Ts).  In each pulse train, the pulse is on for a certain amount of time before turning off. The percentage which the pulse’s on time compared to its time off is called the duty cycle.  Duty Cycle is expressed as…

D=\frac{T_{on}}{T_{on}+T_{off}}

PIC18F And PWM

As you’re already aware of, all microcontrollers feature a set number of PWM pins. But what pins are specifically designed for PWM? When reading your PIC18F datasheet, look for pins that have CCP (compare capture pwm) labeled. So let’s look at a picture featuring the PIC18F4553
PIC18F4553-pinout
As you see in the picture, RC1, RC2 and RB3 can be configured as PWM pins.

PWM Calculations

So there are three crucial registers you must take in account for PWM: PR2, and CCPRXL:CCPXCON[5:4]. Configuring the PR2 register, will allow you to configure the PWM period while selecting a proper the value for CCPRXL:CCPXCON[5:4] will allow you to choose the right duty cycle. For choosing the right value for the PR2 register, use the following equation…

PR2=\frac{PWM Period}{4*T_{osc}*TMR Prescale Value}-1

Keep in mind that Tosc is the the inverse of the frequency that your microcontroller is running at. Also, we will use a prescale of 16 as we will use this in the upcoming code example. For CCPRXL:CCPXCON[5:4], we will use the following equation…

CCPRXL:CCPXCON[5:4]=\frac{PWM Duty Cycle}{T_{osc}*TMR Prescale}

CCPRXL:CCPXCON[5:4] is a 10 bit number. To make things easier on you guys, make sure you convert your number to a 10 bit binary number. After you convert the number to a binary number, set your CCPRXL register to the first 8 MSB numbers. Finally, take the last 2 LSB and you use it for your CCPXCON[5:4].

PWM Coding Procedure

Now that I talked about the calculations, let’s talk about how to code your PIC18F microcontroller to use PWM. For this tutorial, we will use the PIC18F4553’s  CCP1 pin, set the PWM period at 1KHZ, a time prescale of 16, a clock frequency of 8*10^6 and a 50% duty cycle.Parts of the final code can be found in my Beginner’s Guide To PIC18F Microcontroller.

First we need to configure the CCP1 pin as an output pin,which is RC2.To do this we set the TRISCbits.RC2 to 0 and PORTCbits.RC2 to 0.

TRISCbits.RC2=0;
PORTCbits.RC2=0;

Now, we need to choose a value for PR2 register. Here is my calculation for PR2.
PR2=\frac{10^{-3}}{4*16*\frac{1}{8*10^{6}}}-1=124
Now that we found the number, let’s convert it to a binary number and use it.

PR2 = 0b1111011;

PWM uses timer2 to work. So we need to not only turn on timer2, but we must set the time scale to 16. This can be done by properly configuring T2CON by setting T2CON’s TMR2ON to 1, and T2CKPS1 to 1.

T2CON = 0b00000111 ;

CCPR1L:CCP1CON[5:4] is the last thing to compute.
CCPR1L:CCP1CON[5:4]=\frac{\frac{1}{10^{3}}*.5}{\frac{1}{8*10^{6}}*16}=250
When we convert 250 to its binary value, we get 0011111010. However, we take the first 8 MSB for CCPR1L and the last 2 LSB for CCP1CON bit 5 and CCP1CON bit 4.

CCPR1L = 0b00111110;
CCP1CON = 0b00101100;

Since you guys been so swell, here’s the final code for this example.

#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <portb.h>

#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config PWRT = ON
#pragma config DEBUG=OFF

int main(void)
{
    TRISCbits.RC2=0;
    PORTCbits.RC2=0;
    PR2 = 0b01111100 ;
    T2CON = 0b00000111 ;
   CCPR1L = 0b00111110;
    CCP1CON = 0b00101100;

    while(1);
}

Well, that’s it for me today! Did I miss something in my tutorial? Still have questions about the tutorial? Then please leave a comment below!


Leave a comment

Tutorials: Getting Started With FPGAs

basys2top_sw

So today, I thought I would talk about something other than microcontrollers. Today, I will talk about how to get started with FPGAs.

What is a FPGA?

An FPGA is a field programmable gate array. Instead of programming a FPGA  like your standard microcontroller, you actually “implement” digital circuits using a hardware description language (HDL). The two common hardware description languages includes VHDL and Verilog. For today’s tutorial, we’ll implement our design using Verilog.

FPGA vs Microcontrollers

So what are the advantages of using a FPGA rather than a microcontroller?  The problem with microcontrollers is that they cannot run co-currently, or only run sequentially, and very time limited. A FPGA runs in parallel, or able to run multiple things at once, and much faster than microcontrollers.  However, FPGAs are very space limited and initially does not come with all of the fancy peripherals that microcontrollers have. Things like timers, UART, and SPI must be implemented by hand on the FPGA. Also, to have your design stay with the FPGA,  you have to use a re-programmable flash memory IC.

Parts List

To get started, you need only the following items

Hardware

Software

Installing Xilinx Web ISE

  • To learn how to install the Xilinx Web ISE, I highly recommend visiting this URL .They have all the necessary information on properly installing the ISE.

Installing Digilent Adept

  • To program the Basys 2 board, you need to download Adept from Digilent’s website. Just follow the on screen prompts for proper installation.

Starting A Project

  • To create a new project, go to file->new project
  • At the create project screen, select a name and location for the project. For this example, let’s name the project “First_Project” and set the location to any location on your computer. Make sure you select “HDL” as your Top-level source type.fpga_tutorial
  • For project settings, make sure you have the following set…
  1. Family is set to Spartan3E
  2. Device is XC3S100E
  3. Package is CP132
  4. Speed is ~4
  5. Synthesis Tool is XST (VHDL/Verilog)
  6. Simulator is ISim (VHDL/Verilog)
  7. Preferred Language is Verilog

fpga_tutorial1

  • At Project Summary, select Finish.

Creating Verilog Module

  • At the project menu, you should see x3s100e-4cp132. Right click on this and select “New Source”fpga_tutorial2
  • At the new source menu, select Verilog module and set the name to blink.
  • When prompted at the define module, just click next. We’ll define the inputs and outputs later on. Afterwards select finish.
  • In your blink.v file, copy and paste the following code into the file.
  • Make sure to hit save.

Creating UCF File

Although we created the module, the FPGA does not know which pins on it should be connected to the module’s inputs and outputs. This is where the UCF file comes in.

  • Right click you blink.v file and select “New Source”
  • In your new source menu, select ” Implementation Constraints File.” We’ll name this file “blink”. Don’t worry, this will not replace the blink.v file.

fpga_tutorial4

  • Hit next and select finish
  • In your blink.ucf file, copy and paste the following code.
  • Make sure to hit save.

Creating Bit File

Okay, so we’re almost ready to program- I mean ” implement”- our digital design on the FPGA. FPGAs are usually programmed using a bit file. We’re going to create a bit file using the Web ISE.

  • Click on blink.v in your project navigator
  • Your should see ” Generate Programming File” in the Processes menu. Click on it.

fpga_tutorial5

  • Wait for a little bit for the ISE to finish creating the bit file.

Checking Connections

It is very important to check to see if the FPGA really routed the inputs and outputs correctly. To do this…

  • Click on your blink.v file
  • In the Processes menu, select ” Design Summary/Reports”
  • In Design Overview click on ” Pinout Report”
  • Check to see if “clk_in” is connected to B8 and “out” is connected to M5.

fpga_tutorial7

  • As shown above, the ISE did not correctly route the connections. This can be solved by resaving your UCF file and clicking the “Generate Programming File” option again.

Uploading the Digital Design To The FPGA

We’re at the final part of the tutorial!

  • Open Digilent Adept
  • Connect the Basys 2 to your computer and turn on it on.

fpga_tutorial6

  • You can upload the bit file to the main FPGA or the board’s re-programmable flash memory. Since we want the board to remember the bit file whenever we turn on or off the board, we’ll program the bit file to the reprogrammable flash (PROM)
  • Click browse button next to the PROM option
  • Navigate to your project folder.  The bit file should be inside the folder.
  • Hit “program” next to browse
  • After the bit file is uploaded to the board, turn off then turn on your board. You should see  LD0 blink every 500ms.

How does it work?

First thing, we need to use the 50MHZ clock on the board to tell the FPGA when 500ms has occurred. But how? Well, frequency is really counts per second. Using unit conversion and an onboard counter , we can find how many counts it takes for 500ms to occur.

\frac{50,000,000}{seconds}*500ms=25,000,000

Now that we know the counts, how do we use it in our design? We can use a register, a variable inside verilog, to function as our counter.  During the positive edge of the clock, we tell the design to increment the counter by 1. When counts reaches 25 million, the design resets the counter to 0 then invert the output of the LED, which is initially set to off.

Well that’s it for today. Hopefully that should help you guys during your FPGA adventures. Feel free to leave a comment below!


2 Comments

Tutorial: X-Bee Wireless Radios

xbee

I remembered my professor at RPI had a famous saying about RF electronics: “Once you build an RF transmitter from scratch that can transmit 50 feet, then you’re ready to get a P.H.D.” The point she was trying to make is that RF electronics is one of the toughest electronics engineering fields out there.  It requires a lot of impedance matching, fabricating printed circuit boards, and choosing the right amplifier just to make a RF transmitter. Rather than trying to build an RF transmitter/receiver from scratch, Digi sells the X-Bee Wireless Radio, which is specifically designed to work with microcontrollers. Today’s tutorial will show you how to get started using X-Bee Radios. Although I typically use PIC microcontrollers for my tutorials and this tutorial can be revised for any microcontroller, I will use an Arduino Mega board this time.

Parts List

X-Bee_Radio_Communication

Hardware

  1. Arduino Mega (1x)
  2. FTDI Programmer (1x)
  3. X-Bee Radios (2x)
  4. X-Bee Radio Breakout Boards (2x)
  5. Breadboard (2x)
  6. 3.3V Voltage Regulator (1x)

Software

  1. Arduino IDE
  2. X-CTU

First things, first

Solder the X-Bees to the X-Bee breakout board. The X-Bee modules cannot fit onto a breadboard due to the pitch spacing and the breakout boards allow the modules to be placed on a breadboard.

Setting Up The X-Bee For The Computer

Wire the FTDI programmer, 3.3V voltage regulator, and X-Bee Radio to the breadboard as shown in the figure below.

xbee_tutorial_one

Make sure that…

  1. GND of the FTDI programmer is connected to GND (pin 1) of the voltage regulator and GND ( pin 10) of the X-Bee module
  2. VCC of the FTDI programmer is connected to VIN (pin 3) of the voltage regulator.
  3. VOUT (pin 2) of the voltage regulator to Vin (pin 1) of the X-Bee module.
  4. RX of the FTDI programmer to Dout (pin 2) of the X-Bee module.

Now connect a USB cable to your FTDI programmer. From there, open up X-CTU.

xbee_tutorial_2

Select one of the COM ports (for me, my FTDI programmer was connected to COM6) and select Test/Query. This just checks to make sure your FTDI programmer is properly connected to X-CTU. You should receive this message if the FTDI programmer is successfully connected to the computer.

xbee_tutorial_3

If you did not receive this prompt, then it could mean the following…

  1. One of the connections is loose. Make sure your connections are solid.
  2. The COM port is in use. Disconnect the programmer and reconnect it to the computer. If that does not work, then restart the computer and try again.
  3. You could have a bad programmer if 1 and 2 does not work.

Now click on terminal. Leave this open! We will come back to this later.

xbee_tutorial_4

Setting Up The Arduino Mega

Now upload the following code to the Arduino Mega.

void setup()
{
  Serial2.begin(9600);
}

void loop()
{
  Serial2.print("Hello World \r\n");
  delay(1000);

}

While your Arduino is connected, make the following connection.

xbee_arduino_pic

Make sure that….

  1. 3V3 of the Arduino Mega is connected to VCC (pin 1) of the X-Bee
  2. RX2 of the Arduino Mega is connected to Din (pin 3) of the X-Bee
  3. GND of the Arduino Mega is connected to GND (pin 10) of the X-Bee

Now For Some Action!

Navigate back to your X-CTU terminal window. You should start to see “Hello World” printed on the screen multiple times.

terminal

Couple Key Things To Keep In Mind

  1. The X-Bees in this tutorial  can only work with a baudrate of 9600. If you want a X-Bee with a higher baudrate, then look at this one.
  2. X-Bees can only work with 3.3V power. DO NOT CONNECT ANYTHING HIGHER! IT WILL NOT WORK.
  3. X-Bees can work with any microcontroller. Just connect the microcontroller TX line to the X-Bees Din to allow your microcontroller to transmit data. If you want your microcontroller to recieve data, then connect its RX line to the X-Bee’s Dout line.

Well that’s it for me! Feel free to post a comment underneath this post for any comments or suggestions. See you guys later!


3 Comments

Tutorials: USART Communication For PIC18 Microcontroller

In my last post, I mentioned two awesome devices I’ve been messing around with lately. The cool thing about those devices is that it only needed one actual pin from the PIC microcontroller to properly work. This pin is one of the pins needed to achieve proper USART communication. Today’s tutorial will serve as a guide in order to achieve proper USART communication. Although I mentioned that USART is used to communicate with the devices I mentioned in the last post, today’s focus will be on achieving communication between PIC18F2550 and the computer using USART.

Circuity
USART (Universal Synchronous Asynchronous Receiver Transmitter)/ UART (Universal Asynchronous Receiver Transmitter) requires only two pins from any microcontroller to send and receive serial data: TX and RX. You can properly guess that TX is the pin which the microcontroller data is transmitted through, and RX is responsible for data received by the microcontroller. The figure below shows the TX and RX pins on the PIC18F2550.
usartpic
Keep in mind; we need one more device to have the PIC18F2550 communicate with the computer. If you have an old computer, then you could use a MAX232 and connect it directly to the serial port of that computer. For this tutorial, we’re going to use a laptop to talk to the PIC. In order to do this, we’re going to use a FTDI to USB board from Sparkfun. We’re just going to use 4 pins of the FTDI to USB board: VCC, GND, TX and RX. Look at the figure below to learn how to set up the circuit. Please note that JP2 is connected to the PICKIT3.
Capture23
Code
Now, create a new project in MPLab X, create a new C file, and code and paste the following code. If you forgot how to create a project in MPlab X, visit my tutorial on starting with PIC18 microcontrollers.

#include <p18f4553.h>
#include <delays.h>
#include <portb.h>
#include <usart.h>
#include <stdio.h>
#include <stdlib.h>

/*	PIC Configuratings */
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config PWRT = ON
#pragma config DEBUG= OFF

/* UART Global variables */
char message;						//	Used for receiving commands from the computer



void main(void)
{
	//	Set all of PORTC as outputs. TX and RX must be set as outputs first
	TRISC=0x00;
	
	//	Configure UART
	OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 10);
        Delay1KTCYx(4);
        while(1)
        {
            while(!DataRdyUSART());
            message=ReadUSART();
            putcUSART(message);
            Delay1KTCYx(4);
        }
}

So open a Serial Terminal program. For this tutorial, I will use the Arduino IDE. Open the serial monitor in the Arduino IDE and set the baudrate to 115200. Finally, type a single character and hit send. You should see the same character you entered appear in the window.
USART_2
Important! Please Read!

Make sure you enter the right baudrate. If you do not enter the correct baudrate, you will lose data. The figure below shows what happens if we set the serial program to 9600 baud rate even though the PIC is set to a baud rate of 115200.
usart_1

But how do you know if you set the USART to the right baud rate? Let’s look at the following piece of code.

OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 10);

As you can tell by the name, this is responsible for setting up the USART on the PIC. We’re going to look at the last two parameters of this command. The last two parameters correspond to the baudrate of the USART. USART_BRGH_HIGH sets the USART to a high baudrate, but we can set it to a low baudrate by replacing USART_BRGH_HIGH with USART_BRGH_LOW. The final parameter is a spbrg value. Although there are a set of equations you can use to find this value, as long as you’re using a 1Mhz, 2Mhz, 4Mhz, 10Mhz, 20Mhz, or 40Mhz crystal, you can find the SPBRG value in the datasheet. The figure below shows where we can find the spbrg value.
tableofusart
Well, I hope this helped you guys! If you have any questions, or comments, please feel free to post a comment!


6 Comments

Tutorials: Beginner’s Guide To PIC18 Microcontrollers

download

Starting today, I will focus on tutorials for  Microchip’s PIC18F series microcontrollers. Why am I shifting my focus from Atmega’s to PIC18F microcontrollers? As I stated in the first post of the N64 emulator controller, which is now Pygames Controller, I spent a semester at RPI learning how to use these microcontrollers and completely forgot how to use Atmegas. Not to mention, a lot of PIC microcontrollers are used for more commercial applications than Atmegas. If I remember correctly, Atmegas are mostly used for automotive applications while PICs are used for more commercial devices (e.g. medical devices). But enough about that! Let’s talk about how to start your first PIC18 microcontroller project.

Necessary Parts

  1. 1x PIC18F4553
  2. 2x 22pf capacitors
  3. 1 20Mhz crystal
  4. 1 PICKIT 3 programmer
    1. It’s highly recommended that you read this user guide for the programmer.
    2. Lots of male headers
    3. 1x Breadboard
    4. Lots of wire
    5. 1x 330ohm resistor
    6. 1x LED (any color)
    7. 1x 10k resistor

Here’s the schematic of the circuit. Although the picture shows a PIC18F4455, they have the same pinout

BASICCIRCUIT

Necessary Software

  1. MPlab X
  2. C18 Complier

Under the C18 Complier link, select MPlab C for PIC18.So once you guys install the necessary software, follow the next steps for setting up your project with MPlab X. First select file->New Project. Select Standalone project and hit next.

step1

Type in “PIC18F4553” under Devices drop in menu. Hit next.
STEP2

Select PICKit3 under tools. Press next.

STEP3

Select C18 as your complier. Hit next.

STEP4

Now we have create a project name and directory. Normally the project location and folder will be placed under my documents. Before this tutorial, I set the project location under my PIC folder. Remember to select “Set as main project.” This will save you the frustration later on. Finally hit finish.

STEP5

Now we have to create a main file. Right click on Source files and select New-> C Main file. Type in main.c and hit finish.

STEP6

STEP7

Finally copy and paste this code into your main.c

#include &lt;p18f4553.h&gt;
#include &lt;delays.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

/*	PIC Configuratings */
#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config PWRT = ON
#pragma config DEBUG= OFF

void main(void)
{
    TRISCbits.RC0=0;
    PORTCbits.RC0=0;

    while(1)
    {
        PORTCbits.RC0=1;
        Delay10KTCYx(50);
        PORTCbits.RC0=0;
        Delay10KTCYx(50);
    }
}

Now we’re going to build and program our mcu. First select build. You should receive a confirmation that the project was built successfully.

STEP8

Once it finishes building, hit the program button.

STEP9

Congrats! Your LED is flashing at 10hz!

Here how the code works. Out al of the #pragma in the code, pay close attention to #pragma config FOSC =HS. This tells the PIC microcontroller to accept the external cystal we connect. Please note that if we were using the 28pin PIC18 series microcontroller, this would most likely be changed to #pragma config OSC =HS.

Now for the code. Whenever we want to set a particular pin as an output, we have to access the TRISx, where the x stands for the letter of the port we want (in this case, TRISC for PORTC), and write a 0 to that pin. Thus we write TRISCbits.RC0=0 to set PORTC0 as an output. Finally, to either turn on or turn off this particular pin, we just have to write a 0 (off) or 1 (on)  to PORTC. Thus PORTCbits.RC0=0 is setting PORTC0 off.

Finally, the rest of the code is just flashing the LED on and off every 100ms. The while(1) is there to make sure it runs forever.We, that’s all I got for you guys today! Thank you guys for reading! If you have any comments or suggestions, please leave a comment!