Saturday, September 5, 2015

Taobao - abundance supply of cheap tools and components

China has been a great supplier for cheap electronic tools and modules. Even the sparkfun is sourcing their multimeter, soldering station from China, which in turn, show the reliability and usability of China tools.

When you go jalan pasar, most of the components and tools, which I believe also sourced from China.

Recently, taobao has that 2rmb promotion for delivery (direct from china to malaysia) for first time user.

Best of all, you are able to pay taobao using the malaysia online banking system.

A full tutorial is provided by taobao.

Taking this opportunity, I have registered an account and start finding for cheap multimeter and soldering station.

It is lucky that I know Chinese, make it easy for me to search for particular items.
But I also use baidu to help me do the translation and i copy the exact phase to taobao. Although you can use english to search but the results is not as many as your Chinese search term.


The search results return a series of options,


 and depending on cost, I choose the best selling one.

The victor vc890c+, has always been a popular choice. Good review, maybe due to it's fluke like look.


There are a few packages to choose, some with extra chargeable battery, a pouch, probe...
I buy an extra probe. And it only cost me roughly RM60. I think is fairly a good bargain for a multimeter with ac voltage/current (sensing range is until 20 ampere), capacitance, temperature, resistance sensing. Furthermore, transistor NPN/PNP checking is included. A quick search on vc890c+ in lelong, it cost around RM100. So, final conclusion, is a damn good deal.


I have a urge to build my own electronic home lab. So a basic two tools, I think it will be multimeter and solder station. For the soldering station, there are two variants, soldering iron, hot air, and combination or two. At first, I plan to buy soldering iron with hot air reflow, as the price is cheap as well. After saw the technician in my company, on how they can solder smt component easily using single solder iron. I have decided to purchase a soldering station with temperature control.


and I choose the cheapest best selling 936 series.
The Hakko has a tremendous success on soldering station 936, now every China soldering brands will have at least one model with 936 name.

The one I chose is kasadi 936, the name is nice and the advertisement look very promising on assuring the quality. Furthermore, at the taobao page, the reviews on Kasadi 936 are quit ok and has some small defects but still acceptable.

I buy a package which consists solder, solder flux, sucker, tweezer........
Easier for me, as I dont want to buy the items separately as it will troublesome and no guarantee it will be cheaper.

Also, I add in solder cleaner (Reason: Cheap). This type of cleaner is suitable for lazy me, dont need pour water, use and toss item but it can last for quit some times.


In this order, my friend "tumpang" a pair of wifi esp8266 module and a pair of bluetooth ble 4.0 module.




Total delivery fee for these items is roughly 9rmb, 7rmb is charged to deliver the wireless module to a warehouse in china. After consolidating all items, 2rmb is charged for delivery to malaysia. The multimeter and solder station seller doesn't charge fee for delivery in china.


You can check the status of your items from time to time. Even bar code is provided for easy checking, but I never try it.



Taobao will hold your payment for roughly 10 days (I think). You can extend the payment date if the items is not reached within 10 days. Also you can ask for refund but terms and conditions applied (I have no idea on this but it is refundable with conditions).

Nonetheless, my items arrived less than one week (6 days to be exact).



The multimeter set

Solder station


I didnt open my friend wireless module, it is quit light, almost felt empty. I let him open himself.

Total cost for multimeter and soldering station, cost me RM130, but is a good buy. In the mean time, I am going to let it collecting dust in cupboard.





Friday, July 24, 2015

HD44780 lcd16x2 - custom character

The HD44780 has character generator RAM (CGRAM) to store user-defined character pattern. There are two font types - 5x8, 5x10. For 5x8 dots, 8 character patterns can be written, and for 5x10 dots, 4 character patterns can be written.

Since 2 lines mode is always preferred, the below setting is for font 5x8.

To understand the process of storing custom character, the correspondence between EPROM Address Data and Character Pattern (5x8 Dots) is needed to be studied.
For each custom character for 5x8 font, 8 lines of address are needed to store one character pattern.
So address, A2, A1, and A0 are the 8 selectable line position of the character pattern.
while data O4, O3, O2, O1, O0 correspond to character pattern data.
Last, the A5, A4, and A3 represent the 8 selectable address to a character.

The relationship between CGRAM Addresses, Character Codes (DDRAM) and Character
Patterns (CGRAM Data) can be better shown at figure below

The CGRAM address bits 0,1,2 are the line pattern position,
The CGRAM address bits 3,4,5 are the custom character position, it is linked with DDRAM location.

The DDRAM address bits 3 (*) indicates no effect which means address 0000 *000 can represent two locations 0000 0000, and 0000 1000. Both locations store the same custom character.

To go to CGRAM address the below instruction is used.

The address bits DB5, DB4, DB3 are custom character position,
while address bits DB2, DB1, DB0 are line pattern position.

When adding a custom character, it is only required to point to custom character position (DB5, DB4, DB3), and leave the line pattern position (DB2, DB1, DB0) equal to zero.
As the first line pattern is written, the HD44780 will automatically point to next location as (DB2, DB1, DB0) will be increased accordingly. So, pointing to next CGRAM location is not needed in next line pattern writing.

(DB5, DB4, DB3) - Custom character position
(DB2, DB1, DB0) - Line pattern position

To create custom character, use the instruction as follow:
rom char battery1[8] = {
        0b01110,
    0b01010,
    0b10001,
    0b10001,
    0b10001,
    0b10001,
    0b10001,
    0b11111
    };
rom specific that this data is saved at flash memory.

To save the custom character, use the instruction
HD44780_AddCharacter(0, battery1);


All the custom characters are stored at the specific location as shown at figure below:


HD44780_AddCharacter(0, battery1);
location 0 is upper bits 0000, lower bits 0000.

To write the custom character to lcd display, just simply use the write data to DDRAM such as:
HD44780_WriteData(0x00);

BTW, to display the  character "0", HD44780_WriteData(0x30) and HD44780_WriteData("0") will get the same result.

Finally, the display of custom character - battery. I let the led1 and led2 blinking in forever loop (just for fun).


I keep all the source code at github.





Friday, July 10, 2015

HD44780 lcd16x2 and c18

The HD44780 is a common display used in hobbies project, the tutorials, datasheets, and implementation guide are all over the web. Even with available source code on the web, I am planning to write my driver using PIC18f. Previously, I had modified a source code online for msp430, it is a 4 bits data line communication. This time I am planning to write my own 8 bits data line communication 16x2 lcd driver using c18.

I write and test my code for on-going two months. Nothing work and I have no idea what went wrong as I wrote the code according to data sheet. Maybe the HD44780 has it's variant which make the implementation slightly difference from one and other.

I have to search on-line and read other people source code to understand how the initialisation of lcd is done. After some research, I found that elm-chan website has the most detail guide on implementation and the most accurate one also. For the source code, I found that armandas is the best written source code so far. I do follow some of the way armandas naming and code structure. Also there is simplified but detail datasheet from spickles.

The main problem I faced is the initialisation part, so I am going to start with this. There are two types of initialisation process. First is setup with internal reset, second is initialisation with software. The internal reset procedure is relatively easy, and protostack even shows an initialisation example using solely dip switch. This is pretty amazing as no a single MCU is involved in this.

Second type is the initialisation by instruction.
The initialisation diagram is as follow:

I had highlighted the changes I made.

For the last 4 steps, the long delay is needed as weird character will come out. Usually weird character mean the time needed for the lcd to process data is not enough.
For the third step from last, I found that 0b00001000 is not the correct initialisation. Although most of the datasheet online, and even microchip application note have shown that 0b00001000 is the correct value. Nonetheless, I do encounter some problem when initializing the lcd. Maybe the HD44780 variant is different somehow.

In this configuration, I choose, 8 bits data transmission and 2 lines display.

..............................................................................................................................................

For the 16x2 lcd, a pointer to particular location in lcd is important. The HD44780 in (8 bits, 2 lines configuration) able to store 80 locations. But only the first 16 locations of the 1st line and 2nd line able to be displayed.

The DDRAM address mapping is as follow:

The numbering is at hexadecimal format.
To point to first row, third column, DDRAM is set at 0x02.
To point to second row, third colum, DDRAM is set at 0x42.

The pseudo code is easy
if first row, DDRAM = address_point_by_user;
if second row, DDRAM = address_point_by_user + 0x40;

..............................................................................................................................................

Microchip has two simple rules to follow when apply read and write operation to IO port which are "0" for output and "1" for input and the second rule is "read from port, write to latch".
In order to write to output, the IO direction is set to output and the write operation is written to latch as follow:

#define HD44780_initPinIO() {TRISBbits.RB4=0; TRISBbits.RB5=0; TRISD=0x00;}
#define HD44780_EN_PIN LATBbits.LATB5
#define HD44780_RS_PIN LATBbits.LATB4
#define HD44780_DATA_PORT LATD

..............................................................................................................................................

To keep the coding easy to use and easy to understand, only three functions are included in the hd44780.h which are

void HD44780_Init(void);
void HD44780_WriteString(const rom char *s);
void HD44780_GoToPoint(char row, char col);

HD44780_Init()-must be defined before the two other functions.
HD44780_WriteString(const rom char *s)-write words to display.
HD44780_GoToPoint(char row, char col)-go to particular location of lcd.

Nevertheless, hd44780.c file can be modified to cater the user's need as most functions are hidden in hd44780.c.
..............................................................................................................................................

Simple note:
One pulse               - EN high, delay 50 us, EN low.
RS pin                    - 0 = COMMAND; 1 = DATA.
DDRAM location  - Location at lcd display.
rom                        - c18 keyword to store data in flash.

..............................................................................................................................................

Output:


Most people would prefer the "hello world" message, but I decide to display 4 alphabet at 4 different locations. ^^

The source code can be found at github.





Friday, June 12, 2015

PIC18f system clock and delay routine using C18

I just started with the PIC18f, so I planning to go into fundamental. With bunch of tutorials and forum discussions online, I still encounter some difficulties to get started with PIC18f.
Simple problem, yet I do a lot of readings before I successfully compile the led blinking (with 1 second delay).

The first problem is setting the system clock. And the second problem is using the C18 default delay routine.

The PIC18f4550/PIC18f4553 has three clock blocks which are primary oscillator, secondary oscillator, and internal oscillator.




The primary oscillator is normally used for high speed oscillator (>=4MHz), secondary oscillator for low speed oscillator such as 32.768kHz. Internal oscillator has two different clock signals; main output is an 8 MHz clock source and second clock source is the internal RC oscillator provides a nominal 31 kHz output.

Primary oscillator has 4 modes which are HS, HSPLL, XT, XTPLL. Depending of frequency of oscillator, suitable mode need to be assigned as shown as table below:

Resonators Freq
Mode
4.0MHz
XT
8.0MHz/16.0MHz
HS

Crystal Freq
Mode
4.0MHz
XT
4.0MHz/8.0MHz/20.0MHz
HS

For some projects, MCU doesnt need to operate at high speed, config CPUDIV can be used to scale the operating frequency into lower speed.

For some applications that require high speed clock, MICROCHIP provides PLL (phase locked loop) function, it is a frequency multiplier to generate a fixed 96MHz from a fixed 4MHz input. On top of that, a PLLDIV is assigned to divide the oscillator frequency to 4MHz which enable the adoption of oscillator with various frequency range.

















The PIC18f's maximum allowable frequency is 48MHz, so after the PLL the 96MHz is divided into half to suit the MCU operating frequency.

The overall setting for CPU frequency is as follow:
1:      #pragma config PLLDIV  = 5                        // (20 MHz crystal)
2:      #pragma config CPUDIV  = OSC1_PLL2    // Divide 96MHz with two
3:      #pragma config USBDIV  = 2                      // Clock source from 96MHz PLL/2
4:      #pragma config FOSC   = HSPLL_HS         // HS oscillator, PLL enabled (HSPLL)  

For more information on setting, open the mplab, go to Help->Topics...->PIC18 Config Settings. From there, choose selected microcontroller, and all settings are listed and explained.

........................................................................................................................................
For PIC18f4553/4550, maximum CPU Speed is 48 MHz (12 MIPS) which means at CPU operating frequency of 48 MHz, it is able to run 12 millions instruction per second. So one instruction need 4 cycles to process.

In order to have one second delay, you need to do nothing for 12 millions instruction time.
For one micro second delay, 12 instructions of _do_nothing are needed. Which in turn,  two micro second delay, 24 (12x2) instructions of _do_nothing are needed.

Microchip c18 provides <delays.h> library to help set the timing, the <delays.h> is located at
"C:\Program Files (x86)\Microchip\mplabc18\v3.47\h\delays.h"

I wrote my own routine on top on <delays.h> and name it as "delay.h"
For one micro second -
#define delay1MicroSecond()    {Delay10TCYx(1); Delay1TCY(); Delay1TCY();}
// delay 12 instructions

#define delay10MicroSecond() Delay10TCYx(12)      // delay 120  instructions

#define delay1MiliSecond() Delay1KTCYx(12)     //delay 12000 instructions

#define delay100MiliSecond() Delay10KTCYx(120) //delay 1,200,000 instructions

#define delay200MiliSecond() Delay10KTCYx(240) //delay 2,400,000 instructions

All delay routines are in the multiplication of 12 (provided that the CPU operating frequency is 48MHz).

For other CPU frequency, scaling is needed to get the correct timing.
Examples:
                  48MHz = 12 MIPS        // for one micro second, delay 12 instructions
                  32MHz =   9 MIPS        // for one micro second, delay   9 instructions
                  24MHz =   6 MIPS        // for one micro second, delay   6 instructions
                  16MHz =   3 MIPS        // for one micro second, delay   3 instructions

..............................................................................................................................................

To blink led,
the pic18f has one general rule to read/write IO - read from port and write to latch.

First set the IO pin to output,
#define mInitAllLEDs()     TRISBbits.TRISB6=0; TRISBbits.TRISB7=0; //define output
//pic18f rule for input/output is
//0 = output;
//1 = input;

#define mLED_1              LATBbits.LATB6    // write to particular bit (pin b6)
#define mLED_2              LATBbits.LATB7    // write to particular bit (pin b7)

// to on led just assigned mLED_1 = 1;
// to off led just assigned mLED_1 = 0;
..............................................................................................................................................

Now you can start with blinking led, all the source codes can be downloaded at github.




Saturday, May 9, 2015

PIC18f - USB - CDC

The most two common USB protocols are HID and CDC. Most of the popular open source projects which use the microchip are using the CDC for interfacing with PC. For example, the popular bus pirate, logic scrimp, ir toy are using CDC. The dangerous prototype has their open source USB stack so the bus pirate, logic  scrimp, logic pirate are all using the Honken USB Stack source. Another popular open source stack is M-Stack (Free USB Stack for PIC 16F, 18F, 24F, and 32MX Microcontrollers), it has more organised source code and more detail commented. However it only has support on x series compiliers like xc8, xc16, and xc32.
Last, is the microchip default USB stacks, the most common, least problem and abundant of examples. Shouldn't be troublesome if you are using microchip library and microchip compiler. In this tutorial, I am using microchip library.

     I like CDC over HID because it has higher transfer rate, and it appeared as serial communication. The latter feature ensures all of the terminal software can be used. My favourite terminal software is termite,

it is easy to use, no installation required, auto detect and auto configure the serial port.
There are other popular terminal software like tera-term, and real term. Both of these are popular among hobbies as the functions provided are vast and stable. Nevertheless, the learning curve for both software are longer. Whereas, Termite is like a software for dummy, basically no learning curve at all.
To start a project USB CDC project, the easiest way is modifying the Microchip example. Leaving the example file as it is, copy all the source codes to your workspace as shown in the figure below.


It is hard to locate all source file but you can refer back to here, it is more or less the same.

For the USB to run at full speed, the USB need to operate at 48Mhz. The below configuration is to scale the frequency to 48Mhz (everything is at the example file).

This example is going to do three things,
First is the detection of button pressed,

 and if the button is pressed, then "Button Pressed" is printed as shown at code above.

Second is the sending the exact strings with a plus one back to PC as shown at code below:


The third thing is the indication of USB states.



Since the CDC acts like series communication, at the usb_function_cdc,c mentions the serial communication configuration:

You can try to modify the parameters but after some googling, I found out that these parameters are dummy and has not effect when you setting up the serial communication.

...................................................................................................................................................................
After you program everything into PIC, and plug the PIC into PC you will see this message,


You need to go device manager and manually detect the USB driver, you need to click "Browse my computer for driver software"

the driver is provided by MICROCHIP
Usually located at "C:\microchip_solutions_v2013-06-15\USB\Device - CDC - Basic Demo\inf".

If the driver is successfully installed, following will be appeared


.................................................................................................................................................................
Time to play, double click on the Termite terminal software, and you will see the following

Everything is automatically configured. If you click at the "Settings" icon, you can see this
which is different from what we programmed in PIC. It can be concluded the setting in source code doesnt effect the setting at PC side.

Try type any character in the termite terminal and you can the exact plus one back from PIC.


I am using the cytron development kit SK40c, so when I press the SW2 button on the board, I can get the string "Button Pressed"

Since I solely supply the board using the supply from USB, I didnt test the USB status led blinking.

The source file created for SK40c is shared in github.





Thursday, March 26, 2015

Isolated current sensing using LA25-NP

Another popular sensor used in my laboratory is current sensor LA25. In power electronic research, we tend to use sensor with safety standard. LA25 seem to fit in.


This sensor has nominal sensing current of 25A, and peak sensing current is 36A.
It needs +/- 15V supply voltage, and output current is 25mA.

It can be configured to change the sensing range in order to get more accurate result.
I draw each of the configuration on real picture as it will be more visual-able compared with datasheet.

25 AMP configuration

Pins 1 to 5 are connected together for "input sensed current", 
while pins 6 to 10 are connected for "output sensed current". 
M is the measurement, while +/- is for +/-15V supply.

12 AMP configuration
Pins 1 to 3 are connected together for "input sensed current", 
pins 4, 5, 8, 9, 10 are linked together. 
Last pins 6 and 7 are connected for "output sensed current".

8 AMP configuration
Pins 1, 2 are connected together for "input sensed current", 
pins 3, 9, 10 are connected together, 
pins 4, 5, 8 are connected.  
Last pins 6 and 7 are connected for "output sensed current".

6 AMP configuration
Pins 1 is for"input sensed current", 
pins 2, 10 are linked together, 
pins 3, 4, 9 are connected, 
pins 5, 7, 8 are also connected.  
Last pins 6 is for "output sensed current".

5 AMP configuration
Pin 1 for input,
Pin 6 for output,
Pins 2 and 10 are connected, pins 3 and 9 are connected, pins 4 and 8 are connected, pins 5 and 7 are connected.

The inner resistance change with each configuration, the lower the sensing range, the higher the internal resistance.
The supply voltage and output signal configuration are same as LV25.