Tuesday, November 13, 2012

PIC16F1503 based MPPT controller for photovoltanic system


Once I found a really simple MPPT circuit on Chris' website: http://www.freewebs.com/acselectronics/buildppt.html I decided to build something inspired by his work. I always wanted to build a switching regulator based on a general purpose simple (and cheap!) microcontroller instead of specialized switching controller. I choose PIC16F1503 because it has got build in two comparators, PWM (maybe not very sophisticated but fully functional) and CWG - new peripheral from Microchip which can be used to generate dead-band time between two outputs.
My first goal was to build something as “prove of concept” so it is not a real MPPT controller because it doesn't measure a PV cell current, but it adjusts load of the PV cell to make sure it works with given output voltage. Here is circuit diagram:
It's actually simple step-down converter with P-channel MOSFET (I decided to use P-channel because it allows use of common ground to measure input and output voltage). Load of this circuit will be a lead-acid battery so I added D3 to make sure that circuit will not discharge battery. D3 introduces lose of efficiency so right now I'm thinking about other fancy circuit which will do that better (with lower voltage loss across itself).
Here is an idea how microcontroller is configured to control switching regulator with its peripherals:
This is straight-forward implementation of switching regulator but implemented in a microcontroller, after initialization of FVR (Fixed Voltage Reference), PWM, comparator and CWG (complementary waveform generator) it works and software can do something else. And it will do: I plan to implement perturb and observe algorithm to search for maximum power condition of PV cell. I plan to introduce some changes to Circuit: I have to add PV cell current measurement. I also have to add another PWM with RC filter as a source of voltage of comparator. PIC16F1503 allows me to use a DAC output as a input signal for comparator but DAC is 5-bit only, it is to small resolution for effective MPPT tracking.
Another improvement will be second MOSFET instead of D4 to increase efficiency. Due to a simplicity of MOSFET driver circuit off-time is longer than I want so I will search for some improvement, maybe totem-pole circuit?

Here is a waveform on pin 6 of microcontroller (channel 2) and gate of MOSFET (channel 1), it is clearly visible that there is much to improve, especially switch-off time.  


Here is picture of a circuit itself during early tests:
It's built on one-side substrate to minimize costs, why should I order PCB in a fab if I know that circuit will anyway need changes?

Here is PCB design, as usually I have to show off how nice PCBs I draw. I used SMD components wherever it was convenient. I even managed to design whole PCB on one layer without a single wire-bridge.  
I will update this post or I will write the new one when I will build next version of this circuit with mentioned improvements.



The software "as it is"

#include <pic.h>

__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF);


void main(void)
{
    OSCCONbits.IRCF=13; //4MHz

    ANSELA &= ~(1<<2);
    TRISA &= ~(1<<2);   //konfigurj LEDa
    PORTA &= ~(1<<2);   //zgas LEDa


    PWM1CON=0;
    PR2=9;           //40 us
    PWM1DCH=7;
    PWM1DCL=1<<7;


    TMR2IF=0;
    T2CONbits.T2CKPS=1; //prescaler /4
    T2CONbits.TMR2ON=1;
    PWM1CONbits.PWM1OE=1;
    PWM1CONbits.PWM1EN=1;
    TRISC &= ~(1<<5);


    FVRCONbits.CDAFVR=1; //4.096V
    FVRCONbits.FVREN=1;  //enable



    ANSELA |= (1<<7);    //comparator
    TRISC |= (1<<3);
    CM1CON1bits.C1PCH=2; //positive input to FVR (1.024V)
    CM1CON1bits.C1NCH=3; //ngative input to C2IN3-
    CM1CON0bits.C1ON=1; //on


    TRISC &= ~(1<<4);  //konfiguruj wyjscie do sterowania mosfetem
    PORTC &= ~(1<<4);
    CWG1CON0bits.G1EN=1;    //wlacz
    CWG1CON0bits.G1OEB=1;   //w?acz wyjscie
    CWG1CON0bits.G1POLB=1;  //inwersja na wyjsciu
    CWG1CON1bits.G1ASDLB=2; //zero w auto shot down
    CWG1CON1bits.G1IS=2;    //na wejsciu jest PWM
    CWG1CON2bits.G1ARSEN=1; //auto restart enabled
    CWG1CON2bits.G1ASDSC1=1; // comparator 1 

    while(1)
    {

        
        if(CMOUTbits.MC1OUT)
            PORTA |= (1<<2);  //wlacz leda
        else
            PORTA &= ~(1<<2); //zgas LEDa
    }
}


Tuesday, November 6, 2012

How to fix Mercedes W201 180 fuel pump controller (so called relay)


A fuel pomp relay switches fuel pump for a definite time (usually about 2 seconds) after the ignition have been switched on to make sure that pressure in fuel system is high enough. This can be easily heard as you are starting your car. The relay also detects crankshafts rotations by monitoring signal from primary winding of a ignition coil, so when you turn on a starter the fuel pomp is immediately switch on stays on as long as the engine is running. Here is PCB of that “relay”:
  And here is a closeup of tracks which conducts fuel pump current, I marked this particular two tracks:
Here is solution for the mentioned problem:
I don't know if the same problem occurs in other relay types but i suggest trying to fix it before you buy another one. If the layout of other relay is similar to this it can and it will cause a similar problem. If you have problem with starting your Mercedes you can check if it's a matter of fuel pomp switch by connecting terminals 30 and 87, you can even leave it that way but I wouldn't recommend this solution. In case of an accident - when engine is stopped fuel pomp should be also stopped to prevent fuel leak, if you will “short connect” fuel pomp - it will not.

Thursday, November 1, 2012

SMS 7-bit encoding doubts in Open AT


Right now I'm writing some software for Sierra Wireless GSM Module - Q2687. This application is written with open AT library supported by GSM module vendor and have to handle special characters in SMS messages. I've set text mode for receiving SMS and started sending messages containing characters from basic characters set extension (see GSM 03.38). I was expecting to receive special character encoded according to this table:
It turns out that this characters are encoded completely different. Neither ESCAPE TO EXTENSION TABLE nor that second byte is like in presented table. I found out that encoding is rather like this:

Hex Dec Character name Character
0x20 32 ESCAPE TO EXTENSION TABLE

0x2020 32 32 CIRCUMLFLEX ACCENT ^
0x2028 32 40 LEFT CURLY BRACKET {
0x2029 32 41 RIGHT CURLY BRACKET }
0x202F 32 47 BACKSLASH \
0x203C 32 60 LEFT SQUARE BRACKET [
0x203D 32 61 TILDE ~
0x203E 32 62 RIGHT SQUARE BRACKET ]
0x20AD 32173 VERTICAL BAR |

As you can see - something is definitely not the way it should be. Look at VERTICAL BAR encoding - it isn't event coded on 7 bit characters, so what encoding it is? I don't know yet but I have to simply implement conversion (which is quite easy to code) before I will treat received SMS with cstring functions.
I tested this situation with messages sent with Android-based mobile as also with plain old Nokia C1.

If you are familiar with Open AT you can test it on your own:

void SmsCtrlHandler(u8 Event, u16 Nb)
{

}

bool SmsMessageHandler(ascii * SmsTel, ascii * SmsTimeOrLength, asii * SmsText)
{
u16 i=0;

while(i<strlen(SmsText))
TRACE((1,”SmsText[%d]=0x%X”, i, SmsText[i])); //display received message byte by byte
return FALSE;
}

s8 smsHandler;
smsHandler = ald_smsSubscribe(SmsMessageHandler, SmsCtrlHandler, ADL_SMS_MODE_TEXT);