/********************************************************************* * Propellor Display Driver Code Example * * File: PIC16BIT_Custom2.c /********************************************************************* * 220812 | BR | Fixed a bug in the rotate display macro when rotating backwards * 250812 | BR | Cart to Polar coordinates system fully tested and working * 270812 | BR | Fixed a number of minor bugs 3D shape fully tested and working Return & parameter types: void char short char* Pin directions: 0 = OUTPUT 2 = INPUT 3 = BIDIRECTIONAL ********************************************************************** [Settings] CLSID={1FA7D877-1AAA-47B4-8AAE-6EAEB7F85B9C} IsAnalogue=0 MultipleAllowed=0 Description=Custom 2 Flowcode Component ********************************************************************** [Port] DesiredPort=0 PortMandatory=0 SamePortAsID=0 [Pins] Count=0 [PinPort] # PORTA = 0 # PORTB = 1 # PORTC = 2 # PORTD = 3 # PORTE = 4 [PinDesiredBit] [PinDirection] # DIR_INPUT = 2 # DIR_OUTPUT = 0 # DIR_BIDIRECTIONAL = 3 [PinMustUsePort] # boolean [SamePortAsPinX] # -1 = any pin # otherwise, the pin number [PinMustUsePin] # boolean [PinValue] # boolean (0 = off, 1 = on) ********************************************************************** [MacroNames] Count=26 1=PWM_Control 2=SpinSync 3=Clear_Display 4=Rotate_Display 5=Reset_Rotation 6=Modify_Pixel 7=Modify_Pixel_XY 8=Draw_Axle 9=Draw_Arc 10=Draw_Circle 11=Draw_Text_Polar 12=Draw_Line 13=Setup_3D_Shape 14=Calc_3D_Shape 15=Calc_Face_Tri 16=Calc_Face_Sq 17=Draw_3D_Shape 18=Draw_Face_Tri 19=Draw_Face_Sq 20=Rotate_3D_Shape 21=Draw_Text 22=Read_Temperature 23=Get_Rev_Count 24=Update_Time_Date 25=Read_Time_Date 26=Timer_Tick [MacroReturns] 1=void 2=void 3=void 4=void 5=void 6=void 7=void 8=void 9=void 10=void 11=void 12=void 13=void 14=void 15=void 16=void 17=void 18=void 19=void 20=void 21=void 22=char 23=char 24=void 25=char 26=void [MacroIsPrivate] 1=1 2=1 3=0 4=0 5=0 6=0 7=0 8=0 9=0 10=0 11=0 12=0 13=0 14=0 15=1 16=1 17=0 18=1 19=1 20=0 21=0 22=0 23=0 24=1 25=0 26=1 [MacroParameters_PWM_Control] Count=0 [MacroParamTypes_PWM_Control] [MacroParameters_SpinSync] Count=0 [MacroParamTypes_SpinSync] [MacroParameters_Clear_Display] Count=0 [MacroParamTypes_Clear_Display] [MacroParameters_Rotate_Display] Count=1 1=direction [MacroParamTypes_Rotate_Display] 1=char [MacroParameters_Reset_Rotation] Count=0 [MacroParamTypes_Reset_Rotation] [MacroParameters_Modify_Pixel] Count=3 1=SEGMENT 2=LED 3=DATA [MacroParamTypes_Modify_Pixel] 1=short 2=char 3=char [MacroParameters_Modify_Pixel_XY] Count=3 1=X 2=Y 3=DATA [MacroParamTypes_Modify_Pixel_XY] 1=short 2=short 3=char [MacroParameters_Draw_Axle] Count=4 1=SEGMENT 2=STARTLED 3=ENDLED 4=COLOUR [MacroParamTypes_Draw_Axle] 1=short 2=char 3=char 4=char [MacroParameters_Draw_Arc] Count=4 1=STARTSEGMENT 2=ENDSEGMENT 3=LED 4=COLOUR [MacroParamTypes_Draw_Arc] 1=short 2=short 3=char 4=char [MacroParameters_Draw_Circle] Count=3 1=LED 2=COLOUR 3=FILL [MacroParamTypes_Draw_Circle] 1=char 2=char 3=char [MacroParameters_Draw_Text_Polar] Count=6 1=STRING 2=SEGMENT 3=LED 4=ORIENTATION 5=TRANSPARENT 6=COLOUR [MacroParamTypes_Draw_Text_Polar] 1=char* 2=short 3=char 4=char 5=char 6=char [MacroParameters_Draw_Line] Count=5 1=X1 2=Y1 3=X2 4=Y2 5=COLOUR [MacroParamTypes_Draw_Line] 1=short 2=short 3=short 4=short 5=char [MacroParameters_Setup_3D_Shape] Count=1 1=shape_template [MacroParamTypes_Setup_3D_Shape] 1=char [MacroParameters_Calc_3D_Shape] Count=0 [MacroParamTypes_Calc_3D_Shape] [MacroParameters_Calc_Face_Tri] Count=4 1=n1 2=n2 3=n3 4=idx [MacroParamTypes_Calc_Face_Tri] 1=short 2=short 3=short 4=char [MacroParameters_Calc_Face_Sq] Count=5 1=n1 2=n2 3=n3 4=n4 5=idx [MacroParamTypes_Calc_Face_Sq] 1=short 2=short 3=short 4=short 5=char [MacroParameters_Draw_3D_Shape] Count=1 1=Colour [MacroParamTypes_Draw_3D_Shape] 1=char [MacroParameters_Draw_Face_Tri] Count=5 1=n1 2=n2 3=n3 4=idx 5=Colour [MacroParamTypes_Draw_Face_Tri] 1=short 2=short 3=short 4=char 5=char [MacroParameters_Draw_Face_Sq] Count=6 1=n1 2=n2 3=n3 4=n4 5=idx 6=Colour [MacroParamTypes_Draw_Face_Sq] 1=short 2=short 3=short 4=short 5=char 6=char [MacroParameters_Rotate_3D_Shape] Count=0 [MacroParamTypes_Rotate_3D_Shape] [MacroParameters_Draw_Text] Count=7 1=String 2=X 3=Y 4=Font 5=Transparent 6=FColour 7=BColour [MacroParamTypes_Draw_Text] 1=char* 2=short 3=short 4=char 5=char 6=char 7=char [MacroParameters_Read_Temperature] Count=0 [MacroParamTypes_Read_Temperature] [MacroParameters_Get_Rev_Count] Count=0 [MacroParamTypes_Get_Rev_Count] [MacroParameters_Update_Time_Date] Count=0 [MacroParamTypes_Update_Time_Date] [MacroParameters_Read_Time_Date] Count=1 1=selection [MacroParamTypes_Read_Time_Date] 1=char [MacroParameters_Timer_Tick] Count=0 [MacroParamTypes_Timer_Tick] ********************************************************************/ /******************************************************************** * ADDITIONAL CODE ********************************************************************/ /*DefinesCode_Start*/ //common defines #define OFFSET 0 #define NUM_LEDS 24 #define NUM_SEGMENTS 240 #define SEGMENT_MASK 0x01 //0=No Segment Blanking, 1=1:1, 3=1:3 #define SEG_CNT (NUM_SEGMENTS * (SEGMENT_MASK + 1)) #define DEG_CNT_180 (NUM_SEGMENTS / 2) #define PI 3.141592 #define RADS_TO_SEGS (DEG_CNT_180 / PI) #define SCALE_3D 50 #define NUM_SIGNALS (NUM_LEDS * 3) #define NUM_BUFFERS (NUM_SIGNALS / 8) #define NUM_BYTES (NUM_SEGMENTS * NUM_BUFFERS) //Thermistor Defines #define MX_ADC_CHANNEL 0 #define MX_ADC_SAMP_TIME 40 #define MX_ADC_CONV_SP 15 #define MX_ADC_VREF_OPT 0 //global variables volatile char SEC = 0; volatile char MIN = 0; volatile char HOUR = 17; volatile char DAY = 6; volatile char WDAY = 0; volatile char MONTH = 3; volatile char YEAR = 11; volatile char REVS = 0; volatile char REVCNT = 0; volatile unsigned short SEG_COUNTER = 0; volatile unsigned short OFFSEG = OFFSET; volatile unsigned short TMR_COUNT = 0xF800; volatile unsigned short SEG_IDX; volatile char COMMAND = 0; volatile char ADDRESS = 0; //Video Memory volatile char VRAM[NUM_BYTES]; //3D Definitions #define SPEED_1X 0.017 #define SPEED_2X 0.034 #define SPEED_4X 0.068 #define DPHI SPEED_4X #define R 1 //Vertex Buffer float x[12], y[12], z[12]; float xview[12], yview[12], zview[12]; short xs[12], ys[12], zs[12]; short xsp[12], ysp[12], zsp[12]; char visible[20]; char visiblep[20]; char Shape = 0; char Vertex_Count = 0; //Viewpoint Settings float eview = 0; char d = 4; //ASCII Pixel data const char ASCII[] = { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , // 32 = 0x00 , 0x06 , 0x5F , 0x06 , 0x00 , // 33 = ! 0x07 , 0x03 , 0x00 , 0x07 , 0x03 , // 34 = " 0x24 , 0x7E , 0x24 , 0x7E , 0x24 , // 35 = # 0x24 , 0x2B , 0x6A , 0x12 , 0x00 , // 36 = $ 0x63 , 0x13 , 0x08 , 0x64 , 0x63 , // 37 = % 0x36 , 0x49 , 0x56 , 0x20 , 0x50 , // 38 = & 0x00 , 0x07 , 0x03 , 0x00 , 0x00 , // 39 = ' 0x00 , 0x3E , 0x41 , 0x00 , 0x00 , // 40 = ( 0x00 , 0x41 , 0x3E , 0x00 , 0x00 , // 41 = ) 0x08 , 0x3E , 0x1C , 0x3E , 0x08 , // 42 = * 0x08 , 0x08 , 0x3E , 0x08 , 0x08 , // 43 = + 0x00 , 0xE0 , 0x60 , 0x00 , 0x00 , // 44 = , 0x08 , 0x08 , 0x08 , 0x08 , 0x08 , // 45 = - 0x00 , 0x60 , 0x60 , 0x00 , 0x00 , // 46 = . 0x20 , 0x10 , 0x08 , 0x04 , 0x02 , // 47 = / 0x3E , 0x51 , 0x49 , 0x45 , 0x3E , // 48 = 0 0x00 , 0x42 , 0x7F , 0x40 , 0x00 , // 49 = 1 0x62 , 0x51 , 0x49 , 0x49 , 0x46 , // 50 = 2 0x22 , 0x49 , 0x49 , 0x49 , 0x36 , // 51 = 3 0x18 , 0x14 , 0x12 , 0x7F , 0x10 , // 52 = 4 0x2F , 0x49 , 0x49 , 0x49 , 0x31 , // 53 = 5 0x3C , 0x4A , 0x49 , 0x49 , 0x30 , // 54 = 6 0x01 , 0x71 , 0x09 , 0x05 , 0x03 , // 55 = 7 0x36 , 0x49 , 0x49 , 0x49 , 0x36 , // 56 = 8 0x06 , 0x49 , 0x49 , 0x29 , 0x1E , // 57 = 9 0x00 , 0x6C , 0x6C , 0x00 , 0x00 , // 58 = : 0x00 , 0xEC , 0x6C , 0x00 , 0x00 , // 59 = ; 0x08 , 0x14 , 0x22 , 0x41 , 0x00 , // 60 = < 0x24 , 0x24 , 0x24 , 0x24 , 0x24 , // 61 = = 0x00 , 0x41 , 0x22 , 0x14 , 0x08 , // 62 = > 0x02 , 0x01 , 0x59 , 0x09 , 0x06 , // 63 = ? 0x3E , 0x41 , 0x5D , 0x55 , 0x1E , // 64 = @ 0x7E , 0x09 , 0x09 , 0x09 , 0x7E , // 65 = A 0x7F , 0x49 , 0x49 , 0x49 , 0x36 , // 66 = B 0x3E , 0x41 , 0x41 , 0x41 , 0x22 , // 67 = C 0x7F , 0x41 , 0x41 , 0x41 , 0x3E , // 68 = D 0x7F , 0x49 , 0x49 , 0x49 , 0x41 , // 69 = E 0x7F , 0x09 , 0x09 , 0x09 , 0x01 , // 70 = F 0x3E , 0x41 , 0x49 , 0x49 , 0x7A , // 71 = G 0x7F , 0x08 , 0x08 , 0x08 , 0x7F , // 72 = H 0x00 , 0x41 , 0x7F , 0x41 , 0x00 , // 73 = I 0x30 , 0x40 , 0x40 , 0x40 , 0x3F , // 74 = J 0x7F , 0x08 , 0x14 , 0x22 , 0x41 , // 75 = K 0x7F , 0x40 , 0x40 , 0x40 , 0x40 , // 76 = L 0x7F , 0x02 , 0x04 , 0x02 , 0x7F , // 77 = M 0x7F , 0x02 , 0x04 , 0x08 , 0x7F , // 78 = N 0x3E , 0x41 , 0x41 , 0x41 , 0x3E , // 79 = O 0x7F , 0x09 , 0x09 , 0x09 , 0x06 , // 80 = P 0x3E , 0x41 , 0x51 , 0x21 , 0x5E , // 81 = Q 0x7F , 0x09 , 0x09 , 0x19 , 0x66 , // 82 = R 0x26 , 0x49 , 0x49 , 0x49 , 0x32 , // 83 = S 0x01 , 0x01 , 0x7F , 0x01 , 0x01 , // 84 = T 0x3F , 0x40 , 0x40 , 0x40 , 0x3F , // 85 = U 0x1F , 0x20 , 0x40 , 0x20 , 0x1F , // 86 = V 0x3F , 0x40 , 0x3C , 0x40 , 0x3F , // 87 = W 0x63 , 0x14 , 0x08 , 0x14 , 0x63 , // 88 = X 0x07 , 0x08 , 0x70 , 0x08 , 0x07 , // 89 = Y 0x71 , 0x49 , 0x45 , 0x43 , 0x00 , // 90 = Z 0x00 , 0x7F , 0x41 , 0x41 , 0x00 , // 91 = [ 0x02 , 0x04 , 0x08 , 0x10 , 0x20 , // 92 = 0x00 , 0x41 , 0x41 , 0x7F , 0x00 , // 93 = ] 0x04 , 0x02 , 0x01 , 0x02 , 0x04 , // 94 = ^ 0x80 , 0x80 , 0x80 , 0x80 , 0x80 , // 95 = _ 0x00 , 0x03 , 0x07 , 0x00 , 0x00 , // 96 = ` 0x20 , 0x54 , 0x54 , 0x54 , 0x78 , // 97 = a 0x7F , 0x44 , 0x44 , 0x44 , 0x38 , // 98 = b 0x38 , 0x44 , 0x44 , 0x44 , 0x28 , // 99 = c 0x38 , 0x44 , 0x44 , 0x44 , 0x7F , // 100 = d 0x38 , 0x54 , 0x54 , 0x54 , 0x18 , // 101 = e 0x08 , 0x7E , 0x09 , 0x09 , 0x00 , // 102 = f 0x18 , 0xA4 , 0xA4 , 0xA4 , 0x7C , // 103 = g 0x7F , 0x04 , 0x04 , 0x78 , 0x00 , // 104 = h 0x00 , 0x00 , 0x7D , 0x00 , 0x00 , // 105 = i 0x40 , 0x80 , 0x84 , 0x7D , 0x00 , // 106 = j 0x7F , 0x10 , 0x28 , 0x44 , 0x00 , // 107 = k 0x00 , 0x00 , 0x7F , 0x40 , 0x00 , // 108 = l 0x7C , 0x04 , 0x18 , 0x04 , 0x78 , // 109 = m 0x7C , 0x04 , 0x04 , 0x78 , 0x00 , // 110 = n 0x38 , 0x44 , 0x44 , 0x44 , 0x38 , // 111 = o 0xFC , 0x44 , 0x44 , 0x44 , 0x38 , // 112 = p 0x38 , 0x44 , 0x44 , 0x44 , 0xFC , // 113 = q 0x44 , 0x78 , 0x44 , 0x04 , 0x08 , // 114 = r 0x08 , 0x54 , 0x54 , 0x54 , 0x20 , // 115 = s 0x04 , 0x3E , 0x44 , 0x24 , 0x00 , // 116 = t 0x3C , 0x40 , 0x20 , 0x7C , 0x00 , // 117 = u 0x1C , 0x20 , 0x40 , 0x20 , 0x1C , // 118 = v 0x3C , 0x60 , 0x30 , 0x60 , 0x3C , // 119 = w 0x6C , 0x10 , 0x10 , 0x6C , 0x00 , // 120 = x 0x9C , 0xA0 , 0x60 , 0x3C , 0x00 , // 121 = y 0x64 , 0x54 , 0x54 , 0x4C , 0x00 , // 122 = z 0x08 , 0x3E , 0x41 , 0x41 , 0x00 , // 123 = { 0x00 , 0x00 , 0x7F , 0x00 , 0x00 , // 124 = | 0x00 , 0x41 , 0x41 , 0x3E , 0x08 , // 125 = } 0x02 , 0x01 , 0x02 , 0x01 , 0x00 // 126 = ~ }; //Temperature LUT data const char TLUT[] = { 0, 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 18, 19, 20, 21, 21, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 39, 39, 39, 40 }; //RTCC #define RTCCLock() RCFGCALbits.RTCWREN=0; #define RTCCOn() RCFGCALbits.RTCEN = 1; #define RTCCOff() RCFGCALbits.RTCEN = 0; #define EnableSecOsc() __builtin_write_OSCCONL(0x02); //UART Baud #define SPBRG_VAL ( 43 / MX_CLKS_PER_INST) /*DefinesCode_End*/ /*InitialisationCode_Start*/ /* // Configure Oscillator to operate the device at 40Mhz // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 // Fosc= 10M*32/(2*2)=80Mhz for 8M input clock PLLFBD=30; // M=32 CLKDIVbits.PLLPOST=0; // N1=2 CLKDIVbits.PLLPRE=0; // N2=2 // Clock switching to incorporate PLL __builtin_write_OSCCONH(0x03); // Initiate Clock Switch to Primary // Oscillator with PLL (NOSC=0b011) __builtin_write_OSCCONL(0x01); // Start clock switching while (OSCCONbits.COSC != 0b011); // Wait for Clock switch to occur // Wait for PLL to lock while(OSCCONbits.LOCK!=1) {}; */ EnableSecOsc(); // Enable the 32.768kHz Secondary oscillator //Configure Ports TRISB = 0xFF9A; PORTB = 0x0000; TRISC = 0xFF00; //Lower 8 Bits - LED Port PORTC = 0x00FF; //Configure Peripherals RPINR0 = 0x0700; //INT1 - RP7 - RB7 //RPINR18 = 0x1F0E; //UART1 RX - RP14 - RB14 //RPOR1 = 0x1200; //OC1 - RP3 - RB3 //RPOR0 = 0x0300; //UART1 TX - RP1 - RB1 //Timer PWM Interrupt //Interrupt: Enable TMR1 T1CON = ((T1CON & 0xfffd) | 0x8000); T1CON &= 0xff0f; PR1 = 0xFFFF; IEC0bits.T1IE = 1; //Hall Effect Interrupt //Interrupt: Enable INT0 INTCON2bits.INT0EP = 0; IEC0bits.INT0IE = 1; //RTCC /* asm("MOV #0x55,W0"); asm("MOV W0,NVMKEY"); // Write the 0x55 key asm("MOV #0xAA,W1"); asm("MOV W1,NVMKEY"); // Write the 0xAA key RCFGCALbits.RTCWREN = 1; RTCCOff(); //disable the RTCC peripheral PADCFG1 = 0x0000; //RTCC pin pad conected to Alarm // Enable the RTCC interrupt IFS3bits.RTCIF = 0; //clear the RTCC interrupt flag IEC3bits.RTCIE = 1; //enable the RTCC interrupt // Configure the alarm settings/ ALCFGRPTbits.CHIME = 0; //no rolloever of the repeat count ALCFGRPTbits.AMASK = 0; //alarm mask configuration bits ALCFGRPTbits.ARPT = 0; //alarm repeat counter value bits RCFGCALbits.RTCOE = 1; //enable RTCC output // Load the initial values to the RTCC value registers RCFGCALbits.RTCPTR = 3; //point to Year register RTCVAL = 0x0000; //Year RTCVAL = 0x0000; //Month & Day RTCVAL = 0x0000; //WkDay & Hour RTCVAL = 0x0000; //Min & Sec RCFGCALbits.CAL=0x0000; //No calibration ALCFGRPTbits.ALRMPTR = 2; //Point to Month/Day register ALRMVAL = 0x0000; //load month & day ALRMVAL = 0x0000; //load weekday & hour ALRMVAL = 0x0000; //load minute & seconds ALCFGRPTbits.ALRMEN = 1; //enable the alarm RTCCOn(); //enable RTCC peripheral RTCCLock(); //lock the RTCC value registers */ //Interrupt: Enable TMR3 @ 100Hz Exactly T3CON = ((T3CON & 0xfffd) | 0x8000); T3CON = ((T3CON & 0xff0f) | 0x0010); PR3 = 50000-1; IEC0bits.T3IE = 1; //UART 1 /* U1BRG = SPBRG_VAL; //Set the baud rate 115200bps U1STA = 0; //Reset the UART U1MODE = 0; //Reset the mode U1MODEbits.UARTEN = 1; //turn on serial interCalc_Face_Tri 1 IEC0bits.U1RXIE = 1; //Enable UART RX Interrupt */ /*InitialisationCode_End*/ /*InterruptCode_Start*/ //Handler code for [TMR1] void _ISR _T1Interrupt(void) { PWM_Control(); IFS0bits.T1IF = 0; } //Handler code for [INT0] void _ISR _INT0Interrupt(void) { SpinSync(); IFS0bits.INT0IF = 0; } /* //Handler code for [RTCC] void _ISR _RTCCInterrupt(void) { Update_Time_Date(); IFS3bits.RTCIF = 0; } */ //Handler code for [TMR3] RTCC Replacement void _ISR _T3Interrupt(void) { IFS0bits.T3IF = 0; Timer_Tick(); } /*InterruptCode_End*/ /******************************************************************** * FUNCTIONS ********************************************************************/ void PWM_Control() { /*Macro_PWM_Control_Start*/ //Local variable definitions char LOOP1; static char ODD = 0; TMR1 = TMR_COUNT; //LEDs OFF //Output: 1 -> B6 LATB = (LATB & 0xffbf) | 0x0040; #if (SEGMENT_MASK >= 1) if (ODD == 0) { #endif //Output to all LEDs to draw segment for (LOOP1=0; LOOP1 B5 LATB = (LATB & 0xffdf) | 0x0020; //Output: 0 -> B5 LATB = LATB & 0xffdf; //Load LED PWM into working register LATC = VRAM[SEG_IDX]; SEG_IDX++; if (SEG_IDX == NUM_BYTES) SEG_IDX = 0; } //LEDs ON //Output: 1 -> B6 LATB = LATB & 0xffbf; #if (SEGMENT_MASK >= 1) } ODD = (ODD + 1) & SEGMENT_MASK; #endif SEG_COUNTER++; /*Macro_PWM_Control_End*/ } void SpinSync() { /*Macro_SpinSync_Start*/ unsigned short difference = 0; if (SEG_COUNTER != SEG_CNT) { if (SEG_COUNTER < SEG_CNT) { if (TMR_COUNT < 0xFF00) { difference = SEG_CNT - SEG_COUNTER; if (difference > 5) TMR_COUNT = TMR_COUNT + 0x40; else if (difference > 3) TMR_COUNT = TMR_COUNT + 0x08; else if (difference > 1) TMR_COUNT = TMR_COUNT + 0x04; else TMR_COUNT = TMR_COUNT + 0x01; } } else { if (TMR_COUNT > 0x0040) { difference = SEG_COUNTER - SEG_CNT; if (difference > 5) TMR_COUNT = TMR_COUNT - 0x40; else if (difference > 3) TMR_COUNT = TMR_COUNT - 0x08; else if (difference > 1) TMR_COUNT = TMR_COUNT - 0x04; else TMR_COUNT = TMR_COUNT - 0x01; } } } //Move to first segment SEG_COUNTER = 0; SEG_IDX = (OFFSEG * NUM_BUFFERS); REVCNT++; //Trigger PWM Interrupt TMR1 = 0xFFFF; /*Macro_SpinSync_End*/ } void Timer_Tick() { /*Macro_Timer_Tick_Start*/ FCV_TICK++; if (FCV_TICK == 100) { FCV_TICK = 0; REVS = REVCNT; REVCNT = 0; FCV_REV2 = FCV_REV; FCV_REV = 0; FCV_SEC++; if (FCV_SEC == 60) { FCV_SEC = 0; FCV_MIN++; if (FCV_MIN == 60) { FCV_MIN = 0; FCV_HOUR++; if (FCV_HOUR == 24) { FCV_HOUR = 0; FCV_DAY++; } } } } /*Macro_Timer_Tick_End*/ } void Clear_Display() { /*Macro_Clear_Display_Start*/ unsigned short CNT; CNT = 0; while (CNT < NUM_BYTES) { VRAM[CNT] = 0xFF; CNT = CNT + 1; } /*Macro_Clear_Display_End*/ } void Rotate_Display(char direction) { /*Macro_Rotate_Display_Start*/ if (direction) { if (OFFSEG) OFFSEG = OFFSEG - 1; else OFFSEG = NUM_SEGMENTS - 1; } else { OFFSEG++; if (OFFSEG == NUM_SEGMENTS) OFFSEG = 0; } /*Macro_Rotate_Display_End*/ } void Reset_Rotation() { /*Macro_Reset_Rotation_Start*/ OFFSEG = OFFSET; /*Macro_Reset_Rotation_End*/ } void Modify_Pixel(short SEGMENT, char LED, char DATA) { /*Macro_Modify_Pixel_Start*/ //Local variable definitions char SHIFT = 0; char MASK; unsigned short ADDR = 0; //Init Variables if (LED >= NUM_LEDS) return; if (SEGMENT >= NUM_SEGMENTS) return; DATA = DATA << 5; //Can we save any time by skipping 8/16 leds? while (LED > 8) { LED = LED - 8; ADDR = ADDR + 3; } //Calculate shift position and LED address while (LED) { //Calculate Shift SHIFT = SHIFT + 3; if (SHIFT > 7) { ADDR = ADDR + 1; SHIFT = SHIFT & 0x07; } //Decrement LED count LED = LED - 1; } //Calculate RAM Address ADDR = (SEGMENT * 9) + ADDR; //Read VRAM LED = VRAM[ADDR]; //Update the byte MASK = 0xE0 >> SHIFT; LED = LED | MASK; MASK = (DATA & 0xE0) >> SHIFT; LED = LED & (~MASK); //Write VRAM VRAM[ADDR] = LED; if (SHIFT > 5) { //update address SHIFT = 8 - SHIFT; ADDR = ADDR + 1; //Read VRAM LED = VRAM[ADDR]; //Update the byte MASK = 0xE0 << SHIFT; LED = LED | MASK; MASK = (DATA & 0xE0) << SHIFT; LED = LED & (~MASK); //Write VRAM VRAM[ADDR] = LED; } /*Macro_Modify_Pixel_End*/ } void Modify_Pixel_XY(short X, short Y, char DATA) { /*Macro_Modify_Pixel_XY_Start*/ char quadrant; unsigned short Rad, Angle; float ang_temp; Rad = NUM_LEDS - sqrt((X * X) + (Y * Y)); if (Rad >= NUM_LEDS) //Off the display? return; if (X != 0) ang_temp = (float) Y / X; else ang_temp = (float) 2 * Y; ang_temp = atan(ang_temp); if ((X >= 0) && (Y >= 0)) //Calculate negative angle to get positive segment ang_temp = PI - ang_temp; else if ((X >= 0) && (Y < 0)) ang_temp = PI + (0 - ang_temp); else if ((X < 0) && (Y < 0)) ang_temp = (2 * PI) - ang_temp; else ang_temp = 0 - ang_temp; Angle = RADS_TO_SEGS * ang_temp; //Convert to segments if (Angle >= NUM_SEGMENTS) Angle = Angle - NUM_SEGMENTS; Angle = NUM_SEGMENTS - Angle; //Flip back to positive Modify_Pixel(Angle, Rad, DATA); /*Macro_Modify_Pixel_XY_End*/ } void Draw_Circle(char LED, char COLOUR, char FILL) { /*Macro_Draw_Circle_Start*/ Modify_Pixel(0, LED, COLOUR); Draw_Arc(1, NUM_SEGMENTS, LED, COLOUR); if (FILL) { while (LED < 24) { LED = LED + 1; Modify_Pixel(0, LED, COLOUR); Draw_Arc(1, NUM_SEGMENTS, LED, COLOUR); } } /*Macro_Draw_Circle_End*/ } void Draw_Axle(char SEGMENT, char STARTLED, char ENDLED, char COLOUR) { /*Macro_Draw_Axle_Start*/ //Ensure Addressing Is Valid if (STARTLED >= NUM_LEDS) return; if (ENDLED >= NUM_LEDS) return; while (STARTLED != ENDLED) { Modify_Pixel(SEGMENT, STARTLED, COLOUR); if (STARTLED <= ENDLED) { STARTLED = STARTLED + 1; } else { STARTLED = STARTLED - 1; } } Modify_Pixel(SEGMENT, STARTLED, COLOUR); /*Macro_Draw_Axle_End*/ } void Draw_Arc(char STARTSEGMENT, char ENDSEGMENT, char LED, char COLOUR) { /*Macro_Draw_Arc_Start*/ if (STARTSEGMENT >= NUM_SEGMENTS) return; if (ENDSEGMENT >= NUM_SEGMENTS) return; while (STARTSEGMENT != ENDSEGMENT) { Modify_Pixel(STARTSEGMENT, LED, COLOUR); STARTSEGMENT++; if (STARTSEGMENT == NUM_SEGMENTS) STARTSEGMENT = 0; } Modify_Pixel(STARTSEGMENT, LED, COLOUR); /*Macro_Draw_Arc_End*/ } void Draw_Line(short X1, short Y1, short X2, short Y2, char COLOUR) { /*Macro_Draw_Line_Start*/ int C1, M1; signed int D1 = 0; signed int Pixelx; //number of X-pixels; signed int Pixely; //Number of Y-pixels signed char Xinc = 1; signed char Yinc = 1; X1 = X1 + 24; X2 = X2 + 24; Y1 = Y1 + 24; Y2 = Y2 + 24; Pixelx = X2 - X1; Pixely = Y2 - Y1; if (Pixelx < 0) //If negative X direction { Xinc = -1; Pixelx = 0 - Pixelx; } if (Pixely < 0) //If negative Y direction { Yinc = -1; Pixely = 0 - Pixely; } if (Pixely <= Pixelx) { C1 = 2 * Pixelx; M1 = 2 * Pixely; while (X1 != X2) { Modify_Pixel_XY((X1 - 24), (Y1 - 24), COLOUR); //ForePlot line X1 = X1 + Xinc; D1 = D1 + M1; if (D1 > Pixelx) { Y1 = Y1 + Yinc; D1 = D1 - C1; } } } else { C1 = 2 * Pixely; M1 = 2 * Pixelx; while (Y1 != Y2) { Modify_Pixel_XY((X1 - 24), (Y1 - 24), COLOUR); //ForePlot Line Y1 = Y1 + Yinc; D1 = D1 + M1; if (D1 > Pixely) { X1 = X1 + Xinc; D1 = D1 - C1; } } } Modify_Pixel_XY((X1 - 24), (Y1 - 24), COLOUR); /*Macro_Draw_Line_End*/ } void Setup_3D_Shape(char shape_template) { /*Macro_Setup_3D_Shape_Start*/ short vertex; float theta, phi; float cosvar, sinvar; Shape = shape_template; if (Shape == 0) //Square Face { Vertex_Count = 4; theta = 0.25 * PI; //Top 2 Vertices for (vertex = 0; vertex < 2; vertex++) { if (vertex == 0) phi = 0; if (vertex == 1) phi = PI; sinvar = sin(theta); cosvar = cos(phi); x[vertex] = sinvar * cosvar; x[vertex] = x[vertex] * R; cosvar = sin(phi); y[vertex] = sinvar * cosvar; y[vertex] = y[vertex] * R; cosvar = cos(theta); z[vertex] = R * cosvar; } x[2] = 0 - x[0]; //Bottom 2 Vertices z[2] = 0 - z[0]; y[2] = 0 - y[0]; x[3] = 0 - x[1]; z[3] = 0 - z[1]; y[3] = 0 - y[1]; } if (Shape == 1) //Offset Square Face { Vertex_Count = 4; theta = 0.25 * PI; //Top 2 Vertices for (vertex = 0; vertex < 2; vertex++) { if (vertex == 0) phi = 1.25 * PI; if (vertex == 1) phi = 1.75 * PI; sinvar = sin(theta); cosvar = cos(phi); x[vertex] = sinvar * cosvar; x[vertex] = x[vertex] * R; cosvar = sin(phi); y[vertex] = sinvar * cosvar; y[vertex] = y[vertex] * R; cosvar = cos(theta); z[vertex] = R * cosvar; } x[2] = 0 - x[0]; //Bottom 2 Vertices z[2] = 0 - z[0]; y[2] = y[0]; x[3] = 0 - x[1]; z[3] = 0 - z[1]; y[3] = y[1]; } if (Shape == 2) //Cuboid { Vertex_Count = 8; theta = 0.25 * PI; //Top 4 Vertices for (vertex = 0; vertex < 4; vertex++) { if (vertex == 0) phi = 0.25 * PI; if (vertex == 1) phi = 0.75 * PI; if (vertex == 2) phi = 1.25 * PI; if (vertex == 3) phi = 1.75 * PI; sinvar = sin(theta); cosvar = cos(phi); x[vertex] = sinvar * cosvar; x[vertex] = x[vertex] * R; cosvar = sin(phi); y[vertex] = sinvar * cosvar; y[vertex] = y[vertex] * R; cosvar = cos(theta); z[vertex] = R * cosvar; } x[4] = 0 - x[3]; //Bottom 4 Vertices z[4] = 0 - z[3]; y[4] = 0 - y[3]; x[5] = 0 - x[2]; z[5] = 0 - z[2]; y[5] = 0 - y[2]; x[6] = 0 - x[1]; z[6] = 0 - z[1]; y[6] = 0 - y[1]; x[7] = 0 - x[0]; z[7] = 0 - z[0]; y[7] = 0 - y[0]; } if (Shape == 3) //Isocahedron { Vertex_Count = 12; for (vertex = 0; vertex < 6; vertex++) { theta = 1.10715; //Top 6 Vertices if (vertex == 0) { theta = 0; phi = 0; } if (vertex == 1) phi = 0; if (vertex == 2) phi = 0.4 * PI; if (vertex == 3) phi = 0.8 * PI; if (vertex == 4) phi = 1.2 * PI; if (vertex == 5) phi = 1.6 * PI; sinvar = sin(theta); cosvar = cos(phi); x[vertex] = sinvar * cosvar; x[vertex] = x[vertex] * R; cosvar = sin(phi); y[vertex] = sinvar * cosvar; y[vertex] = y[vertex] * R; cosvar = cos(theta); z[vertex] = R * cosvar; } x[6] = 0 - x[4]; //Bottom 6 Vertices z[6] = 0 - z[4]; y[6] = 0 - y[4]; x[7] = 0 - x[5]; z[7] = 0 - z[5]; y[7] = 0 - y[5]; x[8] = 0 - x[1]; z[8] = 0 - z[1]; y[8] = 0 - y[1]; x[9] = 0 - x[2]; z[9] = 0 - z[2]; y[9] = 0 - y[2]; x[10] = 0 - x[3]; z[10] = 0 - z[3]; y[10] = 0 - y[3]; x[11] = 0 - x[0]; z[11] = 0 - z[0]; y[11] = 0 - y[0]; } /*Macro_Setup_3D_Shape_End*/ } void Calc_3D_Shape() { /*Macro_Calc_3D_Shape_Start*/ if (Shape < 2) { Calc_Face_Sq(0, 1, 2, 3, 0); } if (Shape == 2) { Calc_Face_Sq(0, 1, 2, 3, 0); Calc_Face_Sq(4, 6, 5, 7, 0); //Calc_Face_Sq(0, 1, 4, 5, 0); //Unnessisary faces //Calc_Face_Sq(0, 3, 4, 6, 0); //Calc_Face_Sq(5, 7, 1, 2, 0); //Calc_Face_Sq(6, 7, 2, 3, 0); } if (Shape == 3) { Calc_Face_Tri(0, 1, 2, 0); Calc_Face_Tri(0, 1, 5, 1); Calc_Face_Tri(1, 10, 6, 2); Calc_Face_Tri(5, 10, 9, 3); Calc_Face_Tri(5, 1, 10, 4); Calc_Face_Tri(1, 2, 6, 5); Calc_Face_Tri(2, 7, 6, 6); Calc_Face_Tri(9, 10, 11, 7); Calc_Face_Tri(11, 10, 6, 8); Calc_Face_Tri(11, 7, 6, 9); Calc_Face_Tri(0, 3, 4, 10); Calc_Face_Tri(0, 2, 3, 11); Calc_Face_Tri(0, 4, 5, 12); Calc_Face_Tri(3, 4, 8, 13); Calc_Face_Tri(4, 5, 9, 14); Calc_Face_Tri(4, 8, 9, 15); Calc_Face_Tri(3, 7, 8, 16); Calc_Face_Tri(2, 3, 7, 17); Calc_Face_Tri(7, 8, 11, 18); Calc_Face_Tri(8, 9, 11, 19); } /*Macro_Calc_3D_Shape_End*/ } void Calc_Face_Tri(char n1, char n2, char n3, char idx) { /*Macro_Calc_Face_Tri_Start*/ float nx, ny, nz; float sx, sy, sz; float Dot; float temp; nx = xview[n1] + xview[n2]; nx = (nx + xview[n3]) / 3; ny = yview[n1] + yview[n2]; ny = (ny + yview[n3]) / 3; nz = zview[n1] + zview[n2]; nz = (nz + zview[n3]) / 3; sx = d - nx; sy = 0 - ny; sz = 0 - nz; sx = nx * sx; sy = ny * sy; sz = nz * sz; Dot = sx + sy; Dot = Dot + sz; visiblep[idx] = visible[idx]; //is vector facing away if (Dot < 0) { visible[idx] = 0; return; } visible[idx] = 1; temp = d - xview[n1]; zs[n1] = (zview[n1] / temp) * SCALE_3D; ys[n1] = (yview[n1] / temp) * SCALE_3D; xs[n1] = (xview[n1] / temp) * SCALE_3D; temp = d - xview[n2]; zs[n2] = (zview[n2] / temp) * SCALE_3D; ys[n2] = (yview[n2] / temp) * SCALE_3D; xs[n2] = (xview[n2] / temp) * SCALE_3D; temp = d - xview[n3]; zs[n3] = (zview[n3] / temp) * SCALE_3D; ys[n3] = (yview[n3] / temp) * SCALE_3D; xs[n3] = (xview[n3] / temp) * SCALE_3D; /*Macro_Calc_Face_Tri_End*/ } void Calc_Face_Sq(char n1, char n2, char n3, char n4, char idx) { /*Macro_Calc_Face_Sq_Start*/ float temp; temp = d - xview[n1]; zs[n1] = (zview[n1] / temp) * SCALE_3D; ys[n1] = (yview[n1] / temp) * SCALE_3D; xs[n1] = (xview[n1] / temp) * SCALE_3D; temp = d - xview[n2]; zs[n2] = (zview[n2] / temp) * SCALE_3D; ys[n2] = (yview[n2] / temp) * SCALE_3D; xs[n2] = (xview[n2] / temp) * SCALE_3D; temp = d - xview[n3]; zs[n3] = (zview[n3] / temp) * SCALE_3D; ys[n3] = (yview[n3] / temp) * SCALE_3D; xs[n3] = (xview[n3] / temp) * SCALE_3D; temp = d - xview[n4]; zs[n4] = (zview[n4] / temp) * SCALE_3D; ys[n4] = (yview[n4] / temp) * SCALE_3D; xs[n4] = (xview[n4] / temp) * SCALE_3D; /*Macro_Calc_Face_Sq_End*/ } void Draw_3D_Shape(char Colour) { /*Macro_Draw_3D_Shape_Start*/ if (Shape < 2) { Draw_Face_Sq(0, 1, 2, 3, 0, Colour); } if (Shape == 2) { Draw_Face_Sq(0, 1, 2, 3, 0, Colour); Draw_Face_Sq(4, 5, 6, 7, 0, Colour); //Optimisation - Remaining 4 faces can be done using just 4 lines Draw_Line (ysp[1], zsp[1], ysp[4], zsp[4], 0); Draw_Line (ys[1], zs[1], ys[4], zs[4], Colour); Draw_Line (ysp[0], zsp[0], ysp[5], zsp[5], 0); Draw_Line (ys[0], zs[0], ys[5], zs[5], Colour); Draw_Line (ysp[3], zsp[3], ysp[6], zsp[6], 0); Draw_Line (ys[3], zs[3], ys[6], zs[6], Colour); Draw_Line (ysp[2], zsp[2], ysp[7], zsp[7], 0); Draw_Line (ys[2], zs[2], ys[7], zs[7], Colour); } if (Shape == 3) { //No optimisation available or visible mechanism cannot guarentee edges are not missing Draw_Face_Tri(0, 1, 2, 0, Colour); Draw_Face_Tri(0, 1, 5, 1, Colour); Draw_Face_Tri(1, 10, 6, 2, Colour); Draw_Face_Tri(5, 10, 9, 3, Colour); Draw_Face_Tri(5, 1, 10, 4, Colour); Draw_Face_Tri(1, 2, 6, 5, Colour); Draw_Face_Tri(2, 7, 6, 6, Colour); Draw_Face_Tri(9, 10, 11, 7, Colour); Draw_Face_Tri(11, 10, 6, 8, Colour); Draw_Face_Tri(11, 7, 6, 9, Colour); Draw_Face_Tri(0, 3, 4, 10, Colour); Draw_Face_Tri(0, 2, 3, 11, Colour); Draw_Face_Tri(0, 4, 5, 12, Colour); Draw_Face_Tri(3, 4, 8, 13, Colour); Draw_Face_Tri(4, 5, 9, 14, Colour); Draw_Face_Tri(4, 8, 9, 15, Colour); Draw_Face_Tri(3, 7, 8, 16, Colour); Draw_Face_Tri(2, 3, 7, 17, Colour); Draw_Face_Tri(7, 8, 11, 18, Colour); Draw_Face_Tri(8, 9, 11, 19, Colour); } /*Macro_Draw_3D_Shape_End*/ } void Draw_Face_Tri(char n1, char n2, char n3, char idx, char Colour) { /*Macro_Draw_Face_Tri_Start*/ if (visiblep[idx]) { Draw_Line (ysp[n1], zsp[n1], ysp[n2], zsp[n2], 0); Draw_Line (ysp[n2], zsp[n2], ysp[n3], zsp[n3], 0); Draw_Line (ysp[n3], zsp[n3], ysp[n1], zsp[n1], 0); } if (visible[idx]) { Draw_Line (ys[n1], zs[n1], ys[n2], zs[n2], Colour); Draw_Line (ys[n2], zs[n2], ys[n3], zs[n3], Colour); Draw_Line (ys[n3], zs[n3], ys[n1], zs[n1], Colour); } /*Macro_Draw_Face_Tri_End*/ } void Draw_Face_Sq(char n1, char n2, char n3, char n4, char idx, char Colour) { /*Macro_Draw_Face_Sq_Start*/ Draw_Line (ysp[n1], zsp[n1], ysp[n2], zsp[n2], 0); Draw_Line (ysp[n2], zsp[n2], ysp[n3], zsp[n3], 0); Draw_Line (ysp[n3], zsp[n3], ysp[n4], zsp[n4], 0); Draw_Line (ysp[n4], zsp[n4], ysp[n1], zsp[n1], 0); Draw_Line (ys[n1], zs[n1], ys[n2], zs[n2], Colour); Draw_Line (ys[n2], zs[n2], ys[n3], zs[n3], Colour); Draw_Line (ys[n3], zs[n3], ys[n4], zs[n4], Colour); Draw_Line (ys[n4], zs[n4], ys[n1], zs[n1], Colour); /*Macro_Draw_Face_Sq_End*/ } void Rotate_3D_Shape () { /*Macro_Rotate_3D_Shape_Start*/ short vertex; float xnew, ynew; float tempx, tempy; float coscalc[2], sincalc[2]; coscalc[0] = cos(eview); //View Elevation sincalc[0] = sin(eview); coscalc[1] = cos(DPHI); //Delta Phi - Rotation around Y sincalc[1] = sin(DPHI); for (vertex = 0; vertex < Vertex_Count; vertex++) { xsp[vertex] = xs[vertex]; ysp[vertex] = ys[vertex]; zsp[vertex] = zs[vertex]; xview[vertex] = coscalc[0] * x[vertex]; xview[vertex] = xview[vertex] + (sincalc[0] * z[vertex]); yview[vertex] = y[vertex]; zview[vertex] = -sincalc[0] * x[vertex]; zview[vertex] = zview[vertex] + (coscalc[0] * z[vertex]); xnew = coscalc[1] * x[vertex]; xnew = xnew - (sincalc[1] * y[vertex]); ynew = sincalc[1] * x[vertex]; ynew = ynew + (coscalc[1] * y[vertex]); x[vertex] = xnew; y[vertex] = ynew; } eview = eview + (DPHI / 2); /*Macro_Rotate_3D_Shape_End*/ } void Draw_Text (char* String, char X, char Y, char Font, char Transparent, char FColour, char BColour) { /*Macro_Draw_Text_Start*/ char xpix, ypix, pos_Str, length_Str, xcount, ycount, height, width, i; char Fontwidth = 1; //First we assume small font char Fontheight = 1; unsigned int count, position; char temp[6]; temp[5] = 0x00; //Spacing Line length_Str = strlen(String); //Get length of string if (Font == 1) //Double Height Sizes //Double Width { Fontwidth = 2; } if (Font == 2) //Double Height and Width Sizes { Fontwidth = 2; Fontheight = 2; } if (Font == 3) //Double Height Sizes { Fontheight = 2; } xcount = 0; for (i=0;i 126) return; CHARACTER = CHARACTER - 32; CHARACTER = CHARACTER * 5; //For 5 Bytes of Character for (LOOP3=0; LOOP3<5; LOOP3++) { //Collect 1/5th of ASCII Symbol LOOKUPCHAR = ASCII[CHARACTER]; //Reset Row Counter ROWCOUNT = LED; //5 x 7 Font Size for (LOOP4=0; LOOP4<8; LOOP4++) { //If Pixel Set if (LOOKUPCHAR & 0x01) { //Set Pixel Modify_Pixel(SEGMENT, ROWCOUNT, COLOUR); } else if (TRANSPARENT == 0) //If Transparent { //Clear Pixel Modify_Pixel(SEGMENT, ROWCOUNT, 0); } //If Orientation if (ORIENTATION) { //Count Down ROWCOUNT = ROWCOUNT - 1; } else { //Count Up ROWCOUNT = ROWCOUNT + 1; } LOOKUPCHAR = LOOKUPCHAR >> 1; } CHARACTER = CHARACTER + 1; if (ORIENTATION) { SEGMENT++; if (SEGMENT == NUM_SEGMENTS) SEGMENT = 0; } else { SEGMENT = (SEGMENT - 1); if (SEGMENT < 0) SEGMENT = NUM_SEGMENTS; } } if (ORIENTATION) { SEGMENT++; if (SEGMENT == NUM_SEGMENTS) SEGMENT = 0; } else { SEGMENT = (SEGMENT - 1); if (SEGMENT < 0) SEGMENT = NUM_SEGMENTS; } } /*Macro_Draw_Text_Polar_End*/ } char Read_Sound_Level () { /*Macro_Read_Sound_Level_Start*/ //No Longer Supported /* volatile char cnt; ADCSRA = (1 << ADEN) | 2; ADMUX = (1 << REFS0) | (1 << ADLAR) | 0; //wait the acquisition time cnt = 0; while (cnt < 15) cnt++; //begin conversion and wait until it has finished ADCSRA |= (1 << ADSC); while(ADCSRA & (1 << ADSC)); return ADCH; */ return 0; /*Macro_Read_Sound_Level_End*/ } char Read_Temperature () { /*Macro_Read_Temperature_Start*/ //set up ADC conversion char cnt; char retval; unsigned int old_tris; //Store old tris register and set the i/o pin as an input old_tris = TRISB; set_bit(TRISB, MX_ADC_CHANNEL); //Setup ADC Channel clear_bit(AD1PCFGL, MX_ADC_CHANNEL); AD1CHS0 = (MX_ADC_CHANNEL & 0x0F); //Setup Vref+ functionality #if (MX_ADC_VREF_OPT == 0) AD1CON2bits.VCFG0 = 0; #else AD1CON2bits.VCFG0 = 1; #if (MX_ADC_CHANNEL == 0) #error Target device is currently using AN0 for VREF+ #endif #endif //Assign conversion speed AD1CON3 = (MX_ADC_CONV_SP & 0xFF); AD1CON3bits.ADRC = 1; //Turn on ADC AD1CON1bits.AD12B = 1; AD1CON1bits.ADON = 1; AD1CON1bits.SAMP = 1; //wait the acquisition time cnt = 0; while (cnt < MX_ADC_SAMP_TIME) cnt++; //begin conversion and wait until it has finished AD1CON1bits.SAMP = 0; while (!AD1CON1bits.DONE); //restore old tris value, and reset adc registers TRISB = old_tris; set_bit(AD1PCFGL, MX_ADC_CHANNEL); retval = (ADC1BUF0 >> 4); AD1CON1bits.ADON = 0; //if data is valid if (retval < 62) return TLUT[retval]; return 0; /*Macro_Read_Temperature_End*/ } char Get_Rev_Count () { /*Macro_Get_Rev_Count_Start*/ return REVS; /*Macro_Get_Rev_Count_End*/ } void Update_Time_Date () { /*Macro_Update_Time_Date_Start*/ int Yr, MonDat, WkHr, MinSec; while(RCFGCALbits.RTCSYNC==1); //wait for RTCSYNC bit to become ‘0’ RCFGCALbits.RTCPTR = 3; //points to the Year register Yr = RTCVAL; MonDat = RTCVAL; WkHr = RTCVAL; MinSec = RTCVAL; // read the device value YEAR = Yr & 0x00FF; MONTH = MonDat >> 8; DAY = MonDat & 0x00FF; WDAY = WkHr >> 8; HOUR = WkHr & 0x00FF; MIN = MinSec >> 8; SEC = MinSec & 0x00FF; YEAR = (10 * (YEAR >> 4)) + (YEAR & 0x0F); MONTH = (10 * (MONTH >> 4)) + (MONTH & 0x0F); DAY = (10 * (DAY >> 4)) + (DAY & 0x0F); WDAY = (10 * (WDAY >> 4)) + (WDAY & 0x0F); HOUR = (10 * (HOUR >> 4)) + (HOUR & 0x0F); MIN = (10 * (MIN >> 4)) + (MIN & 0x0F); SEC = (10 * (SEC >> 4)) + (SEC & 0x0F); /*Macro_Update_Time_Date_End*/ } char Read_Time_Date (char selection) { /*Macro_Read_Time_Date_Start*/ switch(selection) { case 0: return SEC; case 1: return MIN; case 2: return HOUR; case 3: return WDAY; case 4: return DAY; case 5: return MONTH; case 6: return YEAR; } return 0; /*Macro_Read_Time_Date_End*/ }