Analog Meter

Code Examples ››
Parent Previous Next

DEMO ANALOG NEEDLE METER

(Download)


Device = 33EP128MC504


Declare Xtal = 150

Declare Auto_Heap_Arrays = On

Declare Auto_Heap_Strings = On

Declare Auto_Variable_Bank_Cross = On

Declare Stack_Expand = On

Declare Watchdog = OFF

Declare Warnings = Off

Declare Access_Upper_64K = On


'================ MCU =======================

$define McuType              1

'================ TFT SCREEN ================

$define TFT_IC               ILI9486

$define TftScreenWidth       480

$define TftScreenHeight      320

$define TftWidthOffset       0

$define TftHeightOffset      0

$define TftPixelCheck        0

$define TFT_COM              PAR_16b

$define TFT_RESET            PORTC.2

$define TFT_CS               PORTC.9

$define TFT_DC               PORTC.1

$define TFT_WR               PORTC.4

$define TFT_RD               PORTA.10

$define TftDataWord          PORTB

Include                      "TftLib\TFT_Graphic_Lib.inc"

Include                      "TftLib\Colors\Colors.inc"

Include                      "TftLib\Drivers\Drivers.inc"

Include                      "TftLib\Drivers\ILI9486.inc"

Include                      "TftLib\System\System.inc"

Include                      "TftLib\System\System_PAR_V2.inc"

Include                      "TftLib\Shapes\Shapes.inc"

Include                      "TftLib\Shapes\Shapes_PAR_V1.inc"

'================ SD CARD ===================

$define SDCardUsed           0

'================ MEMORY ====================

$define MemoryType           0

'================ TFT TOUCH =================

$define TOUCH_IC             0

'================ SPRITES ===================

$define SpritesUsed          0

'=============== SND FONT ===================

$define FontSNDUsed          1

Include                      "TftLib\Fonts\SND\F0_SND_8x8_Basic.inc"

Include                      "TftLib\Fonts\SND\F4_SND_16x16_Regular.inc"

$define FontSmooth           1

$define FontMaxSpace         8

$define FontMaxPixels        8

Include                      "TftLib\Fonts\SND.inc"

Include                      "TftLib\Fonts\SND_PAR_V1.inc"

'=============== BDF FONT ===================

$define FontBDFUsed          1

Include                      "TftLib\Fonts\BDF\F2_BDF_Margarine-Regular-24.inc"

Include                      "TftLib\Fonts\BDF.inc"

Include                      "TftLib\Fonts\BDF_PAR_V1.inc"


Dim bCounter1 As Byte

Dim wTemp As Word

Dim fVoltage As Float 

Dim fCurrent As Float

Dim fVoltageNeedle As Float 

Dim wCurrentSlideX As Word

Dim fCurrentScale As Float


Main:

    Clear

    SetCrystal()

    SetPins()

    TftSetUp(0,0)

    SetScreen()

    

    fCurrentScale = 2 / 175

    wCurrentSlideX = 260

    fVoltageNeedle = 1

    

    'Seed random value

    Seed $0123

    

    While 1 = 1

        fCurrent = (Random / 32768)

        fVoltage = (Random / 655)

        PrintCurrent()

        PrintVoltage()

        DelayMS 250

    Wend

End


Proc PrintCurrent()

    TftRectangleTCG(260,57, 304,73, WHITE,GREEN, 0)

    TftRectangleTCG(305,57, 349,73, GREEN,YELLOW, 0)

    TftRectangleTCG(350,57, 435,73, YELLOW,RED, 0)

    TftRectangleSNCP(wCurrentSlideX,54, 4, 4, GLAUCOUS)

    TftRectangleSNCP(wCurrentSlideX,76, 4, 4, GLAUCOUS)

    

    wCurrentSlideX = 260 + fRound(fCurrent / fCurrentScale)

    TftRectangleSNCP(wCurrentSlideX,54, 4, 4, BLACK)

    TftRectangleSNCP(wCurrentSlideX,76, 4, 4, BLACK)

    TftLineVN(wCurrentSlideX,54,76,BLACK)

    TftPrintSndDecimal(fCurrent, 1, 2, 2, 284, 16, 0, BLACK, LIBERTY, 0)

EndProc


Proc PrintVoltage()

    TftArcRL(60,230, 420,230, 400,0, SKY_BLUE, -33, -100, fVoltageNeedle)

    fVoltageNeedle = 1 - fVoltage / 100

    TftArcRL(60,230, 420,230, 400,0, BLACK, -33, -100, fVoltageNeedle)


    fVoltage = fVoltage / 23.81 

    TftPrintSndDecimal(fVoltage, 1, 2, 3, 37, 27, 0, BLACK, SKY_BLUE, 0)

EndProc


Proc SetScreen()

    TftResetScreen(BLACK)

    TftSetSndFont(2,1)

    TftPrintSndString("Booting ... Please wait!", 1, 55, 130, 0, WHITE, BLACK, 0) 

    DelayMS 2000


    'Display off

    TftDisplayOff()

    

    'Reset screen 

    TftRectangleTCG(0,0, 479,319, CREAM,CITRINE, 1)

    

    'Outside frame

    TftRectangleETTCI(0,0, 479,319,ONYX, 5)

    TftRectangleRC(0,0, 479,319, 20, 0,ONYX, 5,-1)

    

    'Scale window

    TftRectangleRC(30,100, 450,310, 30, 1,SKY_BLUE, 0,0)

    TftRectangleRC(30,100, 450,310, 30, 0,GLAUCOUS, 5,-1)

    

    '5 zone scale

    For bCounter1 = 0 To 30

        TftArcS(60,230, 420,230, 400,0, RED, 0, 0.2, -bCounter1)

    Next

    For bCounter1 = 0 To 30

        TftArcS(60,230, 420,230, 400,0, ORANGE, 0.2, 0.4, -bCounter1)

    Next

    For bCounter1 = 0 To 30

        TftArcS(60,230, 420,230, 400,0, YELLOW, 0.4, 0.6, -bCounter1)

    Next

    For bCounter1 = 0 To 30

        TftArcS(60,230, 420,230, 400,0, GREEN, 0.6, 0.8, -bCounter1)

    Next   

    For bCounter1 = 0 To 30

        TftArcS(60,230, 420,230, 400,0, WHITE, 0.8, 1, -bCounter1)

    Next 

    

    'Scale frame

    TftArcRLThickness(1)        

    TftArcS(60,230, 420,230, 400,0, BLACK, 0, 1, 0)

    TftArcS(60,230, 420,230, 400,0, BLACK, 0, 1, -30)

    TftArcRL(60,230, 420,230, 400,0, BLACK, 0, -30, 0)

    TftArcRL(60,230, 420,230, 400,0, BLACK, 0, -30, 1)

    

    'Scale major dividers

    For bCounter1 = 1 To 4

        wTemp = bCounter1 * 2

        TftArcRL(60,230, 420,230, 400,0, BLACK, -5, -30, wTemp / 10)

    Next

    

    'Minor dividers 

    For bCounter1 = 1 To 24

        wTemp = bCounter1 * 4

        TftArcRL(60,230, 420,230, 400,0, BLACK, -15, -30, wTemp / 100)   

    Next 

    

    'Labels

    TftSetBdfFont(2)

    TftPrintBdfString("MIN", 48, 250, 480, 0, WARM_BLACK, WHITE, 1)

    TftPrintBdfString("20%", 110, 270, 480, 0, WARM_BLACK, WHITE, 1)

    TftPrintBdfString("40%", 186, 280, 480, 0, WARM_BLACK, WHITE, 1)

    TftPrintBdfString("60%", 264, 280, 480, 0, WARM_BLACK, WHITE, 1)

    TftPrintBdfString("80%", 340, 270, 480, 0, WARM_BLACK, WHITE, 1)

    TftPrintBdfString("MAX", 390, 250, 480, 0, WARM_BLACK, WHITE, 1)

    

    'Needle set

    TftArcRLThickness(3)


    'Needle window

    For bCounter1 = 0 To 30

        TftArcS(60,230, 420,230, 400,0, ISABELLINE, 0, 1, -103 - bCounter1)

    Next

    TftRectangleTCG(106,106, 374,138, ICEBERG, ISABELLINE, 1)

    TftRectangleSNTC(160,138, 314,150, ISABELLINE)

    TftSetSndFont(0,1)

    TftPrintSndString("Battery Voltage 4.2v", 1, 160, 126, 0, BLACK, WHITE, 1)

    

    'Readings window

    TftRectangleRC(30,10, 235,94, 10, 1,SKY_BLUE, 0,0)

    TftRectangleRC(30,10, 235,94, 10, 0,GLAUCOUS, 5,-1)

        

    TftSetSndFont(4,1)

    TftPrintSndDecimal(fVoltage, 1, 2, 3, 37, 27, 0, BLACK, SKY_BLUE, 0)

  

    'Current readings

    TftRectangleRC(245,10, 450,94, 10, 1,LIBERTY, 0,0)

    TftRectangleRC(245,10, 450,94, 10, 0,GLAUCOUS, 5,-1)

    TftSetSndFont(0,1)

    TftPrintSndString("Battery Current 2A", 1, 276, 80, 0, BLACK, WHITE, 1)    

    TftRectangleETTCO(260,57, 435,73, GLAUCOUS,5)

    TftRectangleTCG(260,57, 304,73, WHITE,GREEN, 0)

    TftRectangleTCG(305,57, 349,73, GREEN,YELLOW, 0)

    TftRectangleTCG(350,57, 435,73, YELLOW,RED, 0)

    TftSetSndFont(4,1)

    TftPrintSndDecimal(fCurrent, 1, 2, 2, 284, 16, 0, BLACK, LIBERTY, 0)

    

    'Display on

    TftDisplayOn()

EndProc



'Set Fosc    

Proc SetCrystal()

    Clear 

    'Crystal = 16 MHz --> PLLDIV = 68 for 140MHz / PLLDIV = 73 For 150MHz

    CLKDIV = %0000000000000010 'PLLPRE = 2 ; PLLPOST = 0

    PLLFBD.Byte0 = 73 '--> 150MHz Fp

    'Wait for oscillator PLL lock

    While OSCCON.5 = 0 : Wend

    DelayMS 100

EndProc   


Proc SetPins()

    'Set all digital

    ANSELA = 0

    ANSELB = 0

    ANSELC = 0

EndProc


Config FICD = ICS_PGD1, JTAGEN_OFF

Config FPOR = ALTI2C1_OFF, ALTI2C2_OFF, WDTWIN_WIN25

Config FWDT = FWDTEN_OFF, PLLKEN_ON, WDTPOST_PS16384, WDTPRE_PR128, WINDIS_OFF

Config FOSC = FCKSM_CSECMD, IOL1WAY_OFF, OSCIOFNC_OFF, POSCMD_HS

Config FOSCSEL = FNOSC_PRIPLL, IESO_ON, PWMLOCK_OFF

Config FGS = GCP_OFF, GWRP_OFF 

Created with the Personal Edition of HelpNDoc: Make your documentation accessible on any device with HelpNDoc