Cool Cap Engineer

Engineering by an anime nerd


Leave a comment

Projects: Pygames Controller Update #1

broken_trigger
So last Monday, my parts for all of my Pygames controller project, which was previously my N64 emulator controller project,came in. The parts included two analog sticks taken from a PS2 controller, 12 large tactile buttons, 2 triggers taken from an X-Box 360 controller, and breakout boards for the analog sticks and the X-Box 360 triggers.However, I had two very unpleasant surprises when I opened my package. The first problem was the absence of solder for the breakout boards. I had to wait until Wednesday to get the right solder for the breakout board. The last nasty surprise was one of the X-Box 360 controller triggers were broken. Since one of the triggers was broken, I’ll just make due without them. The triggers will be added to a future version of the controller.
debug_window
On Wednesday, I finally got some cheap solder from Radioshack. After going to Radioshack, I immediately soldered the analog sticks to their respective breakout board. Once I finished soldering, I quickly started to write test code for one of the analog sticks. Normally, I would use an USART terminal window like the serial window from the Arduino IDE to see the values from the analog stick, but I wanted to do something different. Instead, I used my 160×120 serial LCD as a debug window. As you see in the figure above, the debug window shows digital values associated with the horizontal and vertical pots of the joystick.

Afterwards, I started to write the actual code of the PIC MCU side of the controller. As of right now, the controller will have four listed functions:
IsControllerConnected (): Checks to see if the controller is connected to the computer. Returns 1 for true and 0 for false.
ReadJoystickX(): Read the state of the joystick’s x direction for the controller. Returns 0 for the neutral position, 1 for right and -1 for left.
ReadJoystickY (): Read the state of the joystick’s y direction for the controller. Returns 0 for neutral position, 1 for up and -1 for down.
IsButtonPressed () : Checks to see if the button is pressed. Returns 1 for true, and 0 for false.

I spent all of Thursday to write for the PIC MCU side of the controller. To make coding a little bit more organized, I decided to create 3 structures: joystick, button, and controller. Here’s the initial controller.h file.

#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <delays.h>
#include <portb.h>
//===============================================================================================
//	Joystick Structure
//===============================================================================================
typedef struct
{
	int X_ADC_CHANNEL;			//	ADC channel for the horizontal component of the joystick
	int Y_ADC_CHANNEL;			//	ADC channel for the vertical component of the joystick
	int	X_ADC_READING;				//	Stores the ADC reading for the horizontal component
	int	Y_ADC_READING;				//	Stores the ADC reading for the vertical component
}JOYSTICK;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Button Structure
//===============================================================================================
typedef struct
{
	unsigned int BUTTON_PIN;		//	Pin which the button is connected to
	unsigned int BUTTON_READING;		//	Digital reading of the button.
}BUTTON;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Controller Structure
//===============================================================================================
typedef struct
{
	JOYSTICK LEFT_JOYSTICK;				//	Left Joystick
	JOYSTICK RIGHT_JOYSTICK;			//	Right Joystick
	BUTTON	YELLOW_BUTTON;				//	Yellow Button
	BUTTON	RED_BUTTON;                             //	Red Button
	BUTTON	GREEN_BUTTON;				//	Green Button
	BUTTON	BLUE_BUTTON;				//	Blue Button
}CONTROLLER;
//===============================================================================================
//
//===============================================================================================




void CONTROLLER_INIT(CONTROLLER MY_CONTROLLER);
int READJOYSTICKX(JOYSTICK MY_JOYSTICK);
int READJOYSTICKY(JOYSTICK MY_JOYSTICK);
unsigned int ISBUTTONPRESSED(BUTTON MY_BUTTON); 

I finished 3/4 of the code, until I ran into a stumbling block with creating the header files and the c files for the controller. Since my controller.h and controller.c needed to use the provided pic18f4553.h library to work, I had some difficulty figuring out how to include the file for not only my controller.c but also my main.c.However, I solved the problem by using ifndef, define, and endif.

#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <delays.h>
#include <portb.h>
//===============================================================================================
//	Joystick Structure
//===============================================================================================
typedef struct
{
	int X_ADC_CHANNEL;			//	ADC channel for the horizontal component of the joystick
	int Y_ADC_CHANNEL;			//	ADC channel for the vertical component of the joystick
	int	X_ADC_READING;				//	Stores the ADC reading for the horizontal component
	int	Y_ADC_READING;				//	Stores the ADC reading for the vertical component
}JOYSTICK;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Button Structure
//===============================================================================================
typedef struct
{
	unsigned int BUTTON_PIN;		//	Pin which the button is connected to
	unsigned int BUTTON_READING;		//	Digital reading of the button.
}BUTTON;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Controller Structure
//===============================================================================================
typedef struct
{
	JOYSTICK LEFT_JOYSTICK;				//	Left Joystick
	JOYSTICK RIGHT_JOYSTICK;			//	Right Joystick
	BUTTON	YELLOW_BUTTON;				//	Yellow Button
	BUTTON	RED_BUTTON;                             //	Red Button
	BUTTON	GREEN_BUTTON;				//	Green Button
	BUTTON	BLUE_BUTTON;				//	Blue Button
}CONTROLLER;
//===============================================================================================
//
//===============================================================================================




void CONTROLLER_INIT(CONTROLLER MY_CONTROLLER);
int READJOYSTICKX(JOYSTICK MY_JOYSTICK);
int READJOYSTICKY(JOYSTICK MY_JOYSTICK);
unsigned int ISBUTTONPRESSED(BUTTON MY_BUTTON); 
#endif

From reading a couple tutorials on creating .h files, this piece of code made sure the header files included in the other .h file gets read. Although the problem with creating the .h and .c files was solved, a new problem occurred. Apparently, the function I wrote to set the ADC channel for the horizontal and vertical components of the joystick was not set, which caused a huge mess in my ADC readings. I simply solved this problem by passing my structures by reference whenever I need to use them for a function. I forgot that to edit the values from a structure, they must be passed by reference. Here’s the current version of my controller.h file.

#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <delays.h>
#include <portb.h>
//===============================================================================================
//	Joystick Structure
//===============================================================================================
typedef struct
{
	int X_ADC_CHANNEL;			//	ADC channel for the horizontal component of the joystick
	int Y_ADC_CHANNEL;			//	ADC channel for the vertical component of the joystick
	int	X_ADC_READING;				//	Stores the ADC reading for the horizontal component
	int	Y_ADC_READING;				//	Stores the ADC reading for the vertical component
}JOYSTICK;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Button Structure
//===============================================================================================
typedef struct
{
	unsigned int BUTTON_PIN;		//	Pin which the button is connected to
	unsigned int BUTTON_READING;		//	Digital reading of the button.
}BUTTON;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Controller Structure
//===============================================================================================
typedef struct
{
	JOYSTICK LEFT_JOYSTICK;				//	Left Joystick
	JOYSTICK RIGHT_JOYSTICK;			//	Right Joystick
	BUTTON	YELLOW_BUTTON;				//	Yellow Button
	BUTTON	RED_BUTTON;                             //	Red Button
	BUTTON	GREEN_BUTTON;				//	Green Button
	BUTTON	BLUE_BUTTON;				//	Blue Button
}CONTROLLER;
//===============================================================================================
//
//===============================================================================================




void CONTROLLER_INIT(CONTROLLER * MY_CONTROLLER);
int READJOYSTICKX(JOYSTICK * MY_JOYSTICK);
int READJOYSTICKY(JOYSTICK * MY_JOYSTICK);
unsigned int ISBUTTONPRESSED(BUTTON * MY_BUTTON);
#endif

I’m still trying to finish up my controller.c code, but I got it working to a point where the code can tell if the joystick is pointing left, pointing right, or in the neutral position.
20121227_231535
Hopefully I should finish the PIC MCU side of the Pygames controller sometime this weekend if not next week. Anyway, hope you guys have a good weekend!


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!


2 Comments

Mini Projects: 160×128 Serial LCD and RFID Fun

download  So lately I’ve been messing around with alot of electronic components lately. One of these beauties is a 160×120 LCD screen. I will admit, they seemed a little daunting at first, but I was fairly surprised how easy it is using these LCD screens.  From what I’ve seen thus far, special commands are executed by sending the hex value 0x7C followed by the proper command. For example, to reset the display, I just need to send 0x7C then 0x00. Of course, to add text to the display, just send an ASCII message to the display. I did not do anything with boxes or lines yet, but will be part of my agenda.

598439_4372418500902_1096039390_nThe last new item I used this week was a RFID ID-120 reader. Like the 160×120 display, it’s controlled via serial. Apparently, this item beauty was shown in Blidr, and the pseudo code for reading the RFID tag was very simple.  In fact, I re-wrote the code to be used with the PIC18F4553, which I will post a tutorial on how to use it before I go back to RPI.

181806_4372421540978_720517165_nThe picture above shows the tag of one of the RFID’s after I ran my RFID reader code on the PIC18F4553. It’s a little slow reading the tag at times, but I find it very reliable.  Well, that’s it for me this week! See you guys on Monday….o wait. The world is gonna end today. Well, it’s nice knowing you guys!

 

 

 


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!


Leave a comment

Projects: N64 Controller Now Pygames Controller

481580_4367919388427_284505977_n

So yesterday, I started testing the Python script to generate the keyboard board events that the emulator could recognize. The way the script works is that whenever I pressed the tact switch connected to the PIC8F4553 microcontroller, it sends a simple c character to the python script. When the python script sees the c character, it will then generate a keyboard event which the x key (A button on the N64 emulator) is pressed.

When I tested the script, it works in Windows perfectly. Heck, I could create a virtual keyboard and mouse for windows based for a couple test scripts alone (future project?). However, whenever I run the script, then run the emulator, the emulator ignores the keyboard events generated. To be fair, it recognizes it sometimes when I click the Python IDE then switch to the emulator window. I sent an email the Project64 creator, posted on the Stackoverflow forums, but it seems like no one knows why the emulator is ignoring the keyboard events.  However, I realized that I will shift the project focus from a controller designed for emulators, to a controller that can be used for Pygames.

By changing the project focus to Pygames, I know for a fact I can finish the project before January 21st as well making this a RCOS project for next semester. Not to mention I still use the parts I ordered a couple days ago. What I’m going to do is get an idea of functions that will allow Pygame developers to expand their games and extra parts I will need. One of the extra parts I will need an accelerometer to add motion controls. I’ll reveal the list of controller functions later on this week.


1 Comment

Projects: N64 Emulator Controller Update #2

partsorder

Yesterday, I just placed the order from Sparkfun for the prototype of the controller. The prototype will be implemented on a breadboard first before progressing any further. As you guys can tell from the shopping cart, I brought 12 tact switches with a nice round top on each one, left and right bumpers, and two analog sticks. One analog stick will be for the analog stick on the N64 and the other analog stick will be for the C buttons. The reason why I’m mapping the C buttons to an analog stick is because I figured I could create a controller similar to a PS3 or 360 controller, which feels natural to me. I have no idea how long it will take the order to get here in Philadelphia, but my guess is next week before Christmas.

Anyway, I started to mess around with the serial library for Python. Using the library was much more simpler than I thought as there are plenty of documentation on how to use the library. To understand how the library works, I wrote a simple little code in python that not only sends the letter c to the PIC microcontroller, but waits up to 1 seconds for the PIC microcontroller to send “hello” to it. Originally, I wanted to paste the code to this paste, however, the code editor for wordpress is really poor and will mess up the coding syntax of Python. The way the code works is that whenever I’m prompted to enter a character, it will either exit the program, or transmit the letter ‘c’ to the PIC18F4553 microcontroller. When the microcontroller pick up the c, it will transmit “hello” to the computer.

pythonpic

It seems like there are some timing issues since I get a weird character after the first transmission. I’m going to look into how to resolve this issue until the parts come in. I will also learn how the keyboard event generator will work in Python.


2 Comments

Projects: Open Source N64 Emulator Controller #1

So, here I am again. I am going to relax after having another brutal semester at RPI. However, next semester will be one of the toughest semesters I ever had. Although I’m only taking 12-15 credits next semester, I will be working at 5 different jobs. The first job is a part time job at TV Ferret redesigning their sewage inspection systems. The second job is working as a lab TA for the Mercer Lab at RPI. Another job will be working at RPI’s Advising Learning and Assistance Center as a tutor for introduction for Electronics and Electric Circuits. The final two jobs will be working at two research centers, but I doubt I will get both of them. Add in my senior design project, and you get a very stressful semester. So I figured going to have to work on a semi big project before going back to RPI. What’s the project I’m working on? A controller for an N64 Emulator.

oot

Recently I started to get into video game emulators, more specifically the Project64. Lately, I’ve been playing The Legend of Zelda; Ocarina of  Time on the emulator. The biggest problem, which I’m sure a lot of people who played emulators have encountered, is the controls. Controlling your characters using a keyboard is not humanly possible. I want to make playing on the N64 emulator as easy as pie. I envision myself, picking the device, and playing a rousing game of Super Mario 64 without attempting to throw my laptop out of the window.

To make the project more interesting, I will set up a deadline for myself. I want to at least finish the prototype before January 21st, or before I go back to RPI. Whether the final prototype will be built on a breadboard or a pcb is up in the air. If I finished the project before January 21st, I will make it into a project for RPI RCOS, which focuses on open source projects,  next semester.  In other words, expect more posts to come from me December 17th until January 20th….as long as the world does not end by that time. So, the question you guys might be wondering how am I going to accomplish this?

PIC18F4553-pinout

So obviously I’m going to use a microcontroller for this project, but it will be a very different microcontroller than the microcontrollers I used in the past. Instead, I will be using Microchip’s PIC18 series microcontrollers. Why? Simple, my dear Watson. I spent one semester using nothing but PIC18 microcontrollers for my research at RPI’s Center for Automation Technologies and Systems.

Slide1

So the picture above shows the overall system I will be working with for the project. Here’s how it will work. I will have the PIC18F4553 microcontroller to read in the state of the buttons on the controller. Afterwards, it will tell a python serial communication script which button is pressed. Once the serial communication script receives the data from the microcontroller, then it will tell another python script to generate the appropriate keyboard events, which will be automatically seen by the emulator.Depending on the progress of the project, I will create a gui interface for the device. The gui will have an on/off button, and an option to determine the COM port the driver should look at. Again, I will give more details as time progresses. It’s going to be a fun 35 days!