Saturday, November 1, 2008

Embedded C Code

PLEASE READ THE POST ABOVE
-----------------------------------------------------------------------------------------
Below I have posted the content of the Embedded C PIC program (CC5X compiler) that drives the software inside the microcontroller. It basically sends the readings from the 2 potentiometers at the very beginning, and then stop transmitting, until it notices a change in any value. If that happens it modifies the high order byte of the conversion in order to notify the computer as to which potentiometer has changed its value.


#pragma chip PIC16F870

#include

#pragma origin 4

bit channel_is_0; //global variable to flag that first A/D channel is selected
unsigned char old_low0; //last converted low byte of channel 0
unsigned char old_high0; //last converted high byte of channel 1
unsigned char old_low1; //last converted low byte of channel 0
unsigned char old_high1; //last converted high byte of channel 1

interrupt isr(void)
{
int_save_registers
if (ADIF == 1) //if we have an interrupt from ADC conversion
{
// if channel 0 conversion and low byte value is different than the last, OR
// if channel 1 conversion and low byte value is different than the last
if (((ADRESL != old_low0) && (channel_is_0)) || ((ADRESL != old_low1) && (!channel_is_0)))
{
while (TXIF !=1 );
if (channel_is_0)
ADRESH.7 = 1; //signal the computer that this is the reading for the first channel
TXREG = ADRESH; //send last 2 bits of the conversion

while (TXIF !=1 );
TXREG = ADRESL; //send first 8 bits of the conversion
}
if (channel_is_0)
{
old_high0 = ADRESH;
old_low0 = ADRESL;
}
else
{
old_high1 = ADRESH;
old_low1 = ADRESL;
}

ADIF = 0; //clear the flag
}
if (RCIF == 1) //we have a byte received in the buffer
{
if (RCREG == 255)
ADIE = 1; //enable A/D interrupt
else
ADIE = 0; //disable A/D interrupt
}

int_restore_registers
}


void main(void)
{
//configuration of ADC
ADCON1 = 0b.1000.0100; //RIGHT Justified format, all pins of PORTA are analog inputs)
ADCON0 = 0b.0100.0001; //FOSC/8, channel 0, ADC module is now ON
//next we must enable global interrupt from ADC complete conversion
INTCON.6 = 1;
INTCON.7 = 1;
//clear ADC interrupt flag bit
ADIF = 0;
ADCON0.2 = 0;
RCIE = 1; //enable reception interrupt
//next we must configure the UART Transmit
SPBRG = 25; //this would generate a 9600 baud rate
TXSTA = 0b.0010.1100; //8 bit trabsmission, enable transmit, async mode, high baud rate
RCSTA = 0b.1001.0000; //serial port enable, 8 bit reception, enable reception
while(1)
{
if (ADCON0.2 == 0) //if ADC GO bit is not set
{
if (ADCON0.3 == 1) //if first channel is selected
{
ADCON0.3 = 0; //select the second channel
channel_is_0 = 0;
}
else
{
ADCON0.3 = 1; //else select the first channel to convert
channel_is_0 = 1;
}

ADCON0.2 = 1; //Set ADC GO bit
}
}
}

Wednesday, October 29, 2008

Faculty License project

"Simulation of an Artificial Horizon system necessary for aircraft piloting" This long and pompous title is the name of my License Project. More news to come after I finish my current Etch a Sketch project, all I can say is that it is going to prove quite a challenge...

Saturday, October 25, 2008

Should I give OpenGL a chance?

Because of a school project, I am compelled to learn OpenGL. I think that it could serve my purposes just fine for this particular project, but as I will progress with more daring projects, which I hope will involve quite a bit of physics, OpenGL will not be a viable solution.

As a side note, I have come up with several other fun applications for this gizmo I am building, for example I could control a game of classic PONG.

Final module is in

I've done it! The nasty part of this project is finally over. I mean the hardware part, the part where I have practically no experience. The board was completed Friday night, today I have tested it with my Win32 Library that I have written a while back. I still have to level out some bugs, but in general it works decent.

Here is a look at how it looks like completely assembled:


And now a back-side view:


Next I must eliminate all the bugs, make the embedded PIC C code as efficient as possible, and then start to write the game using Irrlicht of OPENGL(maybe a new post will follow about my change of heart)

Saturday, October 18, 2008

Serial Module

This week I had the task of soldering the Serial Module to my board.
The serial module consists of the following:

  • Max232 IC
  • 3 1uF capacitors
  • Db9 Serial connector
Things did not go smoothly, but in the end I managed to pull it off. Below is a picture of the finished module:

Now, a short description of why I need MAX232 and how it works. This IC (integrated circuit) converts TTL/CMOS voltage levels for 1 and 0 to RS-232 voltage levels. Here is a quick comparison to show you what I mean : "1" in TTL is roughly 5V, and 11V in RS-232, while "0" in TTL is 0V and -11V in RS-232. When I add the fact that my PIC16F870 knows only TTL voltage levels and any computer knows only RS-232 when it comes to simple serial communication, you understand the need for such a IC.

It works by the aid of 3 charge pumps, which in our case are 3 1uF capacitors, one for the internal voltage doubler, one for the voltage inverter and the last is placed in my case, between the +10V pin and the 5V rail of the board. All is made even clearer by the datasheet.

I tested this module by connecting the board to my computer, to COM1, fired up Hyperterminal, and joined pins 11 and 12 of MAX232 IC, essentially making a loop back, the bytes sent from the computer where converted to TTL levels, and then converted back (pins 11 and 12) to Rs232 levels, then sent back to the computer. It worked fine as I could see the characters I inputted in the terminal where bouncing back on the screen. Job done!

I have only the main microcontroller part to solder on and the eureka moment is finally approaching. Happy me!!

Sunday, October 12, 2008

First module

Today I have soldered the first module, the voltage regulator.



You can see the pre-drilled holes where the linear potentiometers will be, and the Db9 connector is only glued, but not soldered. That is because there is no direct way to solder that connector to a perf board, the pins do not match the holes, so I bent an entire row of pins which are not used by our application. Soldering of this piece will proceed shortly.

Saturday, October 11, 2008

A little shopping

Time for a new post. After a long week at work, I finally managed to write to you again. I finished designing the board, and I also found the time to buy all my necessary parts.

Basically, there are 3 separate modules that come together to form a single unit.First you have a Voltage regulator module, that converts 9V of unregulated voltage and delivers 5V of regulated voltage. Since a microcontroller is very sensible about unstable voltages, this module is very important. Here is a list of parts and a picture of it:

  1. 1 Jack
  2. 1 Diode 1N4007
  3. 1 Voltage regulator LM7805
  4. 2 Capacitors (100 uF and 10 uF)
  5. 1 Switch
  6. 1 330 ohm resistor
  7. 1 LED
Next there is the Microcontroller module, which mainly consists of a 28 pin Socket, where we will place the PIC16F870, the timing circuit for the micro, and the 2 Linear potentiometers which will guide the cursor on our computer monitor. Parts needded:

  1. 1 28 pin microcontroller socket
  2. 1 4 Mhz quartz oscillator
  3. 2 15pF capacitors (as stated in the micro datasheet)
  4. 2 10Kohm linear potentiometers

Next we must deal with the serial module, this is type part of the board that ensures the communication with the computer via a serial cable. Parts we need:

  1. 1 DB9 serial connector
  2. 1 MAX232 IC
  3. At least 3 1uF capacitors (to act as charge pumps for the MAX232 IC)
  4. 1 Male to Female serial cable
In addition we must have a perforated board of about 10cm x 10cm, and a bunch of insulated wires, to make the necessary connection on the board.


Sunday, October 5, 2008

Necesary tools and progress check

Time for a progress check. After trying to write the code in assembler, I realized that I should let my ego have a break and try to find a free C compiler for my PIC.

I am perfectly aware that eventually I could have written the code in asm, but also I would have wasted a lot of time, time which I can use for some other tasks. I fired up good old Google, and did a search on free PIC C compilers. After some reading, I decided to use CC5X free PIC compiler, probably the first ever created. It even integrates itself into MPLAB, which is great, perfect for what I need.

Another quintessential tool is Oshon Soft PicSimulator. It is an amazing software, allowing you to see what your code can do and so many things, until know everything that worked on the simulator, worked the same on the actual microcontroller.

This is how I spent my day, and by the end of it, I had a working program.

I will elaborate further on in my posts about the innerworkings of my Etch a Sketch design. including the hardware and software side.

Saturday, October 4, 2008

Micro Choices

I have a Velleman PIC Programming and Evaluation Board K8048, with which I can program my chosen microcontroller.

Speaking of chosen micro, after viewing the specifications of my board, I had only one option: PIC16F870.

Micro characteristics:
  • 28-pin DIP package
  • 3 I/O ports (25mA sink/source)
  • 5 MIPS performance @ 20MHz
  • 2K word Flash Program memory (14-bit words)
  • 128 bytes EEPROM data memory
  • 64 bytes RAM
  • 8-level hardware stack, 3 addressing modes
  • Selectable oscillator options
  • Two 8-bit timer/counters with programmable prescalers
  • 16-bit timer with prescaler
  • Five channel 10-bit ADC module
  • UART
  • PWM Module 10-bit
  • Brown-out detection circuitry
  • Power-On Reset (POR),Power and Oscillator start up timers and Brown-out reset
  • Sleep mode for power reduction
  • Watchdog Timer with separate on-board oscillator
  • Programmable code protection
  • 2 to 5.5V operation
  • Low power consumption <>
Fortunately my local hardware store had them in stock, or else this project would have crawled to a standstill even before it started.

Thursday, October 2, 2008

Serial Communication

To start things of, I need a C++ library that enables me to establish a serial communication between the computer COM port to an external device. Since I am going to be doing some windows programming, I will need to write a wrapper over the Win32 API functions that enable me to communicate serially.

In Windows, I can not access hardware COM ports directly, this access has been restricted because a lot of guys have used this to write viruses. COM Ports are now viewed as regular Files, and the same functions that open, read, write into files also work with COM ports.

In order to test this library I need to build myself a RS232 Loopback connector, which is essentially a regular DB9 RS232 connector with the transmit and receive lines interconnected. Here is a picture depicting the specific lines and necessary connections that have to be made.




I used a couple of wires to make the connection. I tested the functionality of my connector with Hyper Terminal from Windows, hooked up the connector to a spare serial male connector of my PC (usually COM1) opened a connection in Hyper Terminal with COM1 and started typing random characters. These characters were showing in my window as received data into COM1. So it was working, the data I was sending through COM1 was being received and displayed on the screen.

Wednesday, October 1, 2008

First Project - Etch a Sketch


Maybe some of you have heard of Etch a Sketch. Yes, the childhood toy you used to write your name with. Needless to say that some of you out there may have used it to amaze your friend with works such as the one below


I will try to replicate this little toy using Embedded technology. To be more specific I will design and build a hardware device, hook it up via RS232 to the computer, and write a simulation game, that takes input from the hardware device and etches a line on the screen. Pretty cool, huh?

I admit it is not very flashy, but this will give us a better understanding of the following important embedded basics:
  1. Analog to Digital Converting
  2. Serial Communication
  3. Use of a MAX232 IC (for converting from TTL voltages to Computer RS232 voltages)
  4. Using Perforated Boards for easy prototyping
  5. Interrupt handling (nasty work)

For the Windows programming side, I will use an opensource game engine called Irrlicht. I have chosen an open source game engine because it is sufficient for my needs including everything from sound to networking. I have some experience with DirectX, but that would just eat up time with no visible gain in performance.

Welcome

At the moment this is a concept idea, designed to educate the average student in embedded technologies.

Gadgets are the toys of the 21st century, and most of us wish there existed a special one, and wonder every day, why someone hasn't produced it yet, but in fact you forget that you have the power and the knowledge to build it yourself. All you need is a little guidance, and patience.

This is a blog oriented to the average student at a technical college, so it will contain a strong technical language. It will follow the progress with pictures, videos and if requested tutorials of how things were thought and/or done.

This is the first post in our journey, and hope for more to come.