Friday, March 25, 2016

ADXL335 - Getting started

On the process on learning of pic18f, I like to start with existing code or hardware. So I like modules very much as my pcb design is still in the "room" of improving. I cant get the correct version until the 3rd or 4th version of pcb.

I have this three axis accelerometer breakout, bought from cytron (some time ago). It is the cheapest I can found. Since it is lying on my drawer and I am bored, I decide to test it.


Also, I have a soldering station which I bought from taobao 2 months ago, time to put it on test.

First, I align all the pin at the breadboard,

Then, I place the ADXL335 module on top the pin,


After switch on the soldering station, you need to wait until the red led indication on the soldering station went off, then the solder tip temperature is as set temperature. 
I verify it using vc890 multimeter.

My first try, it is really not bad. The soldering station really not bad, perform better than the 10 ringgit soldering iron that sold at jalan pasar. My soldering station only cost me 45 ringgit, really worth the money.

Actually I had tried popular brand of soldering station before like hakko, weller, goot, but I think this kasadi soldering station "soldering" performance is comparable to those top notch. Maybe the question lied on the robustness,  how long will it last before it break down. Last time, my 15 ringgit soldering iron from jalan pasar only been used for a few times, after leave it for few months, end of life.
Also, my lab weller soldering station is really robust, sometimes, we forgot to switch off it, leave it for two days, but it is still working great. 

I didnt dare to put test on the kasadi soldering station robustness, I handle it with cares, you should not expect much from cheap but working equipment.

Back to story, this is the soldered module
After I have finished solder all the pins, I realise that the top three pins are not necessary, just some dummy pads placed, no track connection to any component.

Now to the coding part, just the simple adc would be fine, c18 comes with some library on pic18f peripheral. However, my purpose is learning so I always look at existing code, datasheet, then start writing my own code.

It make the learning easier and your own code really suit your own need.
I go to c18 library file, copy the adc file out and start editing it for my own use.

In the end, I have only two functions 
- void ADC12_Init(void);
- int ADC12_ReadChannel(unsigned char channel);
which make it very convenient to use, but hard to modify settings (you have to edit the source code).
Overall, the settings of adc are quit general and easily applied to most conditions.

I supply the 5V to the ADXL335 module, it has on board regulator that regulate it to 3.3V. You can also supply the 3.3V to the ADXL335 module directly.

I have disassembly the connections so I have no output picture to show your guys. Nevertheless, the program have been tested and it is working fine.

In this program, the displayed value for X, Y, Z axis are adc value, I have no idea on the calibration (how to convert the adc value to angle using limited equipment). Maybe after some time of learning, trying and googling, I will post it up.

The project file is downloadable at link.



OSA rtos - 2nd

After able to run a simple led blinking using OSA rtos, I plan to integrate one display function ( lcd 16x2) and two push button (event triggered).

So I have added one additional task which is taskDisplay(), the main purpose of task display is showing the some simple words and display simple message when push button is pressed.

The OSAcfg.h file remain the same
 //------------------------------------------------------------------------------
// SYSTEM
//------------------------------------------------------------------------------
// Number of tasks that can be active at one time, two tasks for led blinking, one task for display
#define OS_TASKS               3
// Not using any fancy OS function, so "no priority" setting is set
#define OS_DISABLE_PRIORITY    


For the HD44780 driver, it is remain the same as previous tutorial, I didnt include the OS function in the HD44780 source file.
The development board I used directly ground the RW pin of HD44780, so it is only writeable (unable to implement OS_Wait() function).
I have to use the delay function to wait for HD44780 finishes the operation. The longest wait period is 2ms for HD44780 whereas the OS_Timer() interval is set to be 10ms. If I use one tick delay, it will be 10ms. Still reasonable to implement but I am lazy. After wasting 2ms out of 10ms, I still have 8ms which is a lot of time to do other stuffs, not to mention just simple led blinking.


 //------------------------------------------------------------------------------
// TASK DISPLAY header file
//------------------------------------------------------------------------------
#ifndef TASK_DISPLAY_H
#define TASK_DISPLAY_H

#include "hd44780.h"
#include <osa.h>

#define mInitSW1()   {TRISBbits.TRISB0=1;} // initialise the port direction to input
#define mInitSW2()   {TRISBbits.TRISB1=1;} // initialise the port direction to input

#define mSW_1 PORTBbits.RB0 // switch one
#define mSW_2 PORTBbits.RB1 // switch two

void taskDisplay(void);

#endif /* TASK_DISPLAY_H */

 //------------------------------------------------------------------------------
// TASK DISPLAY source file
//------------------------------------------------------------------------------
void taskDisplay(void)
{
mInitSW1();
mInitSW2();

        // initialize the LCD module is time consuming but it doesn't interfere with OS                                      // operation as OS only run the function above the forever loop on first time.                                          // After that, at each interval, it run the function inside forever loop.
        HD44780_Init();

        HD44780_GoToPoint(1,1);
        HD44780_WriteROMString("  - RTOS OSA -  ");
HD44780_GoToPoint(2,1);
        HD44780_WriteROMString("  -Simple Demo- ");

       for (;;) {
  // wait for switch to be pressed, bad implementation as I do not consider                                               // the switch debounce or noise interference.
        OS_Wait(mSW_1==0);

HD44780_GoToPoint(2,1);


         // String written when switch one is pressed, I make it to be 16 characters so                                          // that I dont have to clear any character left at display.
        HD44780_WriteROMString("Button 1 Pressed");  

  OS_Wait(mSW_2==0);
HD44780_GoToPoint(2,1);

         // String written when switch 2 is pressed.
        HD44780_WriteROMString("Button 2 Pressed");  
    }
}



At the main file, create the task mentioned.
 //------------------------------------------------------------------------------
// CREATE TASK
//------------------------------------------------------------------------------
// Priority zero, all task has equal priority
    OS_Task_Create(0, taskLED1);
    OS_Task_Create(0, taskLED2);
    OS_Task_Create(0, taskDisplay);

Here is the output display,

After initialisation,

After switch one is pressed,

The project file is downloaded at link.